def receive(self, req: Request): if req.get_receiver() == self._network.get_hostname(): # Normal Request self._logger.info(f"Received Request at '{req.get_path()}'") elif req.get_receiver() is None: # Broadcast self._logger.info(f"Received Broadcast at '{req.get_path()}'") else: return req.respond(req.get_payload())
class RestServerRequestManager: _request: Request _response: Optional[Request] def __init__(self, hostname: str, path: str, payload: dict, auth: Optional[AuthContainer]): self._response = None self._create_incoming_request(hostname, path, payload, auth) def __del__(self): pass def _create_incoming_request(self, hostname: str, path: str, payload: dict, auth: Optional[AuthContainer]): session_id = random.randint(0, 30000) sender = f"rest_client_{session_id}" self._request = Request(path=path, session_id=session_id, sender=sender, receiver=hostname, payload=payload) if auth is not None: self._request.set_auth(auth) self._request.set_callback_method(self._get_response_function()) def _get_response_function(self) -> response_callback_type: def respond(req: Request, payload: dict, path: Optional[str]): if req.get_session_id() != self._request.get_session_id(): raise IllegalResponseException( f"Session IDs {req.get_session_id()} and " f"{self._request.get_session_id()} are not matching") if path is not None: res_path = path else: res_path = req.get_path() self._response = Request(path=res_path, session_id=req.get_session_id(), sender=self._request.get_receiver(), receiver=req.get_sender(), payload=payload) return respond def get_request(self) -> Request: return self._request def await_response(self, timeout: int = 2) -> Request: start_time = datetime.now() while self._response is None: time.sleep(0.1) now = datetime.now() if now > (start_time + timedelta(seconds=timeout)): break if self._response is None: raise NoResponseReceivedError(self._request.get_path()) return self._response
def mock_response_function(req: Request, payload: dict, path: str): if path is None: buf_path = req.get_path() else: buf_path = path out_req = Request(path=buf_path, session_id=req.get_session_id(), sender=req.get_receiver(), receiver=req.get_sender(), payload=payload) self._last_response = out_req
def send_request_split(self, path: str, receiver: str, payload: dict, part_max_size: int = 30, timeout: Optional[int] = None) -> Optional[Request]: """ Sends a request to all attached networks. The request will be split into individual parts with a maximum length. :param path: Path to send the request on :param receiver: Receiver for the Request :param payload: Payload to be send :param part_max_size: Maximum size in bytes for each individual payload chunk :param timeout: Maximum timeout to wait for an answer :return: The response if there is any """ if timeout is None: timeout = self._default_timeout req = Request(path, None, self._hostname, receiver, payload) session_id = req.get_session_id() path = req.get_path() sender = req.get_sender() receiver = req.get_receiver() payload_str = json.dumps(req.get_payload()) # Make string ready to be contained in json itself payload_str = payload_str.replace('"', "$*$") payload_len = len(payload_str) parts = [] start = 0 package_index = 0 while start < payload_len: end = start + part_max_size payload_part = payload_str[start:( end if end < payload_len else payload_len)] parts.append(payload_part) start = end last_index = len(parts) for payload_part in parts: out_dict = { "package_index": package_index, "split_payload": payload_part } if package_index == 0: out_dict["last_index"] = last_index out_req = Request(path, session_id, sender, receiver, out_dict) if package_index == last_index - 1: responses = self._send_request_obj(out_req, timeout, 1) if not responses: return None return responses[0] else: self._send_request_obj(out_req, 0, 0) package_index += 1 sleep(0.1) return None