Improved link request/response handling.

This commit is contained in:
Mark Qvist 2021-09-03 16:23:31 +02:00
parent ecf0c55fd6
commit 58d48c18f4
2 changed files with 41 additions and 41 deletions

View File

@ -245,7 +245,7 @@ class Link:
RNS.log("Link "+str(self)+" established with "+str(self.destination)+", RTT is "+str(self.rtt), RNS.LOG_VERBOSE) RNS.log("Link "+str(self)+" established with "+str(self.destination)+", RTT is "+str(self.rtt), RNS.LOG_VERBOSE)
rtt_data = umsgpack.packb(self.rtt) rtt_data = umsgpack.packb(self.rtt)
rtt_packet = RNS.Packet(self, rtt_data, context=RNS.Packet.LRRTT) rtt_packet = RNS.Packet(self, rtt_data, context=RNS.Packet.LRRTT)
rtt_packet.send() #rtt_packet.send()
self.had_outbound() self.had_outbound()
self.status = Link.ACTIVE self.status = Link.ACTIVE
@ -491,53 +491,55 @@ class Link:
self.had_outbound() self.had_outbound()
def handle_request(self, request_id, unpacked_request): def handle_request(self, request_id, unpacked_request):
requested_at = unpacked_request[0] if self.status == Link.ACTIVE:
path_hash = unpacked_request[1] requested_at = unpacked_request[0]
request_data = unpacked_request[2] path_hash = unpacked_request[1]
request_data = unpacked_request[2]
if path_hash in self.destination.request_handlers: if path_hash in self.destination.request_handlers:
request_handler = self.destination.request_handlers[path_hash] request_handler = self.destination.request_handlers[path_hash]
path = request_handler[0] path = request_handler[0]
response_generator = request_handler[1] response_generator = request_handler[1]
allow = request_handler[2] allow = request_handler[2]
allowed_list = request_handler[3] allowed_list = request_handler[3]
allowed = False allowed = False
if not allow == RNS.Destination.ALLOW_NONE: if not allow == RNS.Destination.ALLOW_NONE:
if allow == RNS.Destination.ALLOW_LIST: if allow == RNS.Destination.ALLOW_LIST:
if self.__remote_identity.hash in allowed_list: if self.__remote_identity.hash in allowed_list:
allowed = True
elif allow == RNS.Destination.ALLOW_ALL:
allowed = True allowed = True
elif allow == RNS.Destination.ALLOW_ALL:
allowed = True
if allowed: if allowed:
RNS.log("Handling request "+RNS.prettyhexrep(request_id)+" for: "+str(path), RNS.LOG_DEBUG) RNS.log("Handling request "+RNS.prettyhexrep(request_id)+" for: "+str(path), RNS.LOG_DEBUG)
response = response_generator(path, request_data, request_id, self.__remote_identity, requested_at) response = response_generator(path, request_data, request_id, self.__remote_identity, requested_at)
if response != None: if response != None:
packed_response = umsgpack.packb([request_id, response]) packed_response = umsgpack.packb([request_id, response])
if len(packed_response) <= Link.MDU: if len(packed_response) <= Link.MDU:
RNS.Packet(self, packed_response, RNS.Packet.DATA, context = RNS.Packet.RESPONSE).send() RNS.Packet(self, packed_response, RNS.Packet.DATA, context = RNS.Packet.RESPONSE).send()
else: else:
response_resource = RNS.Resource(packed_response, self, request_id = request_id, is_response = True) response_resource = RNS.Resource(packed_response, self, request_id = request_id, is_response = True)
else: else:
identity_string = RNS.prettyhexrep(self.get_remote_identity()) if self.get_remote_identity() != None else "<Unknown>" identity_string = RNS.prettyhexrep(self.get_remote_identity()) if self.get_remote_identity() != None else "<Unknown>"
RNS.log("Request "+RNS.prettyhexrep(request_id)+" from "+identity_string+" not allowed for: "+str(path), RNS.LOG_DEBUG) RNS.log("Request "+RNS.prettyhexrep(request_id)+" from "+identity_string+" not allowed for: "+str(path), RNS.LOG_DEBUG)
def handle_response(self, request_id, response_data): def handle_response(self, request_id, response_data):
remove = None if self.status == Link.ACTIVE:
for pending_request in self.pending_requests: remove = None
if pending_request.request_id == request_id: for pending_request in self.pending_requests:
remove = pending_request if pending_request.request_id == request_id:
try: remove = pending_request
pending_request.response_received(response_data) try:
except Exception as e: pending_request.response_received(response_data)
RNS.log("Error occurred while handling response. The contained exception was: "+str(e), RNS.LOG_ERROR) except Exception as e:
RNS.log("Error occurred while handling response. The contained exception was: "+str(e), RNS.LOG_ERROR)
break break
if remove != None: if remove != None:
self.pending_requests.remove(remove) self.pending_requests.remove(remove)
def request_resource_concluded(self, resource): def request_resource_concluded(self, resource):
if resource.status == RNS.Resource.COMPLETE: if resource.status == RNS.Resource.COMPLETE:

View File

@ -46,8 +46,6 @@ class Resource:
# bz2 before sending. # bz2 before sending.
AUTO_COMPRESS_MAX_SIZE = MAX_EFFICIENT_SIZE AUTO_COMPRESS_MAX_SIZE = MAX_EFFICIENT_SIZE
# TODO: Should be allocated more
# intelligently
PART_TIMEOUT_FACTOR = 3 PART_TIMEOUT_FACTOR = 3
MAX_RETRIES = 5 MAX_RETRIES = 5
SENDER_GRACE_TIME = 10 SENDER_GRACE_TIME = 10