The great invocation services rethink of 2002! Rearchitected the remote
method invocation services and converted everything to the new style. Could this be my biggest checkin ever? git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1642 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// $Id: BasicDirector.java,v 1.1 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import com.threerings.presents.util.PresentsContext;
|
||||
|
||||
/**
|
||||
* Handles functionality common to nearly all client directors. They
|
||||
* generally need to be session observers so that they can set themselves
|
||||
* up when the client logs on (by overriding {@link #clientDidLogon}) and
|
||||
* clean up after themselves when the client logs off (by overriding
|
||||
* {@link #clientDidLogoff}).
|
||||
*/
|
||||
public class BasicDirector
|
||||
implements SessionObserver
|
||||
{
|
||||
/**
|
||||
* Derived directors will need to provide the basic director with a
|
||||
* context that it can use to register itself with the necessary
|
||||
* entities.
|
||||
*/
|
||||
protected BasicDirector (PresentsContext ctx)
|
||||
{
|
||||
// listen for session start and end
|
||||
Client client = ctx.getClient();
|
||||
client.addClientObserver(this);
|
||||
|
||||
// if we're already logged on, fire off a call to fetch services
|
||||
if (client.isLoggedOn()) {
|
||||
fetchServices(client);
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public void clientDidLogon (Client client)
|
||||
{
|
||||
fetchServices(client);
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public void clientDidLogoff (Client client)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Derived directors can override this method and obtain any services
|
||||
* they'll need during their operation via calls to {@link
|
||||
* Client#getService}. It will automatically be called when the client
|
||||
* logs on or when the director is constructed if it is constructed
|
||||
* after the client is already logged on.
|
||||
*/
|
||||
protected void fetchServices (Client client)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: Client.java,v 1.27 2002/06/04 16:34:24 mdb Exp $
|
||||
// $Id: Client.java,v 1.28 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
@@ -196,6 +196,40 @@ public class Client
|
||||
return _invdir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first bootstrap service that could be located that
|
||||
* implements the supplied {@link InvocationService} derivation.
|
||||
* <code>null</code> is returned if no such service could be found.
|
||||
*/
|
||||
public InvocationService getService (Class sclass)
|
||||
{
|
||||
int scount = _bstrap.services.size();
|
||||
for (int ii = 0; ii < scount; ii++) {
|
||||
InvocationService service = (InvocationService)
|
||||
_bstrap.services.get(ii);
|
||||
if (sclass.isInstance(service)) {
|
||||
return service;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #getService} except that a {@link RuntimeException} is
|
||||
* thrown if the service is not available. Useful to avoid redundant
|
||||
* error checking when you know that the shit will hit the fan if a
|
||||
* particular invocation service is not available.
|
||||
*/
|
||||
public InvocationService requireService (Class sclass)
|
||||
{
|
||||
InvocationService isvc = getService(sclass);
|
||||
if (isvc == null) {
|
||||
throw new RuntimeException(sclass.getName() + " isn't available. " +
|
||||
"I can't bear to go on.");
|
||||
}
|
||||
return isvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reference to the bootstrap data provided to this client
|
||||
* at logon time.
|
||||
@@ -219,7 +253,7 @@ public class Client
|
||||
/**
|
||||
* Returns true if we are logged on, false if we're not.
|
||||
*/
|
||||
public synchronized boolean loggedOn ()
|
||||
public synchronized boolean isLoggedOn ()
|
||||
{
|
||||
// if we have a communicator, we're logged on
|
||||
return (_comm != null);
|
||||
@@ -312,7 +346,7 @@ public class Client
|
||||
notifyObservers(Client.CLIENT_FAILED_TO_LOGON, cause);
|
||||
}
|
||||
};
|
||||
_invdir.init(_comm.getDObjectManager(), _cloid, _bstrap.invOid, rl);
|
||||
_invdir.init(_comm.getDObjectManager(), _cloid, rl);
|
||||
|
||||
// we can't quite call initialization completed at this point
|
||||
// because we need for the invocation director to fully initialize
|
||||
@@ -356,6 +390,8 @@ public class Client
|
||||
public void run () {
|
||||
// clear out our communicator reference
|
||||
_comm = null;
|
||||
// and let our invocation director know we're logged off
|
||||
_invdir.cleanup();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// $Id: InvocationDecoder.java,v 1.1 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
import com.threerings.presents.Log;
|
||||
|
||||
/**
|
||||
* Provides the basic functionality used to dispatch invocation
|
||||
* notification events.
|
||||
*/
|
||||
public abstract class InvocationDecoder
|
||||
{
|
||||
/** The receiver for which we're decoding and dipatching
|
||||
* notifications. */
|
||||
public InvocationReceiver receiver;
|
||||
|
||||
/**
|
||||
* Returns the generated hash code that is used to identify this
|
||||
* invocation notification service.
|
||||
*/
|
||||
public abstract String getReceiverCode ();
|
||||
|
||||
/**
|
||||
* Dispatches the specified method to our receiver.
|
||||
*/
|
||||
public void dispatchNotification (int methodId, Object[] args)
|
||||
{
|
||||
Log.warning("Requested to dispatch unknown method " +
|
||||
"[receiver=" + receiver + ", methodId=" + methodId +
|
||||
", args=" + StringUtil.toString(args) + "].");
|
||||
}
|
||||
}
|
||||
@@ -1,70 +1,69 @@
|
||||
//
|
||||
// $Id: InvocationDirector.java,v 1.20 2002/04/16 21:37:23 mdb Exp $
|
||||
// $Id: InvocationDirector.java,v 1.21 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.StringUtil;
|
||||
import com.samskivert.util.ResultListener;
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
import com.threerings.presents.Log;
|
||||
import com.threerings.presents.data.*;
|
||||
import com.threerings.presents.dobj.*;
|
||||
import com.threerings.presents.util.ClassUtil;
|
||||
import com.threerings.presents.client.InvocationReceiver.Registration;
|
||||
|
||||
import com.threerings.presents.data.ClientObject;
|
||||
import com.threerings.presents.data.InvocationMarshaller.ListenerMarshaller;
|
||||
import com.threerings.presents.data.InvocationMarshaller;
|
||||
|
||||
import com.threerings.presents.dobj.DEvent;
|
||||
import com.threerings.presents.dobj.DObject;
|
||||
import com.threerings.presents.dobj.DObjectManager;
|
||||
import com.threerings.presents.dobj.EventListener;
|
||||
import com.threerings.presents.dobj.InvocationNotificationEvent;
|
||||
import com.threerings.presents.dobj.InvocationRequestEvent;
|
||||
import com.threerings.presents.dobj.InvocationResponseEvent;
|
||||
import com.threerings.presents.dobj.ObjectAccessException;
|
||||
import com.threerings.presents.dobj.Subscriber;
|
||||
|
||||
/**
|
||||
* The invocation services provide client to server invocations (service
|
||||
* requests) and server to client invocations (responses and
|
||||
* notifications). Via this mechanism, the client can make requests of the
|
||||
* server, be notified of its response and the server can asynchronously
|
||||
* invoke code on the client.
|
||||
*
|
||||
* <p> Invocations are like remote procedure calls in that they are named
|
||||
* and take arguments. They are simple in that the arguments can only be
|
||||
* of a small set of supported types (the set of distributed object field
|
||||
* types) and there is no special facility provided for referencing
|
||||
* non-local objects (it is assumed that the distributed object facility
|
||||
* will already be in use for any objects that should be shared).
|
||||
*
|
||||
* <p> The client invocation director delivers invocation requests to the
|
||||
* server invocation manager and maps the responses back to the proper
|
||||
* response target objects when they arrive. It also maintains the mapping
|
||||
* of invocation receivers that can receive asynchronous invocation
|
||||
* notifications at any time from the server.
|
||||
* Handles the client side management of the invocation services.
|
||||
*/
|
||||
public class InvocationDirector
|
||||
implements MessageListener
|
||||
implements EventListener
|
||||
{
|
||||
/**
|
||||
* Initializes the invocation director.
|
||||
* Initializes the invocation director. This is called when the client
|
||||
* establishes a connection with the server.
|
||||
*
|
||||
* @param omgr the distributed object manager via which the invocation
|
||||
* manager will send and receive events.
|
||||
* @param cloid the oid of the object on which invocation
|
||||
* notifications as well as invocation responses will be received.
|
||||
* @param imoid the oid of the object on the server to which to
|
||||
* deliver invocation requests.
|
||||
* @param initListener a result listener that will be notified when
|
||||
* the invocation director is up and running (meaning it has
|
||||
* subscribed successfully to the client object and is ready to
|
||||
* process invocation requests); or when initialization has failed.
|
||||
*/
|
||||
public void init (DObjectManager omgr, final int cloid, int imoid,
|
||||
public void init (DObjectManager omgr, final int cloid,
|
||||
final ResultListener initListener)
|
||||
{
|
||||
// keep this for later
|
||||
_omgr = omgr;
|
||||
_imoid = imoid;
|
||||
_cloid = cloid;
|
||||
|
||||
// add ourselves as a subscriber to the client object
|
||||
_omgr.subscribeToObject(cloid, new Subscriber() {
|
||||
public void objectAvailable (DObject object)
|
||||
{
|
||||
// keep a handle on this bad boy
|
||||
_clobj = (ClientObject)object;
|
||||
|
||||
// add ourselves as a message listener
|
||||
object.addListener(InvocationDirector.this);
|
||||
_clobj.addListener(InvocationDirector.this);
|
||||
|
||||
// assign a mapping to already registered receivers
|
||||
assignReceiverIds();
|
||||
|
||||
// let the client know that we're ready to go now that
|
||||
// we've got our subscription to the client object
|
||||
@@ -83,224 +82,243 @@ public class InvocationDirector
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an invocation request to the server. If a response target is
|
||||
* supplied, the response will be delivered to that object by calling
|
||||
* a member function on it whose name is defined in the response
|
||||
* generated by the invocation implementation. In general, this is a
|
||||
* derivative of the invocation procedure name. For example, if the
|
||||
* caller invoked a procedure named <code>Switch</code>, the response
|
||||
* may be delivered via a call to the <code>handleSwitchSuccess</code>
|
||||
* method on the response target object. The signature of that method
|
||||
* would be defined by the arguments provided in the response message
|
||||
* with the addition of a first argument which is the invocation
|
||||
* identifier for this particular request (that is also returned by
|
||||
* this function).
|
||||
*
|
||||
* @param module the name of the invocation module to use.
|
||||
* @param procedure the name of the procedure within that module.
|
||||
* @param args the arguments of the invocation.
|
||||
* @param rsptarget the object that will receive the response, or null
|
||||
* if no response is desired.
|
||||
*
|
||||
* @return a unique identifier associated with this invocation
|
||||
* request. This identifier will be passed as the first argument to
|
||||
* the response function.
|
||||
* Clears out our session information. This is called when the client
|
||||
* ends its session with the server.
|
||||
*/
|
||||
public int invoke (String module, String procedure, Object[] args,
|
||||
Object rsptarget)
|
||||
public void cleanup ()
|
||||
{
|
||||
int invid = nextInvocationId();
|
||||
// wipe our client object, receiver mappings and listener mappings
|
||||
_clobj = null;
|
||||
_receivers.clear();
|
||||
_listeners.clear();
|
||||
|
||||
// if null arguments were supplied, assume zero arguments
|
||||
if (args == null) {
|
||||
args = new Object[0];
|
||||
// also reset our counters
|
||||
_requestId = 0;
|
||||
_receiverId = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an invocation notification receiver by way of its
|
||||
* notification event decoder.
|
||||
*/
|
||||
public void registerReceiver (InvocationDecoder decoder)
|
||||
{
|
||||
// add the receiver to the list
|
||||
_reclist.add(decoder);
|
||||
|
||||
// if we're already online, assign a receiver id to this decoder
|
||||
if (_clobj != null) {
|
||||
assignReceiverId(decoder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a receiver registration.
|
||||
*/
|
||||
public void unregisterReceiver (String receiverCode)
|
||||
{
|
||||
// remove the receiver from the list
|
||||
for (Iterator iter = _reclist.iterator(); iter.hasNext(); ) {
|
||||
InvocationDecoder decoder = (InvocationDecoder)iter.next();
|
||||
if (decoder.getReceiverCode().equals(receiverCode)) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
// we need an args array for a message that can contain the
|
||||
// invocation names, an invocation id and the invocation arguments
|
||||
Object[] iargs = new Object[args.length+3];
|
||||
iargs[0] = module;
|
||||
iargs[1] = procedure;
|
||||
iargs[2] = new Integer(invid);
|
||||
System.arraycopy(args, 0, iargs, 3, args.length);
|
||||
|
||||
// create a message event on the invocation manager object
|
||||
MessageEvent event = new MessageEvent(
|
||||
_imoid, InvocationObject.REQUEST_NAME, iargs);
|
||||
|
||||
// if we have a response target, register that for later receipt
|
||||
// of the response
|
||||
if (rsptarget != null) {
|
||||
_targets.put(invid, rsptarget);
|
||||
// if we're logged on, clear out any receiver id mapping
|
||||
if (_clobj != null) {
|
||||
_clobj.removeFromReceivers(receiverCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a receiver id to this decoder and publishes it in the
|
||||
* {@link ClientObject#receivers} field.
|
||||
*/
|
||||
protected void assignReceiverId (InvocationDecoder decoder)
|
||||
{
|
||||
Registration reg = new Registration(
|
||||
decoder.getReceiverCode(), nextReceiverId());
|
||||
// stick the mapping into the client object
|
||||
_clobj.addToReceivers(reg);
|
||||
// and map the receiver in our receivers table
|
||||
_receivers.put(reg.receiverId, decoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when we log on; generates mappings for all receivers
|
||||
* registered prior to logon.
|
||||
*/
|
||||
protected void assignReceiverIds ()
|
||||
{
|
||||
// pack all the set add events into a single transaction
|
||||
_clobj.startTransaction();
|
||||
try {
|
||||
for (Iterator iter = _reclist.iterator(); iter.hasNext(); ) {
|
||||
assignReceiverId((InvocationDecoder)iter.next());
|
||||
}
|
||||
} finally {
|
||||
_clobj.commitTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests that the specified invocation request be packaged up and
|
||||
* sent to the supplied invocation oid.
|
||||
*/
|
||||
public void sendRequest (
|
||||
int invOid, int invCode, int methodId, Object[] args)
|
||||
{
|
||||
// configure any invocation listener marshallers among the
|
||||
// arguments
|
||||
int acount = args.length;
|
||||
for (int ii = 0; ii < acount; ii++) {
|
||||
Object arg = args[ii];
|
||||
if (arg instanceof ListenerMarshaller) {
|
||||
ListenerMarshaller lm = (ListenerMarshaller)arg;
|
||||
lm.callerOid = _clobj.getOid();
|
||||
lm.requestId = nextRequestId();
|
||||
// create a mapping for this marshaller so that we can
|
||||
// properly dispatch responses sent to it
|
||||
_listeners.put(lm.requestId, lm);
|
||||
}
|
||||
}
|
||||
|
||||
// create an invocation request event
|
||||
InvocationRequestEvent event =
|
||||
new InvocationRequestEvent(invOid, invCode, methodId, args);
|
||||
|
||||
// because invocation directors are used on the server, we set the
|
||||
// source oid here so that invocation requests are properly
|
||||
// attributed to the right client object when created by
|
||||
// server-side entities only sort of pretending to be a client
|
||||
event.setSourceOid(_cloid);
|
||||
event.setSourceOid(_clobj.getOid());
|
||||
|
||||
// and finally ship off the invocation message
|
||||
// Log.info("Sending invocation request " + event + ".");
|
||||
|
||||
// now dispatch the event
|
||||
_omgr.postEvent(event);
|
||||
|
||||
return invid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the supplied invocation receiver instance as the handler
|
||||
* for all invocation notifications for the specified module.
|
||||
* Process notification and response events arriving on user object.
|
||||
*/
|
||||
public void registerReceiver (String module, InvocationReceiver receiver)
|
||||
public void eventReceived (DEvent event)
|
||||
{
|
||||
if (_receivers == null) {
|
||||
_receivers = new HashMap();
|
||||
}
|
||||
_receivers.put(module, receiver);
|
||||
}
|
||||
if (event instanceof InvocationResponseEvent) {
|
||||
InvocationResponseEvent ire = (InvocationResponseEvent)event;
|
||||
handleInvocationResponse(
|
||||
ire.getRequestId(), ire.getMethodId(), ire.getArgs());
|
||||
|
||||
/**
|
||||
* Removes the registration for the supplied invocation receiver
|
||||
* instance as the handler for invocation notifications for the
|
||||
* specified module.
|
||||
*/
|
||||
public void unregisterReceiver (String module)
|
||||
{
|
||||
if (_receivers != null) {
|
||||
_receivers.remove(module);
|
||||
} else if (event instanceof InvocationNotificationEvent) {
|
||||
InvocationNotificationEvent ine =
|
||||
(InvocationNotificationEvent)event;
|
||||
handleInvocationNotification(
|
||||
ine.getReceiverId(), ine.getMethodId(), ine.getArgs());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process incoming message requests on user object.
|
||||
* Dispatches an invocation response.
|
||||
*/
|
||||
public void messageReceived (MessageEvent event)
|
||||
protected void handleInvocationResponse (
|
||||
int reqId, int methodId, Object[] args)
|
||||
{
|
||||
String name = event.getName();
|
||||
if (name.equals(InvocationObject.RESPONSE_NAME)) {
|
||||
handleInvocationResponse(event.getArgs());
|
||||
} else if (name.equals(InvocationObject.NOTIFICATION_NAME)) {
|
||||
handleInvocationNotification(event.getArgs());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an invocation response message.
|
||||
*/
|
||||
protected void handleInvocationResponse (Object[] args)
|
||||
{
|
||||
String name = (String)args[0];
|
||||
int invid = ((Integer)args[1]).intValue();
|
||||
|
||||
Object rsptarg = _targets.get(invid);
|
||||
if (rsptarg == null) {
|
||||
Log.warning("No target for invocation response " +
|
||||
"[args=" + StringUtil.toString(args) + "].");
|
||||
// look up the invocation marshaller registered for that response
|
||||
ListenerMarshaller listener = (ListenerMarshaller)
|
||||
_listeners.remove(reqId);
|
||||
if (listener == null) {
|
||||
Log.warning("Received invocation response for which we have " +
|
||||
"no registered listener [reqId=" + reqId +
|
||||
", methId=" + methodId +
|
||||
", args=" + StringUtil.toString(args) + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
// prune the invocation id and method arguments from the full
|
||||
// message arguments
|
||||
Object[] rargs = new Object[args.length-1];
|
||||
System.arraycopy(args, 1, rargs, 0, rargs.length);
|
||||
// Log.info("Dispatching invocation response " +
|
||||
// "[listener=" + listener + ", methId=" + methodId +
|
||||
// ", args=" + StringUtil.toString(args) + "].");
|
||||
|
||||
// and invoke the response method; we'd cache these but the key
|
||||
// for the method would have to include all of the class names of
|
||||
// all of the arguments and would probably be more expensive to
|
||||
// create than just reflecting the method (we should really test
|
||||
// this because that's half intuition and half wild-ass guess, but
|
||||
// we're going with it for now)
|
||||
String mname = "handle" + name;
|
||||
Method rspmeth = ClassUtil.getMethod(mname, rsptarg, rargs);
|
||||
if (rspmeth == null) {
|
||||
Log.warning("Unable to resolve response method " +
|
||||
"[target=" + rsptarg.getClass().getName() +
|
||||
", method=" + mname +
|
||||
", args=" + StringUtil.toString(rargs) + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
// and invoke it
|
||||
// dispatch the response
|
||||
try {
|
||||
// Log.info("Invoking method [meth=" +
|
||||
// rsptarg.getClass().getName() + "." + rspmeth.getName() +
|
||||
// ", args=" + StringUtil.toString(rargs) + "].");
|
||||
rspmeth.invoke(rsptarg, rargs);
|
||||
} catch (Exception e) {
|
||||
Log.warning("Error invoking response target method " +
|
||||
"[target=" + rsptarg + ", method=" + rspmeth + "].");
|
||||
Log.logStackTrace(e);
|
||||
listener.dispatchResponse(methodId, args);
|
||||
} catch (Throwable t) {
|
||||
Log.warning("Invocation response listener choked " +
|
||||
"[listener=" + listener + ", methId=" + methodId +
|
||||
", args=" + StringUtil.toString(args) + "].");
|
||||
Log.logStackTrace(t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes an invocation notification message.
|
||||
* Dispatches an invocation notification.
|
||||
*/
|
||||
protected void handleInvocationNotification (Object[] args)
|
||||
protected void handleInvocationNotification (
|
||||
int receiverId, int methodId, Object[] args)
|
||||
{
|
||||
String module = (String)args[0];
|
||||
String proc = (String)args[1];
|
||||
|
||||
InvocationReceiver receiver = null;
|
||||
if (_receivers != null) {
|
||||
receiver = (InvocationReceiver)_receivers.get(module);
|
||||
}
|
||||
if (receiver == null) {
|
||||
Log.warning("No receiver registered for notification " +
|
||||
"[args=" + StringUtil.toString(args) + "].");
|
||||
// look up the decoder registered for this receiver
|
||||
InvocationDecoder decoder = (InvocationDecoder)
|
||||
_receivers.get(receiverId);
|
||||
if (decoder == null) {
|
||||
Log.warning("Received notification for which we have no " +
|
||||
"registered receiver [recvId=" + receiverId +
|
||||
", methodId=" + methodId +
|
||||
", args=" + StringUtil.toString(args) + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
// prune the method arguments from the full message arguments
|
||||
Object[] nargs = new Object[args.length-2];
|
||||
System.arraycopy(args, 2, nargs, 0, nargs.length);
|
||||
// Log.info("Dispatching invocation notification " +
|
||||
// "[receiver=" + decoder.receiver + ", methodId=" + methodId +
|
||||
// ", args=" + StringUtil.toString(args) + "].");
|
||||
|
||||
// and invoke the receiver method; we'd cache these but the key
|
||||
// for the method would have to include all of the class names of
|
||||
// all of the arguments and would probably be more expensive to
|
||||
// create than just reflecting the method (we should really test
|
||||
// this because that's half intuition and half wild-ass guess, but
|
||||
// we're going with it for now)
|
||||
String mname = "handle" + proc + "Notification";
|
||||
Method rspmeth = ClassUtil.getMethod(mname, receiver, nargs);
|
||||
if (rspmeth == null) {
|
||||
Log.warning("Unable to resolve receiver method " +
|
||||
"[target=" + receiver.getClass().getName() +
|
||||
", method=" + mname +
|
||||
", args=" + StringUtil.toString(nargs) + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
// and invoke it
|
||||
try {
|
||||
rspmeth.invoke(receiver, nargs);
|
||||
} catch (Exception e) {
|
||||
Log.warning("Error invoking receiver method " +
|
||||
"[receiver=" + receiver + ", method=" + rspmeth + "].");
|
||||
Log.logStackTrace(e);
|
||||
decoder.dispatchNotification(methodId, args);
|
||||
} catch (Throwable t) {
|
||||
Log.warning("Invocation notification receiver choked " +
|
||||
"[receiver=" + decoder.receiver +
|
||||
", methId=" + methodId +
|
||||
", args=" + StringUtil.toString(args) + "].");
|
||||
Log.logStackTrace(t);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized int nextInvocationId ()
|
||||
/**
|
||||
* Used to generate monotonically increasing invocation request ids.
|
||||
*/
|
||||
protected synchronized short nextRequestId ()
|
||||
{
|
||||
return _invocationId++;
|
||||
return _requestId++;
|
||||
}
|
||||
|
||||
protected static class Response
|
||||
/**
|
||||
* Used to generate monotonically increasing invocation receiver ids.
|
||||
*/
|
||||
protected synchronized short nextReceiverId ()
|
||||
{
|
||||
public String name;
|
||||
public Object target;
|
||||
public Response (String name, Object target)
|
||||
{
|
||||
this.name = name;
|
||||
this.target = target;
|
||||
}
|
||||
return _receiverId++;
|
||||
}
|
||||
|
||||
/** The distributed object manager with which we interact. */
|
||||
protected DObjectManager _omgr;
|
||||
protected int _imoid;
|
||||
protected int _cloid;
|
||||
|
||||
protected int _invocationId;
|
||||
protected HashIntMap _targets = new HashIntMap();
|
||||
protected HashMap _receivers = new HashMap();
|
||||
/** Our client object; invocation responses and notifications are
|
||||
* received on this object. */
|
||||
protected ClientObject _clobj;
|
||||
|
||||
/** Used to generate monotonically increasing request ids. */
|
||||
protected short _requestId;
|
||||
|
||||
/** Used to generate monotonically increasing receiver ids. */
|
||||
protected short _receiverId;
|
||||
|
||||
/** Used to keep track of invocation service listeners which will
|
||||
* receive responses from invocation service requests. */
|
||||
protected HashIntMap _listeners = new HashIntMap();
|
||||
|
||||
/** Used to keep track of invocation notification receivers. */
|
||||
protected HashIntMap _receivers = new HashIntMap();
|
||||
|
||||
/** All registered receivers are maintained in a list so that we can
|
||||
* assign receiver ids to them when we go online. */
|
||||
protected ArrayList _reclist = new ArrayList();
|
||||
}
|
||||
|
||||
@@ -1,36 +1,70 @@
|
||||
//
|
||||
// $Id: InvocationReceiver.java,v 1.4 2001/10/11 04:07:52 mdb Exp $
|
||||
// $Id: InvocationReceiver.java,v 1.5 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import com.threerings.presents.dobj.DSet;
|
||||
|
||||
/**
|
||||
* Classes registered to process invocation notifications should implement
|
||||
* the invocation receiver interface and register themselves with the
|
||||
* invocation director. Because the invocation notification procedures are
|
||||
* looked up using reflection, there are no methods to implement in the
|
||||
* receiver interface, but it serves as a useful point for documentation
|
||||
* and as a useful indicator that the class in question is serving as an
|
||||
* invocation receiver.
|
||||
* Invocation notification receipt interfaces should be defined as
|
||||
* extending this interface. Actual notification receivers will implement
|
||||
* the requisite receiver interface definition and register themselves
|
||||
* with the {@link InvocationDirector} using the generated {@link
|
||||
* InvocationDispatcher} class specific to the notification receiver
|
||||
* interface in question. For example:
|
||||
*
|
||||
* <p> Invocation notifications are identified by a module name and a
|
||||
* procedure name. The module name identifies which invocation receiver
|
||||
* instance will receive the notification. Receivers are registered with
|
||||
* the invocation director as handling all notification procedures for a
|
||||
* particular module. The notification procedure name is used to construct
|
||||
* a method name which is then reflected and invoked.
|
||||
*
|
||||
* <p> The name construction is as follows: a notification message
|
||||
* requesting the invocation of a procedure named <code>Tell</code> will
|
||||
* result in a method named <code>handleTellNotification</code> being
|
||||
* invoked on the invocation receiver instance. The signature of that
|
||||
* method is defined by the arguments supplied with the invocation
|
||||
* notification message. These arguments must always be of the same type
|
||||
* and must exactly match the signature of the implementing method (with
|
||||
* the standard reflection argument type conversion process taken into
|
||||
* account).
|
||||
* <pre>
|
||||
* public class FooDirector implements FooReceiver
|
||||
* {
|
||||
* public FooDirector (PresentsContext ctx)
|
||||
* {
|
||||
* InvocationDirector idir = ctx.getClient().getInvocationDirector();
|
||||
* idir.registerReceiver(new FooDispatcher(this));
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @see InvocationDirector#registerReceiver
|
||||
*/
|
||||
public interface InvocationReceiver
|
||||
{
|
||||
/**
|
||||
* Used to maintain a registry of invocation receivers that can be
|
||||
* used to convert (large) hash codes into (small) registration
|
||||
* numbers.
|
||||
*/
|
||||
public static class Registration implements DSet.Entry
|
||||
{
|
||||
/** The unique hash code associated with this invocation receiver
|
||||
* class. */
|
||||
public String receiverCode;
|
||||
|
||||
/** The unique id assigned to this invocation receiver class at
|
||||
* registration time. */
|
||||
public short receiverId;
|
||||
|
||||
/** Creates and initializes a registration instance. */
|
||||
public Registration (String receiverCode, short receiverId)
|
||||
{
|
||||
this.receiverCode = receiverCode;
|
||||
this.receiverId = receiverId;
|
||||
}
|
||||
|
||||
/** Creates a blank instance suitable for unserialization. */
|
||||
public Registration ()
|
||||
{
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public Comparable getKey ()
|
||||
{
|
||||
return receiverCode;
|
||||
}
|
||||
|
||||
/** Generates a string representation of this instance. */
|
||||
public String toString ()
|
||||
{
|
||||
return "[" + receiverCode + " => " + receiverId + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
//
|
||||
// $Id: InvocationService.java,v 1.1 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
/**
|
||||
* Serves as the base interface for invocation services. An invocation
|
||||
* service can be defined by extending this interface and defining service
|
||||
* methods, as well as response listeners (which must extend {@link
|
||||
* InvocationListener}). For example:
|
||||
*
|
||||
* <pre>
|
||||
* public interface LocationService extends InvocationService
|
||||
* {
|
||||
*
|
||||
* // Used to communicate responses to moveTo() requests.
|
||||
* public interface MoveListener extends InvocationListener
|
||||
* {
|
||||
* // Called in response to a successful moveTo() request.
|
||||
* public void moveSucceeded (PlaceConfig config);
|
||||
* }
|
||||
*
|
||||
* // Requests that this client's body be moved to the specified
|
||||
* // location.
|
||||
* //
|
||||
* // @param placeId the object id of the place object to which the
|
||||
* // body should be moved.
|
||||
* // @param listener the listener that will be informed of success or
|
||||
* // failure.
|
||||
* public void moveTo (int placeId, MoveListener listener);
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* From this interface, a <code>LocationProvider</code> interface will be
|
||||
* generated which should be implemented by whatever server entity that
|
||||
* will actually provide the server side of this invocation service. That
|
||||
* provider interface would look like the following:
|
||||
*
|
||||
* <pre>
|
||||
* public interface LocationProvider extends InvocationProvider
|
||||
* {
|
||||
* // Requests that this client's body be moved to the specified
|
||||
* // location.
|
||||
* //
|
||||
* // @param caller the client object of the client that invoked this
|
||||
* // remotely callable method.
|
||||
* // @param placeId the object id of the place object to which the
|
||||
* // body should be moved.
|
||||
* // @param listener the listener that should be informed of success
|
||||
* // or failure.
|
||||
* public void moveTo (ClientObject caller, int placeId,
|
||||
* MoveListener listener)
|
||||
* throws InvocationException;
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public interface InvocationService
|
||||
{
|
||||
/**
|
||||
* Invocation service methods that require a response should take a
|
||||
* listener argument that can be notified of request success or
|
||||
* failure. The listener argument should extend this interface so that
|
||||
* generic failure can be reported in all cases. For example:
|
||||
*
|
||||
* <pre>
|
||||
* // Used to communicate responses to <code>moveTo</code> requests.
|
||||
* public interface MoveListener extends InvocationListener
|
||||
* {
|
||||
* // Called in response to a successful <code>moveTo</code>
|
||||
* // request.
|
||||
* public void moveSucceeded (PlaceConfig config);
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
public static interface InvocationListener
|
||||
{
|
||||
/**
|
||||
* Called to report request failure. If the invocation services
|
||||
* system detects failure of any kind, it will report it via this
|
||||
* callback. Particular services may also make use of this
|
||||
* callback to report failures of their own, or they may opt to
|
||||
* define more specific failure callbacks.
|
||||
*/
|
||||
public void requestFailed (String cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// $Id: LoggingListener.java,v 1.1 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import com.samskivert.util.Log;
|
||||
|
||||
/**
|
||||
* Implements the basic {@link InvocationListener} and logs the failure.
|
||||
*/
|
||||
public class LoggingListener
|
||||
implements InvocationService.InvocationListener
|
||||
{
|
||||
/**
|
||||
* Constructs a listener that will report the supplied error message
|
||||
* along with the reason for failure to the supplied log object.
|
||||
*/
|
||||
public LoggingListener (Log log, String errmsg)
|
||||
{
|
||||
_log = log;
|
||||
_errmsg = errmsg;
|
||||
}
|
||||
|
||||
// documentation inherited from interface
|
||||
public void requestFailed (String reason)
|
||||
{
|
||||
_log.warning(_errmsg + " [reason=" + reason + "].");
|
||||
}
|
||||
|
||||
protected Log _log;
|
||||
protected String _errmsg;
|
||||
}
|
||||
@@ -1,36 +1,32 @@
|
||||
//
|
||||
// $Id: TimeBaseService.java,v 1.2 2002/05/29 18:44:36 mdb Exp $
|
||||
// $Id: TimeBaseService.java,v 1.3 2002/08/14 19:07:54 mdb Exp $
|
||||
|
||||
package com.threerings.presents.client;
|
||||
|
||||
import com.threerings.presents.Log;
|
||||
import com.threerings.presents.client.Client;
|
||||
import com.threerings.presents.client.InvocationDirector;
|
||||
import com.threerings.presents.data.TimeBaseCodes;
|
||||
import com.threerings.presents.client.InvocationService;
|
||||
|
||||
/**
|
||||
* Provides a means by which to obtain access to a time base object which
|
||||
* can be used to convert delta times into absolute times.
|
||||
*/
|
||||
public class TimeBaseService
|
||||
implements TimeBaseCodes
|
||||
public interface TimeBaseService extends InvocationService
|
||||
{
|
||||
/**
|
||||
* Requests the oid of the specified time base object be returned. The
|
||||
* supplied response target must provide two methods to handle the
|
||||
* responses generated by this request:
|
||||
*
|
||||
* <pre>
|
||||
* public void handleTimeOidResponse (int invid, int timeOid);
|
||||
* public void handleGetTimeOidFailed (int invid, String reason);
|
||||
* </pre>
|
||||
* Used to communicated the result of a {@link #getTimeOid} request.
|
||||
*/
|
||||
public static void getTimeOid (
|
||||
Client client, String timeBase, Object rsptarget)
|
||||
public static interface GotTimeBaseListener extends InvocationListener
|
||||
{
|
||||
InvocationDirector invdir = client.getInvocationDirector();
|
||||
invdir.invoke(MODULE_NAME, GET_TIME_OID_REQUEST,
|
||||
new Object[] { timeBase }, rsptarget);
|
||||
Log.debug("Sent getTimeOid request [timeBase=" + timeBase + "].");
|
||||
/**
|
||||
* Communicates the result of a successful {@link #getTimeOid}
|
||||
* request.
|
||||
*/
|
||||
public void gotTimeOid (int timeOid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the oid of the specified time base object be fetched.
|
||||
*/
|
||||
public void getTimeOid (
|
||||
Client client, String timeBase, GotTimeBaseListener listener);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user