def process(data): """ Decapsulte data from request and pass them to AgentInvoker. Encapsulate the feedback. """ procedure_name, para, uuid = DcaProtocol.decap_input(data) # Decapsulate data into structured data result = AgentInvoker.invoke(procedure_name, para) # pass these paras to AgentInvoker feedback = DcaProtocol.encap_output(result, uuid) # Encapsulte the feedback return feedback
def process(data): """ Decapsulte data from request and pass them to AgentInvoker. Encapsulate the feedback. """ procedure_name, para, uuid = DcaProtocol.decap_input( data) # Decapsulate data into structured data result = AgentInvoker.invoke(procedure_name, para) # pass these paras to AgentInvoker feedback = DcaProtocol.encap_output(result, uuid) # Encapsulte the feedback return feedback
def remote_call(self, procedure_name, para, ip): """ Encapsulte the data and call the Connector to send the request. """ id = uuid.uuid4() # generate a uuid for every request data = DcaProtocol.encap_input(procedure_name, para, id) self.request_dict[id] = False # initialized a dict to indicate if a request is answered self.connector.request(data, ip) return id
def listen(self, request_dict): """ Listen to all sockets created by request in one co-routine. Receives data from them and check if all requests have been answered. """ while True: gevent.sleep(1) # switch to request co-routine # use select to get readable event readable, writable, exceptional = select.select( self.inputs, self.outputs, self.inputs, 0) if not (readable or writable or exceptional): print 'No input event' continue # polling readable event for s in readable: buf = s.recv(9999) data = buf while len(buf): # read until there's no data buf = s.recv(9999) data += buf if data: # print 'recv data:', data, 'from', s.getpeername() self.message_queue[s].put( data) # put data into message_queue self.inputs.remove( s) # remove socket because only wait for one response s.close() else: # no data received print 'close the connection', s.getpeername() if s in self.outputs: self.outputs.remove(s) self.inputs.remove(s) s.close() del self.message_queue[s] # exceptional event for s in exceptional: print "exceptional connection:", s.getpeername() self.inputs.remove(s) if s in self.outputs: self.outputs.remove(s) s.close() del self.message_queue[s] # check if all requests have been answered if DcaProtocol.check_termination(self.message_queue, request_dict): print 'All requests have been answered' return request_dict return
def listen(self, request_dict): """ Listen to all sockets created by request in one co-routine. Receives data from them and check if all requests have been answered. """ while True: gevent.sleep(1) # switch to request co-routine # use select to get readable event readable, writable, exceptional = select.select(self.inputs, self.outputs, self.inputs, 0) if not (readable or writable or exceptional): print 'No input event' continue # polling readable event for s in readable: buf = s.recv(9999) data = buf while len(buf): # read until there's no data buf = s.recv(9999) data += buf if data: # print 'recv data:', data, 'from', s.getpeername() self.message_queue[s].put(data) # put data into message_queue self.inputs.remove(s) # remove socket because only wait for one response s.close() else: # no data received print 'close the connection', s.getpeername() if s in self.outputs: self.outputs.remove(s) self.inputs.remove(s) s.close() del self.message_queue[s] # exceptional event for s in exceptional: print "exceptional connection:", s.getpeername() self.inputs.remove(s) if s in self.outputs: self.outputs.remove(s) s.close() del self.message_queue[s] # check if all requests have been answered if DcaProtocol.check_termination(self.message_queue, request_dict): print 'All requests have been answered' return request_dict return