Search in sources :

Example 96 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project wifikeyboard by IvanVolosyuk.

the class HttpService method doStartServer.

public static void doStartServer(HttpService context) {
    ServerSocketChannel socket = makeSocket(context);
    context.port = socket.socket().getLocalPort();
    server = new KeyboardHttpServer(context, socket);
    Editor editor = context.getSharedPreferences("port", MODE_PRIVATE).edit();
    editor.putInt("port", context.port);
    editor.commit();
    try {
        if (context.portUpdateListener != null) {
            context.portUpdateListener.portUpdated(context.port);
        }
    } catch (RemoteException e) {
        Log.e("wifikeyboard", "port update failure", e);
    }
    context.updateNotification(true);
    server.start();
}
Also used : Editor(android.content.SharedPreferences.Editor) RemoteException(android.os.RemoteException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 97 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project voldemort by voldemort.

the class ServerTestUtilsTest method testFindFreePorts.

// @Test
public void testFindFreePorts() throws Exception {
    int numPorts = 25000;
    ServerSocketChannel[] serverSocketChannel = new ServerSocketChannel[numPorts];
    int[] ports = ServerTestUtils.findFreePorts(numPorts);
    for (int i = 0; i < numPorts; i++) {
        boolean bound = false;
        while (!bound) {
            try {
                serverSocketChannel[i] = ServerSocketChannel.open();
                serverSocketChannel[i].socket().bind(new InetSocketAddress(ports[i]));
                serverSocketChannel[i].socket().setReuseAddress(true);
                bound = true;
            } catch (IOException ioe) {
                System.err.println("Attempt: " + i + ", port: " + ports[i]);
                ioe.printStackTrace();
                Thread.sleep(10);
            }
        }
    }
    for (int i = 0; i < numPorts; i++) {
        try {
            serverSocketChannel[i].socket().close();
        } catch (IOException ioe) {
            System.err.println("Attempt: " + i + ", port: " + ports[i]);
            ioe.printStackTrace();
            assertTrue(false);
            return;
        }
    }
    assertTrue(true);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ServerSocketChannel(java.nio.channels.ServerSocketChannel)

Example 98 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project heron by twitter.

the class HandleWriteTest method testHandleWrite.

/**
   * Test write into network
   */
@Test
public void testHandleWrite() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
        for (int i = 0; i < 10; i++) {
            // We randomly choose some messages writing to stream mgr
            streamManagerClient.sendMessage(UnitTestHelper.getRegisterInstanceResponse());
        }
        for (int i = 0; i < 10; i++) {
            incomingPacket = new IncomingPacket();
            while (incomingPacket.readFromChannel(socketChannel) != 0) {
                // 1ms sleep to mitigate busy looping
                SysUtils.sleep(1);
            }
            typeName = incomingPacket.unpackString();
            rid = incomingPacket.unpackREQID();
            StreamManager.RegisterInstanceResponse.Builder builder = StreamManager.RegisterInstanceResponse.newBuilder();
            incomingPacket.unpackMessage(builder);
            StreamManager.RegisterInstanceResponse response = builder.build();
            Assert.assertNotNull(response);
            Assert.assertTrue(response.isInitialized());
            Assert.assertEquals(Common.StatusCode.OK, response.getStatus().getStatus());
            Assert.assertEquals(1, response.getPplan().getStmgrsCount());
            Assert.assertEquals(2, response.getPplan().getInstancesCount());
            Assert.assertEquals(1, response.getPplan().getTopology().getBoltsCount());
            Assert.assertEquals(1, response.getPplan().getTopology().getSpoutsCount());
            Assert.assertEquals(TopologyAPI.TopologyState.RUNNING, response.getPplan().getTopology().getState());
        }
        nioLooper.exitLoop();
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) InetSocketAddress(java.net.InetSocketAddress) StreamManager(com.twitter.heron.proto.stmgr.StreamManager) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Example 99 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project heron by twitter.

the class HeronServerTest method testHandleAccept.

/**
   * Method: handleAccept(SelectableChannel channel)
   */
@Test
public void testHandleAccept() throws Exception {
    runBase();
    Map<SocketChannel, SocketChannelHelper> activeConnections = heronServer.getActiveConnections();
    ServerSocketChannel acceptChannel = heronServer.getAcceptChannel();
    Assert.assertNotNull(acceptChannel);
    Assert.assertTrue(acceptChannel.isOpen());
    Assert.assertNotNull(activeConnections);
    Assert.assertEquals(1, activeConnections.size());
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Test(org.junit.Test)

Example 100 with ServerSocketChannel

use of java.nio.channels.ServerSocketChannel in project heron by twitter.

the class HandleReadTest method testHandleRead.

/**
   * Test reading from network
   */
@Test
public void testHandleRead() throws Exception {
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.socket().bind(new InetSocketAddress(HOST, serverPort));
    SocketChannel socketChannel = null;
    try {
        runStreamManagerClient();
        socketChannel = serverSocketChannel.accept();
        configure(socketChannel);
        socketChannel.configureBlocking(false);
        close(serverSocketChannel);
        // Receive request
        IncomingPacket incomingPacket = new IncomingPacket();
        while (incomingPacket.readFromChannel(socketChannel) != 0) {
            // 1ms sleep to mitigate busy looping
            SysUtils.sleep(1);
        }
        // Send back response
        // Though we do not use typeName, we need to unpack it first,
        // since the order is required
        String typeName = incomingPacket.unpackString();
        REQID rid = incomingPacket.unpackREQID();
        OutgoingPacket outgoingPacket = new OutgoingPacket(rid, UnitTestHelper.getRegisterInstanceResponse());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            InstanceControlMsg instanceControlMsg = inControlQueue.poll();
            if (instanceControlMsg != null) {
                break;
            } else {
                SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
            }
        }
        outgoingPacket = new OutgoingPacket(REQID.zeroREQID, constructMockMessage());
        outgoingPacket.writeToChannel(socketChannel);
        for (int i = 0; i < Constants.RETRY_TIMES; i++) {
            if (!inStreamQueue.isEmpty()) {
                break;
            }
            SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
        }
        nioLooper.exitLoop();
        Assert.assertEquals(1, inStreamQueue.size());
        HeronTuples.HeronTupleSet msg = inStreamQueue.poll();
        HeronTuples.HeronTupleSet heronTupleSet = msg;
        Assert.assertTrue(heronTupleSet.hasData());
        Assert.assertFalse(heronTupleSet.hasControl());
        HeronTuples.HeronDataTupleSet heronDataTupleSet = heronTupleSet.getData();
        Assert.assertEquals("test-spout", heronDataTupleSet.getStream().getComponentName());
        Assert.assertEquals("default", heronDataTupleSet.getStream().getId());
        String res = "";
        for (HeronTuples.HeronDataTuple heronDataTuple : heronDataTupleSet.getTuplesList()) {
            res += heronDataTuple.getValues(0).toStringUtf8();
            Assert.assertEquals(1, heronDataTuple.getRootsCount());
        }
        Assert.assertEquals("ABABABABAB", res);
    } catch (ClosedChannelException ignored) {
    } finally {
        close(socketChannel);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) IncomingPacket(com.twitter.heron.common.network.IncomingPacket) InstanceControlMsg(com.twitter.heron.instance.InstanceControlMsg) InetSocketAddress(java.net.InetSocketAddress) ByteString(com.google.protobuf.ByteString) HeronTuples(com.twitter.heron.proto.system.HeronTuples) REQID(com.twitter.heron.common.network.REQID) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OutgoingPacket(com.twitter.heron.common.network.OutgoingPacket) Test(org.junit.Test)

Aggregations

ServerSocketChannel (java.nio.channels.ServerSocketChannel)114 SocketChannel (java.nio.channels.SocketChannel)62 InetSocketAddress (java.net.InetSocketAddress)52 IOException (java.io.IOException)41 ByteBuffer (java.nio.ByteBuffer)26 SelectionKey (java.nio.channels.SelectionKey)19 ServerSocket (java.net.ServerSocket)18 Selector (java.nio.channels.Selector)13 Socket (java.net.Socket)12 Test (org.junit.Test)11 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InetAddress (java.net.InetAddress)9 ClosedSelectorException (java.nio.channels.ClosedSelectorException)8 SocketException (java.net.SocketException)6 Benchmark (org.openjdk.jmh.annotations.Benchmark)6 CancelledKeyException (java.nio.channels.CancelledKeyException)5 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)5 URISyntaxException (java.net.URISyntaxException)4 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4