Example #1
0
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()
Example #2
0
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")