Search in sources :

Example 1 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class TestV10EventHandling method testAsynchronousDelivery_fullEvent.

@Test
public void testAsynchronousDelivery_fullEvent() throws Exception {
    final SimpleChannelListener listener = new SimpleChannelListener();
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        channel.addChannelListener(listener);
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        establishChannel.join(500);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        out.writeInt(op_event);
        out.writeInt(513);
        out.writeBuffer(new byte[] { 1, 3, 69, 86, 84, 3, 0, 0, 0 });
        out.writeLong(0);
        out.writeInt(7);
        out.flush();
        Thread.sleep(500);
        List<AsynchronousChannelListener.Event> receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", 1, receivedEvents.size());
        AsynchronousChannelListener.Event event = receivedEvents.get(0);
        assertEquals("Unexpected eventId", 7, event.getEventId());
        assertEquals("Unexpected event count", 3, event.getEventCount());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Test(org.junit.Test)

Example 2 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class TestV10EventHandling method testAsynchronousDelivery_largeNumberOfEvents.

@Test
public void testAsynchronousDelivery_largeNumberOfEvents() throws Exception {
    final SimpleChannelListener listener = new SimpleChannelListener();
    try (SimpleServer simpleServer = new SimpleServer()) {
        final FbWireAsynchronousChannel channel = new V10AsynchronousChannel(createDummyDatabase());
        channel.addChannelListener(listener);
        Thread establishChannel = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    channel.connect("localhost", simpleServer.getPort(), 1);
                } catch (SQLException e) {
                // suppress
                }
            }
        });
        establishChannel.start();
        simpleServer.acceptConnection();
        AsynchronousProcessor.getInstance().registerAsynchronousChannel(channel);
        assertTrue("Expected connected channel", channel.isConnected());
        final XdrOutputStream out = new XdrOutputStream(simpleServer.getOutputStream());
        // Write a large number of events
        final int testEventCount = 1024;
        for (int count = 1; count <= testEventCount; count++) {
            out.writeInt(op_event);
            out.writeInt(513);
            out.writeBuffer(new byte[] { 1, 5, 69, 86, 69, 78, 84, (byte) count, (byte) (count >> 8), (byte) (count >> 16), (byte) (count >> 24) });
            out.writeLong(0);
            out.writeInt(7);
        }
        out.flush();
        // Need to sleep for thread to process all events, might still fail on slower computers
        Thread.sleep(500);
        List<AsynchronousChannelListener.Event> receivedEvents = listener.getReceivedEvents();
        assertEquals("Unexpected number of events", testEventCount, receivedEvents.size());
        AsynchronousChannelListener.Event event = receivedEvents.get(0);
        assertEquals("Unexpected eventId", 7, event.getEventId());
        assertEquals("Unexpected event count", 1, event.getEventCount());
        AsynchronousChannelListener.Event lastEvent = receivedEvents.get(testEventCount - 1);
        assertEquals("Unexpected eventId", 7, lastEvent.getEventId());
        assertEquals("Unexpected event count", testEventCount, lastEvent.getEventCount());
    }
}
Also used : SimpleServer(org.firebirdsql.common.SimpleServer) SQLException(java.sql.SQLException) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) Test(org.junit.Test)

Example 3 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Database method reconnectTransaction.

@Override
public final FbTransaction reconnectTransaction(long transactionId) throws SQLException {
    try {
        checkAttached();
        synchronized (getSynchronizationObject()) {
            try {
                final XdrOutputStream xdrOut = getXdrOut();
                xdrOut.writeInt(op_reconnect);
                xdrOut.writeInt(getHandle());
                final byte[] transactionIdBuffer = getTransactionIdBuffer(transactionId);
                xdrOut.writeBuffer(transactionIdBuffer);
                xdrOut.flush();
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ioex).toSQLException();
            }
            try {
                final GenericResponse response = readGenericResponse(null);
                final FbWireTransaction transaction = protocolDescriptor.createTransaction(this, response.getObjectHandle(), TransactionState.PREPARED);
                transactionAdded(transaction);
                return transaction;
            } catch (IOException ioex) {
                throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ioex).toSQLException();
            }
        }
    } catch (SQLException ex) {
        exceptionListenerDispatcher.errorOccurred(ex);
        throw ex;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 4 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Database method initAsynchronousChannel.

@Override
public final FbWireAsynchronousChannel initAsynchronousChannel() throws SQLException {
    checkAttached();
    final int auxHandle;
    final int port;
    synchronized (getSynchronizationObject()) {
        try {
            final XdrOutputStream xdrOut = getXdrOut();
            xdrOut.writeInt(op_connect_request);
            // Connection type (p_req_type)
            xdrOut.writeInt(P_REQ_async);
            // p_req_object
            xdrOut.writeInt(getHandle());
            // p_req_partner
            xdrOut.writeInt(0);
            xdrOut.flush();
        } catch (IOException ex) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(ex).toSQLException();
        }
        try {
            final GenericResponse response = readGenericResponse(null);
            auxHandle = response.getObjectHandle();
            final byte[] data = response.getData();
            // bytes 0 - 1: sin family (ignore)
            // bytes 2 - 3: sin port (port to connect to)
            port = ((data[2] & 0xFF) << 8) + (data[3] & 0xFF);
        // remaining bytes: IP address + other info(?) (ignore, can't be trusted, and invalid in FB3 and higher; always use original hostname)
        } catch (IOException ex) {
            throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(ex).toSQLException();
        }
    }
    final FbWireAsynchronousChannel channel = protocolDescriptor.createAsynchronousChannel(this);
    channel.connect(connection.getServerName(), port, auxHandle);
    return channel;
}
Also used : FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException)

Example 5 with XdrOutputStream

use of org.firebirdsql.gds.impl.wire.XdrOutputStream in project jaybird by FirebirdSQL.

the class V10Database method doReleaseObjectPacket.

/**
 * Sends - without flushing - the (release) operation and objectId.
 *
 * @param operation
 *         Operation
 * @param objectId
 *         Id of the object to release
 * @throws IOException
 *         For errors writing to the connection
 * @throws SQLException
 *         If the database connection is not available
 */
protected final void doReleaseObjectPacket(int operation, int objectId) throws IOException, SQLException {
    final XdrOutputStream xdrOut = getXdrOut();
    xdrOut.writeInt(operation);
    xdrOut.writeInt(objectId);
}
Also used : XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream)

Aggregations

XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)41 SQLException (java.sql.SQLException)25 IOException (java.io.IOException)24 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)22 SimpleServer (org.firebirdsql.common.SimpleServer)4 Encoding (org.firebirdsql.encodings.Encoding)3 XdrInputStream (org.firebirdsql.gds.impl.wire.XdrInputStream)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 SQLNonTransientConnectionException (java.sql.SQLNonTransientConnectionException)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)2 GenericResponse (org.firebirdsql.gds.ng.wire.GenericResponse)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 BitSet (java.util.BitSet)1 WireCrypt (org.firebirdsql.gds.ng.WireCrypt)1 ClientAuthBlock (org.firebirdsql.gds.ng.wire.auth.ClientAuthBlock)1 EncryptionIdentifier (org.firebirdsql.gds.ng.wire.crypt.EncryptionIdentifier)1