<commit_before>import argparse
from lxml import etree
from tornado import gen, ioloop, tcpserver
from tornado.iostream import IOStream, StreamClosedError
import sdminfoxml_parser, obsxml_parser, antxml_parser


# parse name of server(s)
parser = argparse.ArgumentParser()
parser.add_argument("name", type=str, help="Name of document to expect (xml, sdminfo, obs, ant, all)")
parser.add_argument("--port", default=0, type=int, help="Optional port number for xml listening")
opts = parser.parse_args()
docname = opts.name.lower()
port = opts.port

# standard CBE addresses and ports
sdminfoaddress = '172.16.58.3'
sdminfoport = 55002 
obsaddress = '192.168.127.12'
obsport = 53001
antaddress = '192.168.3.11'
antport = 53000

assert docname in ["xml", "sdminfo", "obs", "ant", "all"]

class XMLServer(tcpserver.TCPServer):

    @gen.coroutine
    def handle_stream(self, stream, address):
        try:
            msg = yield stream.read_until_close()
            print("Recieved msg of length {0}".format(len(msg)))

            root = etree.fromstring(msg)
            for child in root:
                print(child.tag, child.attrib)

        except StreamClosedError:
            print("Error connecting")
            yield gen.sleep(5)


class SDMinfoServer(tcpserver.TCPServer):

    @property
    def address:
        return sdminfoaddress

    @gen.coroutine
    def handle_stream(self, stream, address):
        try:
            msg = yield stream.read_until_close()
            print("Recieved msg of length {0}".format(len(msg)))

            sdminfo = sdminfoxml_parser.parseString(msg)
            print('Parsed sdminfo: {0}, {1}'.format(sdminfo.ScanNumber, sdminfo.scanIntents))

        except StreamClosedError:
            print("Error connecting")
            yield gen.sleep(5)


class ObsServer(tcpserver.TCPServer):

    @property
    def address:
        return obsaddress

    @gen.coroutine
    def handle_stream(self, stream, address):
        try:
            msg = yield stream.read_until_close()
            print("Recieved msg of length {0}".format(len(msg)))

            obsinfo = obsxml_parser.parseString(msg)
            print('Parsed obsinfo: {0}, {1}'.format('obs test', 'obs test'))

        except StreamClosedError:
            print("Error connecting")
            yield gen.sleep(5)


class AntServer(tcpserver.TCPServer):

    @property
    def address:
        return antaddress

    @gen.coroutine
    def handle_stream(self, stream, address):
        try:
            msg = yield stream.read_until_close()
            print("Recieved msg of length {0}".format(len(msg)))

            antinfo = antxml_parser.parseString(msg)
            print('Parsed antinfo: {0}, {1}'.format('ant test', 'ant test'))

        except StreamClosedError:
            print("Error connecting")
            yield gen.sleep(5)


if __name__ == '__main__':
    servers = []

    if docname in ['xml', 'all']:
        address = 'localhost'

        server = XMLServer()
        server.listen(port, address=address)
        servers.append(server)
        print('Started XMLServer at {0}:{1}'.format(server.address, port))

    if docname in ['sdminfo', 'all']:
        if not port: port = sdminfoport

        server = SDMinfoServer()
        server.listen(port, address=server.address)
        servers.append(server)
        print('Started SDMinfoServer at {0}:{1}'.format(server.address, port))

    if docname in ['obs', 'all']:
        if not port: port = obsport

        server = ObsServer()
        server.listen(port, address=server.address)
        servers.append(server)
        print('Started ObsServer at {0}:{1}'.format(server.address, port))

    if docname in ['antinfo', 'all']:
        if not port: port = antport

        server = AntServer()
        server.listen(port, address=server.address)
        servers.append(server)
        print('Started AntServer at {0}:{1}'.format(server.address, port))

    io_loop = ioloop.IOLoop.current()
    io_loop.start()<commit_msg>pulling out references to tcp servers for multicast stuff<commit_after>import argparse
from lxml import etree
from tornado import gen, ioloop, tcpserver
from tornado.iostream import IOStream, StreamClosedError
import sdminfoxml_parser, obsxml_parser, antxml_parser


# parse name of server(s)
parser = argparse.ArgumentParser()
parser.add_argument("--port", default=0, type=int, help="Optional port number for xml listening")
opts = parser.parse_args()
port = opts.port


# standard CBE addresses and ports
sdminfoaddress = '172.16.58.3'
sdminfoport = 55002 
obsaddress = '192.168.127.12'
obsport = 53001
antaddress = '192.168.3.11'
antport = 53000


class XMLServer(tcpserver.TCPServer):

    def __init__(self, address=None, port=None):
        super(self.__class__, self).__init__()
        self.address = address
        self.port = port


    @gen.coroutine
    def handle_stream(self, stream, address):
        try:
            msg = yield stream.read_until_close()
            print("Recieved msg of length {0}".format(len(msg)))

            root = etree.fromstring(msg)
            for child in root:
                print(child.tag, child.attrib)

        except StreamClosedError:
            print("Error connecting")
            yield gen.sleep(5)

if __name__ == '__main__':

    server = XMLServer(address='localhost', port=port)
    server.listen(server.port, address=server.address)
    servers.append(server)

    io_loop = ioloop.IOLoop.current()
    io_loop.start()

    print('Started XMLServer at {0}:{1}'.format(server.address, server.port))
{'fraction_non_alphanumeric': 0.11575735821966977, 'fraction_numerical': 0.020459440057430008, 'mean_word_length': 2.886331938633194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 6, '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': '6691618', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1693, 'n_words': 420}