<commit_before>"""Riemann protocol buffer client"""

from __future__ import absolute_import

import abc
import socket
import struct

import riemann.riemann_pb2


class Transport(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, host='localhost', port=5555):
        self.host = host
        self.port = port

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.disconnect()

    @abc.abstractmethod
    def connect(self):
        pass

    @abc.abstractmethod
    def disconnect(self):
        pass

    @abc.abstractmethod
    def write(self):
        pass

    @property
    def address(self):
        return self.host, self.port


class UDPTransport(Transport):
    def connect(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def disconnect(self):
        self.socket.close()

    def write(self, message):
        self.socket.sendto(message.SerializeToString(), self.address)


class TCPTransport(Transport):
    def connect(self):
        self.socket = socket.create_connection(self.address)

    def disconnect(self):
        self.socket.close()

    def write(self, message):
        # Send the message to the server
        data = message.SerializeToString()
        self.socket.send(struct.pack('!I', len(data)) + data)

        # Return the server's response
        response_len = struct.unpack('!I', self.socket.recv(4))[0]
        response = riemann.riemann_pb2.Msg()
        response.ParseFromString(self.socket.recv(response_len))

        # Temporary sanity check, to be replaced with a retrying transport
        assert not response.ok

        return response


class Client(object):
    """An abstract Riemann client"""

    def __init__(self, transport=None):
        self.transport = transport or UDPTransport<commit_msg>Riemann error handling and basic RetryingTCPTransport<commit_after>"""Riemann protocol buffer client"""

from __future__ import absolute_import

import abc
import socket
import struct

import riemann.riemann_pb2


class RiemannError(Exception):
    """Error class for errors recived from the Riemann server"""
    pass


class Transport(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, host='localhost', port=5555):
        self.host = host
        self.port = port

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.disconnect()

    @abc.abstractmethod
    def connect(self):
        pass

    @abc.abstractmethod
    def disconnect(self):
        pass

    @abc.abstractmethod
    def write(self):
        pass

    @property
    def address(self):
        return self.host, self.port


class UDPTransport(Transport):
    def connect(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def disconnect(self):
        self.socket.close()

    def write(self, message):
        self.socket.sendto(message.SerializeToString(), self.address)


class TCPTransport(Transport):
    def connect(self):
        self.socket = socket.create_connection(self.address)

    def disconnect(self):
        self.socket.close()

    def write(self, message):
        # Send the message to the server
        data = message.SerializeToString()
        self.socket.send(struct.pack('!I', len(data)) + data)

        # Return the server's response
        response_len = struct.unpack('!I', self.socket.recv(4))[0]
        response = riemann.riemann_pb2.Msg()
        response.ParseFromString(self.socket.recv(response_len))

        # Handle error messages
        if not response.ok:
            raise RiemannError(response.error)

        return response


class RetryingTCPTransport(TCPTransport):
    def write(self, message, tries=3):
        for i in xrange(reversed(tries)):
            try:
                response = super(RetryingTCPTransport, self).write(message)
            except (socket.error, struct.error, RiemannError) as error:
                if i <= 0:
                    raise error
            else:
                return response


class Client(object):
    """An abstract Riemann client"""

    def __init__(self, transport=None):
        self.transport = transport or UDPTransport
{'fraction_non_alphanumeric': 0.0978387171740646, 'fraction_numerical': 0.004183128050197537, 'mean_word_length': 2.6817792985457656, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3259223', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1176, 'n_words': 305}