Widening.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4843 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2007-10-17 21:06:17 +00:00
parent 45034b895e
commit 6b46554eaf
@@ -94,8 +94,7 @@ public class InvocationDirector
{ {
// remove the receiver from the list // remove the receiver from the list
for (var ii :int = _reclist.length - 1; ii >= 0; ii--) { for (var ii :int = _reclist.length - 1; ii >= 0; ii--) {
var decoder :InvocationDecoder = var decoder :InvocationDecoder = (_reclist[ii] as InvocationDecoder);
(_reclist[ii] as InvocationDecoder);
if (decoder.getReceiverCode() === receiverCode) { if (decoder.getReceiverCode() === receiverCode) {
_reclist.splice(ii, 1); _reclist.splice(ii, 1);
} }
@@ -104,15 +103,13 @@ public class InvocationDirector
// if we're logged on, clear out any receiver id mapping // if we're logged on, clear out any receiver id mapping
if (_clobj != null) { if (_clobj != null) {
var rreg :InvocationReceiver_Registration = var rreg :InvocationReceiver_Registration =
(_clobj.receivers.get(receiverCode) as (_clobj.receivers.get(receiverCode) as InvocationReceiver_Registration);
InvocationReceiver_Registration);
if (rreg == null) { if (rreg == null) {
log.warning("Receiver unregistered for which we have no " + log.warning("Receiver unregistered for which we have no id to code mapping " +
"id to code mapping [code=" + receiverCode + "]."); "[code=" + receiverCode + "].");
} else { } else {
var odecoder :Object = _receivers.remove(rreg.receiverId); var odecoder :Object = _receivers.remove(rreg.receiverId);
// log.info("Cleared receiver " + // log.info("Cleared receiver " + StringUtil.shortClassName(odecoder) +
// StringUtil.shortClassName(odecoder) +
// " " + rreg + "."); // " " + rreg + ".");
} }
_clobj.removeFromReceivers(receiverCode); _clobj.removeFromReceivers(receiverCode);
@@ -120,21 +117,19 @@ public class InvocationDirector
} }
/** /**
* Assigns a receiver id to this decoder and publishes it in the * Assigns a receiver id to this decoder and publishes it in the {@link ClientObject#receivers}
* {@link ClientObject#receivers} field. * field.
*/ */
internal function assignReceiverId (decoder :InvocationDecoder) :void internal function assignReceiverId (decoder :InvocationDecoder) :void
{ {
var reg :InvocationReceiver_Registration = var reg :InvocationReceiver_Registration =
new InvocationReceiver_Registration(decoder.getReceiverCode(), new InvocationReceiver_Registration(decoder.getReceiverCode(), nextReceiverId());
nextReceiverId());
_clobj.addToReceivers(reg); _clobj.addToReceivers(reg);
_receivers.put(reg.receiverId, decoder); _receivers.put(reg.receiverId, decoder);
} }
/** /**
* Called when we log on; generates mappings for all receivers * Called when we log on; generates mappings for all receivers registered prior to logon.
* registered prior to logon.
*/ */
internal function assignReceiverIds () :void internal function assignReceiverIds () :void
{ {
@@ -149,11 +144,10 @@ public class InvocationDirector
} }
/** /**
* Requests that the specified invocation request be packaged up and * Requests that the specified invocation request be packaged up and sent to the supplied
* sent to the supplied invocation oid. * invocation oid.
*/ */
public function sendRequest ( public function sendRequest (invOid :int, invCode :int, methodId :int, args :Array) :void
invOid :int, invCode :int, methodId :int, args :Array) :void
{ {
// configure any invocation listener marshallers among the args // configure any invocation listener marshallers among the args
for each (var arg :Object in args) { for each (var arg :Object in args) {
@@ -164,34 +158,26 @@ public class InvocationDirector
lm.requestId = nextRequestId(); lm.requestId = nextRequestId();
lm.mapStamp = getTimer(); lm.mapStamp = getTimer();
// create a mapping for this marshaller so that we can // create a mapping for this marshaller so that we can properly dispatch responses
// properly dispatch responses sent to it // sent to it
_listeners.put(lm.requestId, lm); _listeners.put(lm.requestId, lm);
} }
} }
// create an invocation request event // create an invocation request event and dispatch it
var event :InvocationRequestEvent = _omgr.postEvent(new InvocationRequestEvent(invOid, invCode, methodId, args));
new InvocationRequestEvent(invOid, invCode, methodId, args);
// now, dispatch the event
_omgr.postEvent(event);
} }
// documentation inherited from interface EventListener // documentation inherited from interface EventListener
public function eventReceived (event :DEvent) :void public function eventReceived (event :DEvent) :void
{ {
if (event is InvocationResponseEvent) { if (event is InvocationResponseEvent) {
var ire :InvocationResponseEvent = var ire :InvocationResponseEvent = (event as InvocationResponseEvent);
(event as InvocationResponseEvent); handleInvocationResponse(ire.getRequestId(), ire.getMethodId(), ire.getArgs());
handleInvocationResponse(ire.getRequestId(), ire.getMethodId(),
ire.getArgs());
} else if (event is InvocationNotificationEvent) { } else if (event is InvocationNotificationEvent) {
var ine :InvocationNotificationEvent = var ine :InvocationNotificationEvent = (event as InvocationNotificationEvent);
(event as InvocationNotificationEvent); handleInvocationNotification(ine.getReceiverId(), ine.getMethodId(), ine.getArgs());
handleInvocationNotification(ine.getReceiverId(), ine.getMethodId(),
ine.getArgs());
} else if (event is MessageEvent) { } else if (event is MessageEvent) {
var me :MessageEvent = (event as MessageEvent); var me :MessageEvent = (event as MessageEvent);
@@ -204,35 +190,29 @@ public class InvocationDirector
/** /**
* Dispatches an invocation response. * Dispatches an invocation response.
*/ */
protected function handleInvocationResponse protected function handleInvocationResponse (reqId :int, methodId :int, args :Array) :void
(reqId :int, methodId :int, args :Array) :void
{ {
var listener :InvocationMarshaller_ListenerMarshaller = var listener :InvocationMarshaller_ListenerMarshaller =
(_listeners.remove(reqId) as InvocationMarshaller_ListenerMarshaller); (_listeners.remove(reqId) as InvocationMarshaller_ListenerMarshaller);
if (listener == null) { if (listener == null) {
log.warning("Received invocation response for which we have " + log.warning("Received invocation response for which we have no registered listener " +
"no registered listener [reqId=" + reqId + "[reqId=" + reqId + ", methId=" + methodId + ", args=" + args + "]. " +
", methId=" + methodId + "It is possble that this listener was flushed because the response did " +
", args=" + args + "]. " + "not arrive within " + LISTENER_MAX_AGE + " milliseconds.");
"It is possble that this listener was flushed " +
"because the response did not arrive within " +
LISTENER_MAX_AGE + " milliseconds.");
return; return;
} }
unwrapArgs(args); unwrapArgs(args);
// log.info("Dispatching invocation response " + // log.info("Dispatching invocation response [listener=" + listener +
// "[listener=" + listener + ", methId=" + methodId + // ", methId=" + methodId + ", args=" + StringUtil.toString(args) + "].");
// ", args=" + StringUtil.toString(args) + "].");
// dispatch the response // dispatch the response
try { try {
listener.dispatchResponse(methodId, args); listener.dispatchResponse(methodId, args);
} catch (e :Error) { } catch (e :Error) {
log.warning("Invocation response listener choked " + log.warning("Invocation response listener choked [listener=" + listener +
"[listener=" + listener + ", methId=" + methodId + ", methId=" + methodId + ", args=" + args + "].");
", args=" + args + "].");
log.logStackTrace(e); log.logStackTrace(e);
} }
@@ -251,52 +231,43 @@ public class InvocationDirector
receiverId :int, methodId :int, args :Array) :void receiverId :int, methodId :int, args :Array) :void
{ {
// look up the decoder registered for this receiver // look up the decoder registered for this receiver
var decoder :InvocationDecoder = var decoder :InvocationDecoder = (_receivers.get(receiverId) as InvocationDecoder);
(_receivers.get(receiverId) as InvocationDecoder);
if (decoder == null) { if (decoder == null) {
log.warning("Received notification for which we have no " + log.warning("Received notification for which we have no registered receiver " +
"registered receiver [recvId=" + receiverId + "[recvId=" + receiverId + ", methodId=" + methodId +
", methodId=" + methodId +
", args=" + args + "]."); ", args=" + args + "].");
return; return;
} }
unwrapArgs(args); unwrapArgs(args);
// log.info("Dispatching invocation notification " + // log.info("Dispatching invocation notification [receiver=" + decoder.receiver +
// "[receiver=" + decoder.receiver + ", methodId=" + methodId + // ", methodId=" + methodId + ", args=" + StringUtil.toString(args) + "].");
// ", args=" + StringUtil.toString(args) + "].");
try { try {
decoder.dispatchNotification(methodId, args); decoder.dispatchNotification(methodId, args);
} catch (e :Error) { } catch (e :Error) {
log.warning("Invocation notification receiver choked " + log.warning("Invocation notification receiver choked [receiver=" + decoder.receiver +
"[receiver=" + decoder.receiver + ", methId=" + methodId + ", args=" + args + "].");
", methId=" + methodId +
", args=" + args + "].");
log.logStackTrace(e); log.logStackTrace(e);
} }
} }
/** /**
* Flushes listener mappings that are older than {@link * Flushes listener mappings that are older than {@link #LISTENER_MAX_AGE} milliseconds. An
* #LISTENER_MAX_AGE} milliseconds. An alternative to flushing * alternative to flushing listeners that did not explicitly receive a response within our
* listeners that did not explicitly receive a response within our * expiry time period is to have the server's proxy listener send a message to the client when
* expiry time period is to have the server's proxy listener send a * it is finalized. We then know that no server entity will subsequently use that proxy
* message to the client when it is finalized. We then know that no * listener to send a response to the client. This involves more network traffic and complexity
* server entity will subsequently use that proxy listener to send a * than seems necessary and if a user of the system does respond after their listener has been
* response to the client. This involves more network traffic and * flushed, an informative warning will be logged. (Famous last words.)
* complexity than seems necessary and if a user of the system does
* respond after their listener has been flushed, an informative
* warning will be logged. (Famous last words.)
*/ */
protected function flushListeners (now :Number) :void protected function flushListeners (now :Number) :void
{ {
var then :Number = now - LISTENER_MAX_AGE; var then :Number = now - LISTENER_MAX_AGE;
for each (var reqId :int in _listeners.keys()) { for each (var reqId :int in _listeners.keys()) {
var lm :InvocationMarshaller_ListenerMarshaller = var lm :InvocationMarshaller_ListenerMarshaller =
(_listeners.get(reqId) as (_listeners.get(reqId) as InvocationMarshaller_ListenerMarshaller);
InvocationMarshaller_ListenerMarshaller);
if (lm.mapStamp < then) { if (lm.mapStamp < then) {
_listeners.remove(reqId); _listeners.remove(reqId);
} }
@@ -304,10 +275,9 @@ public class InvocationDirector
} }
/** /**
* Called when the server has informed us that our previous client * Called when the server has informed us that our previous client object is going the way of
* object is going the way of the Dodo because we're changing screen * the Dodo because we're changing screen names. We subscribe to the new object and report to
* names. We subscribe to the new object and report to the client once * the client once we've got our hands on it.
* we've got our hands on it.
*/ */
protected function handleClientObjectChanged (newCloid :int) :void protected function handleClientObjectChanged (newCloid :int) :void
{ {
@@ -315,8 +285,7 @@ public class InvocationDirector
} }
/** /**
* Unwrap any arguments that have arrived from the server in wrapped * Unwrap any arguments that have arrived from the server in wrapped types.
* types.
*/ */
protected function unwrapArgs (args :Array) :void protected function unwrapArgs (args :Array) :void
{ {
@@ -346,8 +315,8 @@ public class InvocationDirector
} }
/** /**
* Called by the ClientObject SubscriberAdapter when the client object * Called by the ClientObject SubscriberAdapter when the client object has been returned by the
* has been returned by the server. * server.
*/ */
internal function gotClientObject (clobj :ClientObject) :void internal function gotClientObject (clobj :ClientObject) :void
{ {
@@ -355,18 +324,16 @@ public class InvocationDirector
clobj.setReceivers(new DSet()); clobj.setReceivers(new DSet());
_clobj = clobj; _clobj = clobj;
assignReceiverIds(); assignReceiverIds();
_client.gotClientObject(clobj); _client.gotClientObject(clobj);
} }
/** /**
* Called by the ClientObject SubscriberAdapter when it fails. * Called by the ClientObject SubscriberAdapter when it fails.
*/ */
internal function gotClientObjectFailed ( internal function gotClientObjectFailed (oid :int, cause :ObjectAccessError) :void
oid :int, cause :ObjectAccessError) :void
{ {
log.warning("Invocation director unable to subscribe to " + log.warning("Invocation director unable to subscribe to client object [cloid=" + oid +
"client object [cloid=" + oid + ", cause=" + cause + "]!"); ", cause=" + cause + "]!");
_client.getClientObjectFailed(cause); _client.getClientObjectFailed(cause);
} }
@@ -376,8 +343,7 @@ public class InvocationDirector
/** The client for whom we're working. */ /** The client for whom we're working. */
internal var _client :Client; internal var _client :Client;
/** Our client object; invocation responses and notifications are /** Our client object; invocation responses and notifications are received on this object. */
* received on this object. */
internal var _clobj :ClientObject; internal var _clobj :ClientObject;
/** Used to generate monotonically increasing request ids. */ /** Used to generate monotonically increasing request ids. */
@@ -386,15 +352,15 @@ public class InvocationDirector
/** Used to generate monotonically increasing receiver ids. */ /** Used to generate monotonically increasing receiver ids. */
protected var _receiverId :int; protected var _receiverId :int;
/** Used to keep track of invocation service listeners which will /** Used to keep track of invocation service listeners which will receive responses from
* receive responses from invocation service requests. */ * invocation service requests. */
protected var _listeners :HashMap = new HashMap(); protected var _listeners :HashMap = new HashMap();
/** Used to keep track of invocation notification receivers. */ /** Used to keep track of invocation notification receivers. */
protected var _receivers :HashMap = new HashMap(); protected var _receivers :HashMap = new HashMap();
/** All registered receivers are maintained in a list so that we can /** All registered receivers are maintained in a list so that we can assign receiver ids to
* assign receiver ids to them when we go online. */ * them when we go online. */
internal var _reclist :Array = []; internal var _reclist :Array = [];
/** The last time we flushed our listeners. */ /** The last time we flushed our listeners. */