cleanup, operator and ui refactoring

This commit is contained in:
Swann 2019-02-11 17:31:03 +01:00
parent 6114b5070f
commit 5e6d05ca92
No known key found for this signature in database
GPG Key ID: B02D0B41F8B6D2EE
3 changed files with 37 additions and 61 deletions

View File

@ -91,6 +91,7 @@ class Session():
self.is_running = False
class Client():
def __init__(self, context=zmq.Context(), id="default"):
@ -102,8 +103,10 @@ class Client():
self.id = id
self.bind_ports()
# Main client loop registration
self.is_running = False
self.task = asyncio.ensure_future(self.main())
self.store = []
logger.info("{} client initialized".format(id))
def bind_ports(self):
@ -125,6 +128,7 @@ class Client():
async def main(self):
logger.info("{} client launched".format(id))
self.is_running = True
# Prepare our context and publisher socket
while True:
# TODO: find a better way
@ -137,12 +141,15 @@ class Client():
if self.pull_sock in socks:
message = self.pull_sock.recv_multipart(zmq.NOBLOCK)
logger.info("{}:{}".format(message[0].decode('ascii'), umsgpack.unpackb(message[1])))
# Store message
self.store.append([message[0].decode('ascii'), umsgpack.unpackb(message[1])])
def send_msg(self, msg):
self.push_sock.send(umsgpack.packb(msg))
def stop(self):
logger.info("Stopping client")
self.is_running = False
self.task.cancel()
self.push_sock.close()
self.pull_sock.close()

View File

@ -93,9 +93,9 @@ class close(bpy.types.Operator):
# CLIENT-SERVER
class client_connect(bpy.types.Operator):
bl_idname = "client.connect"
bl_label = "connect"
class session_join(bpy.types.Operator):
bl_idname = "session.join"
bl_label = "join"
bl_description = "connect to a net server"
bl_options = {"REGISTER"}
@ -107,7 +107,6 @@ class client_connect(bpy.types.Operator):
global client
client = net_components.Client()
time.sleep(1)
bpy.ops.asyncio.loop()
@ -115,10 +114,10 @@ class client_connect(bpy.types.Operator):
return {"FINISHED"}
class client_send(bpy.types.Operator):
bl_idname = "client.send"
bl_label = "connect"
bl_description = "connect to a net server"
class session_send(bpy.types.Operator):
bl_idname = "session.send"
bl_label = "send"
bl_description = "broadcast a message to connected clients"
bl_options = {"REGISTER"}
message: bpy.props.StringProperty(default="Hi")
@ -134,32 +133,10 @@ class client_send(bpy.types.Operator):
return {"FINISHED"}
class client_stop(bpy.types.Operator):
bl_idname = "client.stop"
bl_label = "connect"
bl_description = "connect to a net server"
bl_options = {"REGISTER"}
message: bpy.props.StringProperty(default="Hi")
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global client
client.stop()
bpy.ops.asyncio.stop()
return {"FINISHED"}
class server_run(bpy.types.Operator):
bl_idname = "server.run"
bl_label = "connect"
bl_description = "connect to a net server"
class session_create(bpy.types.Operator):
bl_idname = "session.create"
bl_label = "create"
bl_description = "create to a net session"
bl_options = {"REGISTER"}
@classmethod
@ -180,10 +157,10 @@ class server_run(bpy.types.Operator):
return {"FINISHED"}
class server_stop(bpy.types.Operator):
bl_idname = "server.stop"
bl_label = "connect"
bl_description = "connect to a net server"
class session_stop(bpy.types.Operator):
bl_idname = "session.stop"
bl_label = "close"
bl_description = "stop net service"
bl_options = {"REGISTER"}
@classmethod
@ -194,34 +171,26 @@ class server_stop(bpy.types.Operator):
global server
global client
if server and client:
client.stop()
if server :
server.stop()
if client:
client.stop()
bpy.ops.asyncio.stop()
else:
logger.info("Server is not running")
logger.info("No server/client running.")
return {"FINISHED"}
classes = (
# join,
# create,
# close,
# send,
client_connect,
client_send,
client_stop,
server_run,
server_stop,
session_join,
session_send,
session_stop,
session_create,
)
def register():
global session
# session = net_components.Session()
from bpy.utils import register_class
for cls in classes:
register_class(cls)

View File

@ -20,8 +20,8 @@ class SessionPanel(bpy.types.Panel):
# Create a simple row.
row = layout.row()
if net_operators.session.is_running:
row.operator("session.close")
if net_operators.client:
row.operator("session.stop")
row = layout.row()
row = layout.row(align=True)
@ -34,9 +34,9 @@ class SessionPanel(bpy.types.Panel):
row = layout.row()
area_msg = row.box()
if len(net_operators.session.msg) > 0:
for msg in net_operators.session.msg:
area_msg.label(text=str(msg))
if len(net_operators.client.store) > 0:
for (id,msg) in net_operators.client.store:
area_msg.label(text="{}:{}".format(id,msg))
else:
area_msg.label(text="Empty")
else:
@ -48,7 +48,7 @@ class SessionPanel(bpy.types.Panel):
classes = (
# SessionPanel,
SessionPanel,
)