Exemplo n.º 1
0
def main():

    #app=a.app   # ,if necessary
    #win=a.win   # ,if necessary

    global gui
    gui = docked_gui()  # comment 1 line above out, put class defns above

    #QtGui.QApplication.instance().exec_() # this start command needs to be here to enable cursors, and hence this must initialize last

    #pdb.set_trace()
    #time.sleep(3)

    import qtreactor.pyqt4reactor
    qtreactor.pyqt4reactor.install()

    last_connection_time = time.time()
    time_last_check = time.time()
    time_now = time.time()

    from twisted.internet import reactor
    from twisted.internet import task
    from autobahn.twisted.websocket import WebSocketServerProtocol
    from autobahn.twisted.websocket import WebSocketServerFactory
    from autobahn.twisted.websocket import WebSocketClientFactory
    from autobahn.twisted.websocket import WebSocketClientProtocol
    from autobahn.twisted.websocket import connectWS
    from autobahn.twisted.websocket import listenWS
    from twisted.internet.protocol import DatagramProtocol
    import twisted.internet.error
    from twisted.python import log
    log.startLogging(sys.stdout)

    from twisted.internet import stdio
    from twisted.protocols import basic
    from twisted.internet import error

    class Keyboard_Input(basic.LineReceiver):
        """
        Keyboard input protocol - for simultaneous input of python commands
        and browsing of server objects while the server is running
        """
        from os import linesep as delimiter  # doesn't seem to work
        if os.name == 'nt': delimiter = "\n"

        def __init__(self):
            self.pre_e = colorama.Style.BRIGHT + colorama.Back.RED + colorama.Fore.WHITE
            self.post_e = colorama.Fore.RESET + colorama.Back.RESET + colorama.Style.RESET_ALL

        #        self.setRawMode()
        def connectionMade(self):
            pass

        def lineReceived(self, line):
            """
            called when a line of input received - executes and prints result
            or passes error message to console
            """
            rbuffer = StringIO()
            po = sys.stdout
            sys.stdout = rbuffer
            err = False
            try:
                exec line in globals(), locals()
            except Exception as e:
                err = e
            sys.stdout = po
            print '>u> ' + line
            if err:
                out = self.pre_e + str(e) + self.post_e
            else:
                out = rbuffer.getvalue()
            if out != "":
                print '>s> ' + out

    class MyServerProtocol(WebSocketServerProtocol):
        def onConnect(self, request):
            if DEBUG:
                print("class data_guis.MyServerProtocol, func onConnect: {0}".
                      format(request.peer))

        def onOpen(self):
            if DEBUG: print("class data_guis.MyServerProtocol, func onOpen")

        def onMessage(self, payload_, isBinary):
            if DEBUG: print "class data_guis.MyServerProtocol, func onMessage"
            #self.log_message()
            if isBinary:
                payload = msgpack.unpackb(payload_)
                if not payload.has_key('IDLSocket_ResponseFunction'):
                    return None
                try:
                    #ThisResponseFunction = getattr(self.factory.app.command_library,
                    #                           payload['IDLSocket_ResponseFunction'])
                    ThisResponseFunction = getattr(
                        self.factory.command_library,
                        payload['IDLSocket_ResponseFunction'])
                except AttributeError:
                    if DEBUG:
                        print('Missing Socket_ResponseFunction:',
                              payload['IDLSocket_ResponseFunction'])
                ThisResponseFunction(payload)
            else:
                print payload_.keys()

        def onClose(self, wasClean, code, reason):
            if DEBUG:
                print("class data_guis.MyServerProtocol, func onClose: {0}".
                      format(reason))
            server_shutdown()

    def check_for_main_server():
        global time_last_check
        global time_now
        time_last_check = time_now
        time_now = time.time()
        #print time_last_check, time_now, last_connection_time
        if (time_now - last_connection_time) > 1100000 and (
                time_now - time_last_check) < 11:
            server_shutdown()

    def server_shutdown():
        if DEBUG:
            print "----------------Shutting Down DataGuiServer Now!----------------"
        #win.close()
        #app.quit()
        reactor.callLater(0.01, reactor.stop)

    keyboard = Keyboard_Input()
    stdio.StandardIO(keyboard)

    sys.argv.append('localhost')
    sys.argv.append('9100')
    #sys.argv[0] = file name of this script
    # szys.argv[1] = ip address of this server
    # sys.argv[2] = port to listen on
    factory = WebSocketServerFactory("ws://" + 'localhost' + ":" +
                                     str(sys.argv[2]),
                                     debug=False)
    factory.setProtocolOptions(failByDrop=False)
    factory.protocol = MyServerProtocol
    try:
        reactor.listenTCP(int(sys.argv[2]), factory)
        #a.factory = factory
        command_library = CommandLibrary()
        factory.command_library = command_library
        command_library.factory = factory
        factory.gui = gui
    except twisted.internet.error.CannotListenError:
        server_shutdown()

    reactor.run()
def main():

            
        
    #app=a.app   # ,if necessary
    #win=a.win   # ,if necessary
    
    global gui
    gui = docked_gui()  # comment 1 line above out, put class defns above
 
    
    #QtGui.QApplication.instance().exec_() # this start command needs to be here to enable cursors, and hence this must initialize last

    #pdb.set_trace()
    #time.sleep(3)
    
    import qtreactor.pyqt4reactor
    qtreactor.pyqt4reactor.install()
    
    last_connection_time = time.time()
    time_last_check = time.time()   
    time_now = time.time()


    
    from twisted.internet import reactor
    from twisted.internet import task
    from autobahn.twisted.websocket import WebSocketServerProtocol
    from autobahn.twisted.websocket import WebSocketServerFactory
    from autobahn.twisted.websocket import WebSocketClientFactory
    from autobahn.twisted.websocket import WebSocketClientProtocol
    from autobahn.twisted.websocket import connectWS
    from autobahn.twisted.websocket import listenWS
    from twisted.internet.protocol import DatagramProtocol
    import twisted.internet.error
    from twisted.python import log
    log.startLogging(sys.stdout)
   
    from twisted.internet import stdio
    from twisted.protocols import basic
    from twisted.internet import error

    
    class Keyboard_Input(basic.LineReceiver):
        """
        Keyboard input protocol - for simultaneous input of python commands
        and browsing of server objects while the server is running
        """
        from os import linesep as delimiter # doesn't seem to work
        if os.name=='nt': delimiter="\n"
        def __init__(self):
            self.pre_e=colorama.Style.BRIGHT+ colorama.Back.RED+colorama.Fore.WHITE
            self.post_e=colorama.Fore.RESET+colorama.Back.RESET+colorama.Style.RESET_ALL
        #        self.setRawMode()
        def connectionMade(self):
            pass
        def lineReceived(self, line):
            """
            called when a line of input received - executes and prints result
            or passes error message to console
            """
            rbuffer = StringIO()
            po = sys.stdout
            sys.stdout = rbuffer
            err = False
            try:
                exec line in globals(),locals()
            except Exception as e:
                err = e
            sys.stdout = po
            print '>u> ' + line
            if err:
                out = self.pre_e + str(e) + self.post_e
            else:
                out = rbuffer.getvalue()
            if out != "":
                print '>s> ' + out
        
    class MyServerProtocol(WebSocketServerProtocol):
    
        def onConnect(self, request):
            if DEBUG: print("class data_guis.MyServerProtocol, func onConnect: {0}".format(request.peer))
            
        def onOpen(self):
            if DEBUG: print("class data_guis.MyServerProtocol, func onOpen")
            
        def onMessage(self, payload_, isBinary):
            if DEBUG: print "class data_guis.MyServerProtocol, func onMessage"
            #self.log_message()
            if isBinary:
                payload = msgpack.unpackb(payload_)
                if not payload.has_key('IDLSocket_ResponseFunction'):
                    return None
                try:
                    #ThisResponseFunction = getattr(self.factory.app.command_library,
                    #                           payload['IDLSocket_ResponseFunction'])
                    ThisResponseFunction = getattr(self.factory.command_library,
                                               payload['IDLSocket_ResponseFunction'])
                except AttributeError:
                    if DEBUG: print ('Missing Socket_ResponseFunction:',
                                     payload['IDLSocket_ResponseFunction'])
                ThisResponseFunction(payload)
            else:
                print payload_.keys()
            
            
        def onClose(self, wasClean, code, reason):
            if DEBUG: print("class data_guis.MyServerProtocol, func onClose: {0}".format(reason))
            server_shutdown()
    
               
    def check_for_main_server():
        global time_last_check
        global time_now
        time_last_check = time_now
        time_now = time.time()
        #print time_last_check, time_now, last_connection_time
        if (time_now - last_connection_time) > 1100000 and (time_now - time_last_check) < 11:
            server_shutdown()
        
        
    def server_shutdown():
        if DEBUG: print "----------------Shutting Down DataGuiServer Now!----------------"
        #win.close()
        #app.quit()
        reactor.callLater(0.01, reactor.stop)
    
    
    keyboard = Keyboard_Input()
    stdio.StandardIO(keyboard)
    
    sys.argv.append('localhost')
    sys.argv.append('9100')
    #sys.argv[0] = file name of this script
    # szys.argv[1] = ip address of this server
    # sys.argv[2] = port to listen on
    factory = WebSocketServerFactory("ws://" + 'localhost' + ":"+str(sys.argv[2]), debug = False)
    factory.setProtocolOptions(failByDrop=False)
    factory.protocol = MyServerProtocol
    try:
        reactor.listenTCP(int(sys.argv[2]), factory)
        #a.factory = factory
        command_library = CommandLibrary()
        factory.command_library = command_library
        command_library.factory = factory
        factory.gui = gui
    except twisted.internet.error.CannotListenError:
        server_shutdown()
    
    
    reactor.run()
Exemplo n.º 3
0
 def _start_engine(self):
     """
     start the qtreactor and socket communications
     """
     import qtreactor.pyqt4reactor
     qtreactor.pyqt4reactor.install()
     
     last_connection_time = time.time()
     time_last_check = time.time()   
     time_now = time.time()
     
     from twisted.internet import reactor
     from twisted.internet import task
     from autobahn.twisted.websocket import WebSocketServerProtocol
     from autobahn.twisted.websocket import WebSocketServerFactory
     from autobahn.twisted.websocket import WebSocketClientFactory
     from autobahn.twisted.websocket import WebSocketClientProtocol
     from autobahn.twisted.websocket import connectWS
     from autobahn.twisted.websocket import listenWS
     from twisted.internet.protocol import DatagramProtocol
     import twisted.internet.error
     from twisted.python import log
     log.startLogging(sys.stdout)
    
     from twisted.internet import stdio
     from twisted.protocols import basic
     from twisted.internet import error
 
     class MyServerProtocol(WebSocketServerProtocol):
     
         def onConnect(self, request):
             if DEBUG: print("class data_guis.MyServerProtocol, func onConnect: {0}".format(request.peer))
             
         def onOpen(self):
             if DEBUG: print("class data_guis.MyServerProtocol, func onOpen")
             
         def onMessage(self, payload_, isBinary):
             if DEBUG: print "class data_guis.MyServerProtocol, func onMessage"
             #self.log_message()
             if isBinary:
                 payload = msgpack.unpackb(payload_)
                 if not payload.has_key('IDLSocket_ResponseFunction'):
                     return None
                 try:
                     #ThisResponseFunction = getattr(self.factory.app.command_library,
                     #                           payload['IDLSocket_ResponseFunction'])
                     ThisResponseFunction = getattr(self.factory.command_library,
                                                payload['IDLSocket_ResponseFunction'])
                 except AttributeError:
                     if DEBUG: print ('Missing Socket_ResponseFunction:',
                                      payload['IDLSocket_ResponseFunction'])
                 ThisResponseFunction(payload)
             else:
                 print payload_.keys()
             
             
         def onClose(self, wasClean, code, reason):
             if DEBUG: print("class data_guis.MyServerProtocol, func onClose: {0}".format(reason))
             server_shutdown()
     
                
     def check_for_main_server():
         global time_last_check
         global time_now
         time_last_check = time_now
         time_now = time.time()
         #print time_last_check, time_now, last_connection_time
         if (time_now - last_connection_time) > 1100000 and (time_now - time_last_check) < 11:
             server_shutdown()
         
         
     def server_shutdown():
         if DEBUG: print "----------------Shutting Down DataGuiServer Now!----------------"
         #win.close()
         #app.quit()
         reactor.callLater(0.01, reactor.stop)
     
     sys.argv.append('localhost')
     sys.argv.append('9100')
     #sys.argv[0] = file name of this script
     # szys.argv[1] = ip address of this server
     # sys.argv[2] = port to listen on
     factory = WebSocketServerFactory("ws://" + 'localhost' + ":"+str(sys.argv[2]), debug = False)
     factory.setProtocolOptions(failByDrop=False)
     factory.protocol = MyServerProtocol
     try:
         reactor.listenTCP(int(sys.argv[2]), factory)
         #a.factory = factory
         command_library = CommandLibrary()
         factory.command_library = command_library
         command_library.factory = factory
         factory.gui = self.gui
     except twisted.internet.error.CannotListenError:
         server_shutdown()
     
     reactor.run()