# Path: sio/protocol/rpc.py
# class TimeoutError(Exception):
# class ProtocolError(Exception):
# class RemoteError(Exception):
# class NoSuchMethodError(RemoteError):
# class WorkerRPC(NetstringReceiver):
#     def __init__(self, err, tb=None, uid=None):
#     def __init__(self, err=None, uid=None):
# def makeRemoteException(msg, uid=None):
#     def __init__(self, server=False, timeout=DEFAULT_TIMEOUT):
#     def connectionMade(self):
#     def connectionLost(self, reason):
#     def _processMessage(self, msg):
#     def getHelloData(self):
#     def stringReceived(self, string):
#     def _reply(self, value, request=None):
#     def _replyError(self, err, request=None):
#     def _timeout(self, rid):
#     def sendMsg(self, msg_type, **kwargs):
#     def call(self, cmd, *args, **kwargs):
#         def cb(ignore):
#     MAX_LENGTH = 2 ** 20  # 1MB should be enough
#     DEFAULT_TIMEOUT = 30


from twisted.internet.protocol import ServerFactory
from twisted.internet import defer
from sio.protocol import rpc
from twisted.logger import Logger
from __future__ import absolute_import

log = Logger()


class DuplicateWorker(Exception):
    """A worker connected twice"""


class WorkerRejected(Exception):
    """This worker was rejected for some reason."""


class WorkerServer(rpc.WorkerRPC):
    def __init__(self):
        rpc.WorkerRPC.__init__(self, server=True)
        self.ready.addCallback(self.established)
        self.name = None
        self.uniqueID = None

    def established(self, ignore=None):
        addr = self.transport.getPeer()
        self.clientInfo['host'] = (addr.host, addr.port)
        self.name = self.clientInfo.get('name', '<unnamed>')
        self.uniqueID = '%s@%s:%d' % (self.name, addr.host, addr.port)
        log.info('{addr!s} connected, name: {name}', addr=addr, name=self.name)
        return self.factory.workerConnected(self)

    def connectionLost(self, reason):

# Path: sio/protocol/rpc.py
# class TimeoutError(Exception):
# class ProtocolError(Exception):
# class RemoteError(Exception):
# class NoSuchMethodError(RemoteError):
# class WorkerRPC(NetstringReceiver):
#     def __init__(self, err, tb=None, uid=None):
#     def __init__(self, err=None, uid=None):
# def makeRemoteException(msg, uid=None):
#     def __init__(self, server=False, timeout=DEFAULT_TIMEOUT):
#     def connectionMade(self):
#     def connectionLost(self, reason):
#     def _processMessage(self, msg):
#     def getHelloData(self):
#     def stringReceived(self, string):
#     def _reply(self, value, request=None):
#     def _replyError(self, err, request=None):
#     def _timeout(self, rid):
#     def sendMsg(self, msg_type, **kwargs):
#     def call(self, cmd, *args, **kwargs):
#         def cb(ignore):
#     MAX_LENGTH = 2 ** 20  # 1MB should be enough
#     DEFAULT_TIMEOUT = 30


# Path: sio/sioworkersd/server.py
from twisted.internet.protocol import ServerFactory
from twisted.internet import defer
from sio.protocol import rpc
from twisted.logger import Logger

from __future__ import absolute_import

log = Logger()


class DuplicateWorker(Exception):
    """A worker connected twice"""


class WorkerRejected(Exception):
    """This worker was rejected for some reason."""


class WorkerServer(rpc.WorkerRPC):
    def __init__(self):
        rpc.WorkerRPC.__init__(self, server=True)
        self.ready.addCallback(self.established)
        self.name = None
        self.uniqueID = None

    def established(self, ignore=None):
        addr = self.transport.getPeer()
        self.clientInfo['host'] = (addr.host, addr.port)
        self.name = self.clientInfo.get('name', '<unnamed>')
        self.uniqueID = '%s@%s:%d' % (self.name, addr.host, addr.port)
        log.info('{addr!s} connected, name: {name}', addr=addr, name=self.name)
        return self.factory.workerConnected(self)

    def connectionLost(self, reason):

        rpc.WorkerRPC.connectionLost(self, reason)