def callRemote(self, command, **kw): """ Asyncronously call a remote AMP command. """ # compose packet dataList = [ampy.COMMAND, command.commandName] retVal = None # remember that we sent this command if a response is expected if command.requiresAnswer: # XXX TODO a race condition exists between the two lines below askKey = str(self.counter) self.counter += 1 retVal = defer.Deferred() self.awaitingResponse[askKey] = (command, retVal) dataList.extend( [ampy.ASK, askKey] ) #for kv in COMMAND, command.commandName, ASK, askKey: # dataList.append(struct.pack('!H', len(kv))) # dataList.append(kv) command.serializeRequest(dataList, kw) ampy.insertPrefixes(dataList) data = ''.join(dataList) # write packet self.push(data) return retVal
def _eb_gotResponseError(self, f, command, askKey): key = f.check(*command.errors.keys()) if key: code = command.errors[key] descr = "" # TODO what should go here? else: sys.stderr.write("Unhandled exception raised in AMP Command handler:\n") f.printTraceback() code = ampy.UNKNOWN_ERROR_CODE descr = "Unknown Error" resp = [ampy.ERROR, askKey, ampy.ERROR_CODE, code, ampy.ERROR_DESCRIPTION, descr] self.push(''.join(ampy.insertPrefixes(resp)))
def processFullMessage(self, box): if ampy.COMMAND in box: cmdName = box[ampy.COMMAND] command, handler = self.responders.get(cmdName, (None, None)) askKey = box.get(ampy.ASK, None) if not handler: if askKey: resp = [ampy.ERROR, askKey, ampy.ERROR_CODE, ampy.UNHANDLED_ERROR_CODE, ampy.ERROR_DESCRIPTION, "No handler for command"] self.push(''.join(ampy.insertPrefixes((resp)))) else: kw = command.deserializeRequest(box) if askKey: defer.maybeDeferred(handler, **kw).addCallbacks(self._cb_gotResponse, self._eb_gotResponseError, callbackArgs=(command, askKey), errbackArgs=(command, askKey)) else: handler(**kw) elif ampy.ANSWER in box: answerKey = box[ampy.ANSWER] command, deferred = self.awaitingResponse.pop(answerKey, (None,None) ) if command: kw = command.deserializeResponse(box) deferred.callback(kw) else: sys.stderr.write("Got answer key %s, but we weren't waiting for it! weird!\n" % (answerKey,)) elif ampy.ERROR in box: errorKey = box[ampy.ERROR] command, deferred = self.awaitingResponse.pop(errorKey, (None,None) ) if command: e = ampy.AMPError(box[ampy.ERROR_CODE], box[ampy.ERROR_DESCRIPTION]) deferred.errback(e) else: sys.stderr.write("Got error key %s, but we weren't waiting for it! weird!\n" % (errorKey,)) else: sys.stderr.write("Insane AMP packet!\n")
def _cb_gotResponse(self, kw, command, askKey): dataList = [ampy.ANSWER, askKey] command.serializeResponse(dataList, kw) self.push(''.join(ampy.insertPrefixes(dataList)))