Search in sources :

Example 16 with NioClientSocketChannelFactory

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));
}
Also used : NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) LBShutdownRequestPacket(org.mobicents.tools.heartbeat.packets.LBShutdownRequestPacket) ClientPipelineFactory(org.mobicents.tools.heartbeat.client.ClientPipelineFactory)

Example 17 with NioClientSocketChannelFactory

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());
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress)

Example 18 with NioClientSocketChannelFactory

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();
        }
    };
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) Channel(org.jboss.netty.channel.Channel) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap)

Example 19 with NioClientSocketChannelFactory

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);
}
Also used : NioClientBossPool(org.jboss.netty.channel.socket.nio.NioClientBossPool) NioWorkerPool(org.jboss.netty.channel.socket.nio.NioWorkerPool) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ExecutorService(java.util.concurrent.ExecutorService) PinpointThreadFactory(com.navercorp.pinpoint.common.util.PinpointThreadFactory)

Example 20 with NioClientSocketChannelFactory

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");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) TerasologyClientPipelineFactory(org.terasology.network.internal.pipelineFactory.TerasologyClientPipelineFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)25 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)19 InetSocketAddress (java.net.InetSocketAddress)11 ChannelFuture (org.jboss.netty.channel.ChannelFuture)9 NioClientBossPool (org.jboss.netty.channel.socket.nio.NioClientBossPool)5 NioWorkerPool (org.jboss.netty.channel.socket.nio.NioWorkerPool)5 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)4 ExecutorService (java.util.concurrent.ExecutorService)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 Channel (org.jboss.netty.channel.Channel)3 ChannelFactory (org.jboss.netty.channel.ChannelFactory)3 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)3 BossPool (org.jboss.netty.channel.socket.nio.BossPool)3 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)3 WorkerPool (org.jboss.netty.channel.socket.nio.WorkerPool)3 HashedWheelTimer (org.jboss.netty.util.HashedWheelTimer)3 ClientPipelineFactory (org.mobicents.tools.heartbeat.client.ClientPipelineFactory)3 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)2 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)2 Slf4JLoggerFactory (org.jboss.netty.logging.Slf4JLoggerFactory)2