org.sourceforge.jemm.comm.server
Class RPCServer

java.lang.Object
  extended by java.lang.Thread
      extended by org.sourceforge.jemm.comm.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.

Released under the Apache License V2.0 license by the JEMM Project

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<RPCClientId,ServerThread> clientThreads
          The map of clientIds to active clients
protected  boolean closing
          Used for graceful shutdown.
protected  ServerConnectionFactory connectionFactory
           
protected  java.util.concurrent.CountDownLatch initialisationLatch
          A latch to ensure server is ready when initialisation call returns.
protected static org.apache.log4j.Logger LOG
           
protected  java.util.concurrent.ExecutorService msgProccessingThreadPool
          The thread pool for processing client call requests (shared among all clients)
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
RPCServer(ServerConnectionFactory connectionFactory, java.util.concurrent.ExecutorService executorService)
          Creates an RPCServer instance
RPCServer(ServerConnectionFactory connectionFactory, int noProcessingThreads)
          Creates an RPCServer instance
 
Method Summary
 void clientDisconnected(RPCClientId clientId)
          Notification that a given client has disconnected.
 RPCClientId getClientId()
          Used by called methods to find out ID of calling client.
 java.lang.Object getClientIF(RPCClientId clientId, java.lang.Class<?> ifClass)
          Retrieve a proxy for the given interface on the given client.
 void notifyNewClient(RPCClientId clientId, java.lang.String clientAddress)
           
 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

LOG

protected static final org.apache.log4j.Logger LOG

clientThreads

protected java.util.Map<RPCClientId,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)


connectionFactory

protected final ServerConnectionFactory connectionFactory
Constructor Detail

RPCServer

public RPCServer(ServerConnectionFactory connectionFactory,
                 java.util.concurrent.ExecutorService executorService)
Creates an RPCServer instance

Parameters:
connectionFactory - The connection factory for accepting client connections.
executorService - The executor used to service requests.

RPCServer

public RPCServer(ServerConnectionFactory connectionFactory,
                 int noProcessingThreads)
Creates an RPCServer instance

Parameters:
connectionFactory - The connection factory for accepting client connections.
noProcessingThreads - The number of processing threads.
Method Detail

clientDisconnected

public void clientDisconnected(RPCClientId 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.

getClientId

public RPCClientId 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(RPCClientId 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.

notifyNewClient

public void notifyNewClient(RPCClientId clientId,
                            java.lang.String clientAddress)
Parameters:
clientId - The id of the new client.
clientHostname - The clients hostname.

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.

run

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

setClientListener

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

Parameters:
listener - The listener to receive disconnection events.

shutdown

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


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.



Copyright © 2009. All Rights Reserved.