<commit_before>from __future__ import print_function
import threading
import time
import socket
import logging
import select


from DotStar_Emulator.emulator import config, strip_data

log = logging.getLogger("data")

__all__ = ["TCPReader", ]


class TCPReader(threading.Thread):
    def __init__(self):
        """
        Read emulator strip SPI data in a TCP socket.
        TCPReader runs in its own thread, and will acquire a lock from strip_data before
        writing data.

        HOST and PORT set in config.

        :return:
        """
        super(TCPReader, self).__init__()

        self.host = config.get("HOST")
        self.port = config.get("PORT")
        self.backlog = 5
        self.size = 1024
        self.server = None

        # Thread Loop
        self.running = True

        # Report back to the main thread if we could bind to the port or not. Main thread will not continue
        # if port was not bound to.
        self.startup = threading.Event()
        self.startup_success = False

    def open_socket(self):
        """
        Bind to the specified socket.  Set an event and report to the main thread if it was successful

        :return: None
        """
        try:
            self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server.setblocking(0)
            self.server.bind((self.host, self.port))
            self.server.listen(self.backlog)

            # Set events and return
            log.info("listening on '%s', %s", self.host, self.port)
            self.startup_success = True
            self.startup.set()
            return True
        except socket.error:
            if self.server:
                self.server.close()

            # Set events and return
            log.exception("Could not bind socket '%s', %s", self.host, self.port)
            self.startup_success = False
            self.startup.set()
            return False

    def run(self):
        """
        Run the TCPReader thread to open and listen on a TCP socket for a connection to emulate the SPI bus.

        :return: None
        """
        log.info("Starting thread")
        if self.open_socket():

            while self.running:

                try:
                    conn, addr = self.server.accept()
                except socket.error:
                    conn, addr = None, None

                if conn:
                    log.info("Connection opened by '%s'", addr[0])
                    buff = bytearray()
                    while self.running:
                        rlist, wlist, elist = select.select([conn, ], [conn, ], [conn, ], 0.1)
                        if conn in rlist:
                            try:
                                data = conn.recv(1)
                            except:
                                data = None

                            if not data:
                                break

                            for i in data:
                                strip_data.spi_in(ord(i))

                        elif conn in elist:
                            break

                    conn.shutdown(socket.SHUT_RDWR)<commit_msg>increased socket read size to 1024<commit_after>from __future__ import print_function
import threading
import time
import socket
import logging
import select


from DotStar_Emulator.emulator import config, strip_data

log = logging.getLogger("data")

__all__ = ["TCPReader", ]


class TCPReader(threading.Thread):
    def __init__(self):
        """
        Read emulator strip SPI data in a TCP socket.
        TCPReader runs in its own thread, and will acquire a lock from strip_data before
        writing data.

        HOST and PORT set in config.

        :return:
        """
        super(TCPReader, self).__init__()

        self.host = config.get("HOST")
        self.port = config.get("PORT")
        self.backlog = 5
        self.size = 1024
        self.server = None

        # Thread Loop
        self.running = True

        # Report back to the main thread if we could bind to the port or not. Main thread will not continue
        # if port was not bound to.
        self.startup = threading.Event()
        self.startup_success = False

    def open_socket(self):
        """
        Bind to the specified socket.  Set an event and report to the main thread if it was successful

        :return: None
        """
        try:
            self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server.setblocking(0)
            self.server.bind((self.host, self.port))
            self.server.listen(self.backlog)

            # Set events and return
            log.info("listening on '%s', %s", self.host, self.port)
            self.startup_success = True
            self.startup.set()
            return True
        except socket.error:
            if self.server:
                self.server.close()

            # Set events and return
            log.exception("Could not bind socket '%s', %s", self.host, self.port)
            self.startup_success = False
            self.startup.set()
            return False

    def run(self):
        """
        Run the TCPReader thread to open and listen on a TCP socket for a connection to emulate the SPI bus.

        :return: None
        """
        log.info("Starting thread")
        if self.open_socket():

            while self.running:

                try:
                    conn, addr = self.server.accept()
                except socket.error:
                    conn, addr = None, None

                if conn:
                    log.info("Connection opened by '%s'", addr[0])
                    buff = bytearray()
                    while self.running:
                        rlist, wlist, elist = select.select([conn, ], [conn, ], [conn, ], 0.1)
                        if conn in rlist:
                            try:
                                data = conn.recv(1024)
                            except:
                                data = None

                            if not data:
                                break

                            for i in data:
                                strip_data.spi_in(ord(i))

                        elif conn in elist:
                            break

                    conn.shutdown(socket.SHUT_RDWR)
{'fraction_non_alphanumeric': 0.08052846807172066, 'fraction_numerical': 0.0042466184334696445, 'mean_word_length': 1.343036109064112, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2731776', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1579, 'n_words': 514}