def control_message (self): msg = self.pipe.recv_multipart() command = msg.pop(0) if command == "CONNECT": address = msg.pop(0) port = int(msg.pop(0)) if len(self.servers) < SERVER_MAX: self.servers.append(CloneServer(self.ctx, address, port, self.subtree)) self.publisher.connect("%s:%i" % (address,port+2)) else: logging.error("E: too many servers (max. %i)", SERVER_MAX) elif command == "SET": key,value,sttl = msg ttl = int(sttl) # Send key-value pair on to server kvmsg = KVMsg(0, key=key, body=value) kvmsg.store(self.kvmap) if ttl: kvmsg["ttl"] = ttl kvmsg.send(self.publisher) elif command == "GET": key = msg[0] value = self.kvmap.get(key) self.pipe.send(value.body if value else '')
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, SUBTREE) subscriber.connect("tcp://localhost:5557") publisher = ctx.socket(zmq.PUB) publisher.linger = 0 publisher.connect("tcp://localhost:5558") random.seed(time.time()) kvmap = {} # Get state snapshot sequence = 0 snapshot.send_multipart(["ICANHAZ?", SUBTREE]) while True: try: kvmsg = KVMsg.recv(snapshot) except: raise return # Interrupted if kvmsg.key == "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: 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) action = "update" if kvmsg.body else "delete" print "I: received %s=%d" % (action, sequence) # If we timed-out, generate a random kvmsg if time.time() >= alarm: kvmsg = KVMsg(0) kvmsg.key = SUBTREE + "%d" % random.randint(1,10000) kvmsg.body = "%d" % random.randint(1,1000000) kvmsg['ttl'] = random.randint(0,30) kvmsg.send(publisher) kvmsg.store(kvmap) alarm = time.time() + 1. print " Interrupted\n%d messages in" % sequence
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_string(zmq.SUBSCRIBE, "") 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_string("ICANHAZ?") while True: try: kvmsg = KVMsg.recv(snapshot) except: return # Interrupted if kvmsg.key == b"KTHXBAI": sequence = kvmsg.sequence print(f"S: Received snapshot {sequence} {kvmap}") break # Done kvmsg.store(kvmap) poller = zmq.Poller() poller.register(subscriber, zmq.POLLIN) alarm = time.time() + 1.0 while True: tickless = 1000 * max(0, alarm - time.time()) try: items = dict(poller.poll(tickless)) except: 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(f"U: received {sequence} {kvmap}") # If we timed-out, generate a random kvmsg if time.time() >= alarm: kvmsg = KVMsg(0) kvmsg.key = ("%d" % random.randint(1, 10000)).encode() kvmsg.body = ("%d" % random.randint(1, 1000000)).encode() kvmsg.send(publisher) kvmsg.store(kvmap) alarm = time.time() + 1.0 print(f"E: Interrupted\nE: {sequence} messages handled")
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, SUBTREE) subscriber.connect("tcp://localhost:5557") publisher = ctx.socket(zmq.PUB) publisher.linger = 0 publisher.connect("tcp://localhost:5558") random.seed(time.time()) kvmap = {} # Get state snapshot sequence = 0 snapshot.send_multipart(["ICANHAZ?", SUBTREE]) while True: try: kvmsg = KVMsg.recv(snapshot) except: raise return # Interrupted if kvmsg.key == "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: 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) action = "update" if kvmsg.body else "delete" print "I: received %s=%d" % (action, sequence) # If we timed-out, generate a random kvmsg if time.time() >= alarm: kvmsg = KVMsg(0) kvmsg.key = SUBTREE + "%d" % random.randint(1, 10000) kvmsg.body = "%d" % random.randint(1, 1000000) kvmsg['ttl'] = random.randint(0, 30) kvmsg.send(publisher) kvmsg.store(kvmap) alarm = time.time() + 1. print " Interrupted\n%d messages in" % sequence
def control_message(self): msg = self.pipe.recv_multipart() command = msg.pop(0) if self.debug: logger.debug('cmd=%s msg=%s', command, msg) pp(msg) if command == "CONNECT": #connect_kwargs = pipeline.load(msg[0]) #self.connect(**connect_kwargs) self.connect(msg[0]) self.pipe.send_multipart(['OK']) elif command == 'CONNECT_DISCOVERY': self.connect_via_discovery() self.pipe.send_multipart(['OK']) elif command == "DISCONNECT": address = msg.pop(0) port = int(msg.pop(0)) self.disconnect(address, port) self.pipe.send_multipart(['OK']) elif command == "SET": #key, value, sttl, serializer = msg key, value, sttl = msg ttl = int(sttl) value = pipeline.load(value) # Send key-value pair on to server kvmsg = KVMsg(0, key=key, body=value) kvmsg.store(self.kvmap) if ttl: kvmsg["ttl"] = ttl #if serializer: # kvmsg["serializer"] = serializer kvmsg.send(self.publisher) self.pipe.send_multipart(['OK']) elif command == "GET": key = msg[0] value = self.kvmap.get(key) if value: body = value.body #serializer = str(value.properties.get('serializer', '')) else: body = '' #serializer = '' body = pipeline.dump(body) #self.pipe.send_multipart([body, serializer]) self.pipe.send_multipart([body]) elif command == "SHOW": key = msg[0].upper() if key == 'SERVERS': self.pipe.send_multipart([ str(','.join([x.__repr__() for x in self.servers])), '']) elif key == 'SERVER': self.pipe.send_multipart([str(self.cur_server), '']) elif key == 'SEQ': self.pipe.send_multipart([str(self.sequence), '']) elif key == 'STATUS': self.pipe.send_multipart([str(self.cur_status), '']) elif key == 'KVMAP': #kvmap_s = pickle.dumps(self.kvmap) #self.pipe.send_multipart([kvmap_s, 'pickle']) kvmap_s = pipeline.dump(self.kvmap) self.pipe.send_multipart([kvmap_s])