def client_actor(ctx, pipe, *args): pipe.signal() port = zio.Port("client", zmq.CLIENT,'') port.connect(server_address) port.online(None) # peer not needed if port only direct connects cflow = zio.flow.Flow(port) msg = zio.Message(label=json.dumps(dict(direction='extract',credit=2))) cflow.send_bot(msg) print ("client sent BOT:\n%s\n" % (msg,)) msg = cflow.recv_bot(1000) assert(msg) print ("client got BOT:\n%s\n" % (msg,)) for count in range(10): cflow.put(zio.Message()) print ("client sent DAT") cflow.send_eot() print ("client sent EOT") msg = cflow.recv_eot() assert(msg) print ("client done") pipe.recv() # wait for signal to exit
def client_actor(ctx, pipe, address): 'An actor function talking to a broker on given address' pipe.signal() port = zio.Port("client", zmq.CLIENT, '') port.connect(address) port.online(None) # peer not needed if port only direct connects log.debug("made flow") cflow = zio.flow.Flow(port) msg = zio.Message(seqno=0, label=json.dumps(dict(direction='extract', credit=2))) cflow.send_bot(msg) log.debug(f'client sent BOT: {msg}') msg = cflow.recv_bot(1000) assert (msg) log.debug(f'client got BOT: {msg}') msg = zio.Message(seqno=1) cflow.put(msg) log.debug(f'client sent {msg}') eot = zio.Message(seqno=2) cflow.send_eot(eot) log.debug(f'client sent {eot}') eot = cflow.recv_eot() assert (eot) log.debug(f'client done with {eot}') pipe.recv() # wait for signal to exit
def client_actor(ctx, pipe, address): 'An actor function talking to a broker on given address' pipe.signal() port = zio.Port("client", zmq.CLIENT,'') port.connect(address) port.online(None) # peer not needed if port only direct connects log.debug ("made flow") direction='extract' credit=2 cflow = Flow(port, direction, credit) bot = cflow.bot() log.debug (f'client did BOT: {bot}') assert(bot) cflow.begin() msg = zio.Message(form='FLOW', label_object={'flow':'DAT'}) log.debug (f'client put DAT with {cflow.credit}/{cflow.total_credit} {msg}') cflow.put(msg) log.debug (f'client did DAT') cflow.eot() log.debug (f'client did EOT') pipe.recv() # wait for signal to exit
def test_ctor_default(self): m = zio.Message(); print (m.origin) assert (m.origin == 0) assert (m.seqno == 0) assert (m.level == zio.MessageLevel.undefined) assert (m.label == "") assert (m.form == " ")
def test_ctor_default(self): m = zio.Message(form='FORM') log.debug(m.origin) assert (m.origin == 0) assert (m.seqno == 0) assert (m.level == zio.MessageLevel.undefined) assert (m.label == "") assert (m.form == "FORM") assert (m.routing_id == 0)
def send_bot(self, msg=None): if msg is None: msg = zio.Message() msg.form = "FLOW" my_fobj = dict(flow='BOT', credit=self.sm.total_credit, direction=self.sm.direction) fobj = msg.label_object fobj.update(my_fobj) self.debug(f'send_bot: {fobj}', '->') msg.label_object = fobj self.send(msg)
def send_pay(self): if self.giver(): # sanity check application raise TypeError('flow inject does not snd pay') if not self.credit: self.debug("send_pay: no credit to send") return pay = zio.Message(form='FLOW', label_object={'flow': 'PAY'}) if not self.sm.FlushPay(pay): raise RuntimeError('flow send pay failed') if self.credit: raise RuntimeError('LOGIC ERROR') self.debug(f"send_pay: {pay}", '->') self.port.send(pay)
def eotsend(self, msg=None): '''Acknowledge the recipt of an EOT. If msg it given it is sent, else a generic one is created. This should be called if EOT is received during put() or get(). ''' if msg is None: msg = zio.Message(form='FLOW') fobj = msg.label_object fobj['flow'] = 'EOT' msg.label_object = fobj self.send(msg)
def test_pingpong(self): sport = self.snode.port("sport") cport = self.cnode.port("cport") msg = zio.Message(form="TEXT", label="This is a message to you, Rudy", payload=["Stop your messing around", "Better think of your future"]) cport.send(msg); msg2 = sport.recv(timeout=1000) msg2.payload=['Time you straighten right out', "Else you'll wind up in jail"] assert (type(msg2.payload[0]) == bytes) #print("sending to rid %d" % msg2.routing_id) sport.send(msg2) # should forward the routing_id msg3 = cport.recv(timeout=1000) assert (msg2.payload[0] == msg3.payload[0])
def test_ctor_headers(self): msg = zio.Message(coord = zio.CoordHeader(seqno=100)) assert(msg.seqno == 100)
def test_ctor_encstr(self): msg = zio.Message(payload = 'hello world'.split()) assert (2 == len(msg.payload)) enc = msg.encode()
def test_ctor_parts(self): ph = zio.PrefixHeader() ch = zio.CoordHeader() msg = zio.Message(parts=[bytes(ph), bytes(ch), b'Hello', b'World']) assert (2 == len(msg.payload))
def test_pbhdf(): # In a "real" app, this would live off in a server. The group # name would be chosen based on some unique identifier for the # flow. fp = h5py.File(filename, 'w') base = fp.create_group("unique/flow/id") writer = Writer(base, pb, frompb) # Here we pretend to be some remote source of ZIO messages depo = pb.Depo(ident=99, pos=pb.Point(x=1, y=2, z=3), time=100.0, charge=1000.0, trackid=12345, pdg=11, extent_long=9.6, extent_tran=6.9) a = Any() a.Pack(depo) # pl = pb.Payload() # pl.objects.append(a) msg = zio.Message(form='FLOW', label=json.dumps({'stream': 'depos'}), payload=[a.SerializeToString()]) msg.seqno = 42 # # Normally, msg is created in remote client, sent over ZIO flow to # some server and dispatched to a matching writer. Wave our hands # hiding all that and directly save. # writer.save(msg) # # Normally, one writer would service one type of stream, but in # this test we'll force it to also save a frame. This test # should use the "sparse" frame save mode. # frame = pb.Frame(ident=2, time=time.time(), tick=500.0) chanset = list(range(1000)) negs = frame.tagged_traces["negs"].elements negs_tot = frame.trace_summaries["negs"].elements for ind in range(500): chan = random.choice(chanset) chanset.remove(chan) tbin = int(random.uniform(0, 1000)) nticks = int(random.uniform(tbin, 1000)) if nticks == 0: nticks = 1 tbin -= 1 samples = numpy.random.normal(size=nticks) tr = pb.Trace(channel=chan, tbin=tbin) for s in samples: tr.samples.elements.append(s) frame.traces.append(tr) tot = numpy.sum(samples) if tot < 0: negs.append(ind) negs_tot.append(tot) brl = frame.channel_masks["bad"].bin_range_lists[chan] brl.beg.append(tbin) brl.end.append(tbin + nticks) if len(negs) > 0: frame.frame_tags.append("negged") a = Any() a.Pack(frame) msg = zio.Message(form='FLOW', label=json.dumps({'stream': 'frames'}), payload=[a.SerializeToString()]) msg.seqno = 43 writer.save(msg) # # Do it again with an accidentally rectangular frame # frame = pb.Frame(ident=3, time=time.time(), tick=500.0) nchans = 500 nticks = 1000 samples = numpy.random.normal(size=(nchans, nticks)) chanset = list(range(1000)) tbin = 0 for ind in range(500): chan = random.choice(chanset) chanset.remove(chan) tr = pb.Trace(channel=chan, tbin=tbin) for s in samples[ind]: tr.samples.elements.append(s) frame.traces.append(tr) tot = numpy.sum(samples[ind]) if tot < 0: negs.append(ind) negs_tot.append(tot) brl = frame.channel_masks["bad"].bin_range_lists[chan] brl.beg.append(tbin) brl.end.append(tbin + nticks) if len(negs) > 0: frame.frame_tags.append("negged") a = Any() a.Pack(frame) msg = zio.Message(form='FLOW', label=json.dumps({'stream': 'frames'}), payload=[a.SerializeToString()]) msg.seqno = 44 writer.save(msg)