예제 #1
0
def rpc_factory() -> JsonRpc:
    rpc = JsonRpc()

    rpc.add_methods(
        ("", list_sites),
        ("", list_site_files),
        ("", get_file_content),
        ("", put_file_content),
        ("", render),
        ("", build),
        ("", slugify),
    )

    return rpc
예제 #2
0
    def start_server(loop, user_data):

        app = Application()

        async def start_server_async():
            runner = AppRunner(app)
            await runner.setup()
            site = TCPSite(runner, 'localhost', 8080)
            try:
                await site.start()
            except OSError as e:
                logger.warning(e)

        rpc = JsonRpc()

        methods = []
        for pname, p in providers.PROVIDERS.items():
            methods += [(pname, func) for name, func in p.RPC_METHODS]

        rpc.add_methods(*methods)
        app.router.add_route("*", "/", rpc.handle_request)
        asyncio.create_task(start_server_async())
예제 #3
0
from aiohttp.web import Application
from aiohttp_json_rpc import JsonRpc
import asyncio


async def fit_sendTransaction(request):
    print('from fit_sendTransaction')
    return 'pong'


if __name__ == '__main__':
    loop = asyncio.get_event_loop()

    rpc = JsonRpc()
    rpc.add_methods(('heloo', fit_sendTransaction), )

    app = Application(loop=loop)
    app.router.add_route('*', '/', rpc)

    handler = app.make_handler()

    print("Starting RPC server ")
    server = loop.run_until_complete(
        loop.create_server(handler, '0.0.0.0', 4000))

    loop.run_forever()
예제 #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Florian Scherf <*****@*****.**>

from aiohttp.web import Application
from aiohttp_json_rpc import JsonRpc
import asyncio


@asyncio.coroutine
def ping(request):
    return 'pong'


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    rpc = JsonRpc()

    rpc.add_methods(('', ping), )

    app = Application(loop=loop)
    app.router.add_route('*', '/', rpc)

    handler = app.make_handler()

    server = loop.run_until_complete(
        loop.create_server(handler, '0.0.0.0', 8080))

    loop.run_forever()
예제 #5
0
def server(directory, config_path, runner, tool_override):
    loop = asyncio.get_event_loop()

    # EM
    em = EventEmitter(loop)

    config = read_toml_config(config_path, tool_override)

    # TODO: Re-add support for multiple test suites
    suite = TestSuite(config["name"], runner, em, config)
    suites = {config["name"]: suite}

    # Tests
    tests = Tests(suites)

    async def collect_all(request):
        LOGGER.info("Collect ALL")
        tasks = [
            suite.collect_all(directory, em, loop=loop)
            for suite in suites.values()
        ]
        await asyncio.gather(*tasks, loop=loop)
        return "ok"

    async def run_all(request):
        LOGGER.info("Run ALL")
        tasks = [
            suite.launch_all(directory, em, loop=loop)
            for suite in suites.values()
        ]
        await asyncio.gather(*tasks, loop=loop)
        return "ok"

    async def run_selected(request):
        tasks = []
        LOGGER.info("Run selected: %r", request.params)
        for suite_name, suite_tests in request.params.items():
            suite = suites[suite_name]
            tasks.append(suite.launch_tests(directory, em, loop, suite_tests))

        await asyncio.gather(*tasks)
        return "ok"

    rpc = JsonRpc()
    logging.getLogger("aiohttp-json-rpc.server").setLevel(logging.DEBUG)

    async def forward_notifications(message):
        LOGGER.debug("Forwarding to %d clients: %r", len(rpc.clients), message)
        for client in rpc.clients:
            data = {
                "jsonrpc": "2.0",
                "id": None,
                "method": "test",
                "params": message
            }
            r = await client.ws.send_str(json.dumps(data))

    em.register(forward_notifications)
    em.register(process_notification)

    loop = asyncio.get_event_loop()
    rpc.add_methods(("", collect_all), ("", run_selected), ("", run_all))
    rpc.add_topics("test")

    app = Application(loop=loop, debug=True)
    web_interfaces_route = get_static_path()
    app.router.add_static("/interface/",
                          web_interfaces_route,
                          show_index=True,
                          name="static")
    app.router.add_route("*", "/", rpc)

    run_app(app, port=8889)
예제 #6
0
async def test_cuncurrent_transactions(event_loop, unused_tcp_port):
    """
    This test tests aiohttp_json_rpc.django.patch_db_connections.

    The test sets up a JsonRpc and starts two concurrent clients.
    Both clients try to open a Django transaction.
    The test is successful if only the first client is able to commit its
    database changes.

    Note:
        The transactions are slowed down artificial and Client #2 sleeps 200ms
        on start using asyncio.sleep to make the test log more readable and
        ensure the transactions are concurrent.

    Note:
        The test starts an watchdog to make sure the test does not hang if the
        rpc communication is broken or hanging.
    """

    from django_project.models import Item
    from aiohttp_json_rpc.django import patch_db_connections

    def create_client(client_id, *args, **kwargs):
        url = 'http://localhost:{}'.format(unused_tcp_port)
        future = asyncio.ensure_future(client(client_id, url, *args, **kwargs))
        future.client_id = client_id

        return future

    patch_db_connections()

    # just to be sure
    assert Item.objects.count() == 0

    # setup rpc
    app = Application(loop=event_loop)
    rpc = JsonRpc()

    rpc.add_methods(('', add), )

    app.router.add_route('*', '/', rpc)

    await event_loop.create_server(app.make_handler(), 'localhost',
                                   unused_tcp_port)

    # setup clients and watchdog
    tasks = [
        create_client(1, list(range(0, 10))),
        create_client(2, list(range(2, 5)), sleep=0.2),
    ]

    tasks = [
        *tasks,
        asyncio.ensure_future(watchdog(tasks)),
    ]

    # run
    await asyncio.gather(*tasks)

    # checks
    assert Item.objects.filter(client_id=1).count() == 10
    assert not Item.objects.filter(client_id=2).exists()

    assert tasks[0].result() == 0  # client #1
    assert tasks[1].result() == 1  # client #2
    assert tasks[2].result() == 0  # watchdog
    return 'pong'


@login_required
@asyncio.coroutine
def pong(request):
    return 'ping'


if __name__ == '__main__':
    PASSWD_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                               'user.db')  # login: admin:admin

    loop = asyncio.get_event_loop()
    rpc = JsonRpc(auth_backend=PasswdAuthBackend(PASSWD_FILE))

    rpc.add_methods(
        ('', ping),
        ('', pong),
    )

    app = Application(loop=loop)
    app.router.add_route('*', '/', rpc)

    handler = app.make_handler()

    server = loop.run_until_complete(
        loop.create_server(handler, '0.0.0.0', 8080))

    loop.run_forever()
예제 #8
0
# -*- coding: utf-8 -*-
# Author: Florian Scherf <*****@*****.**>

from django_example_project.wsgi import application as django_app
from aiohttp.web import Application
from aiohttp_wsgi import WSGIHandler
from aiohttp_json_rpc import JsonRpc
from aiohttp_json_rpc.auth.django import DjangoAuthBackend
import asyncio


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    wsgi_handler = WSGIHandler(django_app)
    app = Application(loop=loop)
    rpc = JsonRpc(auth_backend=DjangoAuthBackend())

    rpc.add_methods(
        ('', 'django_example_app.rpc')
    )

    app.router.add_route('*', '/rpc', rpc)
    app.router.add_route('*', '/{path_info:.*}', wsgi_handler.handle_request)

    handler = app.make_handler()

    server = loop.run_until_complete(
        loop.create_server(handler, '0.0.0.0', 8080))

    loop.run_forever()
예제 #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Florian Scherf <*****@*****.**>

from django_example_project.wsgi import application as django_app
from aiohttp.web import Application, run_app
from aiohttp_wsgi import WSGIHandler
from aiohttp_json_rpc import JsonRpc
from aiohttp_json_rpc.auth.django import DjangoAuthBackend
import asyncio


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    wsgi_handler = WSGIHandler(django_app)
    app = Application(loop=loop)
    rpc = JsonRpc(auth_backend=DjangoAuthBackend())

    rpc.add_methods(
        ('', 'django_example_app.rpc')
    )

    app.router.add_route('*', '/rpc', rpc.handle_request)
    app.router.add_route('*', '/{path_info:.*}', wsgi_handler.handle_request)

    run_app(app, host='0.0.0.0', port=8080)
예제 #10
0
class LaborApi:
    def __init__(self, loop=None, logger=None, state_file=None):
        self.loop = loop or asyncio.get_event_loop()
        self.logger = logger or logging.getLogger('labor-api')

        # setup rpc
        self.rpc = JsonRpc()

        self.rpc.add_topics(('room-state'), )

        self.rpc.add_methods(('', self.set_room_state), )

        # setup cache
        self.state_file = state_file

        self.cache = {
            'state': {
                'since': None,
                'open': None,
            },
            'archive': [],
            'responses': {
                'room': '',
                'room_archive': '',
            },
        }

        if not state_file:
            self.logger.debug('No state file.'
                              'rooms state will be closed by default')

            self._set_room_state(False)

        # setup app
        self.app = Application(loop=self.loop)

        # get room
        self.app.router.add_route('GET', '/api/room/', self.get_room)
        self.app.router.add_route('GET', '/api/room', self.get_room)

        # set room
        self.app.router.add_route('POST', '/api/room/', self.set_room)
        self.app.router.add_route('POST', '/api/room', self.set_room)

        # room archive
        self.app.router.add_route('GET', '/api/room_archive/',
                                  self.get_room_archive)

        self.app.router.add_route('GET', '/api/room_archive',
                                  self.get_room_archive)

        # websockets
        self.app.router.add_route('GET', '/api/rpc/', self.rpc)
        self.app.router.add_route('GET', '/api/rpc', self.rpc)

    def _set_room_state(self, open):
        self.logger.debug('set room state open=%s', open)

        state = {
            'since': int(time.time()),
            'open': bool(open),
        }

        self.cache['state'] = state

        self.cache['archive'].append({
            'lastchange': state['since'],
            'open': open,
        })

        self.cache['responses']['room'] = json.dumps(self.cache['state'])

        self.cache['responses']['room_archive'] = json.dumps(
            self.cache['archive'])

        # notify
        self.rpc.notify('room-state', state)

    def get_bool_from_str(self, str):
        if str.lower().strip() in ('1', 'true'):
            return True

        return False

    # methods
    async def set_room_state(self, request):
        self._set_room_state(request.params['open'])

        return self.cache['state']

    async def get_room(self, request):
        self.logger.debug('LaborApi.get_room(%s)', request)

        response = Response(text=self.cache['responses']['room'],
                            content_type='application/json')

        response.headers['Access-Control-Allow-Origin'] = '*'

        return response

    async def set_room(self, request):
        data = await request.post()

        self.logger.debug('LaborApi.set_room(%s(%s))', request, data)

        open = self.get_bool_from_str(data['open'])
        self._set_room_state(open=open)

        return Response(content_type='application/json',
                        text=json.dumps({
                            'success': True,
                            'status': '',
                        }))

    async def get_room_archive(self, request):
        self.logger.debug('LaborApi.get_room_archive(%s)', request)

        return Response(text=self.cache['responses']['room_archive'],
                        content_type='application/json')
async def test_cuncurrent_transactions(event_loop, unused_tcp_port):
    """
    This test tests aiohttp_json_rpc.django.patch_db_connections.

    The test sets up a JsonRpc and starts two concurrent clients.
    Both clients try to open a Django transaction.
    The test is successful if only the first client is able to commit its
    database changes.

    Note:
        The transactions are slowed down artificial and Client #2 sleeps 200ms
        on start using asyncio.sleep to make the test log more readable and
        ensure the transactions are concurrent.

    Note:
        The test starts an watchdog to make sure the test does not hang if the
        rpc communication is broken or hanging.
    """

    from django.db import connections

    from django_project.models import Item
    from aiohttp_json_rpc.django import patch_db_connections

    def create_client(client_id, *args, **kwargs):
        url = 'http://localhost:{}'.format(unused_tcp_port)
        future = asyncio.ensure_future(client(client_id, url, *args, **kwargs))
        future.client_id = client_id

        return future

    patch_db_connections()

    # just to be sure
    assert Item.objects.count() == 0

    # setup rpc
    app = Application(loop=event_loop)
    rpc = JsonRpc()

    rpc.add_methods(
        ('', add),
    )

    app.router.add_route('*', '/', rpc)

    await event_loop.create_server(
        app.make_handler(), 'localhost', unused_tcp_port)

    # setup clients and watchdog
    tasks = [
        create_client(1, list(range(0, 10))),
        create_client(2, list(range(2, 5)), sleep=0.2),
    ]

    tasks = [
        *tasks,
        asyncio.ensure_future(watchdog(tasks)),
    ]

    # run
    await asyncio.gather(*tasks)

    # checks
    assert Item.objects.filter(client_id=1).count() == 10
    assert not Item.objects.filter(client_id=2).exists()

    assert tasks[0].result() == 0  # client #1
    assert tasks[1].result() == 1  # client #2
    assert tasks[2].result() == 0  # watchdog