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 test_invalid(loc, mock): pl = Payload("loc", '{"garbage": "garbage"}', 12345) put_in_queue(pl, loc.inbox) recv = [] t0 = time.time() while t0 - time.time() < 5: pl = get_from_queue(loc.outbox) if pl is not None: recv.append(pl) if "garbage" in pl.msg: break assert "error" in pl.msg
def test_set_response(loc, mock): "coil_0_response" payload = Payload("loc", '{"get": "coil_0_amplitude"}', 12345) put_in_queue(payload, loc.inbox) recv = [] t0 = time.time() while t0 - time.time() < 5: pl = get_from_queue(loc.outbox) if pl is not None: recv.append(pl) if "coil_0_amplitude" in pl.msg: break assert "coil_0_amplitude" in pl.msg
def test_trigger(loc, mock): pl = Payload("loc", '{"single_pulse":"COIL_0"}', 12345) put_in_queue(pl, loc.inbox) recv = [] t0 = time.time() while t0 - time.time() < 5: pl = get_from_queue(loc.outbox) if pl is not None: recv.append(pl) if "coil_0_didt" in pl.msg: break assert "coil_0_didt" in pl.msg
def run(self): self.is_running.set() print("CTRL started") while self.is_running.is_set(): payload = get_from_queue(self.queue) if payload is None: sleep(0.001) continue print(f"CTRL:RECV {payload}") if payload.fmt == "cmd": if has_poison(payload): put_in_queue(payload, self.loc) put_in_queue(payload, self.mrk) self.is_running.clear() break elif has_ping(payload): continue else: print("CTRL:CMD {0} unknown".format(payload.msg)) elif payload.fmt == "loc": put_in_queue(payload, self.loc) elif payload.fmt == "mrk": put_in_queue(payload, self.mrk) else: print("CTRL:FMT {0} fmt".format(payload.fmt)) print("Shutting CTRL down")
def loc(mock): inbox = Queue() outbox = Queue() loc = LOC(address=(host, port), inbox=inbox, outbox=outbox) loc.start() loc.await_running() yield loc # shut down in less than 7s pl = Payload("cmd", "poison-pill", 12345) put_in_queue(pl, loc.inbox) t0 = time.time() d = 0 while loc.is_running.is_set() and d < 7: d = time.time() - t0 assert not loc.is_running.is_set()
def loc(): inbox = Queue() outbox = Queue() loc = LOC(address=(os.environ["LOCALITE_HOST"], 6666), inbox=inbox, outbox=outbox) loc.start() loc.await_running() yield loc pl = Payload("cmd", "poison-pill", 12345) put_in_queue(pl, loc.inbox) t0 = time.time() d = 0 while loc.is_running.is_set() and d < 7: d = time.time() - t0 assert not loc.is_running.is_set()
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 test_get(loc, mock): msg = json.dumps({ "coil_0_response": { "mepmaxtime": 18, "mepamplitude": 50, "mepmin": -25, "mepmax": 25, } }) payload = Payload("loc", msg, 12345) put_in_queue(payload, loc.inbox) recv = [] t0 = time.time() while t0 - time.time() < 5: pl = get_from_queue(loc.outbox) if pl is not None: recv.append(pl) if "coil_0_response" in pl.msg: break assert pl.fmt == "mrk" assert "coil_0_response" in pl.msg
def test_setting_amplitude(loc): payload = Payload("loc", '{"coil_0_amplitude": 20}', 1) put_in_queue(payload, loc.inbox) time.sleep(0.1) payload = Payload("loc", '{"coil_0_amplitude": 0}', 1) put_in_queue(payload, loc.inbox) time.sleep(0.1) payload = Payload("loc", '{"single_pulse": "coil_0"}', 1) put_in_queue(payload, loc.inbox)
def run(self): listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind((self.host, self.port)) listener.listen(1) # one unaccepted client is allowed self.is_running.set() print(f"EXT {self.host}:{self.port} started") while self.is_running.is_set(): try: client, address = listener.accept() payload = read_msg(client) print(f"EXT:RECV {payload}") if not payload: continue put_in_queue(payload, self.queue) if has_poison(payload): self.is_running.clear() break except Exception as e: # pragma no cover print(e) finally: client.shutdown(socket.SHUT_RDWR) client.close() print("Shutting EXT down")