<commit_before>__author__ = 'tdpreece'
__author__ = 'tdpreece'
import logging
import time
import json
from collections import OrderedDict

import stomp

logger = logging.getLogger('tdl.client')
logger.addHandler(logging.NullHandler())


class Client(object):
    def __init__(self, hostname, port, username):
        self.hostname = hostname
        self.port = port

    def go_live_with(self, implementation_map):
        hosts = [(self.hostname, self.port)]
        try:
            conn = stomp.Connection(host_and_ports=hosts)
            conn.start()
            handling_strategy = RespondToAllRequests(implementation_map)
            listener = Listener(conn, handling_strategy)
            conn.connect(wait=True)
            remote_broker = RemoteBroker(conn)
            remote_broker.subscribe(listener)
            time.sleep(1)
            conn.disconnect()
        except Exception as e:
            logger.exception('Problem communicating with the broker.')

    def trial_run_with(self, implementation_map):
        hosts = [(self.hostname, self.port)]
        conn = stomp.Connection(host_and_ports=hosts)
        conn.start()
        conn.connect(wait=True)
        handling_strategy = PeekAtFirstRequest(implementation_map)
        listener = Listener(conn, handling_strategy)
        remote_broker = RemoteBroker(conn)
        remote_broker.subscribe(listener)
        time.sleep(1)
        conn.disconnect()

class RespondToAllRequests(object):
    def __init__(self, implementation_map):
        self.implementation_map = implementation_map

    def process_next_message_from(self, remote_broker, headers, message):
        response = Listener.respond_to(self.implementation_map, message)
        if response is not None:
            remote_broker.acknowledge(headers)
            remote_broker.publish(response)

class PeekAtFirstRequest(object):
    def __init__(self, implementation_map):
        self.implementation_map = implementation_map

    def process_next_message_from(self, remote_broker, headers, message):
        Listener.respond_to(self.implementation_map, message)

class Listener(stomp.ConnectionListener):
    def __init__(self, conn, handling_strategy):
        self.conn = conn
        self.remote_broker = RemoteBroker(self.conn)
        self.handling_strategy = handling_strategy

    def on_message(self, headers, message):
        self.handling_strategy.process_next_message_from(self.remote_broker, headers, message)<commit_msg>Add base class for handling strategy.<commit_after>__author__ = 'tdpreece'
__author__ = 'tdpreece'
import logging
import time
import json
from collections import OrderedDict

import stomp

logger = logging.getLogger('tdl.client')
logger.addHandler(logging.NullHandler())


class Client(object):
    def __init__(self, hostname, port, username):
        self.hostname = hostname
        self.port = port

    def go_live_with(self, implementation_map):
        hosts = [(self.hostname, self.port)]
        try:
            conn = stomp.Connection(host_and_ports=hosts)
            conn.start()
            handling_strategy = RespondToAllRequests(implementation_map)
            listener = Listener(conn, handling_strategy)
            conn.connect(wait=True)
            remote_broker = RemoteBroker(conn)
            remote_broker.subscribe(listener)
            time.sleep(1)
            conn.disconnect()
        except Exception as e:
            logger.exception('Problem communicating with the broker.')

    def trial_run_with(self, implementation_map):
        hosts = [(self.hostname, self.port)]
        conn = stomp.Connection(host_and_ports=hosts)
        conn.start()
        conn.connect(wait=True)
        handling_strategy = PeekAtFirstRequest(implementation_map)
        listener = Listener(conn, handling_strategy)
        remote_broker = RemoteBroker(conn)
        remote_broker.subscribe(listener)
        time.sleep(1)
        conn.disconnect()


class HandlingStrategy(object):
    def __init__(self, implementation_map):
        self.implementation_map = implementation_map


class RespondToAllRequests(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        response = Listener.respond_to(self.implementation_map, message)
        if response is not None:
            remote_broker.acknowledge(headers)
            remote_broker.publish(response)

class PeekAtFirstRequest(HandlingStrategy):
    def process_next_message_from(self, remote_broker, headers, message):
        Listener.respond_to(self.implementation_map, message)

class Listener(stomp.ConnectionListener):
    def __init__(self, conn, handling_strategy):
        self.conn = conn
        self.remote_broker = RemoteBroker(self.conn)
        self.handling_strategy = handling_strategy

    def on_message(self, headers, message):
        self.handling_strategy.process_next_message_from(self.remote_broker, headers, message)
{'fraction_non_alphanumeric': 0.08350222942845562, 'fraction_numerical': 0.0008107012565869477, 'mean_word_length': 3.2950391644908614, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1602226', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1362, 'n_words': 271}