Exemplo n.º 1
0
    def _handle_client(self, client_socket):
        """This currently only handles a single client and blocks on that call.

        If multiple clients need to connect, the server needs to track the
        clients and send the data to each one (each of which would have a queue
        of data to be sent)
        """
        wfile = client_socket.makefile(mode='wb')

        # Send initialization data according to the protocol
        for msg in self.init_messages():
            wfile.write(msg)

        # This needs to be handled differently if we decide to allow
        # multiple clients. Producer needs to be managed by the class,
        # and the same message should be sent to each client.
        data_queue = Queue()

        # Construct a new generator each time to get consistent results.
        with Producer(data_queue,
                      generator=self.make_generator(),
                      freq=1 / self.sample_rate()):
            while self.running():
                try:
                    # block if necessary, for up to 5 seconds
                    item = data_queue.get(True, 5)
                except Empty:
                    client_socket.close()
                    break
                try:
                    wfile.write(item)
                except IOError:
                    break
        client_socket.close()
        log.debug("[*] Client disconnected")
Exemplo n.º 2
0
    def run(self):
        """Main loop of the thread. Continuously streams data to the stream
        outlet at a rate consistent with the sample frequency. May also
        output markers at a different interval."""

        sample_counter = 0
        self.started = True

        data_queue = Queue()
        with Producer(data_queue,
                      generator=self.generator,
                      freq=1 / self.sample_hz):
            while self.running():
                sample_counter += 1
                try:
                    sample = data_queue.get(True, 2)
                    self.outlet.push_sample(sample)
                    if self.add_markers and sample_counter % 1000 == 0:
                        self.markers_outlet.push_sample(
                            [str(random.randint(1, 100))])
                except (Empty, AttributeError):
                    # outlet.push_sample(sample) may cause an error after
                    # the server has been stopped since the attribute is
                    # deleted in another thread.
                    break

        logging.debug("[*] No longer pushing data")
Exemplo n.º 3
0
    def test_frequency(self):
        """Data should be generated at the provided frequency"""
        sample_hz = 300
        runtime = 0.2
        data_queue = queue.Queue()
        producer = Producer(data_queue, freq=1 / sample_hz)
        producer.start()
        time.sleep(runtime)
        producer.stop()

        data_n = data_queue.qsize()
        expected_n = sample_hz * runtime
        tolerance = 5
        self.assertTrue(data_n + tolerance >= expected_n)
        self.assertTrue(data_n <= expected_n + tolerance)
Exemplo n.º 4
0
    def test_max_iters(self):
        """Producer should stop producing data after maxiters if param is
        provided."""

        sample_hz = 300
        runtime = 0.2
        maxiters = 10
        data_queue = queue.Queue()
        producer = Producer(data_queue, freq=1 / sample_hz, maxiters=maxiters)
        producer.start()
        time.sleep(runtime)
        producer.stop()

        expected_n = sample_hz * runtime
        tolerance = 10
        self.assertTrue(expected_n - tolerance > maxiters)
        data_n = data_queue.qsize()
        self.assertEqual(data_n, maxiters)
Exemplo n.º 5
0
    def test_custom_generator(self):
        """Producer should be able to take a custom generator."""
        def gen():
            counter = 0
            while True:
                counter += 1
                yield counter

        data_queue = queue.Queue()
        producer = Producer(data_queue, freq=1 / 300, generator=gen())
        producer.start()
        time.sleep(0.1)
        producer.stop()

        lst = list(data_queue.queue)
        self.assertTrue(len(lst) > 0)
        self.assertEqual(lst[0], 1)
        self.assertEqual(lst[-1], len(lst))
        print(lst[-1])