added a small for fast testing, cleanup

This commit is contained in:
Swann 2019-02-08 15:44:02 +01:00
parent 6171afe4e6
commit c86953eb75
No known key found for this signature in database
GPG Key ID: B02D0B41F8B6D2EE
5 changed files with 197 additions and 5 deletions

View File

@ -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():
del bpy.types.Scene.message
bsyncio.unregister()
net_operators.unregister()
net_ui.unregister()

View File

@ -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.
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()

View File

87
net_operators.py Normal file
View File

@ -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()

42
net_ui.py Normal file
View File

@ -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()