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