<commit_before>from twisted.internet.protocol import Factory, Protocol
from txsockjs.factory import SockJSResource
from .messaging import hxdispatcher

import json


class MessageHandlerProtocol(Protocol):
    dispatcher = hxdispatcher
    guid = None

    def dataReceived(self, data):

        """
            Takes "data" which we assume is json encoded
            If data has a uid attribute, we pass that to the dispatcher
            as the message_id so it will get carried through into any 
            return communications and be identifiable to the client

            falls back to just passing the message along...

        """

        try:
            data = json.loads(data)
            self.dispatcher.send(self.guid, data, message_id=data.get('subject_id'))
        except:
            self.dispatcher.send(self.guid, {'message':data})

    def connectionMade(self):
        """
            establish the address of this 
        """
        self.guid = self.dispatcher.add(self.transport)


    def connectionLost(self, something):
        # print 'connection lost:',something
        self.dispatcher.remove(self.guid)


def get_MessageHandler():
    return SockJSResource(Factory.forProtocol(MessageHandlerProtocol))<commit_msg>added doc strings<commit_after>from twisted.internet.protocol import Factory, Protocol
from txsockjs.factory import SockJSResource
from .messaging import hxdispatcher

import json


class MessageHandlerProtocol(Protocol):
    """
        A basic protocol for socket messaging 
        using a hendrix messaging dispatcher to handle
        addressing messages to active sockets from
        differnt contexts
    """
    dispatcher = hxdispatcher
    guid = None

    def dataReceived(self, data):

        """
            Takes "data" which we assume is json encoded
            If data has a uid attribute, we pass that to the dispatcher
            as the message_id so it will get carried through into any 
            return communications and be identifiable to the client

            falls back to just passing the message along...

        """

        try:
            data = json.loads(data)
            self.dispatcher.send(self.guid, data, message_id=data.get('subject_id'))
        except:
            self.dispatcher.send(self.guid, {'message':data})

    def connectionMade(self):
        """
            establish the address of this new connection and add it to the list of 
            sockets managed by the dispatcher
        """
        self.guid = self.dispatcher.add(self.transport)


    def connectionLost(self, something):
        """
            clean up the no longer useful socket in the dispatcher
        """
        self.dispatcher.remove(self.guid)


def get_MessageHandler():
    """
        create an instance of the SockJSResource for use 
        as a child to the main wsgi app
    """

    return SockJSResource(Factory.forProtocol(MessageHandlerProtocol))
{'fraction_non_alphanumeric': 0.0702886247877759, 'fraction_numerical': 0.0, 'mean_word_length': 2.4057803468208094, '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': '4420896', 'n_tokens_mistral': 755, 'n_tokens_neox': 720, 'n_words': 259}