Skip to content

Commit

Permalink
Merge branch 'lnoel' into 'master'
Browse files Browse the repository at this point in the history
Refactor, logging, server side improvements

See merge request motion-pictures/blender/dccsync!3
  • Loading branch information
Celeborn2BeAlive committed Feb 13, 2020
2 parents f62e17d + 2f04c67 commit 9624352
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 287 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ $RECYCLE.BIN/
*.zip
*.7z
*.rar

# Virtual envs
.venv

# VS Code
.vscode
54 changes: 0 additions & 54 deletions VRtistPanel.py

This file was deleted.

69 changes: 33 additions & 36 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,58 @@
from . import ui
from . import operators
from . import data
import bpy
import atexit
import logging

bl_info = {
"name" : "VRtist",
"author" : "Ubisoft",
"description" : "VR manipultation",
"blender" : (2 ,80, 0),
"location" : "",
"warning" : "",
"category" : "Generic"
"name": "VRtist",
"author": "Ubisoft",
"description": "VR manipultation",
"blender": (2, 80, 0),
"location": "",
"warning": "",
"category": "Generic"
}

from . import vrtistOperators
from . import VRtistPanel

def refreshRoomListHack():
bpy.ops.scene.vrtistroomlistupdate()
bpy.ops.dcc_sync.update_room_list()
return None


def cleanup():
try:
if vrtistOperators.shareData.localServerProcess:
vrtistOperators.shareData.localServerProcess.kill()
if operators.shareData.localServerProcess:
operators.shareData.localServerProcess.kill()
except Exception:
pass

def register():

bpy.utils.register_class(vrtistOperators.ROOM_UL_ItemRenderer)
bpy.utils.register_class(vrtistOperators.VRtistOperator)
bpy.utils.register_class(vrtistOperators.VRtistRoomItem)
bpy.utils.register_class(vrtistOperators.VRtistConnectProperties)
bpy.utils.register_class(vrtistOperators.VRtistCreateRoomOperator)
bpy.utils.register_class(vrtistOperators.VRtistRoomListUpdateOperator)
bpy.utils.register_class(vrtistOperators.VRtistSendSelectionOperator)
bpy.utils.register_class(vrtistOperators.VRtistJoinRoomOperator)
bpy.types.Scene.vrtistconnect = bpy.props.PointerProperty(type=vrtistOperators.VRtistConnectProperties)
bpy.utils.register_class(VRtistPanel.VRtistPanel)
def register():
logger = logging.getLogger(__package__)
if len(logger.handlers) == 0:
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

operators.register()
ui.register()
data.register()

bpy.app.timers.register(refreshRoomListHack, first_interval=0)
atexit.register(cleanup)



def unregister():
bpy.utils.unregister_class(VRtistPanel.VRtistPanel)
del bpy.types.Scene.vrtistconnect
bpy.utils.unregister_class(vrtistOperators.VRtistSendSelectionOperator)
bpy.utils.unregister_class(vrtistOperators.VRtistConnectProperties)
bpy.utils.unregister_class(vrtistOperators.VRtistCreateRoomOperator)
bpy.utils.unregister_class(vrtistOperators.VRtistRoomListUpdateOperator)
bpy.utils.unregister_class(vrtistOperators.VRtistJoinRoomOperator)
bpy.utils.unregister_class(vrtistOperators.VRtistRoomItem)
bpy.utils.unregister_class(vrtistOperators.VRtistOperator)
bpy.utils.unregister_class(vrtistOperators.ROOM_UL_ItemRenderer)
operators.unregister()
ui.unregister()
data.unregister()

cleanup()
atexit.unregister(cleanup)



if __name__ == "__main__":
register()

118 changes: 69 additions & 49 deletions cli.py → broadcaster/cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import time
import queue
import argparse
import logging

import broadcaster.common as common
import broadcaster.client as client
import common
import client

logging.basicConfig(
format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)


TIMEOUT = 10 # in seconds


class ServerError(RuntimeError):
def __init__(self, message):
super().__init__(message)


class CliClient(client.Client):
def __init__(self, args):
super().__init__(args.host, args.port)
Expand Down Expand Up @@ -36,24 +45,25 @@ def clearRoom(self, name):

def listRoomClients(self, name):
def _printResult(command: common.Command):
clients, _ = common.decodeStringArray(command.data, 0)
clients, _ = common.decodeJson(command.data, 0)
if len(clients) == 0:
print(f'No clients in "{name}" room')
else:
print(f'{len(clients)} client(s) in "{name}" room:\n - ', end='')
print('\n - '.join(clients))

print(f'{len(clients)} client(s) in "{name}" room :')
for c in clients:
print(f' - {c["ip"]}:{c["port"]} name = {c["name"]}')
command = common.Command(common.MessageType.LIST_ROOM_CLIENTS, name.encode())
self.processCommand(command, _printResult)

def listClients(self):
def _printResult(command: common.Command):
clients, _ = common.decodeStringArray(command.data, 0)
clients, _ = common.decodeJson(command.data, 0)
if len(clients) == 0:
print('No clients')
else:
print(f'{len(clients)} client(s):\n - ', end='')
print('\n - '.join(clients))
print(f'{len(clients)} client(s):')
for c in clients:
print(f' - {c["ip"]}:{c["port"]} name = {c["name"]} room = {c["room"]}')

command = common.Command(common.MessageType.LIST_CLIENTS)
self.processCommand(command, _printResult)
Expand All @@ -67,6 +77,8 @@ def processCommand(self, command: common.Command, callback=None):
if callback is not None:
command = self.consume()
if command:
if command.type == common.MessageType.SEND_ERROR:
raise ServerError(common.decodeString(command.data, 0)[0])
callback(command)

def consume(self):
Expand All @@ -82,61 +94,69 @@ def consume(self):
def process_room_command(args):
client = None

if args.command == 'list':
client = CliClient(args)
client.listRooms()

elif args.command == 'delete':
count = len(args.name)
if count:
client = CliClient(args)
for name in args.name:
client.deleteRoom(name)
else:
print('Expected one or more room names')

elif args.command == 'clear':
count = len(args.name)
if count:
try:
if args.command == 'list':
client = CliClient(args)
for name in args.name:
client.clearRoom(name)
else:
print('Expected one or more room names')

elif args.command == 'clients':
count = len(args.name)
if count:
client = CliClient(args)
for name in args.name:
client.listRoomClients(name)
else:
print('Expected one or more room names')
client.listRooms()

elif args.command == 'delete':
count = len(args.name)
if count:
client = CliClient(args)
for name in args.name:
client.deleteRoom(name)
else:
print('Expected one or more room names')

elif args.command == 'clear':
count = len(args.name)
if count:
client = CliClient(args)
for name in args.name:
client.clearRoom(name)
else:
print('Expected one or more room names')

elif args.command == 'clients':
count = len(args.name)
if count:
client = CliClient(args)
for name in args.name:
client.listRoomClients(name)
else:
print('Expected one or more room names')
except ServerError as e:
logging.error(e)
finally:
if client:
client.disconnect()

if client:
client.disconnect()

def process_client_command(args):
client = None

if args.command == 'list':
client = CliClient(args)
client.listClients()

if client is not None:
client.disconnect()
try:
if args.command == 'list':
client = CliClient(args)
client.listClients()
except ServerError as e:
logging.error(e)
finally:
if client is not None:
client.disconnect()


parser = argparse.ArgumentParser(prog='cli', description='Command Line Interface for VRtist server')
sub_parsers = parser.add_subparsers()

parser.add_argument('--host', help='Host name', default=client.HOST)
parser.add_argument('--port', help='Port', default=client.PORT)
parser.add_argument('--host', help='Host name', default=common.DEFAULT_HOST)
parser.add_argument('--port', help='Port', default=common.DEFAULT_PORT)
parser.add_argument('--timeout', help='Timeout for server response', default=TIMEOUT)

# Room commands are relative to... a room!
room_parser = sub_parsers.add_parser('room', help='Rooms related commands')
room_parser.add_argument('command', help='Commands. Use "list" to list all the rooms of the server. Use "delete" to delete one or more rooms. Use "clear" to clear the commands stack of rooms. Use "clients" to list the clients connected to rooms.', choices=('list', 'delete', 'clear', 'clients'))
room_parser.add_argument('command', help='Commands. Use "list" to list all the rooms of the server. Use "delete" to delete one or more rooms. Use "clear" to clear the commands stack of rooms. Use "clients" to list the clients connected to rooms.', choices=(
'list', 'delete', 'clear', 'clients'))
room_parser.add_argument('name', help='Room name. You can specify multiple room names separated by spaces.', nargs='*')
room_parser.set_defaults(func=process_room_command)

Expand Down
9 changes: 3 additions & 6 deletions broadcaster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
except ImportError:
import common

HOST = "localhost"
PORT = 12800

class Client:
def __init__(self, host = HOST, port = PORT):
def __init__(self, host=common.DEFAULT_HOST, port=common.DEFAULT_PORT):
self.host = host
self.port = port
self.receivedCommands = queue.Queue()
Expand All @@ -26,7 +24,7 @@ def __init__(self, host = HOST, port = PORT):
self.socket.connect((host, port))
print(f"Connection on port {port}")
except Exception as e:
print("Connection error ",e)
print("Connection error ", e)
self.socket = None

if self.socket:
Expand Down Expand Up @@ -59,7 +57,7 @@ def addCommand(self, command):
self.pendingCommands.put(command)

def joinRoom(self, roomName):
common.writeMessage(self.socket,common.Command(common.MessageType.JOIN_ROOM, roomName.encode('utf8'), 0) )
common.writeMessage(self.socket, common.Command(common.MessageType.JOIN_ROOM, roomName.encode('utf8'), 0))

def send(self, data):
with common.mutex:
Expand Down Expand Up @@ -115,4 +113,3 @@ def blenderExists(self):
client.addCommand(common.Command(common.MessageType.DELETE, encodedMsg[6:]))
elif msg.startswith("Room"):
client.joinRoom(msg[4:])

Loading

0 comments on commit 9624352

Please sign in to comment.