use of org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory in project load-balancer by RestComm.
the class Server method sendPacket.
@Override
public void sendPacket(String command, String host, int port) {
this.nioClientSocketChannelFactory = new NioClientSocketChannelFactory(executor, executor);
this.clientBootstrap = new ClientBootstrap(nioClientSocketChannelFactory);
this.clientBootstrap.setPipelineFactory(new ClientPipelineFactory(serverListener));
future = clientBootstrap.connect(new InetSocketAddress(host, port));
switch(command) {
case Protocol.STOP:
packet = new LBShutdownRequestPacket(lbAddress.getHostAddress(), lbPort);
break;
}
future.awaitUninterruptibly();
future.getChannel().write(createRequest(command));
}
use of org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory in project bigbluebutton by bigbluebutton.
the class Client method connect.
/**
* Attempt to establish an authenticated connection to the nominated FreeSWITCH ESL server socket.
* This call will block, waiting for an authentication handshake to occur, or timeout after the
* supplied number of seconds.
*
* @param host can be either ip address or hostname
* @param port tcp port that server socket is listening on (set in event_socket_conf.xml)
* @param password server event socket is expecting (set in event_socket_conf.xml)
* @param timeoutSeconds number of seconds to wait for the server socket before aborting
*/
public void connect(String host, int port, String password, int timeoutSeconds) throws InboundConnectionFailure {
// If already connected, disconnect first
if (canSend()) {
close();
}
// Configure this client
ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
// Add ESL handler and factory
InboundClientHandler handler = new InboundClientHandler(password, protocolListener);
bootstrap.setPipelineFactory(new InboundPipelineFactory(handler));
// Attempt connection
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
// Wait till attempt succeeds, fails or timeouts
if (!future.awaitUninterruptibly(timeoutSeconds, TimeUnit.SECONDS)) {
throw new InboundConnectionFailure("Timeout connecting to " + host + ":" + port);
}
// Did not timeout
channel = future.getChannel();
// But may have failed anyway
if (!future.isSuccess()) {
log.warn("Failed to connect to [{}:{}]", host, port);
log.warn(" * reason: {}", future.getCause());
channel = null;
bootstrap.releaseExternalResources();
throw new InboundConnectionFailure("Could not connect to " + host + ":" + port, future.getCause());
}
// Wait for the authentication handshake to call back
while (!authenticatorResponded.get()) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
// ignore
}
}
if (!authenticated) {
throw new InboundConnectionFailure("Authentication failed: " + authenticationResponse.getReplyText());
}
}
use of org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory in project neo4j by neo4j.
the class Client method start.
@Override
public void start() {
bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(newCachedThreadPool(daemon(getClass().getSimpleName() + "-boss@" + destination)), newCachedThreadPool(daemon(getClass().getSimpleName() + "-worker@" + destination))));
bootstrap.setPipelineFactory(this);
channelPool = new ResourcePool<ChannelContext>(maxUnusedChannels, new ResourcePool.CheckStrategy.TimeoutCheckStrategy(DEFAULT_CHECK_INTERVAL, Clocks.systemClock()), new LoggingResourcePoolMonitor(msgLog)) {
@Override
protected ChannelContext create() {
msgLog.info(threadInfo() + "Trying to open a new channel from " + origin + " to " + destination, true);
// We must specify the origin address in case the server has multiple IPs per interface
ChannelFuture channelFuture = bootstrap.connect(destination, origin);
channelFuture.awaitUninterruptibly(5, TimeUnit.SECONDS);
if (channelFuture.isSuccess()) {
msgLog.info(threadInfo() + "Opened a new channel from " + channelFuture.getChannel().getLocalAddress() + " to " + channelFuture.getChannel().getRemoteAddress());
return new ChannelContext(channelFuture.getChannel(), ChannelBuffers.dynamicBuffer(), ByteBuffer.allocate(1024 * 1024));
}
Throwable cause = channelFuture.getCause();
String msg = Client.this.getClass().getSimpleName() + " could not connect from " + origin + " to " + destination;
msgLog.debug(msg, true);
throw traceComException(new ComException(msg, cause), "Client.start");
}
@Override
protected boolean isAlive(ChannelContext context) {
return context.channel().isConnected();
}
@Override
protected void dispose(ChannelContext context) {
Channel channel = context.channel();
if (channel.isConnected()) {
msgLog.info(threadInfo() + "Closing: " + context + ". " + "Channel pool size is now " + currentSize());
channel.close();
}
}
private String threadInfo() {
return "Thread[" + Thread.currentThread().getId() + ", " + Thread.currentThread().getName() + "] ";
}
};
/*
* This is here to couple the channel releasing to Response.close() itself and not
* to TransactionStream.close() as it is implemented here. The reason is that a Response
* that is returned without a TransactionStream will still hold the channel and should
* release it eventually. Also, logically, closing the channel is not dependent on the
* TransactionStream.
*/
resourcePoolReleaser = () -> {
if (channelPool != null) {
channelPool.release();
}
};
}
use of org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory in project pinpoint by naver.
the class DefaultPinpointClientFactory method createChannelFactory.
private NioClientSocketChannelFactory createChannelFactory(int bossCount, int workerCount, Timer timer) {
ExecutorService boss = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Client-Boss", true));
NioClientBossPool bossPool = new NioClientBossPool(boss, bossCount, timer, ThreadNameDeterminer.CURRENT);
ExecutorService worker = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Client-Worker", true));
NioWorkerPool workerPool = new NioWorkerPool(worker, workerCount, ThreadNameDeterminer.CURRENT);
return new NioClientSocketChannelFactory(bossPool, workerPool);
}
use of org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory in project Terasology by MovingBlocks.
the class NetworkSystemImpl method join.
@Override
public JoinStatus join(String address, int port) throws InterruptedException {
if (mode == NetworkMode.NONE) {
if (hibernationSettings.isPresent()) {
hibernationSettings.get().setHibernationAllowed(false);
}
factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setPipelineFactory(new TerasologyClientPipelineFactory(this));
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
ChannelFuture connectCheck = bootstrap.connect(new InetSocketAddress(address, port));
try {
connectCheck.await();
} catch (InterruptedException e) {
connectCheck.cancel();
connectCheck.getChannel().getCloseFuture().awaitUninterruptibly();
factory.releaseExternalResources();
throw e;
}
if (!connectCheck.isSuccess()) {
logger.warn("Failed to connect to server", connectCheck.getCause());
connectCheck.getChannel().getCloseFuture().awaitUninterruptibly();
factory.releaseExternalResources();
return new JoinStatusImpl("Failed to connect to server - " + connectCheck.getCause().getMessage());
} else {
allChannels.add(connectCheck.getChannel());
ClientConnectionHandler connectionHandler = connectCheck.getChannel().getPipeline().get(ClientConnectionHandler.class);
if (connectionHandler == null) {
JoinStatusImpl status = new JoinStatusImpl();
status.setComplete();
return status;
} else {
return connectionHandler.getJoinStatus();
}
}
}
return new JoinStatusImpl("Network system already active");
}
Aggregations