multi-user/net_operators.py

211 lines
4.1 KiB
Python
Raw Normal View History

import bpy
from . import net_components
2019-02-11 22:48:07 +08:00
import time
2019-02-12 00:02:25 +08:00
import logging
logger = logging.getLogger(__name__)
session = None
2019-02-11 22:48:07 +08:00
client = None
server = None
context = None
2019-02-11 22:48:07 +08:00
# SESSION Operators
2019-02-09 01:34:10 +08:00
2019-02-12 00:02:25 +08:00
class join(bpy.types.Operator):
bl_idname = "session.join"
2019-02-09 01:34:10 +08:00
bl_label = "join"
bl_description = "Connect to a net session"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
2019-02-09 01:34:10 +08:00
# global session
2019-02-09 01:34:10 +08:00
if session.join():
bpy.ops.asyncio.loop()
else:
print('fail to create session, avorting loop')
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class create(bpy.types.Operator):
bl_idname = "session.create"
bl_label = "create"
bl_description = "create to a net session"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global session
2019-02-09 01:34:10 +08:00
if session.create():
bpy.ops.asyncio.loop()
else:
print('fail to create session, avorting loop')
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class send(bpy.types.Operator):
bl_idname = "session.send"
2019-02-09 01:34:10 +08:00
bl_label = "Send"
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
2019-02-09 01:34:10 +08:00
session.send(self.message)
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
class close(bpy.types.Operator):
bl_idname = "session.close"
2019-02-09 01:34:10 +08:00
bl_label = "Close 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.close()
2019-02-09 01:34:10 +08:00
bpy.ops.asyncio.stop()
return {"FINISHED"}
2019-02-12 00:02:25 +08:00
# CLIENT-SERVER
2019-02-11 22:48:07 +08:00
2019-02-12 00:31:03 +08:00
class session_join(bpy.types.Operator):
bl_idname = "session.join"
bl_label = "join"
2019-02-11 22:48:07 +08:00
bl_description = "connect to a net server"
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global client
client = net_components.Client()
time.sleep(1)
bpy.ops.asyncio.loop()
return {"FINISHED"}
2019-02-12 00:02:25 +08:00
2019-02-12 00:31:03 +08:00
class session_send(bpy.types.Operator):
bl_idname = "session.send"
bl_label = "send"
bl_description = "broadcast a message to connected clients"
2019-02-12 00:02:25 +08:00
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"}
2019-02-12 00:31:03 +08:00
class session_create(bpy.types.Operator):
bl_idname = "session.create"
bl_label = "create"
bl_description = "create to a net session"
2019-02-11 22:48:07 +08:00
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global server
2019-02-12 00:02:25 +08:00
global client
2019-02-11 22:48:07 +08:00
server = net_components.Server()
2019-02-12 00:02:25 +08:00
client = net_components.Client()
2019-02-11 22:48:07 +08:00
time.sleep(1)
2019-02-12 00:02:25 +08:00
2019-02-11 22:48:07 +08:00
bpy.ops.asyncio.loop()
2019-02-12 00:02:25 +08:00
return {"FINISHED"}
2019-02-12 00:31:03 +08:00
class session_stop(bpy.types.Operator):
bl_idname = "session.stop"
bl_label = "close"
bl_description = "stop net service"
2019-02-12 00:02:25 +08:00
bl_options = {"REGISTER"}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
global server
global client
2019-02-11 22:48:07 +08:00
2019-02-12 00:31:03 +08:00
if server :
2019-02-12 00:02:25 +08:00
server.stop()
2019-02-12 00:49:28 +08:00
del server
server = None
2019-02-12 00:31:03 +08:00
if client:
client.stop()
2019-02-12 00:49:28 +08:00
del client
client = None
2019-02-12 00:02:25 +08:00
bpy.ops.asyncio.stop()
else:
2019-02-12 00:31:03 +08:00
logger.info("No server/client running.")
2019-02-11 22:48:07 +08:00
return {"FINISHED"}
2019-02-09 01:34:10 +08:00
2019-02-12 00:02:25 +08:00
classes = (
2019-02-12 00:31:03 +08:00
session_join,
session_send,
session_stop,
session_create,
)
2019-02-09 01:34:10 +08:00
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
2019-02-09 01:34:10 +08:00
register()