Cleanup, server/client are now working
This commit is contained in:
parent
048411308e
commit
6114b5070f
@ -91,39 +91,6 @@ class Session():
|
||||
self.is_running = False
|
||||
|
||||
|
||||
class Client_poller():
|
||||
def __init__(self, id):
|
||||
|
||||
self.id = id
|
||||
self.listen = asyncio.ensure_future(self.listen())
|
||||
logger.info("client initiated {}".format(self.id))
|
||||
|
||||
async def listen(self):
|
||||
context = zmq.Context()
|
||||
logger.info("...context initiated {}".format(self.id))
|
||||
socket = context.socket(zmq.DEALER)
|
||||
identity = self.id
|
||||
socket.identity = identity.encode('ascii')
|
||||
logger.info("...socket initiated {}".format(self.id))
|
||||
logger.info("client {} started".format(self.id))
|
||||
poll = zmq.Poller()
|
||||
poll.register(socket, zmq.POLLIN)
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(0.016)
|
||||
sockets = dict(poll.poll(1))
|
||||
|
||||
if socket in sockets:
|
||||
msg = socket.recv(zmq.NOBLOCK)
|
||||
logger.info("{} received:{}".format(self.id, msg))
|
||||
|
||||
def stop(self):
|
||||
logger.info("Stopping client {}".format(self.id))
|
||||
self.listen.cancel()
|
||||
|
||||
|
||||
class Client():
|
||||
def __init__(self, context=zmq.Context(), id="default"):
|
||||
|
||||
@ -169,10 +136,10 @@ class Client():
|
||||
|
||||
if self.pull_sock in socks:
|
||||
message = self.pull_sock.recv_multipart(zmq.NOBLOCK)
|
||||
print(message)
|
||||
logger.info("{}:{}".format(message[0].decode('ascii'), umsgpack.unpackb(message[1])))
|
||||
|
||||
def send_msg(self, msg):
|
||||
self.push_sock.send_multipart(msg.encode())
|
||||
self.push_sock.send(umsgpack.packb(msg))
|
||||
|
||||
def stop(self):
|
||||
logger.info("Stopping client")
|
||||
@ -224,7 +191,7 @@ class Server():
|
||||
|
||||
if self.pull_sock in socks:
|
||||
msg = self.pull_sock.recv_multipart(zmq.NOBLOCK)
|
||||
print("{}:{}".format(msg[0].decode('ascii'), msg[1].decode()))
|
||||
#print("{}:{}".format(msg[0].decode('ascii'), umsgpack.packb(msg[1])))
|
||||
|
||||
# Update all clients
|
||||
self.pub_sock.send_multipart(msg)
|
||||
|
@ -1,6 +1,9 @@
|
||||
import bpy
|
||||
from . import net_components
|
||||
import time
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
session = None
|
||||
client = None
|
||||
@ -9,6 +12,7 @@ context = None
|
||||
|
||||
# SESSION Operators
|
||||
|
||||
|
||||
class join(bpy.types.Operator):
|
||||
bl_idname = "session.join"
|
||||
bl_label = "join"
|
||||
@ -88,6 +92,7 @@ class close(bpy.types.Operator):
|
||||
|
||||
# CLIENT-SERVER
|
||||
|
||||
|
||||
class client_connect(bpy.types.Operator):
|
||||
bl_idname = "client.connect"
|
||||
bl_label = "connect"
|
||||
@ -107,10 +112,51 @@ class client_connect(bpy.types.Operator):
|
||||
|
||||
bpy.ops.asyncio.loop()
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
|
||||
class client_send(bpy.types.Operator):
|
||||
bl_idname = "client.send"
|
||||
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.send_msg(self.message)
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
class server_connect(bpy.types.Operator):
|
||||
|
||||
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"
|
||||
@ -122,23 +168,53 @@ class server_connect(bpy.types.Operator):
|
||||
|
||||
def execute(self, context):
|
||||
global server
|
||||
global client
|
||||
|
||||
server = net_components.Server()
|
||||
client = net_components.Client()
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
bpy.ops.asyncio.loop()
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
|
||||
class server_stop(bpy.types.Operator):
|
||||
bl_idname = "server.stop"
|
||||
bl_label = "connect"
|
||||
bl_description = "connect to a net server"
|
||||
bl_options = {"REGISTER"}
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return True
|
||||
|
||||
def execute(self, context):
|
||||
global server
|
||||
global client
|
||||
|
||||
if server and client:
|
||||
client.stop()
|
||||
server.stop()
|
||||
|
||||
bpy.ops.asyncio.stop()
|
||||
else:
|
||||
logger.info("Server is not running")
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
|
||||
classes = (
|
||||
join,
|
||||
create,
|
||||
close,
|
||||
send,
|
||||
# join,
|
||||
# create,
|
||||
# close,
|
||||
# send,
|
||||
client_connect,
|
||||
server_connect,
|
||||
client_send,
|
||||
client_stop,
|
||||
server_run,
|
||||
server_stop,
|
||||
)
|
||||
|
||||
|
||||
@ -146,7 +222,6 @@ def register():
|
||||
global session
|
||||
# session = net_components.Session()
|
||||
|
||||
|
||||
from bpy.utils import register_class
|
||||
for cls in classes:
|
||||
register_class(cls)
|
||||
|
Loading…
Reference in New Issue
Block a user