Generate receivers and decoders for actionscript

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6507 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Charlie Groves
2011-02-26 02:04:06 +00:00
parent c3d9b3b238
commit 81bfee5132
11 changed files with 129 additions and 54 deletions
+2 -1
View File
@@ -290,7 +290,8 @@
<exclude name="**/InvocationReceiver.java"/>
</javac>
<!-- now generate the associated files -->
<genreceiver header="lib/SOURCE_HEADER" classpathref="built.classpath" checking="${gencheck}">
<genreceiver header="lib/SOURCE_HEADER" classpathref="built.classpath" checking="${gencheck}"
asroot="${asrc.dir}">
<fileset dir="${src.dir}" includes="**/*Receiver.java"
excludes="**/InvocationReceiver.java,**/SignalReceiver.java"/>
</genreceiver>
@@ -21,7 +21,6 @@
package com.threerings.bureau.client {
import com.threerings.bureau.client.BureauReceiver;
import com.threerings.presents.client.InvocationDecoder;
/**
@@ -49,31 +48,28 @@ public class BureauDecoder extends InvocationDecoder
this.receiver = receiver;
}
// documentation inherited
public override function getReceiverCode () :String
{
return RECEIVER_CODE;
}
// documentation inherited
public override function dispatchNotification (methodId :int, args :Array) :void
{
switch (methodId) {
case CREATE_AGENT:
BureauReceiver(receiver).createAgent(
args[0] as int
(args[0] as int)
);
return;
case DESTROY_AGENT:
BureauReceiver(receiver).destroyAgent(
args[0] as int
(args[0] as int)
);
return;
default:
super.dispatchNotification(methodId, args);
return;
}
}
}
@@ -23,28 +23,12 @@ package com.threerings.bureau.client {
import com.threerings.presents.client.InvocationReceiver;
/**
* Hooks for controlling a previously launched bureau client.
*/
public interface BureauReceiver extends InvocationReceiver
{
/**
* Creates a new agent. Implementors should create a new {@link Agent} and give it access to
* the {@link AgentObject} referred to by the <code>agentId</code> parameter and must notify
* the service that the agent has been created using {@link BureauService#agentCreated}.
* @param client the client receiving the request
* @param agentId the id of the <code>AgentObject</code> that needs an <code>Agent</code>
*/
function createAgent (agentId :int) :void;
// from Java interface BureauReceiver
function createAgent (arg1 :int) :void;
/**
* Destroys a previously created agent. Implementors should destroy the agent that was created
* by the call to <code>createAgent</code> with the same agent id and must notify
* the service that the agent has been created using {@link BureauService#agentDestroyed}.
* @param client the client receiving the request
* @param agentId the id of the <code>AgentObject</code> whose <code>Agent</code>
* should be destroyed
*/
function destroyAgent (agentId :int) :void;
// from Java interface BureauReceiver
function destroyAgent (arg1 :int) :void;
}
}
@@ -21,7 +21,6 @@
package com.threerings.crowd.client {
import com.threerings.crowd.client.LocationReceiver;
import com.threerings.presents.client.InvocationDecoder;
/**
@@ -45,19 +44,16 @@ public class LocationDecoder extends InvocationDecoder
this.receiver = receiver;
}
// documentation inherited
override public function getReceiverCode () :String
public override function getReceiverCode () :String
{
return RECEIVER_CODE;
}
// documentation inherited
override public function dispatchNotification (
methodId :int, args :Array) :void
public override function dispatchNotification (methodId :int, args :Array) :void
{
switch (methodId) {
case FORCED_MOVE:
(receiver as LocationReceiver).forcedMove(
LocationReceiver(receiver).forcedMove(
(args[0] as int)
);
return;
@@ -23,18 +23,9 @@ package com.threerings.crowd.client {
import com.threerings.presents.client.InvocationReceiver;
/**
* Defines, for the location services, a set of notifications delivered
* asynchronously by the server to the client.
*/
public interface LocationReceiver extends InvocationReceiver
{
/**
* Used to communicate a required move notification to the client. The
* server will have removed the client from their existing location
* and the client is then responsible for generating a {@link
* LocationService#moveTo} request to move to the new location.
*/
function forcedMove (placeId :int) :void;
// from Java interface LocationReceiver
function forcedMove (arg1 :int) :void;
}
}
@@ -569,7 +569,6 @@ public abstract class PeerManager
final DObjectAddress remote, final ResultListener<Integer> listener)
{
if (remote.nodeName.equals(_nodeName)) {
System.out.println("Found locally!");
_omgr.postRunnable(new Runnable() {
public void run () {
listener.requestCompleted(remote.oid);
@@ -29,12 +29,14 @@ import java.util.List;
import java.io.File;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;
import com.samskivert.util.ComparableArrayList;
import com.samskivert.util.StringUtil;
import com.threerings.presents.client.InvocationDecoder;
import com.threerings.presents.client.InvocationReceiver;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.server.InvocationSender;
@@ -43,6 +45,14 @@ import com.threerings.presents.server.InvocationSender;
*/
public class GenReceiverTask extends InvocationTask
{
/**
* Configures the path to our ActionScript source files.
*/
public void setAsroot (File asroot)
{
_asroot = asroot;
}
@Override
public void processClass (File source, Class<?> receiver)
throws Exception
@@ -57,11 +67,8 @@ public class GenReceiverTask extends InvocationTask
}
// verify that the receiver class name is as we expect it to be
if (!rname.endsWith("Receiver")) {
System.err.println("Cannot process '" + rname + "':");
System.err.println("Receiver classes must be named SomethingReceiver.");
return;
}
Preconditions.checkArgument(rname.endsWith("Receiver"), "Cannot process '%s'. " +
"Receiver classes must be named SomethingReceiver.", rname);
ImportSet imports = new ImportSet();
ComparableArrayList<ServiceMethod> methods = new ComparableArrayList<ServiceMethod>();
@@ -142,8 +149,33 @@ public class GenReceiverTask extends InvocationTask
"package", rpackage,
"methods", methods,
"imports", implist);
if (_asroot == null) {
return;
}
// generate an ActionScript decoder
String sppath = rpackage.replace('.', File.separatorChar);
String aspath = _asroot + File.separator + sppath + File.separator + name + "Decoder.as";
writeTemplate(AS_DECODER_TMPL, aspath,
"name", name,
"receiver_code", rcode,
"package", rpackage,
"methods", methods,
"imports", implist);
// ... and an ActionScript receiver
aspath = _asroot + File.separator + sppath + File.separator + rname + ".as";
implist.remove(InvocationDecoder.class.getName());
checkedAdd(implist, InvocationReceiver.class.getName());
writeTemplate(AS_RECEIVER_TMPL, aspath,
"name", name,
"package", rpackage,
"methods", methods,
"imports", implist);
}
/** The path to our ActionScript source files. */
protected File _asroot;
/** Specifies the path to the sender template. */
protected static final String SENDER_TMPL =
"com/threerings/presents/tools/sender.tmpl";
@@ -151,4 +183,12 @@ public class GenReceiverTask extends InvocationTask
/** Specifies the path to the decoder template. */
protected static final String DECODER_TMPL =
"com/threerings/presents/tools/decoder.tmpl";
/** Specifies the path to the decoder template. */
protected static final String AS_DECODER_TMPL =
"com/threerings/presents/tools/decoder_as.tmpl";
/** Specifies the path to the decoder template. */
protected static final String AS_RECEIVER_TMPL =
"com/threerings/presents/tools/receiver_as.tmpl";
}
@@ -47,7 +47,7 @@ public class GenUtil extends com.samskivert.util.GenUtil
/** A regular expression for matching the class or interface declaration. */
public static final Pattern NAME_PATTERN =
Pattern.compile("^\\s*public\\s+(?:abstract\\s+)?(interface|class|enum)\\s+(\\S+)(\\W|$)");
Pattern.compile("^\\s*public\\s+(?:abstract\\s+)?(@?interface|class|enum)\\s+([\\w$]+)");
/**
* Returns the name of the supplied class as it would appear in ActionScript code using the
@@ -44,7 +44,7 @@ import com.samskivert.util.StringUtil;
* <p>A few methods also use a "pattern" string parameter that is used to match a class name.
* This is a dumbed down regular expression (to avoid many \.) where "*" means .* and no other
* characters have special meaning. The pattern is also implicitly enclosed with ^$ so that the
* pattern must match the class name in its entirity. Callers will mostly use this to specify a
* pattern must match the class name in its entirety. Callers will mostly use this to specify a
* prefix like "something*" or a suffix like "*something".
*/
public class ImportSet
@@ -0,0 +1,51 @@
package {{package}} {
{{#imports}}
import {{this}};
{{/imports}}
/**
* Dispatches calls to a {@link {{name}}Receiver} instance.
*/
public class {{name}}Decoder extends InvocationDecoder
{
/** The generated hash code used to identify this receiver class. */
public static const RECEIVER_CODE :String = "{{receiver_code}}";
{{#methods}}
/** The method id used to dispatch {@link {{name}}Receiver#{{method.name}}}
* notifications. */
public static const {{code}} :int = {{-index}};
{{/methods}}
/**
* Creates a decoder that may be registered to dispatch invocation
* service notifications to the specified receiver.
*/
public function {{name}}Decoder (receiver :{{name}}Receiver)
{
this.receiver = receiver;
}
public override function getReceiverCode () :String
{
return RECEIVER_CODE;
}
public override function dispatchNotification (methodId :int, args :Array) :void
{
switch (methodId) {
{{#methods}}
case {{code}}:
{{name}}Receiver(receiver).{{method.name}}(
{{getASUnwrappedArgList}}
);
return;
{{/methods}}
default:
super.dispatchNotification(methodId, args);
}
}
}
}
@@ -0,0 +1,17 @@
package {{package}} {
{{#imports}}
import {{this}};
{{/imports}}
public interface {{name}}Receiver extends InvocationReceiver
{
{{#methods}}
{{^-first}}
{{/-first}}
// from Java interface {{name}}Receiver
function {{method.name}} ({{getASArgList}}) :void;
{{/methods}}
}
}