Search in sources :

Example 1 with HostingFailedException

use of org.terasology.network.exceptions.HostingFailedException in project Terasology by MovingBlocks.

the class StartServer method step.

@Override
public boolean step() {
    try {
        Config config = context.get(Config.class);
        int port = config.getNetwork().getServerPort();
        context.get(NetworkSystem.class).host(port, dedicated);
    } catch (HostingFailedException e) {
        context.get(NUIManager.class).pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Failed to Host", e.getMessage() + " - Reverting to single player");
    }
    return true;
}
Also used : Config(org.terasology.config.Config) HostingFailedException(org.terasology.network.exceptions.HostingFailedException) NetworkSystem(org.terasology.network.NetworkSystem) NUIManager(org.terasology.rendering.nui.NUIManager)

Example 2 with HostingFailedException

use of org.terasology.network.exceptions.HostingFailedException in project Terasology by MovingBlocks.

the class NetworkSystemImpl method host.

@Override
public void host(int port, boolean dedicatedServer) throws HostingFailedException {
    if (mode == NetworkMode.NONE) {
        try {
            if (hibernationSettings.isPresent()) {
                hibernationSettings.get().setHibernationAllowed(false);
            }
            mode = dedicatedServer ? NetworkMode.DEDICATED_SERVER : NetworkMode.LISTEN_SERVER;
            for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
                registerNetworkEntity(entity);
            }
            generateSerializationTables();
            factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
            ServerBootstrap bootstrap = new ServerBootstrap(factory);
            bootstrap.setPipelineFactory(new TerasologyServerPipelineFactory(this));
            bootstrap.setOption("child.tcpNoDelay", true);
            bootstrap.setOption("child.keepAlive", true);
            Channel listenChannel = bootstrap.bind(new InetSocketAddress(port));
            allChannels.add(listenChannel);
            logger.info("Started server on port {}", port);
            if (config.getServerMOTD() != null) {
                logger.info("Server MOTD is \"{}\"", config.getServerMOTD());
            } else {
                logger.info("No server MOTD is defined");
            }
            // enumerate all network interfaces that listen
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface ifc = interfaces.nextElement();
                if (!ifc.isLoopback()) {
                    for (InterfaceAddress ifadr : ifc.getInterfaceAddresses()) {
                        InetAddress adr = ifadr.getAddress();
                        logger.info("Listening on network interface \"{}\", hostname \"{}\" ({})", ifc.getDisplayName(), adr.getCanonicalHostName(), adr.getHostAddress());
                    }
                }
            }
            nextNetworkTick = time.getRealTimeInMs();
        } catch (SocketException e) {
            throw new HostingFailedException("Could not identify network interfaces", e);
        } catch (ChannelException e) {
            if (e.getCause() instanceof BindException) {
                throw new HostingFailedException("Port already in use (are you already hosting a game?)", e.getCause());
            } else {
                throw new HostingFailedException("Failed to host game", e.getCause());
            }
        }
    }
}
Also used : SocketException(java.net.SocketException) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) InterfaceAddress(java.net.InterfaceAddress) HostingFailedException(org.terasology.network.exceptions.HostingFailedException) Channel(org.jboss.netty.channel.Channel) TerasologyServerPipelineFactory(org.terasology.network.internal.pipelineFactory.TerasologyServerPipelineFactory) NetworkInterface(java.net.NetworkInterface) BindException(java.net.BindException) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) EntityRef(org.terasology.entitySystem.entity.EntityRef) InetAddress(java.net.InetAddress) ChannelException(org.jboss.netty.channel.ChannelException)

Aggregations

HostingFailedException (org.terasology.network.exceptions.HostingFailedException)2 BindException (java.net.BindException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 InterfaceAddress (java.net.InterfaceAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)1 Channel (org.jboss.netty.channel.Channel)1 ChannelException (org.jboss.netty.channel.ChannelException)1 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)1 Config (org.terasology.config.Config)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 NetworkSystem (org.terasology.network.NetworkSystem)1 TerasologyServerPipelineFactory (org.terasology.network.internal.pipelineFactory.TerasologyServerPipelineFactory)1 NUIManager (org.terasology.rendering.nui.NUIManager)1