Widened, modified sendRequest() to avoid freakout if it is called after the

director is cleaned up.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4624 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2007-03-13 18:18:06 +00:00
parent 31e6bbee01
commit f0fe896194
@@ -52,15 +52,14 @@ public class InvocationDirector
implements EventListener implements EventListener
{ {
/** /**
* Initializes the invocation director. This is called when the client * Initializes the invocation director. This is called when the client establishes a connection
* establishes a connection with the server. * with the server.
* *
* @param omgr the distributed object manager via which the invocation * @param omgr the distributed object manager via which the invocation manager will send and
* manager will send and receive events. * receive events.
* @param cloid the oid of the object on which invocation * @param cloid the oid of the object on which invocation notifications as well as invocation
* notifications as well as invocation responses will be received. * responses will be received.
* @param client a reference to the client for whom we're doing our * @param client a reference to the client for whom we're doing our business.
* business.
*/ */
public void init (DObjectManager omgr, final int cloid, Client client) public void init (DObjectManager omgr, final int cloid, Client client)
{ {
@@ -89,25 +88,23 @@ public class InvocationDirector
// assign a mapping to already registered receivers // assign a mapping to already registered receivers
assignReceiverIds(); assignReceiverIds();
// let the client know that we're ready to go now that // let the client know that we're ready to go now that we've got our subscription
// we've got our subscription to the client object // to the client object
_client.gotClientObject(_clobj); _client.gotClientObject(_clobj);
} }
public void requestFailed (int oid, ObjectAccessException cause) { public void requestFailed (int oid, ObjectAccessException cause) {
// aiya! we were unable to subscribe to the client object. // aiya! we were unable to subscribe to the client object. we're hosed!
// we're hosed, hosed, hosed Log.warning("Invocation director unable to subscribe to client object " +
Log.warning("Invocation director unable to subscribe to " + "[cloid=" + cloid + ", cause=" + cause + "]!");
"client object [cloid=" + cloid +
", cause=" + cause + "]!");
_client.getClientObjectFailed(cause); _client.getClientObjectFailed(cause);
} }
}); });
} }
/** /**
* Clears out our session information. This is called when the client * Clears out our session information. This is called when the client ends its session with the
* ends its session with the server. * server.
*/ */
public void cleanup () public void cleanup ()
{ {
@@ -122,8 +119,7 @@ public class InvocationDirector
} }
/** /**
* Registers an invocation notification receiver by way of its * Registers an invocation notification receiver by way of its notification event decoder.
* notification event decoder.
*/ */
public void registerReceiver (InvocationDecoder decoder) public void registerReceiver (InvocationDecoder decoder)
{ {
@@ -142,8 +138,7 @@ public class InvocationDirector
public void unregisterReceiver (String receiverCode) public void unregisterReceiver (String receiverCode)
{ {
// remove the receiver from the list // remove the receiver from the list
for (Iterator<InvocationDecoder> iter = _reclist.iterator(); for (Iterator<InvocationDecoder> iter = _reclist.iterator(); iter.hasNext(); ) {
iter.hasNext(); ) {
InvocationDecoder decoder = iter.next(); InvocationDecoder decoder = iter.next();
if (decoder.getReceiverCode().equals(receiverCode)) { if (decoder.getReceiverCode().equals(receiverCode)) {
iter.remove(); iter.remove();
@@ -152,15 +147,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) {
Registration rreg = (Registration) Registration rreg = (Registration)_clobj.receivers.get(receiverCode);
_clobj.receivers.get(receiverCode);
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 {
Object decoder = _receivers.remove(rreg.receiverId); Object decoder = _receivers.remove(rreg.receiverId);
// Log.info("Cleared receiver " + // Log.info("Cleared receiver " + StringUtil.shortClassName(decoder) +
// StringUtil.shortClassName(decoder) +
// " " + rreg + "."); // " " + rreg + ".");
} }
_clobj.removeFromReceivers(receiverCode); _clobj.removeFromReceivers(receiverCode);
@@ -168,24 +161,21 @@ 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.
*/ */
protected void assignReceiverId (InvocationDecoder decoder) protected void assignReceiverId (InvocationDecoder decoder)
{ {
Registration reg = new Registration( Registration reg = new Registration(decoder.getReceiverCode(), nextReceiverId());
decoder.getReceiverCode(), nextReceiverId());
// stick the mapping into the client object // stick the mapping into the client object
_clobj.addToReceivers(reg); _clobj.addToReceivers(reg);
// and map the receiver in our receivers table // and map the receiver in our receivers table
_receivers.put(reg.receiverId, decoder); _receivers.put(reg.receiverId, decoder);
// Log.info("Registered receiver " + // Log.info("Registered receiver " + StringUtil.shortClassName(decoder) + " " + reg + ".");
// StringUtil.shortClassName(decoder) + " " + reg + ".");
} }
/** /**
* 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.
*/ */
protected void assignReceiverIds () protected void assignReceiverIds ()
{ {
@@ -201,14 +191,18 @@ 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 void sendRequest ( public void sendRequest (int invOid, int invCode, int methodId, Object[] args)
int invOid, int invCode, int methodId, Object[] args)
{ {
// configure any invocation listener marshallers among the if (_clobj == null) {
// arguments Log.warning("Dropping invocation request on shutdown director [code=" + invCode +
", methodId=" + methodId + "].");
return;
}
// configure any invocation listener marshallers among the arguments
int acount = args.length; int acount = args.length;
for (int ii = 0; ii < acount; ii++) { for (int ii = 0; ii < acount; ii++) {
Object arg = args[ii]; Object arg = args[ii];
@@ -217,19 +211,17 @@ public class InvocationDirector
lm.callerOid = _clobj.getOid(); lm.callerOid = _clobj.getOid();
lm.requestId = nextRequestId(); lm.requestId = nextRequestId();
lm.mapStamp = System.currentTimeMillis(); lm.mapStamp = System.currentTimeMillis();
// 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
InvocationRequestEvent event = InvocationRequestEvent event = new InvocationRequestEvent(invOid, invCode, methodId, args);
new InvocationRequestEvent(invOid, invCode, methodId, args);
// because invocation directors are used on the server, we set the // because invocation directors are used on the server, we set the source oid here so that
// source oid here so that invocation requests are properly // invocation requests are properly attributed to the right client object when created by
// attributed to the right client object when created by
// server-side entities only sort of pretending to be a client // server-side entities only sort of pretending to be a client
event.setSourceOid(_clobj.getOid()); event.setSourceOid(_clobj.getOid());
@@ -246,20 +238,16 @@ public class InvocationDirector
{ {
if (event instanceof InvocationResponseEvent) { if (event instanceof InvocationResponseEvent) {
InvocationResponseEvent ire = (InvocationResponseEvent)event; InvocationResponseEvent ire = (InvocationResponseEvent)event;
handleInvocationResponse( handleInvocationResponse(ire.getRequestId(), ire.getMethodId(), ire.getArgs());
ire.getRequestId(), ire.getMethodId(), ire.getArgs());
} else if (event instanceof InvocationNotificationEvent) { } else if (event instanceof InvocationNotificationEvent) {
InvocationNotificationEvent ine = InvocationNotificationEvent ine = (InvocationNotificationEvent)event;
(InvocationNotificationEvent)event; handleInvocationNotification(ine.getReceiverId(), ine.getMethodId(), ine.getArgs());
handleInvocationNotification(
ine.getReceiverId(), ine.getMethodId(), ine.getArgs());
} else if (event instanceof MessageEvent) { } else if (event instanceof MessageEvent) {
MessageEvent mevt = (MessageEvent)event; MessageEvent mevt = (MessageEvent)event;
if (mevt.getName().equals(ClientObject.CLOBJ_CHANGED)) { if (mevt.getName().equals(ClientObject.CLOBJ_CHANGED)) {
handleClientObjectChanged( handleClientObjectChanged(((Integer)mevt.getArgs()[0]).intValue());
((Integer)mevt.getArgs()[0]).intValue());
} }
} }
} }
@@ -267,33 +255,28 @@ public class InvocationDirector
/** /**
* Dispatches an invocation response. * Dispatches an invocation response.
*/ */
protected void handleInvocationResponse ( protected void handleInvocationResponse (int reqId, int methodId, Object[] args)
int reqId, int methodId, Object[] args)
{ {
// look up the invocation marshaller registered for that response // look up the invocation marshaller registered for that response
ListenerMarshaller listener = _listeners.remove(reqId); ListenerMarshaller listener = _listeners.remove(reqId);
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=" +
", methId=" + methodId + StringUtil.toString(args) + "]. It is possble that this listener was " +
", args=" + StringUtil.toString(args) + "]. " + "flushed because the response did not arrive within " +
"It is possble that this listener was flushed " +
"because the response did not arrive within " +
LISTENER_MAX_AGE + " milliseconds."); LISTENER_MAX_AGE + " milliseconds.");
return; return;
} }
// 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 (Throwable t) { } catch (Throwable t) {
Log.warning("Invocation response listener choked " + Log.warning("Invocation response listener choked [listener=" + listener +
"[listener=" + listener + ", methId=" + methodId + ", methId=" + methodId + ", args=" + StringUtil.toString(args) + "].");
", args=" + StringUtil.toString(args) + "].");
Log.logStackTrace(t); Log.logStackTrace(t);
} }
@@ -308,39 +291,33 @@ public class InvocationDirector
/** /**
* Dispatches an invocation notification. * Dispatches an invocation notification.
*/ */
protected void handleInvocationNotification ( protected void handleInvocationNotification (int receiverId, int methodId, Object[] args)
int receiverId, int methodId, Object[] args)
{ {
// look up the decoder registered for this receiver // look up the decoder registered for this receiver
InvocationDecoder decoder = _receivers.get(receiverId); InvocationDecoder decoder = _receivers.get(receiverId);
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=" + StringUtil.toString(args) + "]."); ", args=" + StringUtil.toString(args) + "].");
return; return;
} }
// 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 (Throwable t) { } catch (Throwable t) {
Log.warning("Invocation notification receiver choked " + Log.warning("Invocation notification receiver choked [receiver=" + decoder.receiver +
"[receiver=" + decoder.receiver + ", methId=" + methodId + ", args=" + StringUtil.toString(args) + "].");
", methId=" + methodId +
", args=" + StringUtil.toString(args) + "].");
Log.logStackTrace(t); Log.logStackTrace(t);
} }
} }
/** /**
* 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 void handleClientObjectChanged (int newCloid) protected void handleClientObjectChanged (int newCloid)
{ {
@@ -372,24 +349,20 @@ public class InvocationDirector
} }
public void requestFailed (int oid, ObjectAccessException cause) { public void requestFailed (int oid, ObjectAccessException cause) {
Log.warning("Aiya! Unable to subscribe to changed " + Log.warning("Aiya! Unable to subscribe to changed client object [cloid=" + oid +
"client object [cloid=" + oid +
", cause=" + cause + "]."); ", cause=" + cause + "].");
} }
}); });
} }
/** /**
* 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 void flushListeners (long now) protected void flushListeners (long now)
{ {
@@ -428,8 +401,7 @@ public class InvocationDirector
/** The client for whom we're working. */ /** The client for whom we're working. */
protected Client _client; protected 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. */
protected ClientObject _clobj; protected ClientObject _clobj;
/** Used to generate monotonically increasing request ids. */ /** Used to generate monotonically increasing request ids. */
@@ -438,19 +410,16 @@ public class InvocationDirector
/** Used to generate monotonically increasing receiver ids. */ /** Used to generate monotonically increasing receiver ids. */
protected short _receiverId; protected short _receiverId;
/** 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 HashIntMap<ListenerMarshaller> _listeners = protected HashIntMap<ListenerMarshaller> _listeners = new HashIntMap<ListenerMarshaller>();
new HashIntMap<ListenerMarshaller>();
/** Used to keep track of invocation notification receivers. */ /** Used to keep track of invocation notification receivers. */
protected HashIntMap<InvocationDecoder> _receivers = protected HashIntMap<InvocationDecoder> _receivers = new HashIntMap<InvocationDecoder>();
new HashIntMap<InvocationDecoder>();
/** 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. */
protected ArrayList<InvocationDecoder> _reclist = protected ArrayList<InvocationDecoder> _reclist = new ArrayList<InvocationDecoder>();
new ArrayList<InvocationDecoder>();
/** The last time we flushed our listeners. */ /** The last time we flushed our listeners. */
protected long _lastFlushTime; protected long _lastFlushTime;