multi-user/net_components.py

364 lines
11 KiB
Python
Raw Normal View History

2019-03-14 15:52:24 +01:00
# import zmq
import asyncio
import logging
from .libs.esper import esper
2019-03-14 16:44:18 +01:00
from .libs import zmq
2019-02-08 18:34:10 +01:00
from .libs import umsgpack
2019-02-11 15:48:07 +01:00
import time
import random
import struct
import collections
2019-03-15 17:37:02 +01:00
from enum import Enum
logger = logging.getLogger(__name__)
2019-03-14 17:04:41 +01:00
logging.basicConfig(level=logging.DEBUG)
2019-03-15 16:50:59 +01:00
CONNECT_TIMEOUT = 2
WAITING_TIME = 0.001
2019-02-22 16:46:35 +01:00
2019-03-15 17:37:02 +01:00
class RCFStatus(Enum):
IDLE = 1
CONNECTING = 2
CONNECTED = 3
class RCFFactory(object):
"""
Abstract layer used to bridge external and inter
"""
def init(self, data):
"""
set the RCFMessage pointer to local data
"""
print("Default setter")
#Setup data accessor
data.get = self.load_getter(data)
data.set = self.load_setter(data)
# TODO: Setup local pointer
def load_getter(self, data):
"""
local program > rcf
"""
print("Default getter")
return None
def load_setter(self, data):
"""
rcf > local program
"""
print("Default setter")
return None
def apply(self,data):
pass
def diff(self, data):
"""
Verify data integrity
"""
pass
class RCFStore(collections.MutableMapping,dict):
2019-03-20 16:46:55 +01:00
def __init__(self, custom_factory=RCFFactory()):
super().__init__()
self.factory = custom_factory
def __getitem__(self,key):
return dict.__getitem__(self,key)
def __setitem__(self, key, value):
dict.__setitem__(self,key,value)
def __delitem__(self, key):
dict.__delitem__(self,key)
def __iter__(self):
return dict.__iter__(self)
def __len__(self):
return dict.__len__(self)
def __contains__(self, x):
return dict.__contains__(self,x)
2019-02-13 11:55:18 +01:00
class RCFMessage(object):
"""
Message is formatted on wire as 2 frames:
frame 0: key (0MQ string) // property path
2019-02-14 15:30:35 +01:00
frame 1: id (0MQ string) // property path
frame 2: mtype (0MQ string) // property path
frame 3: body (blob) // Could be any data
"""
key = None # key (string)
id = None # User (string)
mtype = None # data mtype (string)
body = None # data blob
def __init__(self, key=None, id=None, mtype=None, body=None):
self.key = key
self.mtype = mtype
self.body = body
2019-02-14 15:30:35 +01:00
self.id = id
def store(self, dikt):
"""Store me in a dict if I have anything to store"""
# this currently erasing old value
2019-03-19 17:49:17 +01:00
if self.key is not None and self.body is not None:
2019-02-22 16:46:35 +01:00
dikt[self.key] = self
2019-03-19 17:49:17 +01:00
elif self.key in dikt:
del dikt[self.key]
2019-03-18 18:21:33 +01:00
def send(self, socket):
"""Send key-value message to socket; any empty frames are sent as such."""
2019-02-22 16:46:35 +01:00
key = ''.encode() if self.key is None else self.key.encode()
mtype = ''.encode() if self.mtype is None else self.mtype.encode()
body = ''.encode() if self.body is None else umsgpack.packb(self.body)
2019-03-06 14:01:33 +01:00
id = ''.encode() if self.id is None else self.id
2019-02-22 16:46:35 +01:00
try:
socket.send_multipart([key, id, mtype, body])
except:
logger.info("Fail to send {}".format(key))
@classmethod
def recv(cls, socket):
"""Reads key-value message from socket, returns new kvmsg instance."""
key, id, mtype, body = socket.recv_multipart(zmq.DONTWAIT)
key = key.decode() if key else None
id = id if id else None
mtype = mtype.decode() if body else None
body = umsgpack.unpackb(body) if body else None
return cls(key=key, id=id, mtype=mtype, body=body)
def dump(self):
if self.body is None:
size = 0
data = 'NULL'
else:
size = len(self.body)
data = repr(self.body)
print("[key:{key}][size:{size}][mtype:{mtype}] {data}".format(
key=self.key,
size=size,
mtype=self.mtype,
data=data,
))
2019-02-11 15:48:07 +01:00
class Client():
def __init__(
self,
context=zmq.Context(),
id="default",
on_recv=None,
on_post_init=None,
is_admin=False,
2019-03-15 16:50:59 +01:00
factory=None,
address="localhost"):
2019-03-06 14:01:33 +01:00
# 0MQ vars
2019-02-11 16:25:08 +01:00
self.context = context
self.pull_sock = None
self.req_sock = None
2019-02-11 16:25:08 +01:00
self.poller = None
# Client configuration
self.id = id.encode()
2019-02-22 16:46:35 +01:00
self.on_recv = on_recv
self.on_post_init = on_post_init
self.status = RCFStatus.IDLE
self.is_admin = is_admin
2019-03-15 16:50:59 +01:00
self.address = address
2019-02-22 16:46:35 +01:00
2019-03-15 16:50:59 +01:00
self.bind_ports()
# client routine registration
self.load_task = asyncio.ensure_future(self.load())
self.tick_task = None
2019-02-11 16:25:08 +01:00
self.property_map = RCFStore(custom_factory=factory)
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
2019-03-15 16:50:59 +01:00
self.pull_sock.connect("tcp://{}:5555".format(self.address))
2019-02-11 16:25:08 +01:00
self.pull_sock.setsockopt_string(zmq.SUBSCRIBE, '')
# 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)
2019-02-22 16:46:35 +01:00
# self.req_sock.setsockopt(zmq.SNDHWM, 60)
self.req_sock.linger = 0
2019-03-15 16:50:59 +01:00
self.req_sock.connect("tcp://{}:5556".format(self.address))
# 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-03-15 16:50:59 +01:00
self.push_sock.connect("tcp://{}:5557".format(self.address))
self.push_sock.setsockopt(zmq.SNDHWM, 60)
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)
2019-03-06 14:01:33 +01:00
time.sleep(0.1)
2019-02-22 16:46:35 +01:00
async def load(self):
2019-03-15 17:37:02 +01:00
self.status = RCFStatus.CONNECTING
2019-02-22 16:46:35 +01:00
logger.info("{} client syncing".format(id))
2019-02-11 18:10:56 +01:00
2019-02-22 16:46:35 +01:00
# Late join mecanism
logger.info("{} send snapshot request".format(id))
2019-02-22 16:46:35 +01:00
self.req_sock.send(b"SNAPSHOT_REQUEST")
2019-03-15 16:50:59 +01:00
while True:
try:
2019-02-22 16:46:35 +01:00
rcfmsg_snapshot = RCFMessage.recv(self.req_sock)
2019-02-22 16:46:35 +01:00
if rcfmsg_snapshot.key == "SNAPSHOT_END":
logger.info("snapshot complete")
2019-03-06 14:01:33 +01:00
break
2019-02-22 16:46:35 +01:00
else:
logger.info("received : {}".format(rcfmsg_snapshot.key))
rcfmsg_snapshot.store(self.property_map)
except:
await asyncio.sleep(0.001)
2019-03-06 14:01:33 +01:00
2019-02-22 16:46:35 +01:00
for f in self.on_post_init:
2019-03-06 14:01:33 +01:00
f()
2019-02-22 16:46:35 +01:00
logger.info("{} client running".format(id))
self.push_update("net/clients/{}".format(self.id.decode()),"client",self.id)
self.push_update("net/objects/{}".format(self.id.decode()),"client_object","None")
self.tick_task = asyncio.ensure_future(self.tick())
2019-03-15 17:37:02 +01:00
self.status = RCFStatus.CONNECTED
async def tick(self):
2019-02-22 16:46:35 +01:00
# Main loop
2019-02-11 15:48:07 +01:00
while True:
2019-02-11 16:25:08 +01:00
# TODO: find a better way
socks = dict(self.poller.poll(1))
2019-02-11 15:48:07 +01:00
2019-02-11 16:25:08 +01:00
if self.pull_sock in socks:
rcfmsg = RCFMessage.recv(self.pull_sock)
rcfmsg.store(self.property_map)
2019-02-22 16:46:35 +01:00
for f in self.on_recv:
f(rcfmsg)
else:
2019-03-14 17:04:41 +01:00
await asyncio.sleep(0.0001)
def push_update(self, key, mtype, body):
rcfmsg = RCFMessage(key, self.id, mtype, body)
rcfmsg.send(self.push_sock)
2019-02-11 15:48:07 +01:00
def stop(self):
2019-02-14 15:30:35 +01:00
logger.debug("Stopping client")
2019-02-11 17:49:28 +01:00
self.poller.unregister(self.pull_sock)
self.req_sock.close()
2019-02-11 16:25:08 +01:00
self.push_sock.close()
self.pull_sock.close()
self.load_task.cancel()
self.tick_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-11 16:25:08 +01:00
self.pub_sock = None
self.request_sock = None
self.collector_sock = None
2019-02-11 16:25:08 +01:00
self.poller = None
self.property_map = RCFStore()
2019-02-11 16:25:08 +01:00
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.setsockopt(zmq.SNDHWM, 60)
2019-02-11 16:25:08 +01:00
self.pub_sock.bind("tcp://*:5555")
2019-02-11 15:48:07 +01:00
time.sleep(0.2)
# Update request
self.request_sock = self.context.socket(zmq.ROUTER)
2019-02-22 16:46:35 +01:00
self.request_sock.setsockopt(zmq.IDENTITY, b'SERVER')
self.request_sock.setsockopt(zmq.RCVHWM, 60)
self.request_sock.bind("tcp://*:5556")
# Update collector
self.collector_sock = self.context.socket(zmq.PULL)
self.collector_sock.setsockopt(zmq.RCVHWM, 60)
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()
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-11 15:48:07 +01:00
while True:
# Non blocking poller
socks = dict(self.poller.poll(1))
2019-02-11 16:25:08 +01:00
# Snapshot system for late join (Server - Client)
if self.request_sock in socks:
2019-02-22 16:46:35 +01:00
msg = self.request_sock.recv_multipart(zmq.DONTWAIT)
2019-03-06 14:01:33 +01:00
identity = msg[0]
request = msg[1]
if request == b"SNAPSHOT_REQUEST":
pass
else:
logger.info("Bad snapshot request")
break
2019-03-06 14:01:33 +01:00
for k, v in self.property_map.items():
logger.info(
"Sending {} snapshot to {}".format(k, identity))
self.request_sock.send(identity, zmq.SNDMORE)
v.send(self.request_sock)
2019-03-06 14:01:33 +01:00
msg_end_snapshot = RCFMessage(key="SNAPSHOT_END", id=identity)
self.request_sock.send(identity, zmq.SNDMORE)
msg_end_snapshot.send(self.request_sock)
2019-02-22 16:46:35 +01:00
logger.info("done")
# Regular update routing (Clients / Client)
elif self.collector_sock in socks:
2019-02-22 16:46:35 +01:00
msg = RCFMessage.recv(self.collector_sock)
# Update all clients
2019-02-22 16:46:35 +01:00
msg.store(self.property_map)
msg.send(self.pub_sock)
else:
2019-03-14 17:04:41 +01:00
await asyncio.sleep(0.0001)
2019-02-11 15:48:07 +01:00
def stop(self):
2019-02-14 15:30:35 +01:00
logger.debug("Stopping server")
self.poller.unregister(self.request_sock)
2019-02-11 16:25:08 +01:00
self.pub_sock.close()
self.request_sock.close()
self.collector_sock.close()
2019-02-11 17:49:28 +01:00
self.task.cancel()
2019-03-15 17:37:02 +01:00
self.status= RCFStatus.IDLE