com.jme3.network.base
Class DefaultServer

java.lang.Object
  extended by com.jme3.network.base.DefaultServer
All Implemented Interfaces:
Server

public class DefaultServer
extends java.lang.Object
implements Server

A default implementation of the Server interface that delegates its network connectivity to kernel.Kernel.


Nested Class Summary
protected  class DefaultServer.Connection
           
protected  class DefaultServer.FilterAdapter
           
protected  class DefaultServer.Redispatch
           
 
Constructor Summary
DefaultServer(java.lang.String gameName, int version, Kernel reliable, Kernel fast)
           
 
Method Summary
 int addChannel(int port)
          Adds an alternate channel to the server, using the specified port.
 void addConnectionListener(ConnectionListener listener)
          Adds a listener that will be notified when new hosted connections arrive.
 void addMessageListener(MessageListener<? super HostedConnection> listener)
          Adds a listener that will be notified when any message or object is received from one of the clients.
 void addMessageListener(MessageListener<? super HostedConnection> listener, java.lang.Class... classes)
          Adds a listener that will be notified when messages of the specified types are received from one of the clients.
 void broadcast(Filter<? super HostedConnection> filter, Message message)
          Sends the specified message to all connected clients that match the filter.
 void broadcast(int channel, Filter<? super HostedConnection> filter, Message message)
          Sends the specified message over the specified alternate channel to all connected clients that match the filter.
 void broadcast(Message message)
          Sends the specified message to all connected clients.
protected  void checkChannel(int channel)
           
 void close()
          Closes all client connections, stops and running processing threads, and closes the host connection.
protected  void connectionClosed(Endpoint p)
           
protected  void dispatch(HostedConnection source, Message m)
           
protected  void fireConnectionAdded(HostedConnection conn)
           
protected  void fireConnectionRemoved(HostedConnection conn)
           
protected  int getChannel(KernelAdapter ka)
           
protected  HostedConnection getConnection(Endpoint endpoint)
           
 HostedConnection getConnection(int id)
          Retrieves a hosted connection by ID.
 java.util.Collection<HostedConnection> getConnections()
          Retrieves a read-only collection of all currently connected connections.
 java.lang.String getGameName()
          Returns the 'game name' for this server.
 int getVersion()
          Returns the game-specific version of this server used for detecting mismatched clients.
 boolean hasConnections()
          Returns true if the server has active connections at the time of this call.
 boolean isRunning()
          Returns true if the server has been started.
protected  void registerClient(KernelAdapter ka, Endpoint p, ClientRegistrationMessage m)
           
 void removeConnectionListener(ConnectionListener listener)
          Removes a previously registered connection listener.
 void removeMessageListener(MessageListener<? super HostedConnection> listener)
          Removes a previously registered wildcard listener.
 void removeMessageListener(MessageListener<? super HostedConnection> listener, java.lang.Class... classes)
          Removes a previously registered type-specific listener from the specified types.
 void start()
          Start the server so that it will began accepting new connections and processing messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultServer

public DefaultServer(java.lang.String gameName,
                     int version,
                     Kernel reliable,
                     Kernel fast)
Method Detail

getGameName

public java.lang.String getGameName()
Description copied from interface: Server
Returns the 'game name' for this server. This should match the 'game name' set on connecting clients or they will be turned away.

Specified by:
getGameName in interface Server

getVersion

public int getVersion()
Description copied from interface: Server
Returns the game-specific version of this server used for detecting mismatched clients.

Specified by:
getVersion in interface Server

addChannel

public int addChannel(int port)
Description copied from interface: Server
Adds an alternate channel to the server, using the specified port. This is an entirely separate connection where messages are sent and received in parallel to the two primary default channels. All channels must be added before the connection is started. Returns the ID of the created channel for use when specifying the channel in send or broadcast calls. The ID is returned entirely out of convenience since the IDs are predictably incremented. The first channel is 0, second is 1, and so on.

Specified by:
addChannel in interface Server

checkChannel

protected void checkChannel(int channel)

start

public void start()
Description copied from interface: Server
Start the server so that it will began accepting new connections and processing messages.

Specified by:
start in interface Server

isRunning

public boolean isRunning()
Description copied from interface: Server
Returns true if the server has been started.

Specified by:
isRunning in interface Server

close

public void close()
Description copied from interface: Server
Closes all client connections, stops and running processing threads, and closes the host connection.

Specified by:
close in interface Server

broadcast

public void broadcast(Message message)
Description copied from interface: Server
Sends the specified message to all connected clients.

Specified by:
broadcast in interface Server

broadcast

public void broadcast(Filter<? super HostedConnection> filter,
                      Message message)
Description copied from interface: Server
Sends the specified message to all connected clients that match the filter. If no filter is specified then this is the same as calling broadcast(message) and the message will be delivered to all connections.

Examples:

    // Broadcast to connections: conn1, conn2, and conn3
    server.broadcast( Filters.in( conn1, conn2, conn3 ), message );

    // Broadcast to all connections exception source
    server.broadcast( Filters.notEqualTo( source ), message );
  

Specified by:
broadcast in interface Server

broadcast

public void broadcast(int channel,
                      Filter<? super HostedConnection> filter,
                      Message message)
Description copied from interface: Server
Sends the specified message over the specified alternate channel to all connected clients that match the filter. If no filter is specified then this is the same as calling broadcast(message) and the message will be delivered to all connections.

Examples:

    // Broadcast to connections: conn1, conn2, and conn3
    server.broadcast( Filters.in( conn1, conn2, conn3 ), message );

    // Broadcast to all connections exception source
    server.broadcast( Filters.notEqualTo( source ), message );
  

Specified by:
broadcast in interface Server

getConnection

public HostedConnection getConnection(int id)
Description copied from interface: Server
Retrieves a hosted connection by ID.

Specified by:
getConnection in interface Server

hasConnections

public boolean hasConnections()
Description copied from interface: Server
Returns true if the server has active connections at the time of this call.

Specified by:
hasConnections in interface Server

getConnections

public java.util.Collection<HostedConnection> getConnections()
Description copied from interface: Server
Retrieves a read-only collection of all currently connected connections.

Specified by:
getConnections in interface Server

addConnectionListener

public void addConnectionListener(ConnectionListener listener)
Description copied from interface: Server
Adds a listener that will be notified when new hosted connections arrive.

Specified by:
addConnectionListener in interface Server

removeConnectionListener

public void removeConnectionListener(ConnectionListener listener)
Description copied from interface: Server
Removes a previously registered connection listener.

Specified by:
removeConnectionListener in interface Server

addMessageListener

public void addMessageListener(MessageListener<? super HostedConnection> listener)
Description copied from interface: Server
Adds a listener that will be notified when any message or object is received from one of the clients.

Note about MessageListener multithreading: on the server, message events may be delivered by more than one thread depending on the server implementation used. Listener implementations should treat their shared data structures accordingly and set them up for multithreaded access. The only threading guarantee is that for a single HostedConnection, there will only ever be one thread at a time and the messages will always be delivered to that connection in the order that they were delivered. This is the only restriction placed upon server message dispatch pool implementations.

Specified by:
addMessageListener in interface Server

addMessageListener

public void addMessageListener(MessageListener<? super HostedConnection> listener,
                               java.lang.Class... classes)
Description copied from interface: Server
Adds a listener that will be notified when messages of the specified types are received from one of the clients.

Specified by:
addMessageListener in interface Server

removeMessageListener

public void removeMessageListener(MessageListener<? super HostedConnection> listener)
Description copied from interface: Server
Removes a previously registered wildcard listener. This does not remove this listener from any type-specific registrations.

Specified by:
removeMessageListener in interface Server

removeMessageListener

public void removeMessageListener(MessageListener<? super HostedConnection> listener,
                                  java.lang.Class... classes)
Description copied from interface: Server
Removes a previously registered type-specific listener from the specified types.

Specified by:
removeMessageListener in interface Server

dispatch

protected void dispatch(HostedConnection source,
                        Message m)

fireConnectionAdded

protected void fireConnectionAdded(HostedConnection conn)

fireConnectionRemoved

protected void fireConnectionRemoved(HostedConnection conn)

getChannel

protected int getChannel(KernelAdapter ka)

registerClient

protected void registerClient(KernelAdapter ka,
                              Endpoint p,
                              ClientRegistrationMessage m)

getConnection

protected HostedConnection getConnection(Endpoint endpoint)

connectionClosed

protected void connectionClosed(Endpoint p)