org.sourceforge.jemmrpc.server
Class RPCServer

java.lang.Object
  extended by java.lang.Thread
      extended by org.sourceforge.jemmrpc.server.RPCServer
All Implemented Interfaces:
java.lang.Runnable

public class RPCServer
extends java.lang.Thread

The RPCServer offers RPC interfaces to connecting clients. The server can handle multiple client connections. See EchoClient/EchoServer for an example of usage.

Author:
Rory Graves

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
protected  java.util.Map<ClientId,ServerThread> clientThreads
          The map of clientIds to active clients
protected  boolean closing
          Used for graceful shutdown.
protected  java.util.concurrent.CountDownLatch initialisationLatch
          A latch to ensure server is ready when initialisation call returns.
protected static org.apache.log4j.Logger logger
           
protected  java.util.concurrent.ExecutorService msgProccessingThreadPool
          The thread pool for processing client call requests (shared among all clients)
protected  int port
          The port the server listens on.
protected  java.net.ServerSocket serverSocket
          The server socket which listens for client connections.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
RPCServer(int port, java.util.concurrent.ExecutorService executorService)
          Creates an RPCServer instance
RPCServer(int port, int noProcessingThreads)
          Creates an RPCServer instance
 
Method Summary
 void clientDisconnected(ClientId clientId)
          Notification that a given client has disconnected.
 ClientId getClientId()
          Used by called methods to find out ID of calling client.
 java.lang.Object getClientIF(ClientId clientId, java.lang.Class<?> ifClass)
          Retrieve a proxy for the given interface on the given client.
 void notifyNewClient(ClientId clientId, java.lang.String clientHostname, int clientPort)
           
 void processMessage(ClientId clientId, Message message)
          Process an incoming message.
 void registerInterface(java.lang.Class<?> targetIF, java.lang.Object targetIFImpl)
          Register an interface for RPC calls by clients.
 void run()
           
 void setClientListener(ClientConnectionListener listener)
          Sets the client listener to the given listener.
 void shutdown()
          Request by the system for the server to be shutdown.
 void waitForInitialisation()
          This method will pause the caller until the server is ready to start accepting connections.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

protected static final org.apache.log4j.Logger logger

clientThreads

protected java.util.Map<ClientId,ServerThread> clientThreads
The map of clientIds to active clients


initialisationLatch

protected java.util.concurrent.CountDownLatch initialisationLatch
A latch to ensure server is ready when initialisation call returns.


closing

protected boolean closing
Used for graceful shutdown.


msgProccessingThreadPool

protected java.util.concurrent.ExecutorService msgProccessingThreadPool
The thread pool for processing client call requests (shared among all clients)


port

protected int port
The port the server listens on.


serverSocket

protected java.net.ServerSocket serverSocket
The server socket which listens for client connections.

Constructor Detail

RPCServer

public RPCServer(int port,
                 int noProcessingThreads)
Creates an RPCServer instance

Parameters:
port - The socket to listen for connections on
noProcessingThreads - The number of processing threads.

RPCServer

public RPCServer(int port,
                 java.util.concurrent.ExecutorService executorService)
Creates an RPCServer instance

Parameters:
port - The socket to listen for connections on
executorService - The executor used to service requests.
Method Detail

getClientId

public ClientId getClientId()
Used by called methods to find out ID of calling client. This uses a ThreadLocal which is set before the server method is invoked.

Returns:
The id of the caller.

getClientIF

public java.lang.Object getClientIF(ClientId clientId,
                                    java.lang.Class<?> ifClass)
Retrieve a proxy for the given interface on the given client.

Parameters:
clientId - The id of the client.
ifClass - The required interface class.
Returns:
An object proxying the interface to the given client.

clientDisconnected

public void clientDisconnected(ClientId clientId)
Notification that a given client has disconnected. The client is removed from the active pool.

Parameters:
clientId - The assigned id of the disconnected client.

processMessage

public void processMessage(ClientId clientId,
                           Message message)
Process an incoming message.

Parameters:
clientId - The ID of the sending client.
message - The received message.

waitForInitialisation

public void waitForInitialisation()
This method will pause the caller until the server is ready to start accepting connections. If the server is already running it will return immediately. Mostly used for testing when a client and server are run in the same VM to avoid the client connecting before the server is ready.


run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

shutdown

public void shutdown()
Request by the system for the server to be shutdown.


registerInterface

public void registerInterface(java.lang.Class<?> targetIF,
                              java.lang.Object targetIFImpl)
Register an interface for RPC calls by clients.

Parameters:
targetIF - The interface to offer to clients.
targetIFImpl - The server side implementation of the given interface.

setClientListener

public void setClientListener(ClientConnectionListener listener)
Sets the client listener to the given listener.

Parameters:
listener - The listener to receive disconnection events.

notifyNewClient

public void notifyNewClient(ClientId clientId,
                            java.lang.String clientHostname,
                            int clientPort)
Parameters:
clientId - The id of the new client.
clientHostname - The clients hostname.
clientPort - The clients port.


Copyright © 2008. All Rights Reserved.