Search in sources :

Example 1 with Client

use of com.jme3.network.Client 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 2 with Client

use of com.jme3.network.Client 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 Client

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

the class TestRemoteCall method main.

public static void main(String[] args) throws IOException, InterruptedException {
    Serializer.registerClass(Savable.class, new SavableSerializer());
    createServer();
    Client client = Network.connectToServer("localhost", 5110);
    client.start();
    ObjectStore store = new ObjectStore(client);
    ServerAccess access = store.getExposedObject("access", ServerAccess.class, true);
    boolean result = access.attachChild("Models/Oto/Oto.mesh.xml");
    System.out.println(result);
}
Also used : SavableSerializer(com.jme3.network.serializing.serializers.SavableSerializer) ObjectStore(com.jme3.network.rmi.ObjectStore) Client(com.jme3.network.Client)

Example 4 with Client

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

the class Network method connectToServer.

/**
     *  Creates a Client that communicates with the specified host and and separate TCP and UDP ports
     *  using both reliable and fast transports.  
     *  
     *  @param gameName This is the name that identifies the game.  This must match
     *                  the target server's name or this client will be turned away.
     *  @param version  This is a game-specific verison that helps detect when out-of-date
     *                  clients have connected to an incompatible server.  This must match
     *                  the server's version of this client will be turned away.
     *  @param hostPort  The remote TCP port on the server to which this client should
     *                  send reliable messages. 
     *  @param remoteUdpPort  The remote UDP port on the server to which this client should
     *                  send 'fast'/unreliable messages.   Set to -1 if 'fast' traffic should 
     *                  go over TCP.  This will completely disable UDP traffic for this
     *                  client.
     */
public static Client connectToServer(String gameName, int version, String host, int hostPort, int remoteUdpPort) throws IOException {
    InetAddress remoteAddress = InetAddress.getByName(host);
    UdpConnector fast = remoteUdpPort == -1 ? null : new UdpConnector(remoteAddress, remoteUdpPort);
    SocketConnector reliable = new SocketConnector(remoteAddress, hostPort);
    return new DefaultClient(gameName, version, reliable, fast, new TcpConnectorFactory(remoteAddress));
}
Also used : UdpConnector(com.jme3.network.kernel.udp.UdpConnector) DefaultClient(com.jme3.network.base.DefaultClient) InetAddress(java.net.InetAddress) SocketConnector(com.jme3.network.kernel.tcp.SocketConnector) TcpConnectorFactory(com.jme3.network.base.TcpConnectorFactory)

Example 5 with Client

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

Aggregations

Client (com.jme3.network.Client)3 ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)2 Server (com.jme3.network.Server)1 DefaultClient (com.jme3.network.base.DefaultClient)1 TcpConnectorFactory (com.jme3.network.base.TcpConnectorFactory)1 Endpoint (com.jme3.network.kernel.Endpoint)1 SocketConnector (com.jme3.network.kernel.tcp.SocketConnector)1 UdpConnector (com.jme3.network.kernel.udp.UdpConnector)1 ChannelInfoMessage (com.jme3.network.message.ChannelInfoMessage)1 SerializerRegistrationsMessage (com.jme3.network.message.SerializerRegistrationsMessage)1 ObjectStore (com.jme3.network.rmi.ObjectStore)1 SavableSerializer (com.jme3.network.serializing.serializers.SavableSerializer)1 ObjectMessageDelegator (com.jme3.network.util.ObjectMessageDelegator)1 SessionDataDelegator (com.jme3.network.util.SessionDataDelegator)1 InetAddress (java.net.InetAddress)1