def sendResponse(self, data, address): packet_data = None try: max_size = iohub.net.MAX_PACKET_SIZE / 2 - 20 packet_data = self.pack(data) + '\r\n' packet_data_length = len(packet_data) if packet_data_length >= max_size: num_packets = len(packet_data) / max_size + 1 self.sendResponse(('IOHUB_MULTIPACKET_RESPONSE', num_packets), address) for p in xrange(num_packets - 1): self.socket.sendto( packet_data[p * max_size:(p + 1) * max_size], address) self.socket.sendto( packet_data[(p + 1) * max_size:packet_data_length], address) else: self.socket.sendto(packet_data, address) except: print2err('Error trying to send data to experiment process:') print2err('data length:', len(data)) print2err("=============================") printExceptionDetailsToStdErr() print2err("=============================") first_data_element = "NO_DATA_AVAILABLE" if data: print2err('Data was [{0}]'.format(data)) try: first_data_element = data[0] except: pass packet_data_length = 0 if packet_data: packet_data_length = len(packet_data) print2err('packet Data length: ', len(packet_data)) data = createErrorResult( 'IOHUB_SERVER_RESPONSE_ERROR', msg="The ioHub Server Failed to send the intended response.", first_data_element=str(first_data_element), packet_data_length=packet_data_length, max_packet_size=max_size) packet_data = self.pack(data) + '\r\n' packet_data_length = len(packet_data) self.socket.sendto(packet_data, address)
def handleGetEvents(self,replyTo): try: currentEvents=list(self.iohub.eventBuffer) self.iohub.eventBuffer.clear() if len(currentEvents)>0: sorted(currentEvents, key=itemgetter(DeviceEvent.EVENT_HUB_TIME_INDEX)) self.sendResponse(('GET_EVENTS_RESULT',currentEvents),replyTo) else: self.sendResponse(('GET_EVENTS_RESULT', None),replyTo) return True except Exception,e: self.sendResponse(createErrorResult('IOHUB_GET_EVENTS_ERROR', msg="An error occurred while events were being retrived from the ioHub Server", exception=str(e)), replyTo) return False
def sendResponse(self,data,address): packet_data=None try: max_size=iohub.net.MAX_PACKET_SIZE/2-20 packet_data=self.pack(data)+'\r\n' packet_data_length=len(packet_data) if packet_data_length>= max_size: num_packets=len(packet_data)/max_size+1 self.sendResponse(('IOHUB_MULTIPACKET_RESPONSE',num_packets),address) for p in xrange(num_packets-1): self.socket.sendto(packet_data[p*max_size:(p+1)*max_size],address) self.socket.sendto(packet_data[(p+1)*max_size:packet_data_length],address) else: self.socket.sendto(packet_data,address) except: print2err('Error trying to send data to experiment process:') print2err('data length:',len(data)) print2err("=============================") printExceptionDetailsToStdErr() print2err("=============================") first_data_element="NO_DATA_AVAILABLE" if data: print2err('Data was [{0}]'.format(data)) try: first_data_element=data[0] except: pass packet_data_length=0 if packet_data: packet_data_length=len(packet_data) print2err('packet Data length: ',len(packet_data)) data=createErrorResult('IOHUB_SERVER_RESPONSE_ERROR', msg="The ioHub Server Failed to send the intended response.", first_data_element=str(first_data_element), packet_data_length=packet_data_length, max_packet_size=max_size) packet_data=self.pack(data)+'\r\n' packet_data_length=len(packet_data) self.socket.sendto(packet_data,address)
def handleGetEvents(self, replyTo): try: currentEvents = list(self.iohub.eventBuffer) self.iohub.eventBuffer.clear() if len(currentEvents) > 0: sorted(currentEvents, key=itemgetter(DeviceEvent.EVENT_HUB_TIME_INDEX)) self.sendResponse(('GET_EVENTS_RESULT', currentEvents), replyTo) else: self.sendResponse(('GET_EVENTS_RESULT', None), replyTo) return True except Exception, e: self.sendResponse( createErrorResult( 'IOHUB_GET_EVENTS_ERROR', msg= "An error occurred while events were being retrived from the ioHub Server", exception=str(e)), replyTo) return False
def handle(self, request, replyTo): if self._running is False: return False self.feed(request[:-2]) request = self.unpack() request_type = request.pop(0) if request_type == 'GET_EVENTS': return self.handleGetEvents(replyTo) elif request_type == 'EXP_DEVICE': return self.handleExperimentDeviceRequest(request, replyTo) elif request_type == 'RPC': callable_name = request.pop(0) args = None kwargs = None if len(request) == 1: args = request.pop(0) if len(request) == 1: kwargs = request.pop(0) result = None try: result = getattr(self, callable_name) except: self.sendResponse( createErrorResult( 'RPC_ATTRIBUTE_ERROR', msg= "The method name referenced could not be found by the RPC server.", method_name=callable_name), replyTo) return False if result and callable(result): funcPtr = result try: if args is None and kwargs is None: result = funcPtr() elif args and kwargs: result = funcPtr(*args, **kwargs) elif args and not kwargs: result = funcPtr(*args) elif not args and kwargs: result = funcPtr(**kwargs) edata = ('RPC_RESULT', callable_name, result) self.sendResponse(edata, replyTo) return True except Exception, e: self.sendResponse( createErrorResult( 'RPC_RUNTIME_ERROR', msg= "An error occurred on the ioHub Server while evaulating an RPC request", method_name=callable_name, args=args, kwargs=kwargs, exception=str(e)), replyTo) return False else: self.sendResponse( createErrorResult( 'RPC_NOT_CALLABLE_ERROR', msg= "The method name give is not callable (it is not a method).", method_name=callable_name, resolved_result=str(result)), replyTo) return False
def handleExperimentDeviceRequest(self, request, replyTo): request_type = request.pop(0) if request_type == 'EVENT_TX': exp_events = request.pop(0) for eventAsTuple in exp_events: ioServer.deviceDict['Experiment']._nativeEventCallback( eventAsTuple) self.sendResponse(('EVENT_TX_RESULT', len(exp_events)), replyTo) return True elif request_type == 'DEV_RPC': dclass = request.pop(0) dmethod = request.pop(0) args = None kwargs = None if len(request) == 1: args = request[0] elif len(request) == 2: args = request[0] kwargs = request[1] if len(kwargs) == 0: kwargs = None dev = None if dclass.find('.') > 0: for dname, dev in ioServer.deviceDict.iteritems(): if dname.endswith(dclass): dev = ioServer.deviceDict.get(dname, None) break else: dev = ioServer.deviceDict.get(dclass, None) if dev is None: self.sendResponse( createErrorResult( 'IOHUB_DEVICE_ERROR', msg= "An instance of the ioHub Device class provided is not enabled on the ioHub Server", device_class=dclass), replyTo) return False try: method = getattr(dev, dmethod) except: self.sendResponse( createErrorResult( 'IOHUB_DEVICE_METHOD_ERROR', msg="Device class {0} does not have a method called {1}" .format(dclass, dmethod)), replyTo) return False result = [] try: if args and kwargs: result = method(*args, **kwargs) elif args: result = method(*args) elif kwargs: result = method(**kwargs) else: result = method() self.sendResponse(('DEV_RPC_RESULT', result), replyTo) return True except Exception, e: self.sendResponse( createErrorResult( 'RPC_DEVICE_RUNTIME_ERROR', msg= "An error occurred on the ioHub Server while evaulating an Device RPC request", device=dclass, dmethod=dmethod, args=args, kwargs=kwargs, exception=str(e)), replyTo) return False
'RPC_NOT_CALLABLE_ERROR', msg= "The method name give is not callable (it is not a method).", method_name=callable_name, resolved_result=str(result)), replyTo) return False elif request_type == 'STOP_IOHUB_SERVER': try: self.shutDown() except: printExceptionDetailsToStdErr else: self.sendResponse( createErrorResult( 'RPC_TYPE_NOT_SUPPORTED_ERROR', msg= "The request type provided is not recognized by the ioHub Server.", request_type=request_type), replyTo) return False def handleGetEvents(self, replyTo): try: currentEvents = list(self.iohub.eventBuffer) self.iohub.eventBuffer.clear() if len(currentEvents) > 0: sorted(currentEvents, key=itemgetter(DeviceEvent.EVENT_HUB_TIME_INDEX)) self.sendResponse(('GET_EVENTS_RESULT', currentEvents), replyTo) else:
def handle(self, request, replyTo): if self._running is False: return False self.feed(request[:-2]) request = self.unpack() request_type= request.pop(0) if request_type == 'GET_EVENTS': return self.handleGetEvents(replyTo) elif request_type == 'EXP_DEVICE': return self.handleExperimentDeviceRequest(request,replyTo) elif request_type == 'RPC': callable_name=request.pop(0) args=None kwargs=None if len(request)==1: args=request.pop(0) if len(request)==1: kwargs=request.pop(0) result=None try: result=getattr(self,callable_name) except: self.sendResponse(createErrorResult('RPC_ATTRIBUTE_ERROR', msg="The method name referenced could not be found by the RPC server.", method_name=callable_name), replyTo) return False if result and callable(result): funcPtr=result try: if args is None and kwargs is None: result = funcPtr() elif args and kwargs: result = funcPtr(*args,**kwargs) elif args and not kwargs: result = funcPtr(*args) elif not args and kwargs: result = funcPtr(**kwargs) edata=('RPC_RESULT',callable_name,result) self.sendResponse(edata,replyTo) return True except Exception,e: self.sendResponse(createErrorResult('RPC_RUNTIME_ERROR', msg="An error occurred on the ioHub Server while evaulating an RPC request", method_name=callable_name, args=args, kwargs=kwargs, exception=str(e)) ,replyTo) return False else: self.sendResponse(createErrorResult('RPC_NOT_CALLABLE_ERROR', msg="The method name give is not callable (it is not a method).", method_name=callable_name, resolved_result=str(result)) ,replyTo) return False
def handleExperimentDeviceRequest(self,request,replyTo): request_type= request.pop(0) if request_type == 'EVENT_TX': exp_events=request.pop(0) for eventAsTuple in exp_events: ioServer.deviceDict['Experiment']._nativeEventCallback(eventAsTuple) self.sendResponse(('EVENT_TX_RESULT',len(exp_events)),replyTo) return True elif request_type == 'DEV_RPC': dclass=request.pop(0) dmethod=request.pop(0) args=None kwargs=None if len(request)==1: args=request[0] elif len(request)==2: args=request[0] kwargs=request[1] if len(kwargs)==0: kwargs=None dev=None if dclass.find('.') > 0: for dname, dev in ioServer.deviceDict.iteritems(): if dname.endswith(dclass): dev=ioServer.deviceDict.get(dname,None) break else: dev=ioServer.deviceDict.get(dclass,None) if dev is None: self.sendResponse(createErrorResult('IOHUB_DEVICE_ERROR', msg="An instance of the ioHub Device class provided is not enabled on the ioHub Server", device_class=dclass), replyTo) return False try: method=getattr(dev,dmethod) except: self.sendResponse(createErrorResult('IOHUB_DEVICE_METHOD_ERROR', msg="Device class {0} does not have a method called {1}".format(dclass,dmethod)), replyTo) return False result=[] try: if args and kwargs: result=method(*args, **kwargs) elif args: result=method(*args) elif kwargs: result=method(**kwargs) else: result=method() self.sendResponse(('DEV_RPC_RESULT',result),replyTo) return True except Exception, e: self.sendResponse(createErrorResult('RPC_DEVICE_RUNTIME_ERROR', msg="An error occurred on the ioHub Server while evaulating an Device RPC request", device=dclass, dmethod=dmethod, args=args, kwargs=kwargs, exception=str(e)) ,replyTo) return False
return False else: self.sendResponse(createErrorResult('RPC_NOT_CALLABLE_ERROR', msg="The method name give is not callable (it is not a method).", method_name=callable_name, resolved_result=str(result)) ,replyTo) return False elif request_type == 'STOP_IOHUB_SERVER': try: self.shutDown() except: printExceptionDetailsToStdErr else: self.sendResponse(createErrorResult('RPC_TYPE_NOT_SUPPORTED_ERROR', msg="The request type provided is not recognized by the ioHub Server.", request_type=request_type), replyTo) return False def handleGetEvents(self,replyTo): try: currentEvents=list(self.iohub.eventBuffer) self.iohub.eventBuffer.clear() if len(currentEvents)>0: sorted(currentEvents, key=itemgetter(DeviceEvent.EVENT_HUB_TIME_INDEX)) self.sendResponse(('GET_EVENTS_RESULT',currentEvents),replyTo) else: self.sendResponse(('GET_EVENTS_RESULT', None),replyTo) return True