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());
}
}
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());
}
}
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;
}
}
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;
}
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);
}
Aggregations