def lineReceived(self, line, request_counter): if self.expect_tcp_proxy_protocol_header: # This flag may be set only for TCP transport AND when TCP_PROXY_PROTOCOL # is enabled in server config. Then we expect the first line of the stream # may contain proxy metadata. # We don't expect this header during this session anymore self.expect_tcp_proxy_protocol_header = False if line.startswith('PROXY'): self.proxied_ip = line.split()[2] # Let's process next line request_counter.decrease() return try: message = json.loads(line) except: #self.writeGeneralError("Cannot decode message '%s'" % line) request_counter.finish() raise custom_exceptions.ProtocolException("Cannot decode message '%s'" % line.strip()) if self.factory.debug: log.debug("> %s" % message) msg_id = message.get('id', 0) #msg_method = message.get('method', None) #msg_params = message.get('params', None) msg_result = message.get('result', None) msg_error = message.get('error', None) # If there's an error, handle it as errback if msg_error != None: meta['defer'].errback(custom_exceptions.RemoteServiceException(msg_error)) return if not msg_id: # It's a RPC newWork notification try: result = self.event_handler._handle_event("eth_getWork", msg_result, connection_ref=self) except: failure = Failure() self.process_failure(failure, msg_id, request_counter) else: # It's notification, don't expect the response request_counter.decrease() elif msg_id: # It's a RPC response # Perform lookup to the table of waiting requests. request_counter.decrease() try: meta = self.lookup_table[msg_id] if meta['method'] == "eth_submitWork": response_time = (time.time() - meta['start_time']) * 1000 if msg_result == True: log.info("[%dms] %s from '%s' accepted" % (response_time, meta['method'], meta['worker_name'])) else: log.warning("[%dms] %s from '%s' REJECTED" % (response_time, meta['method'], meta['worker_name'])) del self.lookup_table[msg_id] except KeyError: # When deferred object for given message ID isn't found, it's an error raise custom_exceptions.ProtocolException("Lookup for deferred object for message ID '%s' failed." % msg_id) # If both result and error are null, handle it as a success with blank result meta['defer'].callback(msg_result) if not isinstance(msg_result, bool): try: result = self.event_handler._handle_event("eth_getWork", msg_result, connection_ref=self) if result == None: # event handler must return Deferred object or raise an exception for RPC request raise custom_exceptions.MethodNotFoundException("Event handler cannot process '%s'" % msg_result) except: pass
def lineReceived(self, line, request_counter): if self.expect_tcp_proxy_protocol_header: # This flag may be set only for TCP transport AND when TCP_PROXY_PROTOCOL # is enabled in server config. Then we expect the first line of the stream # may contain proxy metadata. # We don't expect this header during this session anymore self.expect_tcp_proxy_protocol_header = False if line.startswith('PROXY'): self.proxied_ip = line.split()[2] # Let's process next line request_counter.decrease() return try: message = json.loads(line) except: #self.writeGeneralError("Cannot decode message '%s'" % line) request_counter.finish() raise custom_exceptions.ProtocolException( "Cannot decode message '%s'" % line.strip()) if self.factory.debug: log.debug("> %s" % message) msg_id = message.get('id', 0) msg_method = message.get('method') msg_params = message.get('params') msg_result = message.get('result') msg_error = message.get('error') if msg_method: # It's a RPC call or notification try: result = self.event_handler._handle_event(msg_method, msg_params, connection_ref=self) if result == None and msg_id != None: # event handler must return Deferred object or raise an exception for RPC request raise custom_exceptions.MethodNotFoundException( "Event handler cannot process method '%s'" % msg_method) except: failure = Failure() self.process_failure(failure, msg_id, msg_method, msg_params, request_counter) else: if msg_id == None: # It's notification, don't expect the response request_counter.decrease() else: # It's a RPC call result.addCallback(self.process_response, msg_id, msg_method, msg_params, request_counter) result.addErrback(self.process_failure, msg_id, msg_method, msg_params, request_counter) elif msg_id: # It's a RPC response # Perform lookup to the table of waiting requests. request_counter.decrease() try: meta = self.lookup_table[msg_id] del self.lookup_table[msg_id] except KeyError: # When deferred object for given message ID isn't found, it's an error raise custom_exceptions.ProtocolException( "Lookup for deferred object for message ID '%s' failed." % msg_id) # If there's an error, handle it as errback # If both result and error are null, handle it as a success with blank result if msg_error != None: meta['defer'].errback( custom_exceptions.RemoteServiceException(msg_error)) else: meta['defer'].callback(msg_result) else: request_counter.decrease() raise custom_exceptions.ProtocolException( "Cannot handle message '%s'" % line)