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-08 15:44:02 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
2019-02-07 18:07:25 +01:00
|
|
|
class Session():
|
2019-02-08 15:44:02 +01:00
|
|
|
def __init__(self, host='127.0.0.1', port=5555, is_hosting=False):
|
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2019-02-08 18:34:10 +01:00
|
|
|
self.is_running = False
|
2019-02-08 15:44:02 +01:00
|
|
|
# init zmq context
|
|
|
|
self.context = zmq.Context()
|
2019-02-08 18:34:10 +01:00
|
|
|
self.socket = None
|
2019-02-08 15:44:02 +01:00
|
|
|
|
|
|
|
self.msg = []
|
2019-02-08 18:34:10 +01:00
|
|
|
|
|
|
|
#self.listen.add_done_callback(self.close_success())
|
2019-02-08 15:44:02 +01:00
|
|
|
|
|
|
|
# TODO: Add a kill signal to destroy clients session
|
|
|
|
# TODO: Add a join method
|
|
|
|
# TODO: Add a create session method
|
2019-02-08 18:34:10 +01:00
|
|
|
def join(self):
|
|
|
|
logger.info("joinning {}:{}".format(self.host, self.port))
|
|
|
|
try:
|
|
|
|
self.socket = self.context.socket(zmq.REQ)
|
|
|
|
self.socket.connect("tcp://localhost:5555")
|
|
|
|
self.listen = asyncio.ensure_future(self.listen())
|
|
|
|
return True
|
|
|
|
|
|
|
|
except zmq.ZMQError:
|
|
|
|
logger.error("Error while joining {}:{}".format(
|
|
|
|
self.host, self.port))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
# TODO: Find better names
|
|
|
|
def create(self):
|
|
|
|
logger.info("Creating session")
|
|
|
|
try:
|
|
|
|
self.socket = self.context.socket(zmq.REP)
|
|
|
|
self.socket.bind("tcp://*:5555")
|
|
|
|
|
|
|
|
self.listen = asyncio.ensure_future(self.listen())
|
|
|
|
return True
|
|
|
|
except zmq.ZMQError:
|
|
|
|
logger.error("Error while creating session: ",zmq.ZMQError)
|
|
|
|
|
|
|
|
return False
|
2019-02-08 15:44:02 +01:00
|
|
|
|
|
|
|
async def listen(self):
|
|
|
|
logger.info("Listening on {}:{}".format(self.host, self.port))
|
|
|
|
|
2019-02-08 18:34:10 +01:00
|
|
|
self.is_running = True
|
2019-02-08 15:44:02 +01:00
|
|
|
while True:
|
|
|
|
# Ungly blender workaround to prevent blocking...
|
|
|
|
await asyncio.sleep(0.016)
|
|
|
|
try:
|
2019-02-08 18:34:10 +01:00
|
|
|
msg = self.socket.recv(zmq.NOBLOCK)
|
|
|
|
# self.msg.append(umsgpack.unpackb(message))
|
|
|
|
print(msg)
|
|
|
|
logger.info(msg)
|
2019-02-08 15:44:02 +01:00
|
|
|
except zmq.ZMQError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def send(self, msg):
|
|
|
|
logger.info("Sending {} to {}:{} ".format(msg, self.host, self.port))
|
2019-02-08 18:34:10 +01:00
|
|
|
bin = umsgpack.packb(msg)
|
|
|
|
self.socket.send(bin,zmq.NOBLOCK)
|
2019-02-08 15:44:02 +01:00
|
|
|
|
2019-02-08 18:34:10 +01:00
|
|
|
async def close_success(self):
|
|
|
|
self.is_running = False
|
2019-02-08 15:44:02 +01:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
logger.info("Closing session")
|
2019-02-08 18:34:10 +01:00
|
|
|
self.socket.close()
|
2019-02-08 15:44:02 +01:00
|
|
|
self.listen.cancel()
|
2019-02-08 18:34:10 +01:00
|
|
|
del self.listen
|
|
|
|
self.is_running = False
|