com.jme3.network.HostedConnection Java Examples

The following examples show how to use com.jme3.network.HostedConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSerialization.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void messageReceived(HostedConnection source, Message m) {
    TestSerializationMessage cm = (TestSerializationMessage) m;
    System.out.println(cm.z);
    System.out.println(cm.b);
    System.out.println(cm.c);
    System.out.println(cm.s);
    System.out.println(cm.i);
    System.out.println(cm.f);
    System.out.println(cm.l);
    System.out.println(cm.d);
    System.out.println(Arrays.toString(cm.ia));
    System.out.println(cm.ls);
    System.out.println(cm.mp);
    System.out.println(cm.status1);
    System.out.println(cm.status2);
    System.out.println(cm.date);
}
 
Example #2
Source File: KernelAdapter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *  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 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 )
{
    // Because this class is the only one with the information
    // to do it... we need to pull of the registration message
    // 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, "Received message from unconnected endpoint:" + p + "  message:" + m );
            }                    
            return; 
        }
        messageDispatcher.messageReceived( source, m );
    } catch( Exception e ) {
        reportError(p, m, e);
    }
}
 
Example #3
Source File: TestChatServer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void messageReceived(HostedConnection source, Message m) {
    if (m instanceof ChatMessage) {
        // Keep track of the name just in case we 
        // want to know it for some other reason later and it's
        // a good example of session data
        source.setAttribute("name", ((ChatMessage) m).getName());

        System.out.println("Broadcasting:" + m + "  reliable:" + m.isReliable());

        // Just rebroadcast... the reliable flag will stay the
        // same so if it came in on UDP it will go out on that too
        source.getServer().broadcast(m);
    } else {
        System.err.println("Received odd message:" + m);
    }
}
 
Example #4
Source File: ObjectStore.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void onConnection(HostedConnection conn) {
    if (localObjects.size() > 0){
        // send a object definition message
        ObjectDef[] defs = new ObjectDef[localObjects.size()];
        int i = 0;
        for (Entry<LocalObject> entry : localObjects){
            defs[i] = makeObjectDef(entry.getValue());
            i++;
        }

        RemoteObjectDefMessage defMsg = new RemoteObjectDefMessage();
        defMsg.objects = defs;
        if (this.client != null){
            this.client.send(defMsg);
            logger.log(Level.INFO, "Client: Sending {0}", defMsg);
        } else{
            conn.send(defMsg);
            logger.log(Level.INFO, "Server: Sending {0}", defMsg);
        }
    }
}
 
Example #5
Source File: RmiHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *  Sets up RMI hosting services for the hosted connection allowing
 *  getRmiRegistry() to return a valid RmiRegistry object.
 *  This method is called automatically for all new connections if
 *  autohost is set to true.
 */
@SuppressWarnings("unchecked")
public void startHostingOnConnection( HostedConnection hc ) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "startHostingOnConnection:{0}", hc);
    }
    RmiRegistry rmi = new RmiRegistry(hc, rpcService.getRpcConnection(hc), 
                                      rmiId, defaultChannel); 
    hc.setAttribute(ATTRIBUTE_NAME, rmi);
    
    // Register any global shares
    for( Map.Entry<String, GlobalShare> e : globalShares.entrySet() ) {
        GlobalShare share = e.getValue();
        rmi.share(share.channel, e.getKey(), share.object, share.type); 
    }
}
 
Example #6
Source File: RmiHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *  Shares a server-wide object associated with the specified name over the specified
 *  channel.  All connections with RMI hosting started will have access to this shared 
 *  object as soon as they connect and they will all share the same instance.  It is up 
 *  to the shared object to handle any multithreading that might be required.
 *  All network communcation associated with the shared object will be done over
 *  the specified channel. 
 */     
public <T> void shareGlobal( byte channel, String name, T object, Class<? super T> type ) {
    GlobalShare share = new GlobalShare(channel, object, type);
    GlobalShare existing = globalShares.put(name, share);
    if( existing != null ) {
        // Shouldn't need to do anything actually.
    }
    
    // Go through all of the children
    for( HostedConnection conn : getServer().getConnections() ) {
        RmiRegistry child = getRmiRegistry(conn);
        if( child == null ) {
            continue;
        }
        child.share(channel, name, object, type);
    }
}
 
Example #7
Source File: SessionDataDelegator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Returns the attributeName attribute of the supplied source
 *  HostConnection.  If there is no value at that attribute then
 *  the miss() method is called.
 */   
@Override
protected Object getSourceDelegate( HostedConnection source ) {
    Object result = source.getAttribute(attributeName);
    if( result == null ) {
        miss(source);
    }
    return result;
}
 
Example #8
Source File: MessageListener.java    From OpenRTS with MIT License 5 votes vote down vote up
@Override
public void messageReceived(HostedConnection source, Message message) {
	if (message instanceof InputEvent) {
		// do something with the message
		InputEvent helloMessage = (InputEvent) message;
		logger.info("Client #" + source.getId() + " received: '" + helloMessage.getActionCommand() + "'");
		source.getServer().broadcast(Filters.notEqualTo(source), message);
	}
}
 
Example #9
Source File: ServerSerializerRegistrationsService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void connectionAdded(Server server, HostedConnection hc) {
    // Just in case
    super.connectionAdded(server, hc);
 
    // Send the client the registration information
    hc.send(SerializerRegistrationsMessage.INSTANCE);
}
 
Example #10
Source File: KernelAdapter.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public KernelAdapter( DefaultServer server, Kernel kernel, MessageProtocol protocol, MessageListener<HostedConnection> messageDispatcher,
                      boolean reliable )
{
    super( String.valueOf(kernel) );
    this.server = server;
    this.kernel = kernel;
    this.protocol = protocol;
    this.messageDispatcher = messageDispatcher;
    this.reliable = reliable;
    setDaemon(true);
}
 
Example #11
Source File: RmiRegistry.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public RmiRegistry( HostedConnection context, RpcConnection rpc, short rmiId, byte defaultChannel ) {
    this.context = context;
    this.rpc = rpc;
    this.rmiId = rmiId;
    this.defaultChannel = defaultChannel;
    rpc.registerHandler(rmiId, rmiHandler);
}
 
Example #12
Source File: RmiHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Called internally when an existing connection is leaving
 *  the server.  If the current autoHost property is true then
 *  stopHostingOnConnection(hc) is called. 
 */
@Override
public void connectionRemoved(Server server, HostedConnection hc) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "connectionRemoved({0}, {1})", new Object[]{server, hc});
    }
    if( autoHost ) {    
        stopHostingOnConnection(hc);
    }
}
 
Example #13
Source File: RmiHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Called internally when a new connection is detected for
 *  the server.  If the current autoHost property is true then
 *  startHostingOnConnection(hc) is called. 
 */
@Override
public void connectionAdded(Server server, HostedConnection hc) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "connectionAdded({0}, {1})", new Object[]{server, hc});
    }    
    if( autoHost ) {
        startHostingOnConnection(hc);
    }
}
 
Example #14
Source File: RmiHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Removes any RMI hosting services associated with the specified
 *  connection.  Calls to getRmiRegistry() will return null for
 *  this connection.  
 *  This method is called automatically for all leaving connections if
 *  autohost is set to true.
 */
public void stopHostingOnConnection( HostedConnection hc ) {
    RmiRegistry rmi = hc.getAttribute(ATTRIBUTE_NAME);
    if( rmi == null ) {
        return;
    }
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "stopHostingOnConnection:{0}", hc);
    }
    hc.setAttribute(ATTRIBUTE_NAME, null);
    //rpc.close();
}
 
Example #15
Source File: AbstractHostedConnectionService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Called internally when an existing connection is leaving
 *  the server.  This method always calls stopHostingOnConnection(hc).
 *  Implementations should be aware that if they stopHostingOnConnection()
 *  early that they will get a second call when the connection goes away.
 */
@Override
public void connectionRemoved(Server server, HostedConnection hc) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "connectionRemoved({0}, {1})", new Object[]{server, hc});
    }    
    stopHostingOnConnection(hc);
}
 
Example #16
Source File: AbstractHostedConnectionService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Called internally when a new connection is detected for
 *  the server.  If the current autoHost property is true then
 *  startHostingOnConnection(hc) is called. 
 */
@Override
public void connectionAdded(Server server, HostedConnection hc) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "connectionAdded({0}, {1})", new Object[]{server, hc});
    }    
    if( autoHost ) {
        startHostingOnConnection(hc);
    }
}
 
Example #17
Source File: RpcHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Removes any RPC hosting services associated with the specified
 *  connection.  Calls to getRpcConnection() will return null for
 *  this connection.  The connection's RpcConnection is also closed,
 *  releasing any waiting synchronous calls with a "Connection closing"
 *  error.
 *  This method is called automatically for all leaving connections if
 *  autohost is set to true.
 */
@Override
public void stopHostingOnConnection( HostedConnection hc ) {
    RpcConnection rpc = hc.getAttribute(ATTRIBUTE_NAME);
    if( rpc == null ) {
        return;
    }
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "stopHostingOnConnection:{0}", hc);
    }
    hc.setAttribute(ATTRIBUTE_NAME, null);
    rpc.close();
}
 
Example #18
Source File: RpcHostedService.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Sets up RPC hosting services for the hosted connection allowing
 *  getRpcConnection() to return a valid RPC connection object.
 *  This method is called automatically for all new connections if
 *  autohost is set to true.
 */
@Override
public void startHostingOnConnection( HostedConnection hc ) {
    if( log.isLoggable(Level.FINEST) ) {
        log.log(Level.FINEST, "startHostingOnConnection:{0}", hc);
    }
    hc.setAttribute(ATTRIBUTE_NAME, new RpcConnection(hc));
}
 
Example #19
Source File: ObjectStore.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void connectionAdded(Server server, HostedConnection conn) {
    onConnection(conn);
}
 
Example #20
Source File: ObjectStore.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void connectionRemoved(Server server, HostedConnection conn) {
}
 
Example #21
Source File: TestNetworkStress.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void connectionAdded(Server server, HostedConnection conn) {
    System.out.println("Client Connected: "+conn.getId());
    //conn.close("goodbye");
}
 
Example #22
Source File: TestNetworkStress.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void connectionRemoved(Server server, HostedConnection conn) {
}
 
Example #23
Source File: ObjectStore.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void messageReceived(HostedConnection source, Message m) {
    onMessage(source, m);
}
 
Example #24
Source File: RemoteObject.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public RemoteObject(ObjectStore store, HostedConnection client){
    this.store = store;
    this.client = client;
}
 
Example #25
Source File: TestMessages.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void messageReceived(HostedConnection source, com.jme3.network.Message message) {
    if (message instanceof PingMessage){
        System.out.println("Server: Received ping message!");
        source.send(new PongMessage());
    }
}
 
Example #26
Source File: ConnectionListener.java    From OpenRTS with MIT License 4 votes vote down vote up
@Override
public void connectionRemoved(Server server, HostedConnection conn) {
	logger.info(server.getGameName() + " lost a connection:" + conn.getId());

}
 
Example #27
Source File: ConnectionListener.java    From OpenRTS with MIT License 4 votes vote down vote up
@Override
public void connectionAdded(Server server, HostedConnection conn) {
	logger.info(server.getGameName() + " has a new connection:" + conn.getId());

}
 
Example #28
Source File: RmiContext.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static void setRmiConnection( HostedConnection conn ) {
    connection.set(conn);
}
 
Example #29
Source File: HostedServiceManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void connectionRemoved(Server server, HostedConnection hc) {
    removeConnection(hc);
}
 
Example #30
Source File: HostedServiceManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void connectionAdded(Server server, HostedConnection hc) {
    addConnection(hc);
}