Search in sources :

Example 1 with SocketWriter

use of com.hazelcast.internal.networking.SocketWriter in project hazelcast by hazelcast.

the class TcpIpConnection_TransferStressBaseTest method testPackets.

private void testPackets(long verifyTimeoutInMillis) throws Exception {
    TcpIpConnection c = connect(connManagerA, addressB);
    WriteThread thread1 = new WriteThread(1, c);
    WriteThread thread2 = new WriteThread(2, c);
    logger.info("Starting");
    thread1.start();
    thread2.start();
    sleepAndStop(stop, WRITER_THREAD_RUNNING_TIME_IN_SECONDS);
    logger.info("Done");
    thread1.assertSucceedsEventually();
    thread2.assertSucceedsEventually();
    // there is always one packet extra for the bind-request
    final long expectedNormalPackets = thread1.normalPackets + thread2.normalPackets + 1;
    final long expectedUrgentPackets = thread1.urgentPackets + thread2.urgentPackets;
    logger.info("expected normal packets: " + expectedNormalPackets);
    logger.info("expected priority packets: " + expectedUrgentPackets);
    final SocketWriter writer = c.getSocketWriter();
    final SocketReader reader = ((TcpIpConnection) connManagerB.getConnection(addressA)).getSocketReader();
    long start = System.currentTimeMillis();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            logger.info("writer total frames pending   : " + writer.totalFramesPending());
            logger.info("writer last write time millis : " + writer.lastWriteTimeMillis());
            logger.info("reader total frames handled   : " + reader.getNormalFramesReadCounter().get() + reader.getPriorityFramesReadCounter().get());
            logger.info("reader last read time millis  : " + reader.lastReadTimeMillis());
            assertEquals(expectedNormalPackets, reader.getNormalFramesReadCounter().get());
            assertEquals(expectedUrgentPackets, reader.getPriorityFramesReadCounter().get());
        }
    }, verifyTimeoutInMillis);
    logger.info("Waiting for pending packets to be sent and received finished in " + (System.currentTimeMillis() - start) + " milliseconds");
}
Also used : SocketReader(com.hazelcast.internal.networking.SocketReader) AssertTask(com.hazelcast.test.AssertTask) SocketWriter(com.hazelcast.internal.networking.SocketWriter)

Example 2 with SocketWriter

use of com.hazelcast.internal.networking.SocketWriter in project hazelcast by hazelcast.

the class SocketReaderInitializerImpl method init.

@Override
public void init(TcpIpConnection connection, SocketReader reader) throws IOException {
    TcpIpConnectionManager connectionManager = connection.getConnectionManager();
    IOService ioService = connectionManager.getIoService();
    ByteBuffer protocolBuffer = reader.getProtocolBuffer();
    SocketChannelWrapper socketChannel = reader.getSocketChannel();
    int readBytes = socketChannel.read(protocolBuffer);
    if (readBytes == -1) {
        throw new EOFException("Could not read protocol type!");
    }
    if (readBytes == 0 && connectionManager.isSSLEnabled()) {
        // when using SSL, we can read 0 bytes since data read from socket can be handshake frames.
        return;
    }
    if (protocolBuffer.hasRemaining()) {
        // we have not yet received all protocol bytes
        return;
    }
    ReadHandler readHandler;
    String protocol = bytesToString(protocolBuffer.array());
    SocketWriter socketWriter = connection.getSocketWriter();
    if (CLUSTER.equals(protocol)) {
        initInputBuffer(connection, reader, ioService.getSocketReceiveBufferSize());
        connection.setType(MEMBER);
        socketWriter.setProtocol(CLUSTER);
        readHandler = ioService.createReadHandler(connection);
    } else if (CLIENT_BINARY_NEW.equals(protocol)) {
        initInputBuffer(connection, reader, ioService.getSocketClientReceiveBufferSize());
        socketWriter.setProtocol(CLIENT_BINARY_NEW);
        readHandler = new ClientReadHandler(reader.getNormalFramesReadCounter(), connection, ioService);
    } else {
        ByteBuffer inputBuffer = initInputBuffer(connection, reader, ioService.getSocketReceiveBufferSize());
        socketWriter.setProtocol(TEXT);
        inputBuffer.put(protocolBuffer.array());
        readHandler = new TextReadHandler(connection);
        connectionManager.incrementTextConnections();
    }
    if (readHandler == null) {
        throw new IOException("Could not initialize ReadHandler!");
    }
    reader.initReadHandler(readHandler);
}
Also used : IOService(com.hazelcast.nio.IOService) SocketChannelWrapper(com.hazelcast.internal.networking.SocketChannelWrapper) TextReadHandler(com.hazelcast.nio.ascii.TextReadHandler) ReadHandler(com.hazelcast.internal.networking.ReadHandler) EOFException(java.io.EOFException) StringUtil.bytesToString(com.hazelcast.util.StringUtil.bytesToString) IOException(java.io.IOException) SocketWriter(com.hazelcast.internal.networking.SocketWriter) TextReadHandler(com.hazelcast.nio.ascii.TextReadHandler) ByteBuffer(java.nio.ByteBuffer) IOUtil.newByteBuffer(com.hazelcast.nio.IOUtil.newByteBuffer)

Aggregations

SocketWriter (com.hazelcast.internal.networking.SocketWriter)2 ReadHandler (com.hazelcast.internal.networking.ReadHandler)1 SocketChannelWrapper (com.hazelcast.internal.networking.SocketChannelWrapper)1 SocketReader (com.hazelcast.internal.networking.SocketReader)1 IOService (com.hazelcast.nio.IOService)1 IOUtil.newByteBuffer (com.hazelcast.nio.IOUtil.newByteBuffer)1 TextReadHandler (com.hazelcast.nio.ascii.TextReadHandler)1 AssertTask (com.hazelcast.test.AssertTask)1 StringUtil.bytesToString (com.hazelcast.util.StringUtil.bytesToString)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1