class P2PConnection(object): def __init__(self, ctx, addr, ident, sock_type=zmq.XREQ): self.queue = QueueSocket(ctx, addr, sock_type, bind=False, ident=ident) def send(self, style, target, data): self.queue.send([style, target, pickle.dumps(data)]) def recv(self): data = self.queue.recv() data[-1] = pickle.loads(data[-1]) return data def call(self, target, data): self.send('call', target, data) return self.recv() def cast(self, target, data): self.send('cast', target, data) def reply(self, target, style, client_id, data): self.queue.send([target, style, client_id, pickle.dumps(data)]) def close(self): self.queue.close()
class RoundRobinService(client.RoundRobinClient): def __init__(self, ctx, addr, ident=None): self.recvq = None self.ctx = ctx super(RoundRobinService, self).__init__(ctx, addr, ident) def register(self, target): self.send('register', target, {}) style, topic, resp = self.recv() self.recv_addr = resp['address'] self.recvq = QueueSocket(self.ctx, self.recv_addr, zmq.PULL, send=False, bind=False) def pull(self): assert self.recvq, "You need to register first." data = self.recvq.recv() data[-1] = pickle.loads(data[-1]) return data def reply(self, client_id, style, target, data): self.outq.send([client_id, style, target, pickle.dumps(data)]) def close(self): self.outq.close() self.recvq.close()
class PubSubClient(object): def __init__(self, ctx, addr, ident): self.outq = QueueSocket(ctx, addr, zmq.XREQ, recv=False, bind=False, ident=ident) def cast(self, target, data): self.outq.send(['cast', target, pickle.dumps(data)]) def close(self): self.outq.close()
class PubSubService(object): def __init__(self, ctx, addr, subscribe): self.inqueue = QueueSocket(ctx, addr, zmq.SUB, bind=False, send=False, subscribe=subscribe) def pull(self): data = self.inqueue.recv() data[-1] = pickle.loads(data[-1]) return data def close(self): self.inqueue.close()
class RoundRobinClient(object): def __init__(self, ctx, addr, ident): self.outq = QueueSocket(ctx, addr, zmq.XREQ, bind=False, ident=ident) def send(self, style, target, data): self.outq.send([style, target, pickle.dumps(data)]) def recv(self): data = self.outq.recv() data[-1] = pickle.loads(data[-1]) return data def call(self, target, data): self.send('call', target, data) return self.recv() def cast(self, target, data): self.send('cast', target, data) def close(self): self.outq.close()