# Path: opendiamond/protocol.py
# PORT = 5872
# NONCE_LEN = 16
# NULL_NONCE = b'\x00' * NONCE_LEN
# class DiamondRPCFailure(RPCError):
# class DiamondRPCFCacheMiss(RPCError):
# class DiamondRPCCookieExpired(RPCError):
# class DiamondRPCSchemeNotSupported(RPCError):
# class XDR_attribute(XDRStruct):
# class XDR_object(XDRStruct):
# class XDR_blob_list(XDRStruct):
# class XDR_filter_config(XDRStruct):
# class XDR_setup(XDRStruct):
# class XDR_blob_data(XDRStruct):
# class XDR_start(XDRStruct):
# class XDR_stat(XDRStruct):
# class XDR_filter_stats(XDRStruct):
# class XDR_search_stats(XDRStruct):
# class XDR_session_var(XDRStruct):
# class XDR_session_vars(XDRStruct):
# class XDR_reexecute(XDRStruct):
# class XDR_retrain(XDRStruct):
# class XDR_attribute_list(XDRStruct):
# 
# Path: opendiamond/rpc.py
# class RPCHeader(XDRStruct):
#     '''An RPC message header.'''
# 
#     ENCODED_LENGTH = 16
# 
#     members = (
#         'sequence', XDR.uint(),
#         'status', XDR.int(),
#         'cmd', XDR.int(),
#         'datalen', XDR.uint(),
#     )
# 
# RPC_PENDING = -1
# 
# class RPCError(Exception):
#     '''Base class for RPC error codes.'''
#     code = None
# 
#     # pylint doesn't know about __subclasses__
#     # pylint: disable=no-member
#     @classmethod
#     def get_class(cls, code):
#         '''Return the error subclass for the specified code.'''
#         for c in cls.__subclasses__():
#             if c.code == code:
#                 return c
#             try:
#                 return c.get_class(code)
#             except KeyError:
#                 pass
#         raise KeyError('Error code %d' % code)
#     # pylint: enable=no-member
# 
# class RPCEncodingError(RPCError):
#     '''Bad XDR structure.'''
#     code = -2
# 
# class ConnectionFailure(Exception):
#     '''RPC connection died.'''


from builtins import next
from builtins import str
from builtins import object
from tornado import gen, stack_context
from tornado.iostream import IOStream
from opendiamond import protocol
from opendiamond.rpc import (
    RPCHeader, RPC_PENDING, RPCError, RPCEncodingError, ConnectionFailure)
import itertools
import logging
import socket
                raise RPCError.get_class(status)()
            except KeyError:
                err = RPCError()
                err.code = status
                raise err
        else:
            reply = None
        if callback is not None:
            callback(reply)

    # We intentionally have a catch-all exception handler
    # pylint: disable=broad-except
    @gen.engine
    def _reply_handler(self):
        '''Reply-handling coroutine.  Must not throw exceptions.'''
        while True:
            try:
                buf = yield gen.Task(self._read, RPCHeader.ENCODED_LENGTH)
                hdr = RPCHeader.decode(buf)
                # Always read the body, regardless of encoding errors, to
                # keep the stream in sync
                data = yield gen.Task(self._read, hdr.datalen)
            except ConnectionFailure:
                # Error out pending callbacks
                callbacks = list(self._pending.values())
                self._pending.clear()
                for callback in callbacks:
                    callback(None, '')
                return


# Path: opendiamond/protocol.py
# PORT = 5872
# NONCE_LEN = 16
# NULL_NONCE = b'\x00' * NONCE_LEN
# class DiamondRPCFailure(RPCError):
# class DiamondRPCFCacheMiss(RPCError):
# class DiamondRPCCookieExpired(RPCError):
# class DiamondRPCSchemeNotSupported(RPCError):
# class XDR_attribute(XDRStruct):
# class XDR_object(XDRStruct):
# class XDR_blob_list(XDRStruct):
# class XDR_filter_config(XDRStruct):
# class XDR_setup(XDRStruct):
# class XDR_blob_data(XDRStruct):
# class XDR_start(XDRStruct):
# class XDR_stat(XDRStruct):
# class XDR_filter_stats(XDRStruct):
# class XDR_search_stats(XDRStruct):
# class XDR_session_var(XDRStruct):
# class XDR_session_vars(XDRStruct):
# class XDR_reexecute(XDRStruct):
# class XDR_retrain(XDRStruct):
# class XDR_attribute_list(XDRStruct):
# 
# Path: opendiamond/rpc.py
# class RPCHeader(XDRStruct):
#     '''An RPC message header.'''
# 
#     ENCODED_LENGTH = 16
# 
#     members = (
#         'sequence', XDR.uint(),
#         'status', XDR.int(),
#         'cmd', XDR.int(),
#         'datalen', XDR.uint(),
#     )
# 
# RPC_PENDING = -1
# 
# class RPCError(Exception):
#     '''Base class for RPC error codes.'''
#     code = None
# 
#     # pylint doesn't know about __subclasses__
#     # pylint: disable=no-member
#     @classmethod
#     def get_class(cls, code):
#         '''Return the error subclass for the specified code.'''
#         for c in cls.__subclasses__():
#             if c.code == code:
#                 return c
#             try:
#                 return c.get_class(code)
#             except KeyError:
#                 pass
#         raise KeyError('Error code %d' % code)
#     # pylint: enable=no-member
# 
# class RPCEncodingError(RPCError):
#     '''Bad XDR structure.'''
#     code = -2
# 
# class ConnectionFailure(Exception):
#     '''RPC connection died.'''


# Path: opendiamond/blaster/rpc.py
from builtins import next
from builtins import str
from builtins import object
from tornado import gen, stack_context
from tornado.iostream import IOStream
from opendiamond import protocol
from opendiamond.rpc import (
    RPCHeader, RPC_PENDING, RPCError, RPCEncodingError, ConnectionFailure)
import itertools
import logging
import socket

                raise RPCError.get_class(status)()
            except KeyError:
                err = RPCError()
                err.code = status
                raise err
        else:
            reply = None
        if callback is not None:
            callback(reply)

    # We intentionally have a catch-all exception handler
    # pylint: disable=broad-except
    @gen.engine
    def _reply_handler(self):
        '''Reply-handling coroutine.  Must not throw exceptions.'''
        while True:
            try:
                buf = yield gen.Task(self._read, RPCHeader.ENCODED_LENGTH)
                hdr = RPCHeader.decode(buf)
                # Always read the body, regardless of encoding errors, to
                # keep the stream in sync
                data = yield gen.Task(self._read, hdr.datalen)
            except ConnectionFailure:
                # Error out pending callbacks
                callbacks = list(self._pending.values())
                self._pending.clear()
                for callback in callbacks:
                    callback(None, '')
                return


            if hdr.status == RPC_PENDING: