Search in sources :

Example 1 with TerasologyClientPipelineFactory

use of org.terasology.network.internal.pipelineFactory.TerasologyClientPipelineFactory 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

InetSocketAddress (java.net.InetSocketAddress)1 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)1 ChannelFuture (org.jboss.netty.channel.ChannelFuture)1 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)1 TerasologyClientPipelineFactory (org.terasology.network.internal.pipelineFactory.TerasologyClientPipelineFactory)1