# 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
        try:
            self._stream.write(data)
        except IOError:
            self.close()

    def _send_message(self, sequence, status, cmd, body=''):
        hdr = RPCHeader(sequence, status, cmd, len(body))
        self._write(hdr.encode() + body)

    @gen.engine
    def _call(self, cmd, request=None, reply_class=None, callback=None):
        if request is not None:
            body = request.encode()
        else:
            body = ''
        seq = next(self._sequence)
        self._pending[seq] = stack_context.wrap((yield gen.Callback('reply')))
        self._send_message(seq, RPC_PENDING, cmd, body)

        status, data = (yield gen.Wait('reply')).args
        if status == 0 and reply_class is not None:
            reply = reply_class.decode(data)
        elif data:
            raise RPCEncodingError('Unexpected reply data')
        elif status is None:
            raise ConnectionFailure('Connection closed')
        elif status != 0:
            try:
                raise RPCError.get_class(status)()
            except KeyError:

# 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

        try:
            self._stream.write(data)
        except IOError:
            self.close()

    def _send_message(self, sequence, status, cmd, body=''):
        hdr = RPCHeader(sequence, status, cmd, len(body))
        self._write(hdr.encode() + body)

    @gen.engine
    def _call(self, cmd, request=None, reply_class=None, callback=None):
        if request is not None:
            body = request.encode()
        else:
            body = ''
        seq = next(self._sequence)
        self._pending[seq] = stack_context.wrap((yield gen.Callback('reply')))
        self._send_message(seq, RPC_PENDING, cmd, body)

        status, data = (yield gen.Wait('reply')).args
        if status == 0 and reply_class is not None:
            reply = reply_class.decode(data)
        elif data:
            raise RPCEncodingError('Unexpected reply data')
        elif status is None:
            raise ConnectionFailure('Connection closed')
        elif status != 0:
            try:
                raise RPCError.get_class(status)()
            except KeyError:

                err = RPCError()