org.sourceforge.jemm.comm.shared
Class RPCHandler

java.lang.Object
  extended by org.sourceforge.jemm.comm.shared.RPCHandler
All Implemented Interfaces:
java.lang.Runnable

public class RPCHandler
extends java.lang.Object
implements java.lang.Runnable

RPCHandler is used to implement the shared logic used by both org.sourceforge.jemm.comm.server.RPCClient and RPCServer. Given a socket RPCHandler basically manages the client and server interfaces, the proxies and the actual message passing. N.B. A client/server flag is passed to the constructor because ObjectInputStream/ObjectOutputStream must be invoked in alternate order on the two sides of the connection or it deadlocks.

Author:
Rory Graves

Field Summary
protected  boolean closing
           
protected  Connection connection
           
protected  java.lang.Object connectionId
           
protected  java.util.concurrent.CountDownLatch initialisationLatch
           
protected  java.io.ObjectInputStream is
           
protected  boolean isClient
           
protected  RPCHandlerListener listener
           
protected  java.util.Map<java.lang.Class<?>,java.lang.Object> localInterfaces
           
protected  java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.SynchronousQueue<Message>> msgSyncPoints
           
protected  java.io.ObjectOutputStream os
           
protected  java.util.Map<java.lang.Class<?>,java.lang.Object> remoteInterfaces
           
protected  java.util.concurrent.ExecutorService requestExecutor
           
protected  java.lang.ThreadLocal<java.util.concurrent.SynchronousQueue<Message>> syncQueueTL
           
protected  java.lang.ThreadLocal<java.lang.String> threadIdTL
           
 
Constructor Summary
RPCHandler(boolean isClient, Connection connection, java.util.Map<java.lang.Class<?>,java.lang.Object> localInterfaces, java.util.concurrent.ExecutorService requestExecutor, java.lang.Object connectionId)
          Create an RPCHandler
 
Method Summary
 void close()
          Close the connection to the server.
protected  void connectionTerminated()
           
protected  java.lang.Object createProxyClass(java.lang.Class<?> ifClass)
           
static java.lang.Object getConnectionId()
          Retrieve the connection id object associated with the current processing thread.
 java.lang.Object getRemoteIF(java.lang.Class<?> ifClass)
          Returns an local interface for calling the remote interface.
 void initialise()
           
protected  void makeAsyncCall(java.lang.Class<?> ifClass, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object[] args)
          Internal method to make an asynchronous call to a given method.
protected  RPCCallRespMessage makeSyncCall(java.lang.Class<?> ifClass, java.lang.String methodName, java.lang.Class<?>[] parameterTypes, java.lang.Object[] args)
          Internal method to make a make a synchronous call to a given method.
protected  void processCallMessage(RPCCallMessage message)
          Internal method to handle RPC call method.
protected  void receiveMessage(Message message)
           
 void run()
           
 Message sendSyncMessage(Message message)
          Send a synchronous message to the server.
 void setHandlerListener(RPCHandlerListener newListener)
          Set the listener for this handler for event notification.
 void start()
          Start the RPCHandler.
protected  void writeMessage(Message message)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connection

protected Connection connection

initialisationLatch

protected java.util.concurrent.CountDownLatch initialisationLatch

closing

protected volatile boolean closing

is

protected java.io.ObjectInputStream is

os

protected java.io.ObjectOutputStream os

remoteInterfaces

protected java.util.Map<java.lang.Class<?>,java.lang.Object> remoteInterfaces

localInterfaces

protected java.util.Map<java.lang.Class<?>,java.lang.Object> localInterfaces

requestExecutor

protected java.util.concurrent.ExecutorService requestExecutor

threadIdTL

protected java.lang.ThreadLocal<java.lang.String> threadIdTL

syncQueueTL

protected java.lang.ThreadLocal<java.util.concurrent.SynchronousQueue<Message>> syncQueueTL

msgSyncPoints

protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.concurrent.SynchronousQueue<Message>> msgSyncPoints

isClient

protected final boolean isClient

listener

protected RPCHandlerListener listener

connectionId

protected java.lang.Object connectionId
Constructor Detail

RPCHandler

public RPCHandler(boolean isClient,
                  Connection connection,
                  java.util.Map<java.lang.Class<?>,java.lang.Object> localInterfaces,
                  java.util.concurrent.ExecutorService requestExecutor,
                  java.lang.Object connectionId)
Create an RPCHandler

Parameters:
isClient - Is this the client side of the connection (false for server)
connection - The socket connection.
localInterfaces - The interfaces this side offers.
requestExecutor - The service to use for running requests.
connectionId - The id of the connection.
Method Detail

setHandlerListener

public void setHandlerListener(RPCHandlerListener newListener)
Set the listener for this handler for event notification.

Parameters:
newListener - The listener to inform of events.

getRemoteIF

public java.lang.Object getRemoteIF(java.lang.Class<?> ifClass)
Returns an local interface for calling the remote interface.

Parameters:
ifClass - The interface to get.
Returns:
An object which offers the interface.
Throws:
java.lang.IllegalArgumentException - If the requested class is not an interface or not supported.

createProxyClass

protected java.lang.Object createProxyClass(java.lang.Class<?> ifClass)

initialise

public void initialise()

run

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

receiveMessage

protected void receiveMessage(Message message)

processCallMessage

protected void processCallMessage(RPCCallMessage message)
Internal method to handle RPC call method. This will invoke a thread using the executor supplied in the constructor.

Parameters:
message - The message to process.

connectionTerminated

protected void connectionTerminated()

close

public void close()
Close the connection to the server.


writeMessage

protected void writeMessage(Message message)

sendSyncMessage

public Message sendSyncMessage(Message message)
Send a synchronous message to the server. This method sends the given message and waits for a response message.

Parameters:
message - The message to send
Returns:
The message received in reply

makeAsyncCall

protected void makeAsyncCall(java.lang.Class<?> ifClass,
                             java.lang.String methodName,
                             java.lang.Class<?>[] parameterTypes,
                             java.lang.Object[] args)
Internal method to make an asynchronous call to a given method.

Parameters:
ifClass - The interface class being called.
methodName - The method being called.
parameterTypes - The parameter types of the method
args - The actual call arguments (based types wrapped by Proxy.invoke).

makeSyncCall

protected RPCCallRespMessage makeSyncCall(java.lang.Class<?> ifClass,
                                          java.lang.String methodName,
                                          java.lang.Class<?>[] parameterTypes,
                                          java.lang.Object[] args)
Internal method to make a make a synchronous call to a given method.

Parameters:
ifClass - The interface class being called.
methodName - The method being called.
parameterTypes - The parameter types of the method
args - The actual call arguments (based types wrapped by Proxy.invoke).
Returns:
The response message received from the target.

getConnectionId

public static java.lang.Object getConnectionId()
Retrieve the connection id object associated with the current processing thread. A connection id is associated with each execution by the thread-pool.

Returns:
The connection id object supplied.

start

public void start()
Start the RPCHandler. This will trigger the handler to initialise the connection and be ready to start serving calls.



Copyright © 2009. All Rights Reserved.