def listen_and_queue(client: localiteClient, ignore: List[Dict[str, str]], queue: Queue) -> Union[Dict[str, Any], None]: """listen to the localice stream and forward to queue """ msg = client.listen() if msg is None or json.loads(msg) in ignore: return None else: print("LOC:MSG", msg) pl = Payload("mrk", msg, local_clock()) put_in_queue(pl, queue) return json.loads(msg)
def run(self): # initialize clients and message expectations client = localiteClient(host=self.host, port=self.port) lastmessage = LastMessage() response = None self.is_running.set() print(f"LOC {self.host}:{self.port} started") while self.is_running.is_set(): try: payload = get_from_queue(self.inbox) if payload is None: if "status" in lastmessage.msg: response = listen_and_queue(client, ignore=[], queue=self.outbox) else: response = listen_and_queue(client, ignore=self.ignore, queue=self.outbox) # sometimes, the get: "target_index" is ignored. # in these cases, resend if "target_index" in lastmessage.msg: flevel = lastmessage.expects(response) if flevel >= 2: print("LOC:RESEND", lastmessage.msg) client.send(lastmessage.msg) lastmessage.counter = 0 else: print("LOC:RECV", payload) if payload.fmt == "cmd": if payload.msg == "poison-pill": self.is_running.clear() break else: # pragma no cover print("LOC:INVALID", payload) elif payload.fmt == "loc": client.send(payload.msg) lastmessage.update(payload) except Exception as e: # pragma no cover if self.is_running.set(): print("LOC:EXC", e) pl = Payload("mrk", "LOC:EXC " + str(e), local_clock()) put_in_queue(pl, self.outbox) self.is_running.clear() print("Shutting LOC down")
def kill(port: int = 6667, host: str = "127.0.0.1") -> bool: """kill the markerserver is already at that port args ---- host: str the ip of the markerserver (defaults to localhost) port: int the port number of the markerserver (defaults to 6667) returns ------- status: bool True if message was sent, False if server was not available """ return push_payload(Payload("cmd", "poison-pill", local_clock()))
def available(port: int = 6667, host: str = "127.0.0.1") -> bool: """test whether EXT is available at port args ---- host: str the ip of the EXT (defaults to localhost) port: int the port number of the EXT (defaults to 6667) returns ------- status: bool True if available, False if not """ return push_payload(Payload("cmd", "ping", local_clock()))
def push( fmt: str, msg: str, tstamp: int = None, host="127.0.0.1", port: int = 6667, verbose=True, ): "a functional interface to pushing a message" tstamp = tstamp or local_clock() payload = Payload(fmt, msg, tstamp) try: Client(host=host, port=port, verbose=verbose).push(payload) return True except (ConnectionRefusedError, ConnectionResetError) as e: if verbose and msg != "ping": # pragma no cover print(e) print(f"Localite EXT at {host}:{port} is not available") return False
def run(self): outlet, info = make_outlet() # print(info.as_xml()) print(f"MRK {info.name()} started") self.is_running.set() while self.is_running.is_set(): payload = get_from_queue(self.queue) if payload is None: time.sleep(0.001) continue if payload.fmt == "cmd" and payload.msg == "poison-pill": self.is_running.clear() outlet.push_sample([payload.msg]) break outlet.push_sample([str(payload.msg)], payload.tstamp) latency = 1000 * (payload.tstamp - local_clock()) print( f"MRK:PUSH {payload.msg} from {payload.tstamp:.5f} delayed by {latency:1.2f}ms" ) print("Shutting MRK down")
def __init__(self, fmt: str = "", msg: str = "", tstamp: TimeStamp = None): self.fmt = fmt self.msg = msg self.tstamp = tstamp or local_clock()