From c86953eb7514607f4b0fde8d1740bb83ed5d66af Mon Sep 17 00:00:00 2001 From: Swann Date: Fri, 8 Feb 2019 15:44:02 +0100 Subject: [PATCH] added a small for fast testing, cleanup --- __init__.py | 11 +++++- net_components.py | 62 +++++++++++++++++++++++++++++-- net_components_ui.py | 0 net_operators.py | 87 ++++++++++++++++++++++++++++++++++++++++++++ net_ui.py | 42 +++++++++++++++++++++ 5 files changed, 197 insertions(+), 5 deletions(-) delete mode 100644 net_components_ui.py create mode 100644 net_operators.py create mode 100644 net_ui.py diff --git a/__init__.py b/__init__.py index 04175b4..c9cb4d2 100644 --- a/__init__.py +++ b/__init__.py @@ -22,9 +22,18 @@ bl_info = { } from .libs.bsyncio import bsyncio +from . import net_operators +from . import net_ui +import bpy def register(): + bpy.types.Scene.message = bpy.props.StringProperty(default="Hi") bsyncio.register() + net_operators.register() + net_ui.register() def unregister(): - bsyncio.unregister() \ No newline at end of file + del bpy.types.Scene.message + bsyncio.unregister() + net_operators.unregister() + net_ui.unregister() \ No newline at end of file diff --git a/net_components.py b/net_components.py index 39df3f0..728917a 100644 --- a/net_components.py +++ b/net_components.py @@ -1,6 +1,60 @@ -PORT = 5555 -HOST = '127.0.0.1' +import zmq +import asyncio +import logging + + +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.DEBUG) + class Session(): - __init__(self,args,kwargs): - self. \ No newline at end of file + def __init__(self, host='127.0.0.1', port=5555, is_hosting=False): + self.host = host + self.port = port + + # init zmq context + self.context = zmq.Context() + + # init socket interface + if is_hosting: + self.socket = self.context.socket(zmq.REP) + self.socket.bind("tcp://*:5555") + else: + self.socket = self.context.socket(zmq.REQ) + self.socket.connect("tcp://127.0.0.1:5555") + + self.listen = asyncio.ensure_future(self.listen()) + self.msg = [] + + def is_running(self): + try: + return not self.listen.done + except: + return False + + # TODO: Add a kill signal to destroy clients session + # TODO: Add a join method + # TODO: Add a create session method + + async def listen(self): + logger.info("Listening on {}:{}".format(self.host, self.port)) + + while True: + # Ungly blender workaround to prevent blocking... + await asyncio.sleep(0.016) + try: + message = self.socket.recv_multipart(zmq.NOBLOCK) + self.msg.append(message) + logger.info(message) + except zmq.ZMQError: + pass + + def send(self, msg): + logger.info("Sending {} to {}:{} ".format(msg, self.host, self.port)) + + self.socket.send(b"msg") + + def close(self): + logger.info("Closing session") + self.listen.cancel() + diff --git a/net_components_ui.py b/net_components_ui.py deleted file mode 100644 index e69de29..0000000 diff --git a/net_operators.py b/net_operators.py new file mode 100644 index 0000000..f2d8758 --- /dev/null +++ b/net_operators.py @@ -0,0 +1,87 @@ +import bpy +from . import net_components + +session = None + +class join(bpy.types.Operator): + bl_idname = "session.join" + bl_label = "connect to net session" + bl_description = "Connect to a net session" + bl_options = {"REGISTER"} + + + @classmethod + def poll(cls, context): + return True + + def execute(self, context): + global session + + session = net_components.Session() + bpy.ops.asyncio.loop() + return {"FINISHED"} + +class host(bpy.types.Operator): + bl_idname = "session.host" + bl_label = "host a net session" + bl_description = "Connect to a net session" + bl_options = {"REGISTER"} + + + @classmethod + def poll(cls, context): + return True + + def execute(self, context): + global session + + session = net_components.Session(is_hosting=True) + bpy.ops.asyncio.loop() + return {"FINISHED"} + +class send(bpy.types.Operator): + bl_idname = "session.send" + bl_label = "Send a message throught the network" + bl_description = "Connect to a net session" + bl_options = {"REGISTER"} + + message: bpy.props.StringProperty(default="Hi") + + @classmethod + def poll(cls, context): + return True + + def execute(self, context): + global session + + session.send(b"") + return {"FINISHED"} + +class close(bpy.types.Operator): + bl_idname = "session.close" + bl_label = "Send a message throught the network" + bl_description = "Connect to a net session" + bl_options = {"REGISTER"} + + @classmethod + def poll(cls, context): + return True + + def execute(self, context): + global session + + bpy.ops.asyncio.stop() + session.close() + + return {"FINISHED"} + +classes = ( + join, + host, + send +) + +register, unregister = bpy.utils.register_classes_factory(classes) + +if __name__ == "__main__": + register() \ No newline at end of file diff --git a/net_ui.py b/net_ui.py new file mode 100644 index 0000000..b6a3c36 --- /dev/null +++ b/net_ui.py @@ -0,0 +1,42 @@ +import bpy +from . import net_components +from . import net_operators + +class SessionPanel(bpy.types.Panel): + """Creates a Panel in the scene context of the properties editor""" + bl_label = "Net Session" + bl_idname = "SCENE_PT_layout" + bl_space_type = 'PROPERTIES' + bl_region_type = 'WINDOW' + bl_context = "scene" + + # def draw_header(self, context): + # self.layout.prop(context.scene, "use_gravity", text="") + + def draw(self, context): + layout = self.layout + global session + + scene = context.scene + # Create a simple row. + row = layout.row() + + if not session: + row.operator("session.join") + row.operator("session.host") + else: + row.operator("session.close") + + # row.operator("session.send").message = bpy.scene.message + # row.prop(scene,"message") + + +classes = ( + SessionPanel, +) + + +register, unregister = bpy.utils.register_classes_factory(classes) + +if __name__ == "__main__": + register() \ No newline at end of file