# 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
        if self._stream is not None:
            self._stream.close()
        else:
            # close() called before connect().  Synthesize the close event
            # ourselves.
            self._handle_close()

    def _handle_close(self):
        if self._pending_read is not None:
            # The pending read callback will never be called.  Call it
            # ourselves to clean up.
            self._pending_read(None)
        if self._close_callback is not None:
            cb = self._close_callback
            self._close_callback = None
            cb()

    @gen.engine
    def _read(self, count, callback=None):
        if self._pending_read is not None:
            raise RuntimeError('Double read on connection')
        self._pending_read = stack_context.wrap((yield gen.Callback('read')))
        try:
            self._stream.read_bytes(count, callback=self._pending_read)
            buf = yield gen.Wait('read')
            if buf is None:
                # _handle_close() is cleaning us up
                raise ConnectionFailure('Connection closed')
        except IOError as e:
            self.close()

# 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

        if self._stream is not None:
            self._stream.close()
        else:
            # close() called before connect().  Synthesize the close event
            # ourselves.
            self._handle_close()

    def _handle_close(self):
        if self._pending_read is not None:
            # The pending read callback will never be called.  Call it
            # ourselves to clean up.
            self._pending_read(None)
        if self._close_callback is not None:
            cb = self._close_callback
            self._close_callback = None
            cb()

    @gen.engine
    def _read(self, count, callback=None):
        if self._pending_read is not None:
            raise RuntimeError('Double read on connection')
        self._pending_read = stack_context.wrap((yield gen.Callback('read')))
        try:
            self._stream.read_bytes(count, callback=self._pending_read)
            buf = yield gen.Wait('read')
            if buf is None:
                # _handle_close() is cleaning us up
                raise ConnectionFailure('Connection closed')
        except IOError as e:
            self.close()

            raise ConnectionFailure(str(e))