def eot(self, timeout=0): ph = zm.PrefixHeader(form="FLOW", label=json.dumps(dict(flow="EOT"))) ch = zm.CoordHeader(self.origin, now(), self.count) eot = zm.encode_message([bytes(ph), bytes(ch)]) print ("send_eot", ph) self.port.send(eot, routing_id = self.client_id); while True: which = dict(self.poller.poll(timeout)) if not self.port in which: return False frame = self.port.recv(copy=False) parts = zm.decode_message(frame.bytes); ph = zm.PrefixHeader(parts[0]) print("wait for eot, got",ph) fobj = json.loads(ph.label) if fobj["flow"] == "EOT": print ("got eot") return if timeout == 0: return
def send_dat(self): self.recv_pay(0) if self.credit == 0: self.recv_pay(-1) if self.credit == 0: raise RuntimeError("no credit and failed to get payed") ph = zm.PrefixHeader(form="FLOW", label=json.dumps({'flow':'DAT'})) ch = zm.CoordHeader(self.origin, now(), self.count) self.count += 1 self.credit -= 1 dat = zm.encode_message([bytes(ph), bytes(ch)]) print ("send_dat",ph) self.port.send(day, routing_id = self.client_id)
def send_pay(self): if self.credit == 0: print ("send_pay(broke)") return ph = zm.PrefixHeader(form="FLOW", label=json.dumps(dict(flow="PAY", credit=self.credit))) ch = zm.CoordHeader(self.origin, now(), self.count) self.count += 1 self.credit = 0 pay = zm.encode_message([bytes(ph), bytes(ch)]) print ("send_pay",ph) self.port.send(pay, routing_id = self.client_id)
def recv_pay(self, timeout=0): print("recv_pay(to=%d)", timeout) which = dict(self.poller.poll(timeout)) if not self.port in which: return False frame = self.port.recv(copy=False, timeout=timeout); if frame.routing_id != self.client_id: print ("unknown client") return parts = zm.decode_message(frame.bytes); ph = zm.PrefixHeader(parts[0]) print("recv_pay",ph) fobj = json.loads(ph.label) if fobj["flow"] == "PAY": self.credit += fobj["credit"] return True return False
def recv_dat(self, timeout=-1): self.send_pay(); print("recv_dat()") which = dict(self.poller.poll(timeout)) if not self.port in which: return False frame = self.port.recv(copy=False); if frame.routing_id != self.client_id: print ("unknown client") return parts = zm.decode_message(frame.bytes); ph = zm.PrefixHeader(parts[0]) print("recv_dat",ph) ch = zm.CoordHeader(parts[1]) fobj = json.loads(ph.label) if fobj["flow"] == "DAT": self.credit += 1 return True return False
def do_bot(self): frame = self.port.recv(copy=False); print (type(frame)) print (frame.bytes) parts = zm.decode_message(frame.bytes); ph = zm.PrefixHeader(parts[0]) print(ph) ch = zm.CoordHeader(parts[1]) print(ch) fobj = json.loads(ph.label) credit = fobj["credit"] direction = fobj["direction"] if direction == "extract": self.direction = "inject" self.credit = credit self.is_sender = False elif direction == "inject": self.direction = "extract" self.credit = 0 self.is_sender = True else: print ("unknown direction %s" % direction) return fobj["direction"] = self.direction ph.label = json.dumps(fobj) ch.origin = self.origin ch.granule = now() ret = zm.encode_message([bytes(ph), bytes(ch)]) print (ret) self.client_id = frame.routing_id self.port.send(ret, routing_id = frame.routing_id)