2019-02-08 15:44:02 +01:00
|
|
|
import zmq
|
|
|
|
import asyncio
|
|
|
|
import logging
|
2019-02-08 18:34:10 +01:00
|
|
|
from .libs import umsgpack
|
2019-02-11 15:48:07 +01:00
|
|
|
import time
|
|
|
|
import random
|
2019-02-13 14:04:32 +01:00
|
|
|
import struct
|
2019-02-08 15:44:02 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-13 18:53:22 +01:00
|
|
|
# TODO: Add message time and author stamp for reliabilty
|
2019-02-13 11:55:18 +01:00
|
|
|
class RCFMessage(object):
|
2019-02-13 11:52:15 +01:00
|
|
|
"""
|
|
|
|
Message is formatted on wire as 2 frames:
|
|
|
|
frame 0: key (0MQ string) // property path
|
2019-02-13 11:55:18 +01:00
|
|
|
frame 2: body (blob) // Could be any data
|
2019-02-13 11:52:15 +01:00
|
|
|
|
|
|
|
"""
|
2019-02-13 14:04:32 +01:00
|
|
|
key = None # key (string)
|
|
|
|
body = None # blob
|
2019-02-13 11:52:15 +01:00
|
|
|
|
|
|
|
def __init__(self, key=None, body=None):
|
|
|
|
self.key = key
|
|
|
|
self.body = body
|
|
|
|
|
|
|
|
def store(self, dikt):
|
|
|
|
"""Store me in a dict if I have anything to store"""
|
|
|
|
# this seems weird to check, but it's what the C example does
|
2019-02-13 14:04:32 +01:00
|
|
|
# this currently erasing old value
|
2019-02-13 11:52:15 +01:00
|
|
|
if self.key is not None and self.body is not None:
|
|
|
|
dikt[self.key] = self
|
|
|
|
|
|
|
|
def send(self, socket):
|
|
|
|
"""Send key-value message to socket; any empty frames are sent as such."""
|
2019-02-13 18:53:22 +01:00
|
|
|
key = '' if self.key is None else self.key.encode()
|
|
|
|
body = '' if self.body is None else umsgpack.packb(self.body)
|
2019-02-13 14:04:32 +01:00
|
|
|
socket.send_multipart([key, body])
|
2019-02-13 11:52:15 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def recv(cls, socket):
|
|
|
|
"""Reads key-value message from socket, returns new kvmsg instance."""
|
2019-02-13 18:53:22 +01:00
|
|
|
key, body = socket.recv_multipart(zmq.NOBLOCK)
|
|
|
|
key = key.decode() if key else None
|
|
|
|
body = umsgpack.unpackb(body) if body else None
|
|
|
|
|
2019-02-13 11:52:15 +01:00
|
|
|
return cls(key=key, body=body)
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
if self.body is None:
|
|
|
|
size = 0
|
2019-02-13 14:04:32 +01:00
|
|
|
data = 'NULL'
|
2019-02-13 11:52:15 +01:00
|
|
|
else:
|
|
|
|
size = len(self.body)
|
2019-02-13 14:04:32 +01:00
|
|
|
data = repr(self.body)
|
2019-02-13 11:52:15 +01:00
|
|
|
print("[key:{key}][size:{size}] {data}".format(
|
|
|
|
key=self.key,
|
|
|
|
size=size,
|
|
|
|
data=data,
|
|
|
|
))
|
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-11 15:48:07 +01:00
|
|
|
class Client():
|
2019-02-12 10:18:39 +01:00
|
|
|
def __init__(self, context=zmq.Context(), id="default", recv_callback=None):
|
2019-02-11 16:25:08 +01:00
|
|
|
self.context = context
|
|
|
|
self.pull_sock = None
|
2019-02-13 14:04:32 +01:00
|
|
|
self.req_sock = None
|
2019-02-11 16:25:08 +01:00
|
|
|
self.poller = None
|
2019-02-12 10:18:39 +01:00
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
self.id = id.encode()
|
2019-02-12 10:18:39 +01:00
|
|
|
self.recv_callback = recv_callback
|
2019-02-11 16:25:08 +01:00
|
|
|
self.bind_ports()
|
|
|
|
# Main client loop registration
|
|
|
|
self.task = asyncio.ensure_future(self.main())
|
|
|
|
|
2019-02-13 11:52:15 +01:00
|
|
|
self.property_map = {}
|
2019-02-13 18:53:22 +01:00
|
|
|
|
2019-02-11 16:25:08 +01:00
|
|
|
logger.info("{} client initialized".format(id))
|
|
|
|
|
|
|
|
def bind_ports(self):
|
|
|
|
# pull socket: get update FROM server
|
|
|
|
self.pull_sock = self.context.socket(zmq.SUB)
|
|
|
|
self.pull_sock.linger = 0
|
|
|
|
self.pull_sock.connect("tcp://localhost:5555")
|
|
|
|
self.pull_sock.setsockopt_string(zmq.SUBSCRIBE, '')
|
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
# request socket: send request/message over all peers throught the server
|
|
|
|
self.req_sock = self.context.socket(zmq.DEALER)
|
|
|
|
self.req_sock.setsockopt(zmq.IDENTITY, self.id)
|
|
|
|
self.req_sock.linger = 0
|
|
|
|
self.req_sock.connect("tcp://localhost:5556")
|
|
|
|
|
|
|
|
# push update socket
|
|
|
|
self.push_sock = self.context.socket(zmq.PUSH)
|
|
|
|
self.push_sock.setsockopt(zmq.IDENTITY, self.id)
|
2019-02-11 16:25:08 +01:00
|
|
|
self.push_sock.linger = 0
|
2019-02-13 14:04:32 +01:00
|
|
|
self.push_sock.connect("tcp://localhost:5557")
|
2019-02-11 16:25:08 +01:00
|
|
|
|
|
|
|
# Sockets aggregator, not really used for now
|
|
|
|
self.poller = zmq.Poller()
|
|
|
|
self.poller.register(self.pull_sock, zmq.POLLIN)
|
|
|
|
|
|
|
|
async def main(self):
|
|
|
|
logger.info("{} client launched".format(id))
|
2019-02-11 18:10:56 +01:00
|
|
|
|
2019-02-11 15:48:07 +01:00
|
|
|
# Prepare our context and publisher socket
|
|
|
|
while True:
|
2019-02-11 16:25:08 +01:00
|
|
|
# TODO: find a better way
|
2019-02-11 15:48:07 +01:00
|
|
|
await asyncio.sleep(0.016)
|
|
|
|
try:
|
2019-02-11 16:25:08 +01:00
|
|
|
socks = dict(self.poller.poll(1))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
2019-02-11 15:48:07 +01:00
|
|
|
|
2019-02-11 16:25:08 +01:00
|
|
|
if self.pull_sock in socks:
|
2019-02-13 18:53:22 +01:00
|
|
|
rcfmsg = RCFMessage.recv(self.pull_sock)
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-13 18:53:22 +01:00
|
|
|
rcfmsg.store(self.property_map)
|
|
|
|
rcfmsg.dump()
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-13 18:53:22 +01:00
|
|
|
for f in self.recv_callback:
|
|
|
|
f(rcfmsg)
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-13 18:53:22 +01:00
|
|
|
def push_update(self, key, body):
|
|
|
|
rcfmsg = RCFMessage(key,body)
|
|
|
|
rcfmsg.send(self.push_sock)
|
|
|
|
# self.push_sock.send_multipart()
|
2019-02-11 15:48:07 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
logger.info("Stopping client")
|
2019-02-11 17:49:28 +01:00
|
|
|
self.poller.unregister(self.pull_sock)
|
2019-02-13 14:04:32 +01:00
|
|
|
self.req_sock.close()
|
2019-02-11 16:25:08 +01:00
|
|
|
self.push_sock.close()
|
|
|
|
self.pull_sock.close()
|
2019-02-11 17:49:28 +01:00
|
|
|
self.task.cancel()
|
2019-02-11 15:48:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Server():
|
2019-02-11 16:25:08 +01:00
|
|
|
def __init__(self, context=zmq.Context(), id="admin"):
|
|
|
|
self.context = context
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-11 16:25:08 +01:00
|
|
|
self.pub_sock = None
|
2019-02-13 14:04:32 +01:00
|
|
|
self.request_sock = None
|
|
|
|
self.collector_sock = None
|
2019-02-11 16:25:08 +01:00
|
|
|
self.poller = None
|
|
|
|
|
|
|
|
self.id = id
|
|
|
|
self.bind_ports()
|
|
|
|
# Main client loop registration
|
|
|
|
self.task = asyncio.ensure_future(self.main())
|
|
|
|
|
|
|
|
logger.info("{} client initialized".format(id))
|
|
|
|
|
|
|
|
def bind_ports(self):
|
|
|
|
# Update all clients
|
|
|
|
self.pub_sock = self.context.socket(zmq.PUB)
|
|
|
|
self.pub_sock.bind("tcp://*:5555")
|
2019-02-11 15:48:07 +01:00
|
|
|
time.sleep(0.2)
|
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
# Update request
|
|
|
|
self.request_sock = self.context.socket(zmq.ROUTER)
|
|
|
|
self.request_sock.bind("tcp://*:5556")
|
|
|
|
|
|
|
|
# Update collector
|
|
|
|
self.collector_sock = self.context.socket(zmq.PULL)
|
|
|
|
self.collector_sock.bind("tcp://*:5557")
|
2019-02-11 15:48:07 +01:00
|
|
|
|
2019-02-11 16:25:08 +01:00
|
|
|
# poller for socket aggregation
|
|
|
|
self.poller = zmq.Poller()
|
2019-02-13 14:04:32 +01:00
|
|
|
self.poller.register(self.request_sock, zmq.POLLIN)
|
|
|
|
self.poller.register(self.collector_sock, zmq.POLLIN)
|
2019-02-11 16:25:08 +01:00
|
|
|
|
|
|
|
async def main(self):
|
|
|
|
logger.info("{} server launched".format(id))
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-11 15:48:07 +01:00
|
|
|
while True:
|
2019-02-11 16:25:08 +01:00
|
|
|
# TODO: find a better way
|
2019-02-11 15:48:07 +01:00
|
|
|
await asyncio.sleep(0.016)
|
|
|
|
|
2019-02-11 16:25:08 +01:00
|
|
|
try:
|
|
|
|
socks = dict(self.poller.poll(1))
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
if self.request_sock in socks:
|
|
|
|
msg = self.request_sock.recv_multipart(zmq.NOBLOCK)
|
2019-02-11 16:25:08 +01:00
|
|
|
|
|
|
|
# Update all clients
|
|
|
|
self.pub_sock.send_multipart(msg)
|
2019-02-11 15:48:07 +01:00
|
|
|
|
2019-02-13 14:04:32 +01:00
|
|
|
if self.collector_sock in socks:
|
2019-02-13 18:53:22 +01:00
|
|
|
msg = self.collector_sock.recv_multipart(zmq.NOBLOCK)
|
|
|
|
|
|
|
|
# Update all clients
|
|
|
|
self.pub_sock.send_multipart(msg)
|
2019-02-13 14:04:32 +01:00
|
|
|
|
2019-02-11 15:48:07 +01:00
|
|
|
def stop(self):
|
|
|
|
logger.info("Stopping server")
|
2019-02-13 14:04:32 +01:00
|
|
|
self.poller.unregister(self.request_sock)
|
2019-02-11 16:25:08 +01:00
|
|
|
self.pub_sock.close()
|
2019-02-13 14:04:32 +01:00
|
|
|
self.request_sock.close()
|
|
|
|
self.collector_sock.close()
|
2019-02-11 17:49:28 +01:00
|
|
|
self.task.cancel()
|