Example #1
0
 def open(self, remote_host):
     self.is_open = True
     try:
         hostname, sep, port = remote_host.rpartition(':')
         if not is_valid_hostname(hostname) or not re.match(r'\d+', port):
             self.error('invalid connection string')
             self.close()
             raise gen.Return()
         self.socket_path = os.path.join(
             os.path.abspath(os.path.dirname(__file__)), str(uuid.uuid4()))
         self.port_num = self.acquire_port()
         self.subprocess = Subprocess(
             self.on_subprocess_exit,
             args=[
                 'eibd', 'iptn:{}:{}:{}'.format(hostname, port,
                                                self.port_num),
                 '--listen-local={}'.format(self.socket_path)
             ])
         self.subprocess.start()
         f = Future()
         # TODO: fix this crap
         tornado.ioloop.IOLoop.instance().add_timeout(
             datetime.timedelta(seconds=3), partial(f.set_result, None))
         yield f
         self.backend = EIBHandler('local:{}'.format(self.socket_path),
                                   self)
         yield self.backend.fetch_all()
     except Exception as e:
         self.error(str(e))
Example #2
0
 def open(self, remote_host):
     self.is_open = True
     try:
         hostname, sep, port = remote_host.rpartition(':')
         if not is_valid_hostname(hostname) or not re.match(r'\d+', port):
             self.error('invalid connection string')
             self.close()
             raise gen.Return()
         self.socket_path = os.path.join(
             os.path.abspath(os.path.dirname(__file__)),
             str(uuid.uuid4()))
         self.port_num = self.acquire_port()
         self.subprocess = Subprocess(
             self.on_subprocess_exit,
             args=['eibd', 'iptn:{}:{}:{}'.format(hostname, port,
                                                  self.port_num),
                   '--listen-local={}'.format(self.socket_path)])
         self.subprocess.start()
         f = Future()
         # TODO: fix this crap
         tornado.ioloop.IOLoop.instance().add_timeout(
             datetime.timedelta(seconds=3), partial(f.set_result, None))
         yield f
         self.backend = EIBHandler('local:{}'.format(self.socket_path), self)
         yield self.backend.fetch_all()
     except Exception as e:
         self.error(str(e))
Example #3
0
class WebSocket(tornado.websocket.WebSocketHandler):
    subprocess = None
    socket_path = None
    backend = None
    is_open = False
    port_num = None

    def data_received(self, chunk):
        pass

    def _response(self, **kwargs):
        if self.is_open:
            msg = {'timeStamp': str(datetime.datetime.today()),
                   'messageType': 'info'}
            msg.update(kwargs)
            self.write_message(msg)

    def error(self, message):
        self._response(messageType='error', message=message)

    def info(self, message):
        self._response(messageType='info', message=message)

    def on_message(self, message):
        pass

    def on_subprocess_exit(self, status, stdout, stderr):
        self.release_port()
        self.error('EIBD unexpected termination: {} {}'.format(stderr, stdout))
        try:
            os.remove(self.socket_path)
        except:
            pass
        self.close()

    def acquire_port(self):
        num = EIDB_STARTING_PORT
        while not self.application.memcache_connection.add(
                '{}_{}'.format(MEMCACHE_PORT_KEY, num), ''):
            num += 1
        return num

    def release_port(self):
        self.application.memcache_connection.delete('{}_{}'.format(
            MEMCACHE_PORT_KEY, self.port_num))

    @gen.coroutine
    def open(self, remote_host):
        self.is_open = True
        try:
            hostname, sep, port = remote_host.rpartition(':')
            if not is_valid_hostname(hostname) or not re.match(r'\d+', port):
                self.error('invalid connection string')
                self.close()
                raise gen.Return()
            self.socket_path = os.path.join(
                os.path.abspath(os.path.dirname(__file__)),
                str(uuid.uuid4()))
            self.port_num = self.acquire_port()
            self.subprocess = Subprocess(
                self.on_subprocess_exit,
                args=['eibd', 'iptn:{}:{}:{}'.format(hostname, port,
                                                     self.port_num),
                      '--listen-local={}'.format(self.socket_path)])
            self.subprocess.start()
            f = Future()
            # TODO: fix this crap
            tornado.ioloop.IOLoop.instance().add_timeout(
                datetime.timedelta(seconds=3), partial(f.set_result, None))
            yield f
            self.backend = EIBHandler('local:{}'.format(self.socket_path), self)
            yield self.backend.fetch_all()
        except Exception as e:
            self.error(str(e))

    def on_close(self, message=None):
        self.is_open = False
        if self.backend:
            self.backend.close()
            del self.backend
        if self.subprocess:
            self.subprocess.callback = lambda *args: self.on_subprocess_exit(
                0, '', '')
            self.subprocess.cancel()
            del self.subprocess

    def process_telegram(self, data):
        self.write_message(data)
Example #4
0
class WebSocket(tornado.websocket.WebSocketHandler):
    subprocess = None
    socket_path = None
    backend = None
    is_open = False
    port_num = None

    def data_received(self, chunk):
        pass

    def _response(self, **kwargs):
        if self.is_open:
            msg = {
                'timeStamp': str(datetime.datetime.today()),
                'messageType': 'info'
            }
            msg.update(kwargs)
            self.write_message(msg)

    def error(self, message):
        self._response(messageType='error', message=message)

    def info(self, message):
        self._response(messageType='info', message=message)

    def on_message(self, message):
        pass

    def on_subprocess_exit(self, status, stdout, stderr):
        self.release_port()
        self.error('EIBD unexpected termination: {} {}'.format(stderr, stdout))
        try:
            os.remove(self.socket_path)
        except:
            pass
        self.close()

    def acquire_port(self):
        num = EIDB_STARTING_PORT
        while not self.application.memcache_connection.add(
                '{}_{}'.format(MEMCACHE_PORT_KEY, num), ''):
            num += 1
        return num

    def release_port(self):
        self.application.memcache_connection.delete('{}_{}'.format(
            MEMCACHE_PORT_KEY, self.port_num))

    @gen.coroutine
    def open(self, remote_host):
        self.is_open = True
        try:
            hostname, sep, port = remote_host.rpartition(':')
            if not is_valid_hostname(hostname) or not re.match(r'\d+', port):
                self.error('invalid connection string')
                self.close()
                raise gen.Return()
            self.socket_path = os.path.join(
                os.path.abspath(os.path.dirname(__file__)), str(uuid.uuid4()))
            self.port_num = self.acquire_port()
            self.subprocess = Subprocess(
                self.on_subprocess_exit,
                args=[
                    'eibd', 'iptn:{}:{}:{}'.format(hostname, port,
                                                   self.port_num),
                    '--listen-local={}'.format(self.socket_path)
                ])
            self.subprocess.start()
            f = Future()
            # TODO: fix this crap
            tornado.ioloop.IOLoop.instance().add_timeout(
                datetime.timedelta(seconds=3), partial(f.set_result, None))
            yield f
            self.backend = EIBHandler('local:{}'.format(self.socket_path),
                                      self)
            yield self.backend.fetch_all()
        except Exception as e:
            self.error(str(e))

    def on_close(self, message=None):
        self.is_open = False
        if self.backend:
            self.backend.close()
            del self.backend
        if self.subprocess:
            self.subprocess.callback = lambda *args: self.on_subprocess_exit(
                0, '', '')
            self.subprocess.cancel()
            del self.subprocess

    def process_telegram(self, data):
        self.write_message(data)