예제 #1
0
async def start(ctx: Context) -> None:
    port = ctx.config.get('port', 8123)
    app = web.Application()
    app['context'] = ctx
    routeCfgs = []
    rc1 = ctx.config.get('routes') or ['default-routes']
    if not isinstance(rc1, list):
        ctx.logger.warn('routes config setting %r is not a list' % rc1)
        return
    for r in rc1:
        if isinstance(r, str):
            routeCfgs.extend(getPredefinedRoutes(r))
        else:
            routeCfgs.append(r)
    app['routes'] = dict((r['name'], r) for r in routeCfgs)
    routes = [web.route(rc.get('method') or 'GET',
                        rc['path'], 
                        getHandler(ctx, rc['handler']),
                        name=rc['name']) 
              for rc in routeCfgs]
    app.add_routes(routes)

    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, 'localhost', port)
    ctx.state = site
    await site.start()
    await listen(ctx)
    ctx.logger.info('%s finished' % ctx.pname)
예제 #2
0
async def listen(ctx: Context) -> None:
    step = getHandler(ctx, 'step')
    if step is None:
        ctx.logger.warn('step handler for %s not found' % ctx.pname)
    else:
        while await step(ctx):
            pass
    ctx.logger.info('%s finished' % ctx.pname)
예제 #3
0
def run(pctx: Context, name: str) -> None:
    ctx = setup(pctx, name)
    start = getHandler(ctx, 'start')
    if start is None:
        ctx.logger.warn('start handler for %s not found' % name)
    else:
        p = process.run(start, ctx, name)
        pctx.children.append((p, ctx.mailbox))
예제 #4
0
async def step(ctx: Context) -> bool:
    msg = await receive(ctx.mailbox)
    ctx.logger.debug('%s recv: msg=%s.' % (ctx.pname, msg))
    action = getHandler(ctx, 'action')
    if action is None:
        ctx.logger.warn('action handler for %s not found' % ctx.pname)
        return False
    else:
        return await action(ctx, msg)
예제 #5
0
async def start(ctx: Context) -> None:
    url = ctx.config.get('server-url')
    session = ClientSession()
    ctx.state = session
    # await do_listen(ctx)
    listen = getHandler(ctx, 'listen')
    if listen is None:
        ctx.logger.warn('client.web: listen handler not found')
    else:
        await listen(ctx)
        await session.close()
예제 #6
0
async def action(ctx: Context, msg: Message) -> bool:
    # TODO: get handler/action using message type
    if msg is quit:
        # TODO: use getHandler(ctx, 'do_quit')
        fct = do_quit
        cfg = None
    else:
        cmd = msg.payload.get('command') or '???'
        actions = ctx.config.get('actions', {})
        cfg = cast(Dict, isinstance(actions, dict) and actions.get(cmd, {}))
        if not cfg:
            fct = do_ignore
        else:
            handler = cast(str, cfg.get('handler'))
            group = cast(str, cfg.get('group'))
            hdl = getHandler(ctx, handler, group)
            if hdl is None:
                ctx.logger.warn('Handler not found, config is: %s' % cfg)
                fct = do_quit
            else:
                fct = hdl
    res = await fct(ctx, cfg, msg)
    return res
예제 #7
0
async def start(ctx: Context) -> None:
    listen = getHandler(ctx, 'listen')
    if listen is None:
        ctx.logger.warn('listen handler for %s not found' % ctx.pname)
    else:
        await listen(ctx)