Search in sources :

Example 1 with ClientRegistrationMessage

use of com.jme3.network.message.ClientRegistrationMessage in project jmonkeyengine by jMonkeyEngine.

the class DefaultClient method dispatch.

protected void dispatch(Message m) {
    if (log.isLoggable(Level.FINER)) {
        log.log(Level.FINER, "{0} received:{1}", new Object[] { this, m });
    }
    // interested in and then pass on the rest.
    if (m instanceof ClientRegistrationMessage) {
        ClientRegistrationMessage crm = (ClientRegistrationMessage) m;
        // See if it has a real ID
        if (crm.getId() >= 0) {
            // Then we've gotten our real id
            this.id = (int) crm.getId();
            log.log(Level.FINE, "Connection established, id:{0}.", this.id);
            connecting.countDown();
        //fireConnected();
        } else {
            // Else it's a message letting us know that the 
            // hosted services have been started
            startServices();
            // Delay firing 'connected' until the services have all
            // been started to avoid odd race conditions.  If there is some
            // need to get some kind of event before the services have been
            // started then we should create a new event step.               
            fireConnected();
        }
        return;
    } else if (m instanceof ChannelInfoMessage) {
        // This is an interum step in the connection process and
        // now we need to add a bunch of connections
        configureChannels(((ChannelInfoMessage) m).getId(), ((ChannelInfoMessage) m).getPorts());
        return;
    } else if (m instanceof DisconnectMessage) {
        // Can't do too much else yet
        String reason = ((DisconnectMessage) m).getReason();
        log.log(Level.SEVERE, "Connection terminated, reason:{0}.", reason);
        DisconnectInfo info = new DisconnectInfo();
        info.reason = reason;
        closeConnections(info);
    }
    // thread simultaneously.
    synchronized (this) {
        messageListeners.messageReceived(this, m);
    }
}
Also used : DisconnectMessage(com.jme3.network.message.DisconnectMessage) ChannelInfoMessage(com.jme3.network.message.ChannelInfoMessage) DisconnectInfo(com.jme3.network.ClientStateListener.DisconnectInfo) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 2 with ClientRegistrationMessage

use of com.jme3.network.message.ClientRegistrationMessage 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)

Example 3 with ClientRegistrationMessage

use of com.jme3.network.message.ClientRegistrationMessage in project jmonkeyengine by jMonkeyEngine.

the class DefaultClient method start.

@Override
public void start() {
    if (isRunning)
        throw new IllegalStateException("Client is already started.");
    // connectors that we have
    for (ConnectorAdapter ca : channels) {
        if (ca == null)
            continue;
        ca.start();
    }
    // Send our connection message with a generated ID until
    // we get one back from the server.  We'll hash time in
    // millis and time in nanos.
    // This is used to match the TCP and UDP endpoints up on the
    // other end since they may take different routes to get there.
    // Behind NAT, many game clients may be coming over the same
    // IP address from the server's perspective and they may have
    // their UDP ports mapped all over the place.
    //
    // Since currentTimeMillis() is absolute time and nano time
    // is roughtly related to system start time, adding these two
    // together should be plenty unique for our purposes.  It wouldn't
    // hurt to reconcile with IP on the server side, though.
    long tempId = System.currentTimeMillis() + System.nanoTime();
    // Set it true here so we can send some messages.
    isRunning = true;
    ClientRegistrationMessage reg;
    reg = new ClientRegistrationMessage();
    reg.setId(tempId);
    reg.setGameName(getGameName());
    reg.setVersion(getVersion());
    reg.setReliable(true);
    send(CH_RELIABLE, reg, false);
    // Send registration messages to any other configured
    // connectors
    reg = new ClientRegistrationMessage();
    reg.setId(tempId);
    reg.setReliable(false);
    for (int ch = CH_UNRELIABLE; ch < channels.size(); ch++) {
        if (channels.get(ch) == null)
            continue;
        send(ch, reg, false);
    }
}
Also used : ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 4 with ClientRegistrationMessage

use of com.jme3.network.message.ClientRegistrationMessage in project jmonkeyengine by jMonkeyEngine.

the class DefaultClient method configureChannels.

protected void configureChannels(long tempId, int[] ports) {
    try {
        for (int i = 0; i < ports.length; i++) {
            Connector c = connectorFactory.createConnector(i, ports[i]);
            ConnectorAdapter ca = new ConnectorAdapter(c, dispatcher, dispatcher, true);
            int ch = channels.size();
            channels.add(ca);
            // Need to send the connection its hook-up registration
            // and start it.
            ca.start();
            ClientRegistrationMessage reg;
            reg = new ClientRegistrationMessage();
            reg.setId(tempId);
            reg.setReliable(true);
            send(ch, reg, false);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error configuring channels", e);
    }
}
Also used : Connector(com.jme3.network.kernel.Connector) IOException(java.io.IOException) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 5 with ClientRegistrationMessage

use of com.jme3.network.message.ClientRegistrationMessage in project jmonkeyengine by jMonkeyEngine.

the class KernelAdapter method dispatch.

/**
     *  Note on threading for those writing their own server 
     *  or adapter implementations.  The rule that a single connection be 
     *  processed by only one thread at a time is more about ensuring that
     *  the messages are delivered in the order that they are received
     *  than for any user-code safety.  99% of the time the user code should
     *  be writing for multithreaded access anyway.
     *
     *  <p>The issue with the messages is that if a an implementation is
     *  using a general thread pool then it would be possible for a 
     *  naive implementation to have one thread grab an Envelope from
     *  connection 1's and another grab the next Envelope.  Since an Envelope
     *  may contain several messages, delivering the second thread's messages
     *  before or during the first's would be really confusing and hard
     *  to code for in user code.</p>
     *
     *  <p>And that's why this note is here.  DefaultServer does a rudimentary
     *  per-connection locking but it couldn't possibly guard against
     *  out of order Envelope processing.</p>    
     */
protected void dispatch(Endpoint p, Message m) {
    // here.
    if (m instanceof ClientRegistrationMessage) {
        server.registerClient(this, p, (ClientRegistrationMessage) m);
        return;
    }
    try {
        HostedConnection source = getConnection(p);
        if (source == null) {
            if (reliable) {
                // If it's a reliable connection then it's slightly more
                // concerning but this can happen all the time for a UDP endpoint.
                log.log(Level.WARNING, "Recieved message from unconnected endpoint:" + p + "  message:" + m);
            }
            return;
        }
        messageDispatcher.messageReceived(source, m);
    } catch (Exception e) {
        reportError(p, m, e);
    }
}
Also used : HostedConnection(com.jme3.network.HostedConnection) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Aggregations

ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)5 ChannelInfoMessage (com.jme3.network.message.ChannelInfoMessage)2 DisconnectInfo (com.jme3.network.ClientStateListener.DisconnectInfo)1 HostedConnection (com.jme3.network.HostedConnection)1 Connector (com.jme3.network.kernel.Connector)1 Endpoint (com.jme3.network.kernel.Endpoint)1 DisconnectMessage (com.jme3.network.message.DisconnectMessage)1 IOException (java.io.IOException)1