from twisted.internet import defer
from twisted.protocols.basic import NetstringReceiver
import json


class SNError(Exception):
    def __init__(self, *args, **kwargs):
        Exception.__init__(self, args, kwargs)
        self.code = args[1]
        self.request = args[2]


class SNProtocol(NetstringReceiver):
    id_counter = 0

    def stringReceived(self, string):
        packet = json.loads(string.decode("ascii"))
        print("GOT: {0}".format(packet))
        if "reqid" in packet:
            if len(packet["reqid"]) > 2:
                type = packet["reqid"][:2]
                reqid = packet["reqid"][2:]
                if type == "RQ":
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == "RE":
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request["reqid"] = "RQ{0}".format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request["reqid"] = "RE{0}".format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request["reqid"][2:]
        r = {"Error": code, "Request": request, "reqid": "RE{0}".format(str(reqid))}
        self._sendPacket(r)

    def _sendPacket(self, request):
        print("SENT: {0}".format(request))
        json_str = json.dumps(request)
        self.sendString(json_str.encode("ascii"))

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d

    def errorChecker(self, packet):
        if "Error" in packet:
            raise SNError("", int(packet["Error"]), packet["Request"])
        return packet

757b2587b8e1565d776e00641dab38f2849ac9cf
<mask token>


class SNProtocol(NetstringReceiver):
    <mask token>

    def stringReceived(self, string):
        packet = json.loads(string.decode('ascii'))
        print('GOT: {0}'.format(packet))
        if 'reqid' in packet:
            if len(packet['reqid']) > 2:
                type = packet['reqid'][:2]
                reqid = packet['reqid'][2:]
                if type == 'RQ':
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == 'RE':
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request['reqid'] = 'RQ{0}'.format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request['reqid'] = 'RE{0}'.format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request['reqid'][2:]
        r = {'Error': code, 'Request': request, 'reqid': 'RE{0}'.format(str
            (reqid))}
        self._sendPacket(r)
    <mask token>

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d
    <mask token>

<mask token>


class SNProtocol(NetstringReceiver):
    <mask token>

    def stringReceived(self, string):
        packet = json.loads(string.decode('ascii'))
        print('GOT: {0}'.format(packet))
        if 'reqid' in packet:
            if len(packet['reqid']) > 2:
                type = packet['reqid'][:2]
                reqid = packet['reqid'][2:]
                if type == 'RQ':
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == 'RE':
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request['reqid'] = 'RQ{0}'.format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request['reqid'] = 'RE{0}'.format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request['reqid'][2:]
        r = {'Error': code, 'Request': request, 'reqid': 'RE{0}'.format(str
            (reqid))}
        self._sendPacket(r)

    def _sendPacket(self, request):
        print('SENT: {0}'.format(request))
        json_str = json.dumps(request)
        self.sendString(json_str.encode('ascii'))

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d

    def errorChecker(self, packet):
        if 'Error' in packet:
            raise SNError('', int(packet['Error']), packet['Request'])
        return packet

<mask token>


class SNProtocol(NetstringReceiver):
    id_counter = 0

    def stringReceived(self, string):
        packet = json.loads(string.decode('ascii'))
        print('GOT: {0}'.format(packet))
        if 'reqid' in packet:
            if len(packet['reqid']) > 2:
                type = packet['reqid'][:2]
                reqid = packet['reqid'][2:]
                if type == 'RQ':
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == 'RE':
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request['reqid'] = 'RQ{0}'.format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request['reqid'] = 'RE{0}'.format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request['reqid'][2:]
        r = {'Error': code, 'Request': request, 'reqid': 'RE{0}'.format(str
            (reqid))}
        self._sendPacket(r)

    def _sendPacket(self, request):
        print('SENT: {0}'.format(request))
        json_str = json.dumps(request)
        self.sendString(json_str.encode('ascii'))

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d

    def errorChecker(self, packet):
        if 'Error' in packet:
            raise SNError('', int(packet['Error']), packet['Request'])
        return packet

from twisted.internet import defer
from twisted.protocols.basic import NetstringReceiver
import json


class SNError(Exception):

    def __init__(self, *args, **kwargs):
        Exception.__init__(self, args, kwargs)
        self.code = args[1]
        self.request = args[2]


class SNProtocol(NetstringReceiver):
    id_counter = 0

    def stringReceived(self, string):
        packet = json.loads(string.decode('ascii'))
        print('GOT: {0}'.format(packet))
        if 'reqid' in packet:
            if len(packet['reqid']) > 2:
                type = packet['reqid'][:2]
                reqid = packet['reqid'][2:]
                if type == 'RQ':
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == 'RE':
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request['reqid'] = 'RQ{0}'.format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request['reqid'] = 'RE{0}'.format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request['reqid'][2:]
        r = {'Error': code, 'Request': request, 'reqid': 'RE{0}'.format(str
            (reqid))}
        self._sendPacket(r)

    def _sendPacket(self, request):
        print('SENT: {0}'.format(request))
        json_str = json.dumps(request)
        self.sendString(json_str.encode('ascii'))

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d

    def errorChecker(self, packet):
        if 'Error' in packet:
            raise SNError('', int(packet['Error']), packet['Request'])
        return packet

from twisted.internet import defer
from twisted.protocols.basic import NetstringReceiver
import json


class SNError(Exception):
    def __init__(self, *args, **kwargs):
        Exception.__init__(self, args, kwargs)
        self.code = args[1]
        self.request = args[2]


class SNProtocol(NetstringReceiver):
    id_counter = 0

    def stringReceived(self, string):
        packet = json.loads(string.decode("ascii"))
        print("GOT: {0}".format(packet))
        if "reqid" in packet:
            if len(packet["reqid"]) > 2:
                type = packet["reqid"][:2]
                reqid = packet["reqid"][2:]
                if type == "RQ":
                    self.factory.service.handleRequest(packet, reqid, self)
                elif type == "RE":
                    if reqid in self.factory.requests:
                        self.factory.requests[reqid].callback(packet)
                        self.factory.requests.pop(reqid)

    def sendRequest(self, request):
        reqid = str(self.id_counter)
        request["reqid"] = "RQ{0}".format(reqid)
        self._sendPacket(request)
        d = self.createDeferred(reqid)
        self.id_counter += 1
        return d

    def sendResponse(self, request, reqid):
        request["reqid"] = "RE{0}".format(str(reqid))
        self._sendPacket(request)

    def sendError(self, code, request):
        reqid = request["reqid"][2:]
        r = {"Error": code, "Request": request, "reqid": "RE{0}".format(str(reqid))}
        self._sendPacket(r)

    def _sendPacket(self, request):
        print("SENT: {0}".format(request))
        json_str = json.dumps(request)
        self.sendString(json_str.encode("ascii"))

    def connectionMade(self):
        self.factory.service.connectionMade(self)

    def createDeferred(self, reqid):
        d = defer.Deferred()
        d.addCallback(self.errorChecker)
        self.factory.requests[reqid] = d
        return d

    def errorChecker(self, packet):
        if "Error" in packet:
            raise SNError("", int(packet["Error"]), packet["Request"])
        return packet
