"""
Socket IO connections
"""

from datetime import timedelta

from tornado import ioloop, gen
from tornadio2 import SocketConnection, TornadioRouter, SocketServer, event, gen

class QueryConnection(SocketConnection):
    def long_running(self, value, callback):
        """Long running task implementation.
        Simply adds 3 second timeout and then calls provided callback method.
        """
        def finish():
            callback('Handled %s.' % value)

        ioloop.IOLoop.instance().add_timeout(timedelta(seconds=3), finish)

    @event
    def query(self, num):
        """Event implementation

        Because on_event() was wrapped with ``gen.sync_engine``, yield will be treated
        as asynchronous task.
        """
        response = yield gen.Task(self.long_running, num)
        self.emit('response', response)

    @gen.engine
    def on_event(self, name, *args, **kwargs):
        """Wrapped ``on_event`` handler, which will queue events and will allow usage
        of the ``yield`` in the event handlers.

        If you want to use non-queued version, just wrap ``on_event`` with ``gen.engine``.
        """
        return super(QueryConnection, self).on_event(name, *args, **kwargs)


