# Path: examples/tabbedchat/tabbedchat/service.py
# class BaseService(object):
#     _method_prefix = None
# 
#     def configure(self, loop):
#         """Usually used for getting dependencies"""
# 
#     def _checkname(self, val):
#         if not isinstance(val, str):
#             return False
#         if not val.startswith(self._method_prefix):
#             return False
#         val = val[len(self._method_prefix):]
#         if val.startswith('_'):
#             return False
#         return True
# 
#     def __call__(self, msg):
#         if len(msg) < 2:
#             log.warning("Wrong message %r", msg)
#             return
#         if msg[1] == b'heartbeat':
#             return
#         elif msg[1] == b'connect':
#             return
#         elif msg[1] == b'message':
#             try:
#                 data = json.loads(msg[2].decode('utf-8'))
#             except ValueError:
#                 log.warning("Can't unpack json")
#                 return
#             if not data or not self._checkname(data[0]):
#                 log.warning("Wrong json")
#                 return
#             try:
#                 usr = User(cid=msg[0])
#                 meth = getattr(self, data[0][len(self._method_prefix):])
#                 meth(usr, *data[1:])
#             except Exception:
#                 log.exception("Error handling %r", data[0])
#         elif msg[1] == b'msgfrom':
#             try:
#                 data = json.loads(msg[3].decode('utf-8'))
#             except ValueError:
#                 log.warning("Can't unpack json")
#                 return
#             if not data or not self._checkname(data[0]):
#                 log.warning("Wrong json")
#                 return
#             try:
#                 usr = User(cid=msg[0], uid=int(msg[2].split(b':')[1]))
#                 getattr(self, data[0][len('auth.'):])(usr, *data[1:])
#             except Exception:
#                 log.exception("Error handling %r", data[0])
#         elif msg[1] == b'disconnect':
#             self._disconnect_(User(cid=msg[0]))
#         elif msg[1] == b'sync':
#             self._sync_(msg[2:])
#         else:
#             log.warning("Wrong message %r", msg)
#             return
# 
# class User(object):
# 
#     def __init__(self, cid=None, uid=None):
#         self.cid = cid
#         self.uid = uid


import json
import time
import logging
from .service import BaseService, User
                {"text": txt, "author": username, "uid": usr.uid})),
            (b"LTRIM", room_history, b'-100', b'-1'),
            ))
        self._output.publish('room:{0}'.format(room), ['chat.message', room, {
            'author': username,
            'uid': usr.uid,
            'text': txt,
            }])

    def _disconnect_(self, user):
        uid = self._redis.execute(b'GET', b'conn:' + user.cid)
        if uid is None:
            return
        uid = int(uid)
        username, conn, rooms = self._redis.bulk((
            (b'GET', 'user:{0}:name'.format(uid)),
            (b'GET', 'user:{0}:conn'.format(uid)),
            (b'SMEMBERS', 'user:{0}:rooms'.format(uid)),
            ))
        if conn != user.cid:
            # it's old, already dropped connection
            return
        username = username.decode('utf-8')
        bulk = []
        for r in rooms:
            r = int(r)
            room_history = 'room:{0}:history'.format(r)
            rchannel = 'room:{0}'.format(r)
            bulk.append((b'SREM', 'room:{0}:users'.format(r), str(uid)))
            bulk.append((b"RPUSH", room_history, json.dumps(

# Path: examples/tabbedchat/tabbedchat/service.py
# class BaseService(object):
#     _method_prefix = None
# 
#     def configure(self, loop):
#         """Usually used for getting dependencies"""
# 
#     def _checkname(self, val):
#         if not isinstance(val, str):
#             return False
#         if not val.startswith(self._method_prefix):
#             return False
#         val = val[len(self._method_prefix):]
#         if val.startswith('_'):
#             return False
#         return True
# 
#     def __call__(self, msg):
#         if len(msg) < 2:
#             log.warning("Wrong message %r", msg)
#             return
#         if msg[1] == b'heartbeat':
#             return
#         elif msg[1] == b'connect':
#             return
#         elif msg[1] == b'message':
#             try:
#                 data = json.loads(msg[2].decode('utf-8'))
#             except ValueError:
#                 log.warning("Can't unpack json")
#                 return
#             if not data or not self._checkname(data[0]):
#                 log.warning("Wrong json")
#                 return
#             try:
#                 usr = User(cid=msg[0])
#                 meth = getattr(self, data[0][len(self._method_prefix):])
#                 meth(usr, *data[1:])
#             except Exception:
#                 log.exception("Error handling %r", data[0])
#         elif msg[1] == b'msgfrom':
#             try:
#                 data = json.loads(msg[3].decode('utf-8'))
#             except ValueError:
#                 log.warning("Can't unpack json")
#                 return
#             if not data or not self._checkname(data[0]):
#                 log.warning("Wrong json")
#                 return
#             try:
#                 usr = User(cid=msg[0], uid=int(msg[2].split(b':')[1]))
#                 getattr(self, data[0][len('auth.'):])(usr, *data[1:])
#             except Exception:
#                 log.exception("Error handling %r", data[0])
#         elif msg[1] == b'disconnect':
#             self._disconnect_(User(cid=msg[0]))
#         elif msg[1] == b'sync':
#             self._sync_(msg[2:])
#         else:
#             log.warning("Wrong message %r", msg)
#             return
# 
# class User(object):
# 
#     def __init__(self, cid=None, uid=None):
#         self.cid = cid
#         self.uid = uid


# Path: examples/tabbedchat/tabbedchat/chat.py
import json
import time
import logging
from .service import BaseService, User

                {"text": txt, "author": username, "uid": usr.uid})),
            (b"LTRIM", room_history, b'-100', b'-1'),
            ))
        self._output.publish('room:{0}'.format(room), ['chat.message', room, {
            'author': username,
            'uid': usr.uid,
            'text': txt,
            }])

    def _disconnect_(self, user):
        uid = self._redis.execute(b'GET', b'conn:' + user.cid)
        if uid is None:
            return
        uid = int(uid)
        username, conn, rooms = self._redis.bulk((
            (b'GET', 'user:{0}:name'.format(uid)),
            (b'GET', 'user:{0}:conn'.format(uid)),
            (b'SMEMBERS', 'user:{0}:rooms'.format(uid)),
            ))
        if conn != user.cid:
            # it's old, already dropped connection
            return
        username = username.decode('utf-8')
        bulk = []
        for r in rooms:
            r = int(r)
            room_history = 'room:{0}:history'.format(r)
            rchannel = 'room:{0}'.format(r)
            bulk.append((b'SREM', 'room:{0}:users'.format(r), str(uid)))
            bulk.append((b"RPUSH", room_history, json.dumps(

                {"kind": "left", "author": username, "uid": uid})))