# 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 opendiamond import protocol
from opendiamond.rpc import (
    RPCHeader, RPC_PENDING, RPCError, RPCEncodingError, ConnectionFailure)
import itertools
import logging
import socket
import binascii
#
#  This software is distributed under the terms of the Eclipse Public
#  License, Version 1.0 which can be found in the file named LICENSE.
#  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
#  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
#

"""Low-level RPC protocol implementation."""




_log = logging.getLogger(__name__)


class _RPCClientConnection(object):
    """An RPC client connection.
    This class is responsible for establishing a TCP connection,
    sending and receiving a nonce, sending RPC requests with given command and body,
    and receiving RPC replys."""

    def __init__(self):
        self._sequence = itertools.count()
        self._sock = None

    def connect(self, address, nonce=protocol.NULL_NONCE):
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self._sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)

        parts = address.split(':')

# 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/client/rpc.py
from builtins import next
from builtins import str
from builtins import object
from opendiamond import protocol
from opendiamond.rpc import (
    RPCHeader, RPC_PENDING, RPCError, RPCEncodingError, ConnectionFailure)
import itertools
import logging
import socket
import binascii

#
#  This software is distributed under the terms of the Eclipse Public
#  License, Version 1.0 which can be found in the file named LICENSE.
#  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
#  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
#

"""Low-level RPC protocol implementation."""




_log = logging.getLogger(__name__)


class _RPCClientConnection(object):
    """An RPC client connection.
    This class is responsible for establishing a TCP connection,
    sending and receiving a nonce, sending RPC requests with given command and body,
    and receiving RPC replys."""

    def __init__(self):
        self._sequence = itertools.count()
        self._sock = None

    def connect(self, address, nonce=protocol.NULL_NONCE):
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self._sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)

        parts = address.split(':')

        if len(parts) >= 2: