# Path: stator/loop.py
# def parse_message(self, input):
#     raise RuntimeError("abstract method")
# 
# @_skip
# def blocking_io():
#     return False
# 
# class Socket(object):
# 
#     def __init__(self, id):
#         assert isinstance(id, INT_TYPES), (type(id), id)
#         self.id = id
#         TABLE.add(self)
# 
#     def close(self):
#         TABLE.remove(self.id)
# 
#     def parse_message(self, input):
#         raise RuntimeError("abstract method")
# 
#     def __repr__(self):
#         return '<{} {}>'.format(self.__class__.__name__,
#             repr(self.__dict__)[1:-1])
# 
# Path: stator/lib.py



import asyncio
import cbor
from collections import namedtuple
from functools import partial
from .loop import parse_message, blocking_io, Socket
from .lib import dll
from . import redis



LOOP = None
FUTURES = {}
_RedisResponse = namedtuple("_RedisResponse", ["future_id", "value"])


class Redis(Socket):

    def __init__(self, *, host='127.0.0.1', port=6379, db=0):
        id = redis.connect_ip(host=host, port=port, db=db)
        super(Redis, self).__init__(id)

    def execute(self, args):
        cmd_id = redis.command(self.id, args)
        f = asyncio.Future(loop=LOOP)
        f.add_done_callback(partial(FUTURES.pop, cmd_id))
        FUTURES[cmd_id] = f
        return f

    def parse_message(self, input):
        req_id = cbor.load(input)
        data = cbor.load(input)
        return _RedisResponse(req_id, data)


def dispatch_messages(loop, callback):
    while True:

# Path: stator/loop.py
# def parse_message(self, input):
#     raise RuntimeError("abstract method")
# 
# @_skip
# def blocking_io():
#     return False
# 
# class Socket(object):
# 
#     def __init__(self, id):
#         assert isinstance(id, INT_TYPES), (type(id), id)
#         self.id = id
#         TABLE.add(self)
# 
#     def close(self):
#         TABLE.remove(self.id)
# 
#     def parse_message(self, input):
#         raise RuntimeError("abstract method")
# 
#     def __repr__(self):
#         return '<{} {}>'.format(self.__class__.__name__,
#             repr(self.__dict__)[1:-1])
# 
# Path: stator/lib.py



# Path: stator/aio.py
import asyncio
import cbor
from collections import namedtuple
from functools import partial
from .loop import parse_message, blocking_io, Socket
from .lib import dll
from . import redis




LOOP = None
FUTURES = {}
_RedisResponse = namedtuple("_RedisResponse", ["future_id", "value"])


class Redis(Socket):

    def __init__(self, *, host='127.0.0.1', port=6379, db=0):
        id = redis.connect_ip(host=host, port=port, db=db)
        super(Redis, self).__init__(id)

    def execute(self, args):
        cmd_id = redis.command(self.id, args)
        f = asyncio.Future(loop=LOOP)
        f.add_done_callback(partial(FUTURES.pop, cmd_id))
        FUTURES[cmd_id] = f
        return f

    def parse_message(self, input):
        req_id = cbor.load(input)
        data = cbor.load(input)
        return _RedisResponse(req_id, data)


def dispatch_messages(loop, callback):
    while True:

        message = dll.stator_next_message(parse_message, blocking_io)