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

View File

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

View File

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