def HandlePacket(self, oPacketI): try: # Handle Function Request packets if oPacketI.Type != 'Request': raise InvalidOperation("It is illegal to send a worker a packet of type '%s'." % oPacketI.Type) # Sanity check if oPacketI.Target != self.App.Identifier: raise InvalidOperation("A packet with Target='%s' may not be sent to a worker with App.Identifier='%s'." % (oPacketI.Target, self.App.Identifier)) try: self.App.LoadSecurityContext(oPacketI.SecurityContext, oPacketI.ADDR) # Call the API DATA = self.App.CallAPI(oPacketI.Method, oPacketI.Data, Target='<SELF>') finally: self.App.FreeSecurityContext() # Create the response packet (clone core fields off of the incoming packet) oPacketO = ResponsePacket() oPacketO.DataType = oPacketI.DataType oPacketO.Data = DATA # Send the response self.SendPacket(oPacketO) except Exception, e: # Print exception print; traceback.print_exc(); print # Format message: if DE:BUG(3, "Worker %s received %s: \n%s" % (self, type(e).__name__, e.message)) # Create an exception response packet oPacketO = ExceptionPacket() if hasattr(e, 'Data'): oPacketO.Data = e.Data oPacketO.ExceptionType = type(e).__name__ oPacketO.Message = e.message self.SendPacket(oPacketO)
def HandleServerRequest(self, oPacketI): """ This function is called to handle a Server packet. A Server Packet is one with a Target == <SERVER> """ # ---------------------------------------------------------------------- if oPacketI.Method == "Session.Hit": if not oPacketI.AuthToken: raise AuthError("AuthToken not provided.") # This will raise an AuthError if there is a problem. oAuthStruct = AuthManager.Hit(oPacketI.AuthToken) # Send it back to the worker who requested it oPacketO = ResponsePacket() oPacketO.Data = { "AuthToken": oAuthStruct.AuthToken, "DSEG": oAuthStruct.DSEG, "Duration": oAuthStruct.Duration, "Expiration": oAuthStruct.Expiration, "RequestCount": oAuthStruct.RequestCount, "Disabled": oAuthStruct.Disabled, } self.Stack[-1].SendPacket(oPacketO) # ---------------------------------------------------------------------- elif oPacketI.Method == "Session.Set": # Get an auth token from the auth manager data = oPacketI.Data oAuthStruct = AuthManager.Set(oPacketI.DSEG, data["SecurityContext"], data["Duration"]) # Send it back to the worker who requested it oPacketO = ResponsePacket() oPacketO.Data = {"AuthToken": oAuthStruct.AuthToken} self.Stack[-1].SendPacket(oPacketO) # ---------------------------------------------------------------------- elif oPacketI.Method == "Session.End": if not oPacketI.AuthToken: raise AuthError("AuthToken not provided.") # This will raise an AuthError if there is a problem. oAuthStruct = AuthManager.Hit(oPacketI.AuthToken) oAuthStruct.Disable() # Send it back to the worker who requested it oPacketO = ResponsePacket() oPacketO.Data = { "AuthToken": oAuthStruct.AuthToken, "DSEG": oAuthStruct.DSEG, "Duration": oAuthStruct.Duration, "Expiration": oAuthStruct.Expiration, "RequestCount": oAuthStruct.RequestCount, "Disabled": oAuthStruct.Disabled, } self.Stack[-1].SendPacket(oPacketO) # ---------------------------------------------------------------------- else: raise InvalidOperation("Not able to process Server Packet with method '%s'." % oPacketI.Method)