org.sourceforge.jemmrpc.shared
Class RPCHandler

java.lang.Object
  extended by org.sourceforge.jemmrpc.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 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  java.util.concurrent.CountDownLatch initialisationLatch
           
protected  java.io.ObjectInputStream is
           
protected  RPCHandlerListener listener
           
protected  java.io.ObjectOutputStream os
           
 
Constructor Summary
RPCHandler(boolean isClient, java.net.Socket socket, 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.
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 listener)
          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

initialisationLatch

protected java.util.concurrent.CountDownLatch initialisationLatch

closing

protected volatile boolean closing

is

protected java.io.ObjectInputStream is

os

protected java.io.ObjectOutputStream os

listener

protected RPCHandlerListener listener
Constructor Detail

RPCHandler

public RPCHandler(boolean isClient,
                  java.net.Socket socket,
                  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)
socket - 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 listener)
Set the listener for this handler for event notification.

Parameters:
listener - 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)

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 © 2008. All Rights Reserved.