org.iids.aos.agent.proxy
Class AgentProxyHandler

java.lang.Object
  extended by org.iids.aos.agent.proxy.AgentProxyHandler
Direct Known Subclasses:
AuthAgentProxyHandler

public class AgentProxyHandler
extends java.lang.Object

AgentProxyHandler implementation for the server side of an AgentProxy.

The idea is that a service agent registers itself with a proxy handler which allows agents to connect to the service using an AgentProxy so the agents can invoke methods on the server.

TODO be more specific about the interface the service implements.

Author:
rjtimmer, michel

Nested Class Summary
static class AgentProxyHandler.Mode
           
 
Field Summary
protected  java.lang.Object subject
           
 
Constructor Summary
AgentProxyHandler(java.lang.Object subject, AgentHandle local)
          Shortcut for AgentProxyHandler(subject, local, remote, false)

AgentProxyHandler(java.lang.Object subject, AgentHandle local, AgentHandle remote)
          Shortcut for AgentProxyHandler(subject, local, remote, false)

AgentProxyHandler(java.lang.Object subject, AgentHandle local, AgentHandle remote, boolean assignNewHandle)
          Create new proxy handler.
 
Method Summary
 void close()
          Close the handler that receives method invocation requests.
protected  void finalize()
           
static java.lang.reflect.Method findMethod(java.lang.Class<?> serviceClass, java.lang.String methName, java.lang.Object... args)
          Helper method that finds a method from a corresponding class while paying respect to the autoboxed types.
 AgentHandle getAgentHandle()
          Obtain the handle that is used to receive messages.
static java.lang.Object getHandler(AgentHandle ah)
           
 void handleRequest(AgentProxyMessage msg)
          Handles request in foreground.
protected  java.lang.Object invokeRequest(java.lang.String methName, java.lang.Object[] args)
           
 boolean isClosed()
          Test if the proxy handler is still running.
 void run(AgentProxyHandler.Mode mode)
          This method runs the receive/reply loop of the server.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

subject

protected java.lang.Object subject
Constructor Detail

AgentProxyHandler

public AgentProxyHandler(java.lang.Object subject,
                         AgentHandle local,
                         AgentHandle remote,
                         boolean assignNewHandle)
Create new proxy handler. The handler will be able to receive agent messages containing invocation requests, and execute them on the subject object. This way it becomes transparent for clients to access a service implemented by an agent.

Make sure no other threads are using the local handle for receiving messages, as this could cause problems for the proxy handler

Parameters:
subject - The object which the method invocations are executed on.
local - A handle which is used to recieve messages.
remote - Optional. If this is not-null, then incoming method invocations are only accepted if they are sent by this handle. Otherwise, invocations are accepted from all handles.
assignNewHandle - If this is true then the handle local is used to create a new handle which is used for receiving messages. This handle is automatically removed once the proxy handler is closed. If this is false then the handle local is used to receive messages.

AgentProxyHandler

public AgentProxyHandler(java.lang.Object subject,
                         AgentHandle local,
                         AgentHandle remote)
Shortcut for AgentProxyHandler(subject, local, remote, false)

Parameters:
subject - The Object to publish over proxy.
local - The local handle to receive proxy invocations from.
remote - The remote handle to receive messages from. If this is null then messages from all sources are accepted.

AgentProxyHandler

public AgentProxyHandler(java.lang.Object subject,
                         AgentHandle local)
Shortcut for AgentProxyHandler(subject, local, remote, false)

Parameters:
subject - The Object to publish over proxy.
local - The local handle to receive proxy invocations from.
Method Detail

getAgentHandle

public AgentHandle getAgentHandle()
Obtain the handle that is used to receive messages.

Returns:
The local handle used for receiving messages.

isClosed

public boolean isClosed()
Test if the proxy handler is still running.

Returns:
true if the proxy handler is closed.

close

public void close()
Close the handler that receives method invocation requests. This also cleans the assigned local handle (if needed) and any pending handler threads. If there are still active handler threads, these are finished first.


run

public void run(AgentProxyHandler.Mode mode)
This method runs the receive/reply loop of the server. You can choose a desired method for this service to run (foreground/background) for various modes of operation. Foreground only returns once the connection is closed (from the other side).

Note that using this method there is always a dedicated thread that processes messages for this agent. If you do not want this, you could receive the messages manually, and forward the appropriate messages to the handlers handleRequest(org.iids.aos.agent.proxy.AgentProxyMessage) when needed. The thread can do other work the rest of the time.

Parameters:
mode - The mode to run in. Foreground mode means that this method blocks until the proxy handle was finished for some reason. Background mode means this method returns directly and a dedicated thread is responsible for receiving and processing the proxy requests.

findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class<?> serviceClass,
                                                  java.lang.String methName,
                                                  java.lang.Object... args)
                                           throws java.lang.NoSuchMethodException
Helper method that finds a method from a corresponding class while paying respect to the autoboxed types. This is useful whenever an invocation fails which has parameter types of wrapper classes (like Integer) when the class itself requires int.

Argument type mismatch can happen because invocation parameters must be of type Serializable, which a plain int is not.

Parameters:
serviceClass - The class of the object to invoke the method on.
methName - Name of method to invoke.
args - The arguments for method methName
Returns:
A method compatible with the arguments from args.
Throws:
java.lang.NoSuchMethodException

invokeRequest

protected java.lang.Object invokeRequest(java.lang.String methName,
                                         java.lang.Object[] args)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

handleRequest

public void handleRequest(AgentProxyMessage msg)
Handles request in foreground. This method interprets the request and executes the required method to obtain the results. After the method was executed (success or exception) the appropriate reply is sent to the waiting party.

Usually, this method is automatically called from a runtime handler for this proxy. However, a message can also be handed to the handler from an external party. This is convenient if you want only one thread to handle the messaging, whereas normally a thread is completely dedicated to processing proxy requests. This allows a single thread to receive all messages, which can decide at runtime whether the proxy handler should handle the message, or it can process it itself.

Parameters:
msg - The AgentProxyMessage to process.

finalize

protected void finalize()
Overrides:
finalize in class java.lang.Object

getHandler

public static java.lang.Object getHandler(AgentHandle ah)


Copyright ? 2003, 2004 IIDS Group. All Rights Reserved.