Ejemplo n.º 1
0
class RpsServer(object):
    def __init__(self, externalEvent, contexts, port=8080):
        self.port = port
        self.externalEvent = externalEvent
        self.contexts = contexts

    def serve_forever(self):
        try:
            self.running = True
            self.listener = HttpListener()
            prefix = 'http://localhost:%i/' % self.port
            self.listener.Prefixes.Add(prefix)
            try:
                print 'starting listener', prefix
                self.listener.Start()
                print 'started listener'
            except HttpListenerException as ex:
                print 'HttpListenerException:', ex
                return
            waiting = False
            while self.running:
                if not waiting:
                    print 'not waiting'
                    context = self.listener.BeginGetContext(
                        AsyncCallback(self.handleRequest), self.listener)
                    print 'after BeginGetContext'
                waiting = not context.AsyncWaitHandle.WaitOne(100)
        except:
            traceback.print_exc()

    def stop(self):
        print 'stop()'
        self.running = False
        self.listener.Stop()
        self.listener.Close()

    def handleRequest(self, result):
        '''
        pass the request to the RevitEventHandler
        '''
        print "in handleRequest"
        try:
            listener = result.AsyncState
            if not listener.IsListening:
                return
            try:
                context = listener.EndGetContext(result)
            except:
                # Catch the exception when the thread has been aborted
                self.stop()
                return
            self.contexts.append(context)
            self.externalEvent.Raise()
            print 'raised external event'
        except:
            traceback.print_exc()
Ejemplo n.º 2
0
    def serve(self):
        listener = HttpListener()
        prefix = 'http://*:%s/' % str(self.port)
        listener.Prefixes.Add(prefix)
        try:
            listener.Start()
        except HttpListenerException:
            raise ServerException("Error starting server")

        print "Listening on port %s" % str(self.port)

        while True:
            result = listener.BeginGetContext(
                AsyncCallback(self.handleRequest), listener)
            result.AsyncWaitHandle.WaitOne()
Ejemplo n.º 3
0
    def serveforever(self):
        self.failed = False
        listener = HttpListener()
        
        prefix = 'http://%s:%s/' % (self.host, str(self.port))
        listener.Prefixes.Add(prefix)
        
        try:
            listener.Start()
        except HttpListenerException:
            self.failed = True
            return

        while True:
            result = listener.BeginGetContext(AsyncCallback(self.handleRequest), listener)
            result.AsyncWaitHandle.WaitOne()