-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbvrconsole.py
More file actions
277 lines (235 loc) · 10.9 KB
/
bvrconsole.py
File metadata and controls
277 lines (235 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
# file: bvr/bvrconsole.py
## Copyright (C) LIMSI-CNRS (2016)
##
## contributor(s) : Jorge Gascon, Damien Touraine, David Poirier-Quinot,
## Laurent Pointal, Julian Adenauer,
##
## This software is a computer program whose purpose is to distribute
## blender to render on Virtual Reality device systems.
##
## This software is governed by the CeCILL license under French law and
## abiding by the rules of distribution of free software. You can use,
## modify and/ or redistribute the software under the terms of the CeCILL
## license as circulated by CEA, CNRS and INRIA at the following URL
## "http://www.cecill.info".
##
## As a counterpart to the access to the source code and rights to copy,
## modify and redistribute granted by the license, users are provided only
## with a limited warranty and the software's author, the holder of the
## economic rights, and the successive licensors have only limited
## liability.
##
## In this respect, the user's attention is drawn to the risks associated
## with loading, using, modifying and/or developing or reproducing the
## software by the user in light of its specific status of free software,
## that may mean that it is complicated to manipulate, and that also
## therefore means that it is reserved for developers and experienced
## professionals having in-depth computer knowledge. Users are therefore
## encouraged to load and test the software's suitability as regards their
## requirements in conditions enabling the security of their systems and/or
## data to be ensured and, more generally, to use and operate it in the
## same conditions as regards security.
##
## The fact that you are presently reading this means that you have had
## knowledge of the CeCILL license and that you accept its terms.
# <pep8 compliant>
"""Management of BlenderVR Console functionnalities without GUI.
The tools here provide possible callbacks for some GUI interaction,
but
"""
import os
import sys
from os import path as osp
import functools
import builtins # Important: we modify builtins to add our stuff!
import socket
import select
from collections import namedtuple
import logging
logger = logging.getLogger(__name__)
from . import (
RUNTIME,
bvrenv, # Import first ! it setup our execution environment.
)
# Note: as bvr is imported, normally package import of blender DONT start
# their main() functions - this may
from blendervr.tools import connector
from blendervr.tools import protocol
from blendervr.console.base import ConsoleBase
from blendervr.console.logic.console import ConsoleLogic
from blendervr.console import profile
from blendervr.tools import logger as blendervr_logger
from blendervr.console.logic.screens import ConsoleScreensLogic
from blendervr.plugins import getPlugins
from blendervr import plugins # Needed by xml parsing code.
# To debug this module.
DEBUG = True and not RUNTIME
# Communication protocol transmit the message length in a header of this size:
SIZE_LEN = connector.Common.SIZE_LEN
# And the buffer for read is defined here:
BUFFER_LEN = connector.Common.BUFFER_LEN
# For blendervr usage of Configure's parent when it is a module…
_logger = logging.getLogger("BlenderVR")
# For blendervr usage of _profile parent when it is a module…
# Late bind in BVRConsoleControler construction.
_profile = None
# Storage of items for socket listen/read management.
SocketCallback = namedtuple('SocketCallback', "socket_, callback, data")
# Storage of pending data when reading messages.
PendingRead = namedtuple('PendingRead', "remain_size, data")
# This module is based on blendervr.console.console module, adapted to
# our blender tool context.
# In our usage, it is the _main_running_module for bendervr objects.
# getConsole() and getMainRunningModule() return this module.
class BVRConsoleControler(ConsoleBase, ConsoleLogic):
"""Interface to logic part of console code.
:ivar socket_listeners: filenos of sockets to listen.
:type socket_listeners: [int]
:ivar listeners_callbacks: map of fileno to socket management items.
:type listeners_callbacks: [int: SocketCallback]
"""
def __init__(self, profile_file):
global _profile
logger.info("Creating BVRConsoleControler with profile %s", profile_file)
self.socket_listeners = []
self.listeners_callbacks = {}
self.pending_read = {}
# Following attributes are managed by ConsoleLogic:
# _possibleScreenSets = None
# _anchor = None
# _previous_state = None
# _common_processors = []
# TODO: some attributes are not set in the constructor but
# in load_configuration_file() method, so they may NOT be set if
# there is an error in the
# And are "automagically" used by GUI.
self._blender_file = None
self._loader_file = None
self._processor_files = None
self._processor = None
self._update_loader_script = "/".join((BlenderVR_root, 'utils',
'update_loader.py'))
self._profile = profile.Profile(profile_file)
# Bind to module global for blendervr to find it.
_profile = self._profile
self._logger = blendervr_logger.getLogger('BlenderVR')
# In the blendervr class system, the parent can be another object
# or a module.
# Note: this is the object returned as
parent = sys.modules[__name__]
ConsoleBase.__init__(self, parent)
ConsoleLogic.__init__(self)
self._screens = ConsoleScreensLogic()
self._plugins = getPlugins(self, self._logger)
self.profile.setDefault({'config': {'file': '',
'path': []},
'files': {'blender': '',
'processor': '',
'link': True},
'screens': {'display': False},
'window': {'geometry': [0, 0, 0, 0]},
'processor': {'toggle': True}})
@property
def profile(self):
return self._profile
@property
def logger(self):
return self._logger
@property
def plugins(self):
return self._plugins
#def getConsole(self):
# return sys.modules['blendervr.console.plugins']
def display_screen_sets(self, possibleScreenSets):
# TODO: feed current_screens in bvrprops
print("possibleScreenSets:", repr(possibleScreenSets))
pass
# ==================== ADAPTED GUI METHODS ========================
# Method called from logic to activate GUI code.
def addListenTo(self, socket_, callback, data=None):
"""Add a socket listener to notify a callable when some data is ready to read.
:param socket_: network socket object to monitor for reading.
:type socket_: socket.Socket
:return: a tag data used to remove socket monitoring (the socket fileno).
:rtype: int
"""
# Note: blendervr/console/logic/screen.py has been modified to transmit
# a Socket object and not its fileno in case of usage with bvr package.
# TODO: the "socket_" can be a blendervr.tools.connector.Server,
# check if we need to test for this case and deal with it.
# Store socket and memorize its callback.
socknum = socket_.fileno()
if socknum not in self.socket_listeners:
self.socket_listeners.append(socknum)
else:
raise RuntimeError("Socket used more than once")
self.listeners_callbacks[socknum] = SocketCallback(socket_, callback, data)
return socknum # Enough to manage late removing
def removeListenTo(self, tag):
"""Remove a socket listener.
"""
if tag not in self.listeners_callbacks:
self.logger.error("Unknown fileno %r to un-listen socket.", tag)
return
sc = self.listeners_callbacks[tag]
del self.listeners_callbacks[tag]
self.socket_listeners.remove(sc.socket_.fileno())
# ==================== NETWORK SOCKET MONITORING ========================
def nonblocking_read(self):
"""Management of sockets read and message processing in an event loop context.
"""
#TODO: Move all possible code to a background working thread, and just manage
# communication of some events between that thread and the blender event loop.
# May have one thread per socket to listen (and work with blocking select).
if not self.socket_listeners:
#self.logger.debug("No socket active")
return
# As we work in a blender event loop, dont block on sockets (timeout=0)
rready, _, _ = select.select(
self.socket_listeners,
[],
[],
0)
if not rready:
#self.logger.debug("No socket ready")
return
for socknum in rready:
self.logger.debug("Socket operations…")
rawdata = None # In case of error.
cb = self.listeners_callbacks[socknum]
# Detect if we are beginning to read a message or reading next
# parts of a previous message read.
begin_message = socknum not in self.pending_read
if begin_message:
readsize = SIZE_LEN
else:
readsize = self.pending_read[socknum].remain_size
rawdata = cb.socket_.recv(readsize)
if begin_message:
# Retrieved message size.
if readsize == len(rawdata):
messagesize = int(rawdata)
self.pending_read[socknum] = PendingRead(messagesize, "")
else:
# We fail to retrieve the message length!
# What to do ???? Maybe we are unsynchronized on some
# datagram.
# We retrieve pending data, hoping to cleanup the socket.
cb.socket_.recv(BUFFER_LEN)
pass
else:
# Retrieve data (or part of data).
remain_size = self.pending_read[socknum].remain_size - len(rawdata)
rawdata = self.pending_read[socknum].data + rawdata
if remain_size > 0:
self.pending_read[socknum] = PendingRead(remain_size, "")
else:
# Have a complete message, process it.
del self.pending_read[socknum]
try:
message_parts = protocol.decomposeMessage(rawdata)
self.logger.debug("Received message %r", message_parts)
cd.callback(*message_parts)
except:
self.logger.exception("Exception in message processing %r", rawdata)