def test_terminate_server(): osc = OSCThreadServer() assert not osc.join_server(timeout=0.1) assert osc._thread.is_alive() osc.terminate_server() assert osc.join_server(timeout=0.1) assert not osc._thread.is_alive()
def test_intercept_errors(caplog): cont = [] def success(*values): cont.append(True) def broken_callback(*values): raise ValueError("some bad value") osc = OSCThreadServer() sock = osc.listen() port = sock.getsockname()[1] osc.bind(b'/broken_callback', broken_callback, sock) osc.bind(b'/success', success, sock) send_message(b'/broken_callback', [b'test'], 'localhost', port) sleep(0.01) send_message(b'/success', [b'test'], 'localhost', port) assert not osc.join_server(timeout=0.02) # Thread not stopped assert cont == [True] assert len(caplog.records) == 1, caplog.records record = caplog.records[0] assert record.msg == "Unhandled exception caught in oscpy server" assert not record.args assert record.exc_info osc = OSCThreadServer(intercept_errors=False) sock = osc.listen() port = sock.getsockname()[1] osc.bind(b'/broken_callback', broken_callback, sock) send_message(b'/broken_callback', [b'test'], 'localhost', port) assert osc.join_server( timeout=0.02) # Thread properly sets termination event on crash assert len(caplog.records) == 1, caplog.records # Unchanged
class OSC_OT_OSCPyServer(OSC_OT_OSCServer): bl_idname = "nodeosc.oscpy_operator" bl_label = "OSCMainThread" _timer = None count = 0 ##################################### # CUSTOMIZEABLE FUNCTIONS: inputServer = "" #for the receiving socket outputServer = "" #for the sending socket # setup the sending server def setupInputServer(self, context, envars): self.dispatcher = dispatcher.Dispatcher() # setup the receiving server def setupOutputServer(self, context, envars): #For sending self.outputServer = OSCClient(envars.udp_out, envars.port_out) self.outputServer.send_message(b'/NodeOSC', [b'Python server started up']) print("OSCPy Server sended test message to " + envars.udp_out + " on port " + str(envars.port_out)) def sendingOSC(self, context, event): oscMessage = {} # gather all the ouput bound osc messages make_osc_messages(bpy.context.scene.NodeOSC_outputs, oscMessage) # and send them for key, args in oscMessage.items(): values = [] if isinstance(args, (tuple, list)): for argum in args: if type(argum) == str: argum = bytes(argum, encoding='utf-8') values.append(argum) else: if type(args) == str: args = bytes(args, encoding='utf-8') values.append(args) self.outputServer.send_message(bytes(key, encoding='utf-8'), values) # add method def addMethod(self, address, data): pass #already set during creation of inputserver # add default method def addDefaultMethod(self): pass #already set during creation of inputserver # start receiving def startupInputServer(self, context, envars): print("Create OscPy Thread...") # creating a blocking UDP Server # Each message will be handled sequentially on the same thread. self.inputServer = OSCThreadServer(encoding='utf8', default_handler=OSC_callback_oscpy) sock = self.inputServer.listen(address=envars.udp_in, port=envars.port_in, default=True) print("... server started on ", envars.port_in) # stop receiving def shutDownInputServer(self, context, envars): print("OSCPy Server is shutting down...") self.inputServer.stop() # Stop default socket print(" stopping all sockets...") self.inputServer.stop_all() # Stop all sockets print(" terminating server...") self.inputServer.terminate_server() # Request the handler thread to stop looping self.inputServer.join_server() # Wait for the handler thread to finish pending tasks and exit print("... OSCPy Server is shutdown")