Class DefaultServer
java.lang.Object
com.jme3.network.base.DefaultServer
- All Implemented Interfaces:
Server
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classHosted connection implementation backed by one or more endpoints.protected classAdapts hosted-connection filters to endpoint filters.protected classBridges kernel adapter callbacks back into the server. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultServer(String gameName, int version, Kernel reliable, Kernel fast) Creates a server with the specified game name, version, and primary kernels. -
Method Summary
Modifier and TypeMethodDescriptionintaddChannel(int port) Adds an alternate channel to the server, using the specified port.voidaddConnectionListener(ConnectionListener listener) Adds a listener that will be notified when new hosted connections arrive.voidaddMessageListener(MessageListener<? super HostedConnection> listener) Adds a listener that will be notified when any message or object is received from one of the clients.voidaddMessageListener(MessageListener<? super HostedConnection> listener, Class... classes) Adds a listener that will be notified when messages of the specified types are received from one of the clients.protected voidAdds the default hosted services.voidbroadcast(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.voidbroadcast(Filter<? super HostedConnection> filter, Message message) Sends the specified message to all connected clients that match the filter.voidSends the specified message to all connected clients.protected voidcheckChannel(int channel) Validates an alternate server channel index.voidclose()Closes all client connections, stops and running processing threads, and closes the host connection.protected voidHandles closure of an endpoint and removes any associated connection state.protected voiddispatch(HostedConnection source, Message m) Dispatches a received message to registered listeners.protected voidNotifies registered listeners that a connection was added.protected voidNotifies registered listeners that a connection was removed.protected intReturns the internal channel index for the specified adapter or -1 if not foundgetConnection(int id) Retrieves a hosted connection by ID.protected HostedConnectiongetConnection(Endpoint endpoint) Resolves a hosted connection from an endpoint.Retrieves a read-only collection of all currently connected connections.Returns the 'game name' for this server.Returns the manager for hosted services.intReturns the game-specific version of this server used for detecting mismatched clients.booleanReturns true if the server has active connections at the time of this call.booleanReturns true if the server has been started.protected voidRegisters or completes registration for a connecting client.protected voidRemoves partially connected state associated with the specified endpoint.voidremoveConnectionListener(ConnectionListener listener) Removes a previously registered connection listener.voidremoveMessageListener(MessageListener<? super HostedConnection> listener) Removes a previously registered wildcard listener.voidremoveMessageListener(MessageListener<? super HostedConnection> listener, Class... classes) Removes a previously registered type-specific listener from the specified types.voidstart()Start the server so that it will begin accepting new connections and processing messages.
-
Constructor Details
-
DefaultServer
Creates a server with the specified game name, version, and primary kernels.- Parameters:
gameName- the expected game nameversion- the expected protocol versionreliable- the reliable kernelfast- the optional fast kernel
-
-
Method Details
-
addStandardServices
protected void addStandardServices()Adds the default hosted services. -
getGameName
Description copied from interface:ServerReturns the 'game name' for this server. This should match the 'game name' set on connecting clients or they will be turned away.- Specified by:
getGameNamein interfaceServer- Returns:
- the server game name
-
getVersion
public int getVersion()Description copied from interface:ServerReturns the game-specific version of this server used for detecting mismatched clients.- Specified by:
getVersionin interfaceServer- Returns:
- the server protocol version
-
getServices
Description copied from interface:ServerReturns the manager for hosted services. Hosted services extend the functionality of the server.- Specified by:
getServicesin interfaceServer- Returns:
- the hosted service manager
-
addChannel
public int addChannel(int port) Description copied from interface:ServerAdds 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:
addChannelin interfaceServer- Parameters:
port- the port to bind for the new channel- Returns:
- the newly assigned channel id
-
checkChannel
protected void checkChannel(int channel) Validates an alternate server channel index.- Parameters:
channel- the channel index to validate
-
start
-
isRunning
-
close
-
broadcast
-
broadcast
Description copied from interface:ServerSends 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 );
-
broadcast
Description copied from interface:ServerSends 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 );
-
getConnection
Description copied from interface:ServerRetrieves a hosted connection by ID.- Specified by:
getConnectionin interfaceServer- Parameters:
id- the server-assigned connection id- Returns:
- the matching connection, or null if none exists
-
hasConnections
public boolean hasConnections()Description copied from interface:ServerReturns true if the server has active connections at the time of this call.- Specified by:
hasConnectionsin interfaceServer- Returns:
- true if at least one connection is active
-
getConnections
Description copied from interface:ServerRetrieves a read-only collection of all currently connected connections.- Specified by:
getConnectionsin interfaceServer- Returns:
- the current hosted connections
-
addConnectionListener
Description copied from interface:ServerAdds a listener that will be notified when new hosted connections arrive.- Specified by:
addConnectionListenerin interfaceServer- Parameters:
listener- the listener to add
-
removeConnectionListener
Description copied from interface:ServerRemoves a previously registered connection listener.- Specified by:
removeConnectionListenerin interfaceServer- Parameters:
listener- the listener to remove
-
addMessageListener
Description copied from interface:ServerAdds 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:
addMessageListenerin interfaceServer- Parameters:
listener- the listener to add
-
addMessageListener
public void addMessageListener(MessageListener<? super HostedConnection> listener, Class... classes) Description copied from interface:ServerAdds a listener that will be notified when messages of the specified types are received from one of the clients.- Specified by:
addMessageListenerin interfaceServer- Parameters:
listener- the listener to addclasses- the message classes to match
-
removeMessageListener
Description copied from interface:ServerRemoves a previously registered wildcard listener. This does not remove this listener from any type-specific registrations.- Specified by:
removeMessageListenerin interfaceServer- Parameters:
listener- the listener to remove
-
removeMessageListener
public void removeMessageListener(MessageListener<? super HostedConnection> listener, Class... classes) Description copied from interface:ServerRemoves a previously registered type-specific listener from the specified types.- Specified by:
removeMessageListenerin interfaceServer- Parameters:
listener- the listener to removeclasses- the message classes to unregister
-
dispatch
Dispatches a received message to registered listeners.- Parameters:
source- the hosted connection that sent the message, or nullm- the received message
-
fireConnectionAdded
Notifies registered listeners that a connection was added.- Parameters:
conn- the added connection
-
fireConnectionRemoved
Notifies registered listeners that a connection was removed.- Parameters:
conn- the removed connection
-
getChannel
Returns the internal channel index for the specified adapter or -1 if not found- Parameters:
ka- the kernel adapter- Returns:
- the adapter index
-
registerClient
Registers or completes registration for a connecting client.- Parameters:
ka- the adapter that received the registrationp- the endpoint that sent the registrationm- the registration message
-
getConnection
Resolves a hosted connection from an endpoint.- Parameters:
endpoint- the endpoint to resolve- Returns:
- the hosted connection, or null if none is mapped
-
removeConnecting
Removes partially connected state associated with the specified endpoint.- Parameters:
p- the endpoint to remove
-
connectionClosed
Handles closure of an endpoint and removes any associated connection state.- Parameters:
p- the closed endpoint
-