use of java.nio.channels.ServerSocketChannel in project heron by twitter.
the class ConnectTest method testStart.
/**
* Test connection
*/
@Test
public void testStart() 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) {
nioLooper.exitLoop();
threadsPool.shutdownNow();
PhysicalPlanHelper physicalPlanHelper = instanceControlMsg.getNewPhysicalPlanHelper();
Assert.assertEquals("test-bolt", physicalPlanHelper.getMyComponent());
Assert.assertEquals(InetAddress.getLocalHost().getHostName(), physicalPlanHelper.getMyHostname());
Assert.assertEquals(0, physicalPlanHelper.getMyInstanceIndex());
Assert.assertEquals(1, physicalPlanHelper.getMyTaskId());
break;
} else {
SysUtils.sleep(Constants.RETRY_INTERVAL_MS);
}
}
} catch (ClosedChannelException ignored) {
} finally {
close(socketChannel);
}
}
use of java.nio.channels.ServerSocketChannel in project heron by twitter.
the class HeronServerTest method testClose.
/**
* Method: stop()
*/
@Test
public void testClose() throws Exception {
runBase();
heronServer.stop();
Map<SocketChannel, SocketChannelHelper> activeConnections = heronServer.getActiveConnections();
ServerSocketChannel acceptChannel = heronServer.getAcceptChannel();
Assert.assertNotNull(acceptChannel);
Assert.assertTrue(!acceptChannel.isOpen());
Assert.assertNotNull(activeConnections);
Assert.assertEquals(0, activeConnections.size());
}
use of java.nio.channels.ServerSocketChannel in project voldemort by voldemort.
the class ServerTestUtilsTest method testFindFreePort.
// @Test
public void testFindFreePort() throws Exception {
ServerSocketChannel serverSocketChannel;
try {
serverSocketChannel = ServerSocketChannel.open();
} catch (IOException ioe) {
ioe.printStackTrace();
assertTrue(false);
return;
}
int port = ServerTestUtils.findFreePort();
try {
serverSocketChannel.socket().bind(new InetSocketAddress(port));
} catch (IOException ioe) {
ioe.printStackTrace();
assertTrue(false);
return;
}
assertTrue(true);
}
use of java.nio.channels.ServerSocketChannel in project eiger by wlloyd.
the class MessagingService method getServerSocket.
private List<ServerSocket> getServerSocket(InetAddress localEp) throws IOException, ConfigurationException {
final List<ServerSocket> ss = new ArrayList<ServerSocket>();
if (DatabaseDescriptor.getEncryptionOptions().internode_encryption != EncryptionOptions.InternodeEncryption.none) {
ss.add(SSLFactory.getServerSocket(DatabaseDescriptor.getEncryptionOptions(), localEp, DatabaseDescriptor.getSSLStoragePort()));
// setReuseAddress happens in the factory.
logger_.info("Starting Encrypted Messaging Service on SSL port {}", DatabaseDescriptor.getSSLStoragePort());
}
ServerSocketChannel serverChannel = ServerSocketChannel.open();
ServerSocket socket = serverChannel.socket();
socket.setReuseAddress(true);
InetSocketAddress address = new InetSocketAddress(localEp, DatabaseDescriptor.getStoragePort());
try {
socket.bind(address);
} catch (BindException e) {
if (e.getMessage().contains("in use"))
throw new ConfigurationException(address + " is in use by another process. Change listen_address:storage_port in cassandra.yaml to values that do not conflict with other services");
else if (e.getMessage().contains("Cannot assign requested address"))
throw new ConfigurationException("Unable to bind to address " + address + ". Set listen_address in cassandra.yaml to an interface you can bind to, e.g., your private IP address on EC2");
else
throw e;
}
logger_.info("Starting Messaging Service on port {}", DatabaseDescriptor.getStoragePort());
ss.add(socket);
return ss;
}
use of java.nio.channels.ServerSocketChannel in project zm-mailbox by Zimbra.
the class ZimbraSocketAcceptor method accept.
@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
return null;
}
// accept the connection from the client
SocketChannel ch = handle.accept();
if (ch == null) {
return null;
}
return new NioSocketSession(this, processor, ch);
}
Aggregations