Example usage for io.netty.channel ChannelFuture await

List of usage examples for io.netty.channel ChannelFuture await

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture await.

Prototype

boolean await(long timeout, TimeUnit unit) throws InterruptedException;

Source Link

Document

Waits for this future to be completed within the specified time limit.

Usage

From source file:org.spout.engine.SpoutClient.java

License:Open Source License

private boolean connnect() {
    // Connect to server to establish session
    Protocol protocol = null;// w  w w  . j a va 2  s.com
    if (getArguments().protocol != null) {
        protocol = Protocol.getProtocol(getArguments().protocol);
    }
    if (protocol == null) {
        protocol = Protocol.getProtocol("Spout");
    }
    String address;
    if (getArguments().server == null) {
        address = "localhost";
    } else {
        address = getArguments().server;
    }
    int port = getArguments().port != -1 ? getArguments().port : protocol.getDefaultPort();
    PortBindingImpl binding = new PortBindingImpl(protocol, new InetSocketAddress(address, port));
    ChannelFuture connect = bootstrap.connect(binding.getAddress());
    try {
        connect.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        getLogger().log(Level.SEVERE, "Connection took too long! Cancelling connect and stopping engine!");
        stop();
        return false;
    }

    Channel channel = connect.channel();
    if (!connect.isSuccess()) {
        getLogger().log(Level.SEVERE, "Could not connect to " + binding, connect.cause());
        return false;
    }

    getLogger().log(Level.INFO,
            "Connected to " + address + ":" + port + " with protocol " + protocol.getName());
    CommonHandler handler = channel.pipeline().get(CommonHandler.class);
    SpoutClientSession session = new SpoutClientSession(this, channel, protocol);
    handler.setSession(session);

    Class<? extends PlayerNetworkComponent> network = session.getProtocol().getClientNetworkComponent(session);
    final SpoutClientPlayer p = new SpoutClientPlayer(this, network, "Spouty",
            new Transform().setPosition(new Point(getWorld(), 1, 200, 1)));
    session.setPlayer(p);
    p.getNetwork().setSession(session);
    session.getProtocol().initializeClientSession(session);
    player.set(p);
    return true;
}