More progress on the invocation services.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@74 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-07-19 18:08:20 +00:00
parent 406d181d1a
commit f8ccff8f23
8 changed files with 157 additions and 20 deletions
@@ -1,5 +1,5 @@
//
// $Id: InvocationManager.java,v 1.3 2001/07/19 07:48:25 mdb Exp $
// $Id: InvocationManager.java,v 1.4 2001/07/19 18:08:20 mdb Exp $
package com.threerings.cocktail.cher.server;
@@ -51,7 +51,7 @@ public class InvocationManager
* Registers the supplied invocation provider instance as the handler
* for all invocation requests for the specified module.
*/
public void registerProvider (String module, Object provider)
public void registerProvider (String module, InvocationProvider provider)
{
_providers.put(module, provider);
}
@@ -90,10 +90,11 @@ public class InvocationManager
Object[] args = mevt.getArgs();
String module = (String)args[0];
String procedure = (String)args[1];
int invid = ((Integer)args[2]).intValue();
Integer invid = (Integer)args[2];
// locate a provider for this module
Object provider = _providers.get(module);
InvocationProvider provider =
(InvocationProvider)_providers.get(module);
if (provider == null) {
Log.warning("No provider registered for invocation request " +
"[evt=" + mevt + "].");
@@ -115,14 +116,26 @@ public class InvocationManager
}
// and invoke it
Object[] rargs = null;
try {
procmeth.invoke(provider, margs);
rargs = (Object[])procmeth.invoke(provider, margs);
} catch (Exception e) {
Log.warning("Error invoking invocation procedure " +
"[provider=" + provider + ", method=" + procmeth +
", error=" + e + "].");
}
// if there is a response to be delivered, do so
if (rargs != null) {
// fill in the invocation id
rargs[1] = invid;
// and create a message event for delivery to the client
MessageEvent revt = new MessageEvent(
mevt.getSourceOid(), InvocationObject.MESSAGE_NAME, rargs);
// and ship it off
CherServer.omgr.postEvent(revt);
}
return true;
}
@@ -0,0 +1,73 @@
//
// $Id: InvocationProvider.java,v 1.1 2001/07/19 18:08:20 mdb Exp $
package com.threerings.cocktail.cher.server;
/**
* Invocation providers should extend this class when implementing
* invocation services. Because the service procedures are identified by
* strings and the methods that are invoked are looked up via reflection,
* the derived class doesn't override or implement any particular method.
* However, the procedure names are still restricted. For example, a
* procedure identified by the name <code>Tell</code> would result in the
* invocation of a method named <code>handleTellRequest</code>. The
* arguments to that method would be defined by the arguments that
* accompanied the <code>Tell</code> invocation request. If the arguments
* do not match, a reflection error will happen when trying to invoke the
* method and the whole request will fail.
*
* <p> Invocation procedures must also package up their response in a
* particular way which is through the use of the
* <code>createResponse</code> methods. These take a response identifier
* (which determines the name of the method that will be invoked on the
* response target object provided in the client) and a variable number of
* arguments. If a response was created with the identifier
* <code>TellFailed</code>, that would result in the method
* <code>handleTellFailed</code> being invoked on the response target
* object in the client. Again the arguments much match exactly and follow
* the reflection rules for automatic conversion of primitive types
* (supply an <code>Integer</code> object for <code>int</code> params,
* etc.).
*/
public class InvocationProvider
{
/**
* Creates a response array properly configured with the supplied name
* and single argument.
*/
protected Object[] createResponse (String name, Object arg)
{
return new Object[] { name, null, arg };
}
/**
* Creates a response array properly configured with the supplied name
* and two arguments.
*/
protected Object[] createResponse (String name, Object arg1, Object arg2)
{
return new Object[] { name, null, arg1, arg2 };
}
/**
* Creates a response array properly configured with the supplied name
* and three arguments.
*/
protected Object[] createResponse (String name, Object arg1, Object arg2,
Object arg3)
{
return new Object[] { name, null, arg1, arg2, arg3 };
}
/**
* Creates a response array properly configured with the supplied name
* and varying number of arguments.
*/
protected Object[] createResponse (String name, Object[] args)
{
Object[] rsp = new Object[args.length+2];
rsp[0] = name;
System.arraycopy(args, 0, rsp, 2, args.length);
return rsp;
}
}
@@ -1,5 +1,5 @@
//
// $Id: PresentsClient.java,v 1.7 2001/07/19 07:48:25 mdb Exp $
// $Id: PresentsClient.java,v 1.8 2001/07/19 18:08:20 mdb Exp $
package com.threerings.cocktail.cher.server;
@@ -71,6 +71,14 @@ public class Client implements Subscriber, MessageHandler
return _username;
}
/**
* Returns the client object that is associated with this client.
*/
public ClientObject getClientObject ()
{
return _clobj;
}
/**
* Called by the client manager when a new connection arrives that
* authenticates as this already established client. This must only be
@@ -289,10 +297,15 @@ public class Client implements Subscriber, MessageHandler
public void dispatch (Client client, UpstreamMessage msg)
{
ForwardEventRequest req = (ForwardEventRequest)msg;
Log.info("Forwarding event [client=" + client +
", event=" + req.getEvent() + "].");
DEvent fevt = req.getEvent();
// fill in the proper source oid
fevt.setSourceOid(client.getClientObject().getOid());
// forward the event to the omgr for processing
CherServer.omgr.postEvent(req.getEvent());
Log.info("Forwarding event [client=" + client +
", event=" + fevt + "].");
CherServer.omgr.postEvent(fevt);
}
}