blender code refactoring
This commit is contained in:
parent
8402393862
commit
048411308e
@ -40,7 +40,7 @@ def main():
|
|||||||
break
|
break
|
||||||
|
|
||||||
if updates in socks:
|
if updates in socks:
|
||||||
message = updates.recv_multipart()
|
message = updates.recv_multipart(zmq.NOBLOCK)
|
||||||
print(message)
|
print(message)
|
||||||
|
|
||||||
# Send update
|
# Send update
|
||||||
|
@ -10,6 +10,7 @@ import random
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
class Session():
|
class Session():
|
||||||
def __init__(self, host='127.0.0.1', port=5555, is_hosting=False):
|
def __init__(self, host='127.0.0.1', port=5555, is_hosting=False):
|
||||||
self.host = host
|
self.host = host
|
||||||
@ -21,7 +22,7 @@ class Session():
|
|||||||
|
|
||||||
self.msg = []
|
self.msg = []
|
||||||
|
|
||||||
#self.listen.add_done_callback(self.close_success())
|
# self.listen.add_done_callback(self.close_success())
|
||||||
|
|
||||||
# TODO: Add a kill signal to destroy clients session
|
# TODO: Add a kill signal to destroy clients session
|
||||||
# TODO: Add a join method
|
# TODO: Add a join method
|
||||||
@ -52,7 +53,7 @@ class Session():
|
|||||||
self.listen = asyncio.ensure_future(self.listen())
|
self.listen = asyncio.ensure_future(self.listen())
|
||||||
return True
|
return True
|
||||||
except zmq.ZMQError:
|
except zmq.ZMQError:
|
||||||
logger.error("Error while creating session: ",zmq.ZMQError)
|
logger.error("Error while creating session: ", zmq.ZMQError)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -89,6 +90,7 @@ class Session():
|
|||||||
del self.listen
|
del self.listen
|
||||||
self.is_running = False
|
self.is_running = False
|
||||||
|
|
||||||
|
|
||||||
class Client_poller():
|
class Client_poller():
|
||||||
def __init__(self, id):
|
def __init__(self, id):
|
||||||
|
|
||||||
@ -117,86 +119,119 @@ class Client_poller():
|
|||||||
msg = socket.recv(zmq.NOBLOCK)
|
msg = socket.recv(zmq.NOBLOCK)
|
||||||
logger.info("{} received:{}".format(self.id, msg))
|
logger.info("{} received:{}".format(self.id, msg))
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
logger.info("Stopping client {}".format(self.id))
|
logger.info("Stopping client {}".format(self.id))
|
||||||
self.listen.cancel()
|
self.listen.cancel()
|
||||||
|
|
||||||
|
|
||||||
class Client():
|
class Client():
|
||||||
def __init__(self, context=None):
|
def __init__(self, context=zmq.Context(), id="default"):
|
||||||
|
|
||||||
if context is None:
|
self.context = context
|
||||||
logger.info("client init default context")
|
self.pull_sock = None
|
||||||
self.context = zmq.Context()
|
self.push_sock = None
|
||||||
else:
|
self.poller = None
|
||||||
self.context = context
|
|
||||||
|
|
||||||
self.task = asyncio.ensure_future(self.run())
|
self.id = id
|
||||||
logger.info("client initiated")
|
self.bind_ports()
|
||||||
|
# Main client loop registration
|
||||||
|
self.task = asyncio.ensure_future(self.main())
|
||||||
|
|
||||||
async def run(self):
|
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, '')
|
||||||
|
|
||||||
|
# push socket: push update TO server
|
||||||
|
self.push_sock = self.context.socket(zmq.DEALER)
|
||||||
|
self.push_sock.setsockopt(zmq.IDENTITY, self.id.encode())
|
||||||
|
self.push_sock.linger = 0
|
||||||
|
self.push_sock.connect("tcp://localhost:5556")
|
||||||
|
|
||||||
|
# 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))
|
||||||
# Prepare our context and publisher socket
|
# Prepare our context and publisher socket
|
||||||
logger.info("configuring:")
|
|
||||||
updates = self.context.socket(zmq.SUB)
|
|
||||||
logger.info("..socket")
|
|
||||||
updates.linger = 0
|
|
||||||
logger.info("..linger")
|
|
||||||
updates.setsockopt(zmq.SUBSCRIBE,b"10001")
|
|
||||||
logger.info("client launched")
|
|
||||||
updates.connect("tcp://localhost:5556")
|
|
||||||
|
|
||||||
kvmap = {}
|
|
||||||
sequence = 0
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
# TODO: find a better way
|
||||||
await asyncio.sleep(0.016)
|
await asyncio.sleep(0.016)
|
||||||
try:
|
try:
|
||||||
kvmsg = kvsimple.KVMsg.recv(updates)
|
socks = dict(self.poller.poll(1))
|
||||||
except:
|
except KeyboardInterrupt:
|
||||||
break # Interrupted
|
break
|
||||||
kvmsg.store(kvmap)
|
|
||||||
sequence += 1
|
|
||||||
|
|
||||||
|
if self.pull_sock in socks:
|
||||||
|
message = self.pull_sock.recv_multipart(zmq.NOBLOCK)
|
||||||
|
print(message)
|
||||||
|
|
||||||
|
def send_msg(self, msg):
|
||||||
|
self.push_sock.send_multipart(msg.encode())
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
logger.info("Stopping client")
|
logger.info("Stopping client")
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
|
self.push_sock.close()
|
||||||
|
self.pull_sock.close()
|
||||||
|
self.context.term()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
kvmap = {}
|
|
||||||
sequence = 0
|
|
||||||
|
|
||||||
class Server():
|
class Server():
|
||||||
def __init__(self):
|
def __init__(self, context=zmq.Context(), id="admin"):
|
||||||
self.context = zmq.Context()
|
self.context = context
|
||||||
self.task = asyncio.ensure_future(self.run())
|
self.pub_sock = None
|
||||||
logger.info("server initiated ")
|
self.pull_sock = None
|
||||||
|
self.poller = None
|
||||||
|
|
||||||
async def run(self):
|
self.id = id
|
||||||
publisher = self.context.socket(zmq.PUB)
|
self.bind_ports()
|
||||||
|
# Main client loop registration
|
||||||
|
self.task = asyncio.ensure_future(self.main())
|
||||||
|
|
||||||
publisher.bind("tcp://*:5556")
|
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")
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
logger.info("server launched")
|
|
||||||
|
|
||||||
sequence = 0
|
# Update receiver
|
||||||
random.seed(time.time())
|
self.pull_sock = self.context.socket(zmq.ROUTER)
|
||||||
kvmap = {}
|
self.pull_sock.bind("tcp://*:5556")
|
||||||
|
|
||||||
|
# poller for socket aggregation
|
||||||
|
self.poller = zmq.Poller()
|
||||||
|
self.poller.register(self.pull_sock, zmq.POLLIN)
|
||||||
|
|
||||||
|
async def main(self):
|
||||||
|
logger.info("{} server launched".format(id))
|
||||||
|
# Prepare our context and publisher socket
|
||||||
while True:
|
while True:
|
||||||
# Non blocking
|
# TODO: find a better way
|
||||||
await asyncio.sleep(0.016)
|
await asyncio.sleep(0.016)
|
||||||
|
|
||||||
# Distribute as key-value message
|
try:
|
||||||
sequence += 1
|
socks = dict(self.poller.poll(1))
|
||||||
kvmsg = kvsimple.KVMsg(sequence)
|
except KeyboardInterrupt:
|
||||||
kvmsg.key = "%d" % random.randint(1,10000)
|
break
|
||||||
kvmsg.body = "%d" % random.randint(1,1000000)
|
|
||||||
kvmsg.send(publisher)
|
|
||||||
kvmsg.store(kvmap)
|
|
||||||
|
|
||||||
|
if self.pull_sock in socks:
|
||||||
|
msg = self.pull_sock.recv_multipart(zmq.NOBLOCK)
|
||||||
|
print("{}:{}".format(msg[0].decode('ascii'), msg[1].decode()))
|
||||||
|
|
||||||
|
# Update all clients
|
||||||
|
self.pub_sock.send_multipart(msg)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
logger.info("Stopping server")
|
logger.info("Stopping server")
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
|
self.pub_sock.close()
|
||||||
|
self.pull_sock.close()
|
||||||
|
self.context.term()
|
||||||
|
@ -31,9 +31,8 @@ def main():
|
|||||||
break
|
break
|
||||||
|
|
||||||
if state_request in socks:
|
if state_request in socks:
|
||||||
msg = state_request.recv_multipart()
|
msg = state_request.recv_multipart(zmq.NOBLOCK)
|
||||||
print(msg[0].decode('ascii'))
|
print("{}:{}".format(msg[0].decode('ascii'),msg[1].decode()))
|
||||||
print(msg[1].decode())
|
|
||||||
publisher.send(b'Server update')
|
publisher.send(b'Server update')
|
||||||
|
|
||||||
# publisher.send_string('test')
|
# publisher.send_string('test')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user