def main(): # Prepare our context and publisher socket ctx = zmq.Context() publisher = ctx.socket(zmq.PUB) publisher.bind("tcp://*:5556") time.sleep(0.2) sequence = 0 random.seed(time.time()) kvmap = {} try: while True: # Distribute as key-value message sequence += 1 kvmsg = KVMsg(sequence) kvmsg.key = ("%4d" % random.randint(0,9999)).encode() kvmsg.body = ("%6d" % random.randint(0,999999)).encode() kvmsg.send(publisher) kvmsg.store(kvmap) time.sleep(0.1) except KeyboardInterrupt: print (" Interrupted\n%d messages out" % sequence) dump_all(kvmap, 'pub.txt')
def main(): # context and sockets ctx = zmq.Context() snapshot = ctx.socket(zmq.ROUTER) snapshot.bind("tcp://*:5556") publisher = ctx.socket(zmq.PUB) publisher.bind("tcp://*:5557") collector = ctx.socket(zmq.PULL) collector.bind("tcp://*:5558") sequence = 0 kvmap = {} poller = zmq.Poller() poller.register(collector, zmq.POLLIN) poller.register(snapshot, zmq.POLLIN) while True: try: items = dict(poller.poll(1000)) except: break # Interrupted # Apply state update sent from client if collector in items: kvmsg = KVMsg.recv(collector) sequence += 1 kvmsg.sequence = sequence kvmsg.send(publisher) kvmsg.store(kvmap) print("I: publishing update %5d" % sequence) # Execute state snapshot request if snapshot in items: msg = snapshot.recv_multipart() identity = msg[0] request = msg[1] if request == b"ICANHAZ?": pass else: print("E: bad request, aborting\n", ) break # Send state snapshot to client route = Route(snapshot, identity) # For each entry in kvmap, send kvmsg to client for k, v in kvmap.items(): send_single(k, v, route) # Now send END message with sequence number print("Sending state shapshot=%d\n" % sequence, ) snapshot.send(identity, zmq.SNDMORE) kvmsg = KVMsg(sequence) kvmsg.key = b"KTHXBAI" kvmsg.body = b"" kvmsg.send(snapshot) print(" Interrupted\n%d messages handled" % sequence) dump_all(kvmap, 'pub.txt')
def main(): # Prepare our context and subscriber ctx = zmq.Context() snapshot = ctx.socket(zmq.DEALER) snapshot.linger = 0 snapshot.connect("tcp://localhost:5556") subscriber = ctx.socket(zmq.SUB) subscriber.linger = 0 subscriber.setsockopt(zmq.SUBSCRIBE, b'') subscriber.connect("tcp://localhost:5557") kvmap = {} # Get state snapshot sequence = 0 snapshot.send(b"ICANHAZ?") while True: try: kvmsg = KVMsg.recv(snapshot) except: break # Interrupted if kvmsg.key == b"KTHXBAI": sequence = kvmsg.sequence print("Received snapshot=%d" % sequence) break # Done kvmsg.store(kvmap) # Now apply pending updates, discard out-of-sequence messages while True: try: kvmsg = KVMsg.recv(subscriber) except: break # Interrupted if kvmsg.sequence > sequence: sequence = kvmsg.sequence kvmsg.store(kvmap) else: print('dropped duplicates', kvmsg.sequence) print('quiting') dump_all(kvmap, 'sub.txt')
def main(): # Prepare our context and publisher socket ctx = zmq.Context() updates = ctx.socket(zmq.SUB) updates.linger = 0 updates.setsockopt(zmq.SUBSCRIBE, b'') updates.connect("tcp://localhost:5556") kvmap = {} sequence = 0 while True: try: kvmsg = KVMsg.recv(updates) except: break # Interrupted kvmsg.store(kvmap) sequence += 1 if sequence % 100 == 0: print('message in: ', sequence) print("Interrupted\n%d messages in" % sequence) dump_all(kvmap, 'sub.txt')
def main(): # Prepare our context and subscriber ctx = zmq.Context() snapshot = ctx.socket(zmq.DEALER) snapshot.linger = 0 snapshot.connect("tcp://localhost:5556") subscriber = ctx.socket(zmq.SUB) subscriber.linger = 0 subscriber.setsockopt(zmq.SUBSCRIBE, b'') subscriber.connect("tcp://localhost:5557") publisher = ctx.socket(zmq.PUSH) publisher.linger = 0 publisher.connect("tcp://localhost:5558") random.seed(time.time()) kvmap = {} # Get state snapshot sequence = 0 snapshot.send(b"ICANHAZ?") while True: try: kvmsg = KVMsg.recv(snapshot) except: return # Interrupted if kvmsg.key == b"KTHXBAI": sequence = kvmsg.sequence print("I: Received snapshot=%d" % sequence) break # Done kvmsg.store(kvmap) poller = zmq.Poller() poller.register(subscriber, zmq.POLLIN) alarm = time.time()+1. while True: tickless = 1000*max(0, alarm - time.time()) try: items = dict(poller.poll(tickless)) except: print('cli3 quitting') dump_all(kvmap, 'pub.txt') break # Interrupted if subscriber in items: kvmsg = KVMsg.recv(subscriber) # Discard out-of-sequence kvmsgs, incl. heartbeats if kvmsg.sequence > sequence: sequence = kvmsg.sequence kvmsg.store(kvmap) print("I: received update=%d" % sequence) # If we timed-out, generate a random kvmsg if time.time() >= alarm: kvmsg = KVMsg(0) kvmsg.key = ("%d" % random.randint(0,9999)).encode() kvmsg.body = ("%d" % random.randint(0,999999)).encode() kvmsg.send(publisher) kvmsg.store(kvmap) alarm = time.time() + 1. print(" Interrupted\n%d messages in" % sequence)
def state_manager(ctx, pipe): """This thread maintains the state and handles requests from clients for snapshots. """ kvmap = {} pipe.send(b"READY") snapshot = ctx.socket(zmq.ROUTER) snapshot.bind("tcp://*:5556") poller = zmq.Poller() poller.register(pipe, zmq.POLLIN) poller.register(snapshot, zmq.POLLIN) sequence = 0 # Current snapshot version number while True: try: items = dict(poller.poll()) except (zmq.ZMQError, KeyboardInterrupt): dump_all(kvmap, 'pub.txt') print('pub: interrupted') break # interrupt/context shutdown # Apply state update from main thread if pipe in items: parts = pipe.recv_multipart() if len(parts) == 1: assert parts[0] == b'QUIT' print('quitting') dump_all(kvmap, 'pub.txt') break key, seq_s, body = parts key = key if key else None seq = struct.unpack('!l', seq_s)[0] body = body if body else None kvmsg = KVMsg(seq, key=key, body=body) sequence = kvmsg.sequence kvmsg.store(kvmap) # Execute state snapshot request if snapshot in items: msg = snapshot.recv_multipart() identity = msg[0] request = msg[1] if request == b"ICANHAZ?": pass else: print("E: bad request, aborting\n", ) break # Send state snapshot to client route = Route(snapshot, identity) # For each entry in kvmap, send kvmsg to client for k, v in kvmap.items(): send_single(k, v, route) # Now send END message with sequence number print("Sending state shapshot=%d\n" % sequence, ) snapshot.send(identity, zmq.SNDMORE) kvmsg = KVMsg(sequence) kvmsg.key = b"KTHXBAI" kvmsg.body = b"" kvmsg.send(snapshot)