#
# Autogenerated by Thrift Compiler (0.11.0)
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#
#  options string: py
#

from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException
from thrift.protocol.TProtocol import TProtocolException
from thrift.TRecursive import fix_spec

import sys
import logging
from .ttypes import *
from thrift.Thrift import TProcessor
from thrift.transport import TTransport
all_structs = []


class Iface(object):
    def getPreferredServer(self, serverTypes, serverGroups, failedServers):
        """
        Parameters:
         - serverTypes
         - serverGroups
         - failedServers
        """
        pass

    def getAllServersWithPreferredServer(self, serverTypes, serverGroups, failedServers):
        """
        Parameters:
         - serverTypes
         - serverGroups
         - failedServers
        """
        pass


class Client(Iface):
    def __init__(self, iprot, oprot=None):
        self._iprot = self._oprot = iprot
        if oprot is not None:
            self._oprot = oprot
        self._seqid = 0

    def getPreferredServer(self, serverTypes, serverGroups, failedServers):
        """
        Parameters:
         - serverTypes
         - serverGroups
         - failedServers
        """
        self.send_getPreferredServer(serverTypes, serverGroups, failedServers)
        return self.recv_getPreferredServer()

    def send_getPreferredServer(self, serverTypes, serverGroups, failedServers):
        self._oprot.writeMessageBegin('getPreferredServer', TMessageType.CALL, self._seqid)
        args = getPreferredServer_args()
        args.serverTypes = serverTypes
        args.serverGroups = serverGroups
        args.failedServers = failedServers
        args.write(self._oprot)
        self._oprot.writeMessageEnd()
        self._oprot.trans.flush()

    def recv_getPreferredServer(self):
        iprot = self._iprot
        (fname, mtype, rseqid) = iprot.readMessageBegin()
        if mtype == TMessageType.EXCEPTION:
            x = TApplicationException()
            x.read(iprot)
            iprot.readMessageEnd()
            raise x
        result = getPreferredServer_result()
        result.read(iprot)
        iprot.readMessageEnd()
        if result.success is not None:
            return result.success
        if result.error is not None:
            raise result.error
        raise TApplicationException(TApplicationException.MISSING_RESULT, "getPreferredServer failed: unknown result")

    def getAllServersWithPreferredServer(self, serverTypes, serverGroups, failedServers):
        """
        Parameters:
         - serverTypes
         - serverGroups
         - failedServers
        """
        self.send_getAllServersWithPreferredServer(serverTypes, serverGroups, failedServers)
        return self.recv_getAllServersWithPreferredServer()

    def send_getAllServersWithPreferredServer(self, serverTypes, serverGroups, failedServers):
        self._oprot.writeMessageBegin('getAllServersWithPreferredServer', TMessageType.CALL, self._seqid)
        args = getAllServersWithPreferredServer_args()
        args.serverTypes = serverTypes
        args.serverGroups = serverGroups
        args.failedServers = failedServers
        args.write(self._oprot)
        self._oprot.writeMessageEnd()
        self._oprot.trans.flush()

    def recv_getAllServersWithPreferredServer(self):
        iprot = self._iprot
        (fname, mtype, rseqid) = iprot.readMessageBegin()
        if mtype == TMessageType.EXCEPTION:
            x = TApplicationException()
            x.read(iprot)
            iprot.readMessageEnd()
            raise x
        result = getAllServersWithPreferredServer_result()
        result.read(iprot)
        iprot.readMessageEnd()
        if result.success is not None:
            return result.success
        if result.error is not None:
            raise result.error
        raise TApplicationException(TApplicationException.MISSING_RESULT, "getAllServersWithPreferredServer failed: unknown result")


class Processor(Iface, TProcessor):
    def __init__(self, handler):
        self._handler = handler
        self._processMap = {}
        self._processMap["getPreferredServer"] = Processor.process_getPreferredServer
        self._processMap["getAllServersWithPreferredServer"] = Processor.process_getAllServersWithPreferredServer

    def process(self, iprot, oprot):
        (name, type, seqid) = iprot.readMessageBegin()
        if name not in self._processMap:
            iprot.skip(TType.STRUCT)
            iprot.readMessageEnd()
            x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name))
            oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)
            x.write(oprot)
            oprot.writeMessageEnd()
            oprot.trans.flush()
            return
        else:
            self._processMap[name](self, seqid, iprot, oprot)
        return True

    def process_getPreferredServer(self, seqid, iprot, oprot):
        args = getPreferredServer_args()
        args.read(iprot)
        iprot.readMessageEnd()
        result = getPreferredServer_result()
        try:
            result.success = self._handler.getPreferredServer(args.serverTypes, args.serverGroups, args.failedServers)
            msg_type = TMessageType.REPLY
        except TTransport.TTransportException:
            raise
        except SnappyException as error:
            msg_type = TMessageType.REPLY
            result.error = error
        except TApplicationException as ex:
            logging.exception('TApplication exception in handler')
            msg_type = TMessageType.EXCEPTION
            result = ex
        except Exception:
            logging.exception('Unexpected exception in handler')
            msg_type = TMessageType.EXCEPTION
            result = TApplicationException(TApplicationException.INTERNAL_ERROR, 'Internal error')
        oprot.writeMessageBegin("getPreferredServer", msg_type, seqid)
        result.write(oprot)
        oprot.writeMessageEnd()
        oprot.trans.flush()

    def process_getAllServersWithPreferredServer(self, seqid, iprot, oprot):
        args = getAllServersWithPreferredServer_args()
        args.read(iprot)
        iprot.readMessageEnd()
        result = getAllServersWithPreferredServer_result()
        try:
            result.success = self._handler.getAllServersWithPreferredServer(args.serverTypes, args.serverGroups, args.failedServers)
            msg_type = TMessageType.REPLY
        except TTransport.TTransportException:
            raise
        except SnappyException as error:
            msg_type = TMessageType.REPLY
            result.error = error
        except TApplicationException as ex:
            logging.exception('TApplication exception in handler')
            msg_type = TMessageType.EXCEPTION
            result = ex
        except Exception:
            logging.exception('Unexpected exception in handler')
            msg_type = TMessageType.EXCEPTION
            result = TApplicationException(TApplicationException.INTERNAL_ERROR, 'Internal error')
        oprot.writeMessageBegin("getAllServersWithPreferredServer", msg_type, seqid)
        result.write(oprot)
        oprot.writeMessageEnd()
        oprot.trans.flush()

# HELPER FUNCTIONS AND STRUCTURES


class getPreferredServer_args(object):
    """
    Attributes:
     - serverTypes
     - serverGroups
     - failedServers
    """


    def __init__(self, serverTypes=None, serverGroups=None, failedServers=None,):
        self.serverTypes = serverTypes
        self.serverGroups = serverGroups
        self.failedServers = failedServers

    def read(self, iprot):
        if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None:
            iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
            return
        iprot.readStructBegin()
        while True:
            (fname, ftype, fid) = iprot.readFieldBegin()
            if ftype == TType.STOP:
                break
            if fid == 1:
                if ftype == TType.SET:
                    self.serverTypes = set()
                    (_etype241, _size238) = iprot.readSetBegin()
                    for _i242 in range(_size238):
                        _elem243 = iprot.readI32()
                        self.serverTypes.add(_elem243)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            elif fid == 2:
                if ftype == TType.SET:
                    self.serverGroups = set()
                    (_etype247, _size244) = iprot.readSetBegin()
                    for _i248 in range(_size244):
                        _elem249 = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString()
                        self.serverGroups.add(_elem249)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            elif fid == 3:
                if ftype == TType.SET:
                    self.failedServers = set()
                    (_etype253, _size250) = iprot.readSetBegin()
                    for _i254 in range(_size250):
                        _elem255 = HostAddress()
                        _elem255.read(iprot)
                        self.failedServers.add(_elem255)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            else:
                iprot.skip(ftype)
            iprot.readFieldEnd()
        iprot.readStructEnd()

    def write(self, oprot):
        if oprot._fast_encode is not None and self.thrift_spec is not None:
            oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec]))
            return
        oprot.writeStructBegin('getPreferredServer_args')
        if self.serverTypes is not None:
            oprot.writeFieldBegin('serverTypes', TType.SET, 1)
            oprot.writeSetBegin(TType.I32, len(self.serverTypes))
            for iter256 in self.serverTypes:
                oprot.writeI32(iter256)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        if self.serverGroups is not None:
            oprot.writeFieldBegin('serverGroups', TType.SET, 2)
            oprot.writeSetBegin(TType.STRING, len(self.serverGroups))
            for iter257 in self.serverGroups:
                oprot.writeString(iter257.encode('utf-8') if sys.version_info[0] == 2 else iter257)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        if self.failedServers is not None:
            oprot.writeFieldBegin('failedServers', TType.SET, 3)
            oprot.writeSetBegin(TType.STRUCT, len(self.failedServers))
            for iter258 in self.failedServers:
                iter258.write(oprot)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        oprot.writeFieldStop()
        oprot.writeStructEnd()

    def validate(self):
        return

    def __repr__(self):
        L = ['%s=%r' % (key, value)
             for key, value in self.__dict__.items()]
        return '%s(%s)' % (self.__class__.__name__, ', '.join(L))

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not (self == other)
all_structs.append(getPreferredServer_args)
getPreferredServer_args.thrift_spec = (
    None,  # 0
    (1, TType.SET, 'serverTypes', (TType.I32, None, False), None, ),  # 1
    (2, TType.SET, 'serverGroups', (TType.STRING, 'UTF8', False), None, ),  # 2
    (3, TType.SET, 'failedServers', (TType.STRUCT, [HostAddress, None], False), None, ),  # 3
)


class getPreferredServer_result(object):
    """
    Attributes:
     - success
     - error
    """


    def __init__(self, success=None, error=None,):
        self.success = success
        self.error = error

    def read(self, iprot):
        if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None:
            iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
            return
        iprot.readStructBegin()
        while True:
            (fname, ftype, fid) = iprot.readFieldBegin()
            if ftype == TType.STOP:
                break
            if fid == 0:
                if ftype == TType.STRUCT:
                    self.success = HostAddress()
                    self.success.read(iprot)
                else:
                    iprot.skip(ftype)
            elif fid == 1:
                if ftype == TType.STRUCT:
                    self.error = SnappyException()
                    self.error.read(iprot)
                else:
                    iprot.skip(ftype)
            else:
                iprot.skip(ftype)
            iprot.readFieldEnd()
        iprot.readStructEnd()

    def write(self, oprot):
        if oprot._fast_encode is not None and self.thrift_spec is not None:
            oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec]))
            return
        oprot.writeStructBegin('getPreferredServer_result')
        if self.success is not None:
            oprot.writeFieldBegin('success', TType.STRUCT, 0)
            self.success.write(oprot)
            oprot.writeFieldEnd()
        if self.error is not None:
            oprot.writeFieldBegin('error', TType.STRUCT, 1)
            self.error.write(oprot)
            oprot.writeFieldEnd()
        oprot.writeFieldStop()
        oprot.writeStructEnd()

    def validate(self):
        return

    def __repr__(self):
        L = ['%s=%r' % (key, value)
             for key, value in self.__dict__.items()]
        return '%s(%s)' % (self.__class__.__name__, ', '.join(L))

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not (self == other)
all_structs.append(getPreferredServer_result)
getPreferredServer_result.thrift_spec = (
    (0, TType.STRUCT, 'success', [HostAddress, None], None, ),  # 0
    (1, TType.STRUCT, 'error', [SnappyException, None], None, ),  # 1
)


class getAllServersWithPreferredServer_args(object):
    """
    Attributes:
     - serverTypes
     - serverGroups
     - failedServers
    """


    def __init__(self, serverTypes=None, serverGroups=None, failedServers=None,):
        self.serverTypes = serverTypes
        self.serverGroups = serverGroups
        self.failedServers = failedServers

    def read(self, iprot):
        if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None:
            iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
            return
        iprot.readStructBegin()
        while True:
            (fname, ftype, fid) = iprot.readFieldBegin()
            if ftype == TType.STOP:
                break
            if fid == 1:
                if ftype == TType.SET:
                    self.serverTypes = set()
                    (_etype262, _size259) = iprot.readSetBegin()
                    for _i263 in range(_size259):
                        _elem264 = iprot.readI32()
                        self.serverTypes.add(_elem264)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            elif fid == 2:
                if ftype == TType.SET:
                    self.serverGroups = set()
                    (_etype268, _size265) = iprot.readSetBegin()
                    for _i269 in range(_size265):
                        _elem270 = iprot.readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString()
                        self.serverGroups.add(_elem270)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            elif fid == 3:
                if ftype == TType.SET:
                    self.failedServers = set()
                    (_etype274, _size271) = iprot.readSetBegin()
                    for _i275 in range(_size271):
                        _elem276 = HostAddress()
                        _elem276.read(iprot)
                        self.failedServers.add(_elem276)
                    iprot.readSetEnd()
                else:
                    iprot.skip(ftype)
            else:
                iprot.skip(ftype)
            iprot.readFieldEnd()
        iprot.readStructEnd()

    def write(self, oprot):
        if oprot._fast_encode is not None and self.thrift_spec is not None:
            oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec]))
            return
        oprot.writeStructBegin('getAllServersWithPreferredServer_args')
        if self.serverTypes is not None:
            oprot.writeFieldBegin('serverTypes', TType.SET, 1)
            oprot.writeSetBegin(TType.I32, len(self.serverTypes))
            for iter277 in self.serverTypes:
                oprot.writeI32(iter277)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        if self.serverGroups is not None:
            oprot.writeFieldBegin('serverGroups', TType.SET, 2)
            oprot.writeSetBegin(TType.STRING, len(self.serverGroups))
            for iter278 in self.serverGroups:
                oprot.writeString(iter278.encode('utf-8') if sys.version_info[0] == 2 else iter278)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        if self.failedServers is not None:
            oprot.writeFieldBegin('failedServers', TType.SET, 3)
            oprot.writeSetBegin(TType.STRUCT, len(self.failedServers))
            for iter279 in self.failedServers:
                iter279.write(oprot)
            oprot.writeSetEnd()
            oprot.writeFieldEnd()
        oprot.writeFieldStop()
        oprot.writeStructEnd()

    def validate(self):
        return

    def __repr__(self):
        L = ['%s=%r' % (key, value)
             for key, value in self.__dict__.items()]
        return '%s(%s)' % (self.__class__.__name__, ', '.join(L))

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not (self == other)
all_structs.append(getAllServersWithPreferredServer_args)
getAllServersWithPreferredServer_args.thrift_spec = (
    None,  # 0
    (1, TType.SET, 'serverTypes', (TType.I32, None, False), None, ),  # 1
    (2, TType.SET, 'serverGroups', (TType.STRING, 'UTF8', False), None, ),  # 2
    (3, TType.SET, 'failedServers', (TType.STRUCT, [HostAddress, None], False), None, ),  # 3
)


class getAllServersWithPreferredServer_result(object):
    """
    Attributes:
     - success
     - error
    """


    def __init__(self, success=None, error=None,):
        self.success = success
        self.error = error

    def read(self, iprot):
        if iprot._fast_decode is not None and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None:
            iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])
            return
        iprot.readStructBegin()
        while True:
            (fname, ftype, fid) = iprot.readFieldBegin()
            if ftype == TType.STOP:
                break
            if fid == 0:
                if ftype == TType.LIST:
                    self.success = []
                    (_etype283, _size280) = iprot.readListBegin()
                    for _i284 in range(_size280):
                        _elem285 = HostAddress()
                        _elem285.read(iprot)
                        self.success.append(_elem285)
                    iprot.readListEnd()
                else:
                    iprot.skip(ftype)
            elif fid == 1:
                if ftype == TType.STRUCT:
                    self.error = SnappyException()
                    self.error.read(iprot)
                else:
                    iprot.skip(ftype)
            else:
                iprot.skip(ftype)
            iprot.readFieldEnd()
        iprot.readStructEnd()

    def write(self, oprot):
        if oprot._fast_encode is not None and self.thrift_spec is not None:
            oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec]))
            return
        oprot.writeStructBegin('getAllServersWithPreferredServer_result')
        if self.success is not None:
            oprot.writeFieldBegin('success', TType.LIST, 0)
            oprot.writeListBegin(TType.STRUCT, len(self.success))
            for iter286 in self.success:
                iter286.write(oprot)
            oprot.writeListEnd()
            oprot.writeFieldEnd()
        if self.error is not None:
            oprot.writeFieldBegin('error', TType.STRUCT, 1)
            self.error.write(oprot)
            oprot.writeFieldEnd()
        oprot.writeFieldStop()
        oprot.writeStructEnd()

    def validate(self):
        return

    def __repr__(self):
        L = ['%s=%r' % (key, value)
             for key, value in self.__dict__.items()]
        return '%s(%s)' % (self.__class__.__name__, ', '.join(L))

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.__dict__ == other.__dict__

    def __ne__(self, other):
        return not (self == other)
all_structs.append(getAllServersWithPreferredServer_result)
getAllServersWithPreferredServer_result.thrift_spec = (
    (0, TType.LIST, 'success', (TType.STRUCT, [HostAddress, None], False), None, ),  # 0
    (1, TType.STRUCT, 'error', [SnappyException, None], None, ),  # 1
)
fix_spec(all_structs)
del all_structs

