Ejemplo n.º 1
0
def _on_server_start(server):
    _print_url()

    def maybe_open_browser():
        if config.get_option("server.headless"):
            # Don't open browser when in headless mode.
            return

        if server.browser_is_connected:
            # Don't auto-open browser if there's already a browser connected.
            # This can happen if there's an old tab repeatedly trying to
            # connect, and it happens to success before we launch the browser.
            return

        if config.is_manually_set("browser.serverAddress"):
            addr = config.get_option("browser.serverAddress")
        else:
            addr = "localhost"

        util.open_browser(Report.get_url(addr))

    # Schedule the browser to open using the IO Loop on the main thread, but
    # only if no other browser connects within 1s.
    ioloop = tornado.ioloop.IOLoop.current()
    ioloop.call_later(BROWSER_WAIT_TIMEOUT_SEC, maybe_open_browser)
Ejemplo n.º 2
0
 def open(self):
     ioloop = tornado.ioloop.IOLoop.instance()
     ioloop.call_later(self._KEEPALIVE_PING_TIMEOUT, self._send_ping)
     ioloop.add_callback(lambda: log.info('Client connected: {0}'.format(self)))
     self._get_id()
     self._CLIENTS[self.id] = self
     self._log_client_list()
Ejemplo n.º 3
0
def main():
    sc = tornado.stack_context.StackContext(contextor)
    with sc:
        async_task()
    sc2 = tornado.stack_context.StackContext(contextor2)
    with sc2:
        ioloop.call_later(5, ioloop.stop)
    ioloop.start()
    print("ioloop start ...")
    print('end')
Ejemplo n.º 4
0
def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application(mappings)
    application.listen(options.port)

    tornado.ioloop.PeriodicCallback(ResultCache.update, 1800000).start()

    ioloop = tornado.ioloop.IOLoop.current()
    ioloop.call_later(1000, ResultCache.update())
    ioloop.start()
Ejemplo n.º 5
0
def run(args):
    cache = Cache('gsiftp://gridftp-scratch.icecube.wisc.edu/local/simprod/')
    #cache = Cache('gsiftp://gridftp-3.icecube.wisc.edu/mnt/lfs4/simprod/dagtemp2/test/')
    ioloop = tornado.ioloop.IOLoop.current()
    for i in range(args.num):
        ioloop.call_later(i*60,run_dag,Corsika(cache))
        for t in range(1,101):
            ioloop.call_later(i*t*6,run_dag,NuGen_Systematics(cache))
    logging.warn('starting')
    ioloop.start()
    logging.warn('done')
Ejemplo n.º 6
0
    def on_open(self, info: ConnectionInfo) -> None:
        log_data = dict(extra='[transport=%s]' % (self.session.transport_name,))
        record_request_start_data(log_data)

        ioloop = tornado.ioloop.IOLoop.instance()

        self.authenticated = False
        self.session.user_profile = None
        self.close_info = None  # type: Optional[CloseErrorInfo]
        self.did_close = False

        try:
            self.browser_session_id = info.get_cookie(settings.SESSION_COOKIE_NAME).value
            self.csrf_token = info.get_cookie(settings.CSRF_COOKIE_NAME).value
        except AttributeError:
            # The request didn't contain the necessary cookie values.  We can't
            # close immediately because sockjs-tornado doesn't expect a close
            # inside on_open(), so do it on the next tick.
            self.close_info = CloseErrorInfo(403, "Initial cookie lacked required values")
            ioloop.add_callback(self.close)
            return

        def auth_timeout() -> None:
            self.close_info = CloseErrorInfo(408, "Timeout while waiting for authentication")
            self.close()

        self.timeout_handle = ioloop.call_later(10, auth_timeout)
        write_log_line(log_data, path='/socket/open', method='SOCKET',
                       remote_ip=info.ip, email='unknown', client_name='?')
Ejemplo n.º 7
0
    def on_open(self, info: ConnectionInfo) -> None:
        log_data = dict(extra='[transport=%s]' % (self.session.transport_name,))
        record_request_start_data(log_data)

        ioloop = tornado.ioloop.IOLoop.instance()

        self.authenticated = False
        self.session.user_profile = None
        self.close_info = None  # type: Optional[CloseErrorInfo]
        self.did_close = False

        try:
            self.browser_session_id = info.get_cookie(settings.SESSION_COOKIE_NAME).value
            self.csrf_token = info.get_cookie(settings.CSRF_COOKIE_NAME).value
        except AttributeError:
            # The request didn't contain the necessary cookie values.  We can't
            # close immediately because sockjs-tornado doesn't expect a close
            # inside on_open(), so do it on the next tick.
            self.close_info = CloseErrorInfo(403, "Initial cookie lacked required values")
            ioloop.add_callback(self.close)
            return

        def auth_timeout() -> None:
            self.close_info = CloseErrorInfo(408, "Timeout while waiting for authentication")
            self.close()

        self.timeout_handle = ioloop.call_later(10, auth_timeout)
        write_log_line(log_data, path='/socket/open', method='SOCKET',
                       remote_ip=info.ip, email='unknown', client_name='?')
Ejemplo n.º 8
0
def _on_server_start(server: Server) -> None:
    _maybe_print_old_git_warning(server.script_path)
    _print_url(server.is_running_hello)
    report_watchdog_availability()
    _print_new_version_message()

    # Load secrets.toml if it exists. If the file doesn't exist, this
    # function will return without raising an exception. We catch any parse
    # errors and display them here.
    try:
        secrets.load_if_toml_exists()
    except BaseException as e:
        LOGGER.error(f"Failed to load {SECRETS_FILE_LOC}", exc_info=e)

    def maybe_open_browser():
        if config.get_option("server.headless"):
            # Don't open browser when in headless mode.
            return

        if server.browser_is_connected:
            # Don't auto-open browser if there's already a browser connected.
            # This can happen if there's an old tab repeatedly trying to
            # connect, and it happens to success before we launch the browser.
            return

        if config.is_manually_set("browser.serverAddress"):
            addr = config.get_option("browser.serverAddress")
        elif config.is_manually_set("server.address"):
            if server_address_is_unix_socket():
                # Don't open browser when server address is an unix socket
                return
            addr = config.get_option("server.address")
        else:
            addr = "localhost"

        util.open_browser(Report.get_url(addr))

    # Schedule the browser to open using the IO Loop on the main thread, but
    # only if no other browser connects within 1s.
    ioloop = tornado.ioloop.IOLoop.current()
    ioloop.call_later(BROWSER_WAIT_TIMEOUT_SEC, maybe_open_browser)
Ejemplo n.º 9
0
 def text(self, text, timeout=None):
     """Draws a text with an optional timeout in miliseconds.
     After that, the last frame is restored.
     """
     image = Image.new(self.device.mode, self.device.size)
     draw = ImageDraw.Draw(image)
     draw.text((5, 35), text, fill='white', align='center', font=self.font)
     self.draw(image, frame=False)
     if timeout:
         ioloop = tornado.ioloop.IOLoop.current()
         if self.runner:
             ioloop.remove_timeout(self.runner)
         func = functools.partial(self.draw, self.frame, frame=False)
         self.runner = ioloop.call_later(timeout / 1000.0, func)
Ejemplo n.º 10
0
    def connect_handler(self, handler_id: int, client_name: str) -> None:
        self.current_handler_id = handler_id
        self.current_client_name = client_name
        set_descriptor_by_handler_id(handler_id, self)
        self.last_connection_time = time.time()

        def timeout_callback() -> None:
            self._timeout_handle = None
            # All clients get heartbeat events
            self.add_event(dict(type='heartbeat'))
        ioloop = tornado.ioloop.IOLoop.instance()
        interval = HEARTBEAT_MIN_FREQ_SECS + random.randint(0, 10)
        if self.client_type_name != 'API: heartbeat test':
            self._timeout_handle = ioloop.call_later(interval, timeout_callback)
Ejemplo n.º 11
0
    def connect_handler(self, handler_id: int, client_name: Text) -> None:
        self.current_handler_id = handler_id
        self.current_client_name = client_name
        set_descriptor_by_handler_id(handler_id, self)
        self.last_connection_time = time.time()

        def timeout_callback() -> None:
            self._timeout_handle = None
            # All clients get heartbeat events
            self.add_event(dict(type='heartbeat'))
        ioloop = tornado.ioloop.IOLoop.instance()
        interval = HEARTBEAT_MIN_FREQ_SECS + random.randint(0, 10)
        if self.client_type_name != 'API: heartbeat test':
            self._timeout_handle = ioloop.call_later(interval, timeout_callback)
Ejemplo n.º 12
0
async def getChatInfo(roomid):
    url = CHATINFOURL + roomid
    http_client = AsyncHTTPClient()
    res = await http_client.fetch(url)
    chatInfo = json.loads(res.body)
    chatAddr = chatInfo['data']['chat_addr_list'][0]
    socketIP, socketPort = chatAddr.split(':')
    print('chat ip:', socketIP)
    print('chat port:', socketPort)
    stream = await TCPClient().connect(socketIP, socketPort)
    rid = str(chatInfo['data']['rid']).encode('utf-8')
    appid = str(chatInfo['data']['appid']).encode('utf-8')
    authtype = str(chatInfo['data']['authType']).encode('utf-8')
    sign = str(chatInfo['data']['sign']).encode('utf-8')
    ts = str(chatInfo['data']['ts']).encode('utf-8')
    msg = b'u:' + rid + b'@' + appid + b'\nk:1\nt:300\nts:' + ts + b'\nsign:' + sign + b'\nauthtype:' + authtype
    msgLen = len(msg)
    sendMsg = FIRST_REQ + int.to_bytes(msgLen, 2, 'big') + msg
    await stream.write(sendMsg)
    recvMsg = await stream.read_bytes(CHECK_LEN)
    if recvMsg == FIRST_RPS:
        print('成功连接弹幕服务器')
        recvLen = int.from_bytes(await stream.read_bytes(2), 'big')
        await stream.read_bytes(recvLen)
    ioloop.call_later(0, KeepAlive, stream)

    while True:
        recvMsg = await stream.read_bytes(CHECK_LEN)
        if recvMsg == RECVMSG:
            recvLen = int.from_bytes(await stream.read_bytes(2), 'big')
            recvMsg = stream.read_bytes(recvLen)  #ack:0
            totalLen = int.from_bytes(await stream.read_bytes(META_LEN), 'big')
            try:
                await analyseMsg(stream, totalLen)
            except Exception as e:
                pass
Ejemplo n.º 13
0
 def push():
     for app, app_config in info.gramexlog.apps.items():
         for item in app_config.queue:
             item['_index'] = app_config.get('index', app)
         try:
             helpers.bulk(app_config.conn, app_config.queue)
             app_config.queue.clear()
         except Exception:
             # TODO: If the connection broke, re-create it
             # This generic exception should be caught for thread to continue its execution
             app_log.exception('gramexlog: push to %s failed', app)
     if 'handle' in info.gramexlog:
         ioloop.remove_timeout(info.gramexlog.handle)
     # Call again after flush seconds
     info.gramexlog.handle = ioloop.call_later(flush, push)
Ejemplo n.º 14
0
    def _send_ping(self):
        if self.ws_connection:
            ioloop = tornado.ioloop.IOLoop.instance()
            if isinstance(self.ws_connection, tornado.websocket.WebSocketProtocol13):
                future = tornado.gen.Future()
                seq = struct.pack(">q", int(time.time() * 1000))
                self._ping[seq] = future
                self.ping(seq)
            else:
                future = self.call('ping', seq=time.time())

            ioloop.call_later(
                self._KEEPALIVE_PING_TIMEOUT,
                lambda: self.close() if future.running() else None
            )

            resp = yield future
            ts = resp.get('seq', 0)
            delta = (time.time() - (ts/1000.))
            log.debug("%r Pong recieved: %.4f" % (self, delta))
            if delta > self._CLIENT_TIMEOUT:
                self.close()

            ioloop.call_later(self._KEEPALIVE_PING_TIMEOUT, self._send_ping)
Ejemplo n.º 15
0
def gramexlog(conf):
    '''Set up gramexlog service'''
    from gramex.transforms import build_log_info
    try:
        from elasticsearch import Elasticsearch, helpers
    except ImportError:
        app_log.error(
            'gramexlog: elasticsearch missing. pip install elasticsearch')
        return

    # We call push() every 'flush' seconds on the main IOLoop. Defaults to every 5 seconds
    flush = conf.pop('flush', 5)
    ioloop = info._main_ioloop or tornado.ioloop.IOLoop.current()
    # Set the defaultapp to the first config key under gramexlog:
    if len(conf):
        info.gramexlog.defaultapp = next(iter(conf.keys()))
    for app, app_conf in conf.items():
        app_config = info.gramexlog.apps[app] = AttrDict()
        app_config.queue = []
        keys = app_conf.pop('keys', [])
        # If user specifies keys: [port, args.x, ...], these are captured as additional keys.
        # The keys use same spec as Gramex logging.
        app_config.extra_keys = build_log_info(keys)
        # Ensure all gramexlog keys are popped from app_conf, leaving only Elasticsearch keys
        app_config.conn = Elasticsearch(**app_conf)

    def push():
        for app, app_config in info.gramexlog.apps.items():
            for item in app_config.queue:
                item['_index'] = app_config.get('index', app)
            try:
                helpers.bulk(app_config.conn, app_config.queue)
                app_config.queue.clear()
            except Exception:
                # TODO: If the connection broke, re-create it
                # This generic exception should be caught for thread to continue its execution
                app_log.exception('gramexlog: push to %s failed', app)
        if 'handle' in info.gramexlog:
            ioloop.remove_timeout(info.gramexlog.handle)
        # Call again after flush seconds
        info.gramexlog.handle = ioloop.call_later(flush, push)

    info.gramexlog.handle = ioloop.call_later(flush, push)
    info.gramexlog.push = push
Ejemplo n.º 16
0
    def start_hb(self):
        logger.debug("start_hb for %s", self.id)
        hb = self.channels["hb"]
        ioloop = tornado.ioloop.IOLoop.current()

        def pong(message):
            #logger.debug("pong for %s", self.id)
            self._expecting_pong = False

        hb.on_recv(pong)
        self._expecting_pong = False

        def ping():
            #logger.debug("ping for %s", self.id)
            now = ioloop.time()
            if self._expecting_pong:
                logger.warning("kernel %s died unexpectedly", self.id)
                self.stop()
            elif now > self.hard_deadline:
                logger.info("hard deadline reached for %s", self.id)
                self.stop()
            elif (self.timeout > 0
                    and now > self.deadline
                    and self.status == "idle"):
                logger.info("kernel %s timed out", self.id)
                self.stop()
            else:
                hb.send(b'ping')
                self._expecting_pong = True

        self._hb_periodic_callback = tornado.ioloop.PeriodicCallback(
            ping, config.get("beat_interval") * 1000)

        def start_ping():
            logger.debug("start_ping for %s", self.id)
            if self.alive:
                self._hb_periodic_callback.start()

        self._start_ping_handle = ioloop.call_later(
            config.get("first_beat"), start_ping)
        self.alive = True
Ejemplo n.º 17
0
 def game_invite(self, invitees):
     players = {}
     for invitee in invitees:
         player = self.users[invitee]
         #Abort the invite if an invitee is already in a game or has accepted an invite
         if player.current_game:
             return False
         else:
             players[invitee] = player
             
     game = coup.CoupGame(self, players)
     game.id = str(id(game))
     self.games[game.id] = game
     #Add the invite for all players
     for player in game.user_sockets:
         player.invites[game.id] = game
     game.invite()
                 
     #Cancel the game/invitation after a timeout if not everybody accepted
     game.invite_tout_handle = ioloop.call_later(10, self.game_cancel, game)
     return True
Ejemplo n.º 18
0
    # print("checking for file change")
    # print(restart_server)
    if (restart_server):
        print("updating the bigquery client")
        server.stop()
        server = None
        server = start_app()
    ioloop.add_callback(ioloop.stop)


class WatchDogEvent(LoggingEventHandler):
    def on_modified(self, event):
        global restart_server
        restart_server = True


if __name__ == "__main__":
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    observer = Observer()
    observer.schedule(WatchDogEvent(), path, recursive=True)
    server = start_app()
    observer.start()
    try:
        while True:
            ioloop.call_later(callback=restart_tornado, delay=1)
            ioloop.start()
            time.sleep(2)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
Ejemplo n.º 19
0
 def refresh():
     app.soc_runner.loop()
     ioloop.call_later(15, refresh)
Ejemplo n.º 20
0
 def refresh():
     app.soc_runner.loop()
     ioloop.call_later(15, refresh)
Ejemplo n.º 21
0
                await analyseMsg(stream, totalLen)
            except Exception as e:
                pass


async def analyseMsg(s, totalLen):
    while totalLen > 0:
        await s.read_bytes(IGNORE_LEN)
        recvLen = int.from_bytes(await s.read_bytes(META_LEN), 'big')
        recvMsg = await s.read_bytes(recvLen)
        # recv the whole msg.
        while recvLen > len(recvMsg):
            recvMsg = b''.join(recvMsg, s.recv(recvLen - len(recvMsg)))
        await formatMsg(recvMsg)
        totalLen = totalLen - IGNORE_LEN - META_LEN - recvLen


if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-l", "--roomid", dest="roomid", help="roomid")
    (options, args) = parser.parse_args()
    print('options:', options)
    ioloop = tornado.ioloop.IOLoop.current()

    #ioloop.call_later(0,contral_camera,'787552313',2,0,2)
    #print('run request')

    ioloop.call_later(3, getChatInfo, options.roomid)
    ioloop.start()
#