def _on_request_service_received(self, newpacket, info): from twisted.internet.defer import Deferred from logs import lg from lib import serialization from services import driver from p2p import p2p_service from transport import packet_out if len(newpacket.Payload) > 1024 * 10: lg.warn('too long payload') p2p_service.SendFail(newpacket, 'too long payload') return False try: json_payload = serialization.BytesToDict(newpacket.Payload, keys_to_text=True, values_to_text=True) json_payload['name'] json_payload['payload'] except: lg.warn('json payload invalid') p2p_service.SendFail(newpacket, 'json payload invalid') return False service_name = str(json_payload['name']) lg.out( self.debug_level, "service_p2p_hookups.RequestService {%s} from %s" % ( service_name, newpacket.OwnerID, )) if not driver.is_exist(service_name): lg.warn("got wrong payload in %s" % service_name) p2p_service.SendFail(newpacket, 'service %s not exist' % service_name) return False if not driver.is_on(service_name): p2p_service.SendFail(newpacket, 'service %s is off' % service_name) return False try: result = driver.request(service_name, json_payload['payload'], newpacket, info) except: lg.exc() p2p_service.SendFail(newpacket, 'request processing failed with exception') return False if not result: lg.out( self.debug_level, "service_p2p_hookups._send_request_service SKIP request %s" % service_name) return False if isinstance(result, Deferred): lg.out( self.debug_level, "service_p2p_hookups._send_request_service fired delayed execution" ) elif isinstance(result, packet_out.PacketOut): lg.out( self.debug_level, "service_p2p_hookups._send_request_service outbox packet sent") return True
def CancelService(request, info): if _Debug: lg.out(_DebugLevel, "p2p_service.CancelService") # TODO: move code into driver module, use callback module here instead of direct call words = request.Payload.split(' ') if len(words) < 1: lg.warn("got wrong payload in %s" % request) return SendFail(request, 'wrong payload') service_name = words[0] # TODO: add validation if not driver.is_exist(service_name): lg.warn("got wrong payload in %s" % request) return SendFail(request, 'service %s not exist' % service_name) if not driver.is_on(service_name): return SendFail(request, 'service %s is off' % service_name) return driver.cancel(service_name, request, info)
def RequestService(request, info): if len(request.Payload) > 1024 * 10: return SendFail(request, "too long payload") words = request.Payload.split(" ") if len(words) < 1: lg.warn("got wrong payload in %s" % request) return SendFail(request, "wrong payload") service_name = words[0] if _Debug: lg.out(_DebugLevel, "p2p_service.RequestService %s : %s" % (request.OwnerID, service_name)) if not driver.is_exist(service_name): lg.warn("got wrong payload in %s" % service_name) return SendFail(request, "service %s not exist" % service_name) if not driver.is_started(service_name): return SendFail(request, "service %s is off" % service_name) return driver.request(service_name, request, info)
def CancelService(request, info): if _Debug: lg.out(_DebugLevel, "p2p_service.CancelService") words = request.Payload.split(" ") if len(words) < 1: lg.warn("got wrong payload in %s" % request) return SendFail(request, "wrong payload") service_name = words[0] # TODO: - temporary keep that for backward compatibility if service_name == "storage": if not driver.is_started("service_supplier"): return SendFail(request, "supplier service is off") return driver.cancel("service_supplier", request, info) if not driver.is_exist(service_name): lg.warn("got wrong payload in %s" % request) return SendFail(request, "service %s not exist" % service_name) if not driver.is_started(service_name): return SendFail(request, "service %s is off" % service_name) return driver.cancel(service_name, request, info)
def RequestService(request, info): """ """ if len(request.Payload) > 1024 * 10: return SendFail(request, 'too long payload') # TODO: move code into driver module, use callback module here instead of direct call words = request.Payload.split(' ') if len(words) < 1: lg.warn("got wrong payload in %s" % request) return SendFail(request, 'wrong payload') service_name = words[0] if _Debug: lg.out( _DebugLevel, "p2p_service.RequestService %s : %s" % (request.OwnerID, service_name)) if not driver.is_exist(service_name): lg.warn("got wrong payload in %s" % service_name) return SendFail(request, 'service %s not exist' % service_name) if not driver.is_on(service_name): return SendFail(request, 'service %s is off' % service_name) return driver.request(service_name, request, info)