Search in sources :

Example 1 with Server

use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.

the class NioEndpoint method close.

public void close(boolean flushData) {
    if (flushData) {
        closing = true;
        // Enqueue a close marker message to let the server
        // know we should close
        send(CLOSE_MARKER, false, true);
        return;
    }
    try {
        // Note: even though we may be disconnected from the socket.isConnected()
        // standpoint, it's still safest to tell the kernel so that it can be sure
        // to stop managing us gracefully.
        kernel.closeEndpoint(this);
    } catch (IOException e) {
        throw new KernelException("Error closing endpoint for socket:" + socket, e);
    }
}
Also used : IOException(java.io.IOException) KernelException(com.jme3.network.kernel.KernelException)

Example 2 with Server

use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.

the class RpcHostedService method onInitialize.

/**
     *  Used internally to setup the message delegator that will
     *  handle HostedConnection specific messages and forward them
     *  to that connection's RpcConnection.
     */
@Override
protected void onInitialize(HostedServiceManager serviceManager) {
    Server server = serviceManager.getServer();
    // A general listener for forwarding the messages
    // to the client-specific handler
    this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true);
    server.addMessageListener(delegator, delegator.getMessageTypes());
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes()));
    }
}
Also used : Server(com.jme3.network.Server) SessionDataDelegator(com.jme3.network.util.SessionDataDelegator)

Example 3 with Server

use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.

the class RpcHostedService method terminate.

/**
     *  Used internally to remove the message delegator from the
     *  server.
     */
@Override
public void terminate(HostedServiceManager serviceManager) {
    Server server = serviceManager.getServer();
    server.removeMessageListener(delegator, delegator.getMessageTypes());
}
Also used : Server(com.jme3.network.Server)

Example 4 with Server

use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.

the class DefaultServer method addChannel.

@Override
public int addChannel(int port) {
    if (isRunning)
        throw new IllegalStateException("Channels cannot be added once server is started.");
    // Check for consistency with the channels list
    if (channels.size() - CH_FIRST != alternatePorts.size())
        throw new IllegalStateException("Channel and port lists do not match.");
    try {
        int result = alternatePorts.size();
        alternatePorts.add(port);
        Kernel kernel = kernelFactory.createKernel(result, port);
        channels.add(new KernelAdapter(this, kernel, dispatcher, true));
        return result;
    } catch (IOException e) {
        throw new RuntimeException("Error adding channel for port:" + port, e);
    }
}
Also used : IOException(java.io.IOException) Kernel(com.jme3.network.kernel.Kernel) Endpoint(com.jme3.network.kernel.Endpoint)

Example 5 with Server

use of com.jme3.network.Server in project jmonkeyengine by jMonkeyEngine.

the class DefaultServer method registerClient.

protected void registerClient(KernelAdapter ka, Endpoint p, ClientRegistrationMessage m) {
    Connection addedConnection = null;
    // important enough I won't take chances
    synchronized (this) {
        // Grab the random ID that the client created when creating
        // its two registration messages
        long tempId = m.getId();
        // See if we already have one
        Connection c = connecting.remove(tempId);
        if (c == null) {
            c = new Connection(channels.size());
            log.log(Level.FINE, "Registering client for endpoint, pass 1:{0}.", p);
        } else {
            log.log(Level.FINE, "Refining client registration for endpoint:{0}.", p);
        }
        // Fill in what we now know
        int channel = getChannel(ka);
        c.setChannel(channel, p);
        log.log(Level.FINE, "Setting up channel:{0}", channel);
        // and we will send the connection information
        if (channel == CH_RELIABLE) {
            // over the reliable connection at this point.
            if (!getGameName().equals(m.getGameName()) || getVersion() != m.getVersion()) {
                log.log(Level.FINE, "Kicking client due to name/version mismatch:{0}.", c);
                // Need to kick them off... I may regret doing this from within
                // the sync block but the alternative is more code
                c.close("Server client mismatch, server:" + getGameName() + " v" + getVersion() + "  client:" + m.getGameName() + " v" + m.getVersion());
                return;
            }
            // Else send the extra channel information to the client
            if (!alternatePorts.isEmpty()) {
                ChannelInfoMessage cim = new ChannelInfoMessage(m.getId(), alternatePorts);
                c.send(cim);
            }
        }
        if (c.isComplete()) {
            // Then we are fully connected
            if (connections.put(c.getId(), c) == null) {
                for (Endpoint cp : c.channels) {
                    if (cp == null)
                        continue;
                    endpointConnections.put(cp, c);
                }
                addedConnection = c;
            }
        } else {
            // Need to keep getting channels so we'll keep it in
            // the map
            connecting.put(tempId, c);
        }
    }
    // over synchronizing which is the path to deadlocks
    if (addedConnection != null) {
        log.log(Level.FINE, "Client registered:{0}.", addedConnection);
        // Send the ID back to the client letting it know it's
        // fully connected.
        m = new ClientRegistrationMessage();
        m.setId(addedConnection.getId());
        m.setReliable(true);
        addedConnection.send(m);
        // Now we can notify the listeners about the
        // new connection.
        fireConnectionAdded(addedConnection);
        // Send a second registration message with an invalid ID
        // to let the connection know that it can start its services
        m = new ClientRegistrationMessage();
        m.setId(-1);
        m.setReliable(true);
        addedConnection.send(m);
    }
}
Also used : Endpoint(com.jme3.network.kernel.Endpoint) ChannelInfoMessage(com.jme3.network.message.ChannelInfoMessage) Endpoint(com.jme3.network.kernel.Endpoint) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Aggregations

Server (com.jme3.network.Server)3 ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)3 IOException (java.io.IOException)3 HostedConnection (com.jme3.network.HostedConnection)2 Endpoint (com.jme3.network.kernel.Endpoint)2 SimpleApplication (com.jme3.app.SimpleApplication)1 DefaultClient (com.jme3.network.base.DefaultClient)1 DefaultServer (com.jme3.network.base.DefaultServer)1 TcpConnectorFactory (com.jme3.network.base.TcpConnectorFactory)1 Kernel (com.jme3.network.kernel.Kernel)1 KernelException (com.jme3.network.kernel.KernelException)1 SelectorKernel (com.jme3.network.kernel.tcp.SelectorKernel)1 SocketConnector (com.jme3.network.kernel.tcp.SocketConnector)1 UdpConnector (com.jme3.network.kernel.udp.UdpConnector)1 UdpKernel (com.jme3.network.kernel.udp.UdpKernel)1 ChannelInfoMessage (com.jme3.network.message.ChannelInfoMessage)1 ObjectStore (com.jme3.network.rmi.ObjectStore)1 SerializerRegistration (com.jme3.network.serializing.SerializerRegistration)1 SessionDataDelegator (com.jme3.network.util.SessionDataDelegator)1 InetAddress (java.net.InetAddress)1