def test_big_buffer(self): ring_buffer = RingBuffer(size=5) try: ring_buffer.push(IQArray(np.array([1, 2, 3, 4, 5, 6, 7], dtype=np.complex64))) self.assertTrue(False) except ValueError: self.assertTrue(True)
def test_big_buffer(self): ring_buffer = RingBuffer(size=5) try: ring_buffer.push(np.array([1, 2, 3, 4, 5, 6, 7], dtype=np.complex64)) self.assertTrue(False) except ValueError: self.assertTrue(True)
def device(self, value: VirtualDevice): self.__device = value self.__device.is_send_continuous = True self.ringbuffer = RingBuffer( int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10**6) // 8, self.__device.data_type) self.__device.continuous_send_ring_buffer = self.ringbuffer
def __init__(self, backend_handler, name: str): self.__device = VirtualDevice(backend_handler=backend_handler, name=name, mode=Mode.send) self.ringbuffer = RingBuffer( int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10**6) // 8) self.__device.continuous_send_ring_buffer = self.ringbuffer self.__device.is_send_continuous = True
def test_continuous_pop(self): ring_buffer = RingBuffer(size=10) values = IQArray(np.array(list(range(10)), dtype=np.complex64)) ring_buffer.push(values) retrieved = np.empty(0, dtype=np.float32) for i in range(10): retrieved = np.append(retrieved, ring_buffer.pop(1)) self.assertEqual(values, IQArray(retrieved))
def test_continuous_pop(self): ring_buffer = RingBuffer(size=10) values = np.array(list(range(10)), dtype=np.complex64) ring_buffer.push(values) retrieved = np.array([], dtype=np.complex64) for i in range(10): retrieved = np.append(retrieved, ring_buffer.pop(1)) self.assertTrue(np.array_equal(values, retrieved))
class EndlessSender(object): """ Enter endless send mode for a device and send data if data gets pushed to ringbuffer. """ def __init__(self, backend_handler, name: str): self.__device = VirtualDevice(backend_handler=backend_handler, name=name, mode=Mode.send) self.ringbuffer = RingBuffer( int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10**6) // 8, self.__device.data_type) self.__device.continuous_send_ring_buffer = self.ringbuffer self.__device.is_send_continuous = True @property def device(self) -> VirtualDevice: return self.__device @device.setter def device(self, value: VirtualDevice): self.__device = value self.__device.is_send_continuous = True self.ringbuffer = RingBuffer( int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10**6) // 8, self.__device.data_type) self.__device.continuous_send_ring_buffer = self.ringbuffer @property def device_name(self) -> str: return self.device.name @device_name.setter def device_name(self, value: str): if value != self.device_name: self.device = VirtualDevice( backend_handler=self.device.backend_handler, name=value, mode=Mode.send) def start(self): self.device.num_sending_repeats = 0 self.device.start() def stop(self): self.device.stop("EndlessSender stopped.") def push_data(self, data: np.ndarray): self.ringbuffer.push(data)
def __init__(self, messages, modulators): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB * 10**6) // 8) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously) self.process.daemon = True
def __init__(self, messages, modulators, num_repeats=-1): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.num_repeats = num_repeats # -1 or 0 = infinite self.ring_buffer = RingBuffer(int(constants.CONTINUOUS_BUFFER_SIZE_MB*10**6)//8, dtype=Modulator.get_dtype()) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously, args=(self.num_repeats, )) self.process.daemon = True
def test_will_fit(self): ring_buffer = RingBuffer(size=8) self.assertEqual(ring_buffer.space_left, 8) self.assertTrue(ring_buffer.will_fit(4)) self.assertTrue(ring_buffer.will_fit(8)) self.assertFalse(ring_buffer.will_fit(9)) ring_buffer.push(np.array([1, 2, 3, 4], dtype=np.complex64)) self.assertEqual(ring_buffer.space_left, 4) self.assertTrue(ring_buffer.will_fit(3)) self.assertTrue(ring_buffer.will_fit(4)) self.assertFalse(ring_buffer.will_fit(5))
class EndlessSender(object): """ Enter endless send mode for a device and send data if data gets pushed to ringbuffer. """ def __init__(self, backend_handler, name: str): self.__device = VirtualDevice(backend_handler=backend_handler, name=name, mode=Mode.send) self.ringbuffer = RingBuffer(int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10 ** 6) // 8) self.__device.continuous_send_ring_buffer = self.ringbuffer self.__device.is_send_continuous = True self.__device.num_sending_repeats = 0 @property def device(self) -> VirtualDevice: return self.__device @device.setter def device(self, value: VirtualDevice): self.__device = value self.__device.is_send_continuous = True self.__device.num_sending_repeats = 0 self.__device.continuous_send_ring_buffer = self.ringbuffer @property def device_name(self) -> str: return self.device.name @device_name.setter def device_name(self, value: str): if value != self.device_name: self.device = VirtualDevice(backend_handler=self.device.backend_handler, name=value, mode=Mode.send) def start(self): self.device.start() def stop(self): self.device.stop("EndlessSender stopped.") def push_data(self, data: np.ndarray): self.ringbuffer.push(data)
def test_push(self): ring_buffer = RingBuffer(size=10) self.assertEqual(0, ring_buffer.left_index) add1 = np.array([1, 2, 3, 4, 5], dtype=np.complex64) ring_buffer.push(add1) self.assertEqual(5, ring_buffer.right_index) self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1)) add2 = np.array([10, 20, 30, 40, 50, 60], dtype=np.complex64) self.assertFalse(ring_buffer.will_fit(len(add2))) ring_buffer.push(add2[:-1]) self.assertTrue(np.array_equal(ring_buffer.data[5:10], add2[:-1])) self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1))
def __init__(self, messages, modulators): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB*10**6)//8) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously) self.process.daemon = True
def __init__(self, messages, modulators, num_repeats=-1): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.num_repeats = num_repeats # -1 or 0 = infinite self.ring_buffer = RingBuffer(int(constants.CONTINUOUS_BUFFER_SIZE_MB*10**6)//8) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously, args=(self.num_repeats, )) self.process.daemon = True
def send_raw_data_continuously(self, ring_buffer: RingBuffer, num_samples_to_send: int, num_repeats: int): rng = iter(int, 1) if num_repeats <= 0 else range(0, num_repeats) # <= 0 = forever samples_per_iteration = 65536 // 2 for _ in rng: if self.__sending_interrupt_requested: break while self.current_sent_sample < num_samples_to_send: if self.__sending_interrupt_requested: break n = max(0, min(samples_per_iteration, num_samples_to_send - self.current_sent_sample)) data = ring_buffer.pop(n, ensure_even_length=True) self.send_data(data) self.current_sent_sample += len(data) self.current_sending_repeat += 1 self.current_sent_sample = 0 self.current_sent_sample = num_samples_to_send
def send_raw_data_continuously(self, ring_buffer: RingBuffer, num_samples_to_send: int, num_repeats: int): rng = iter(int, 1) if num_repeats <= 0 else range( 0, num_repeats) # <= 0 = forever samples_per_iteration = 65536 // 2 sock = self.prepare_send_connection() if sock is None: return try: for _ in rng: if self.__sending_interrupt_requested: break while num_samples_to_send is None or self.current_sent_sample < num_samples_to_send: while ring_buffer.is_empty and not self.__sending_interrupt_requested: time.sleep(0.1) if self.__sending_interrupt_requested: break if num_samples_to_send is None: n = samples_per_iteration else: n = max( 0, min(samples_per_iteration, num_samples_to_send - self.current_sent_sample)) data = ring_buffer.pop(n, ensure_even_length=True) if len(data) > 0: self.send_data(data, sock) self.current_sent_sample += len(data) time.sleep(0.0000001) self.current_sending_repeat += 1 self.current_sent_sample = 0 self.current_sent_sample = num_samples_to_send finally: self.shutdown_socket(sock)
def send_raw_data_continuously(self, ring_buffer: RingBuffer, num_samples_to_send: int, num_repeats: int): rng = iter(int, 1) if num_repeats <= 0 else range(0, num_repeats) # <= 0 = forever samples_per_iteration = 65536 // 2 sock = self.prepare_send_connection() if sock is None: return try: for _ in rng: if self.__sending_interrupt_requested: break while num_samples_to_send is None or self.current_sent_sample < num_samples_to_send: while ring_buffer.is_empty and not self.__sending_interrupt_requested: time.sleep(0.1) if self.__sending_interrupt_requested: break if num_samples_to_send is None: n = samples_per_iteration else: n = max(0, min(samples_per_iteration, num_samples_to_send - self.current_sent_sample)) data = ring_buffer.pop(n, ensure_even_length=True) if len(data) > 0: self.send_data(data, sock) self.current_sent_sample += len(data) self.current_sending_repeat += 1 self.current_sent_sample = 0 self.current_sent_sample = num_samples_to_send finally: self.shutdown_socket(sock)
def __init__(self, backend_handler, name: str): self.__device = VirtualDevice(backend_handler=backend_handler, name=name, mode=Mode.send) self.ringbuffer = RingBuffer(int(self.BUFFER_SIZE_MB * 10 ** 6) // 8) self.__device.continuous_send_ring_buffer = self.ringbuffer self.__device.is_send_continuous = True self.__device.num_sending_repeats = 0
def test_pop(self): ring_buffer = RingBuffer(size=5) add1 = np.array([1, 2, 3], dtype=np.complex64) ring_buffer.push(add1) self.assertTrue(np.array_equal(add1, ring_buffer.pop(40))) self.assertTrue(ring_buffer.is_empty) add2 = np.array([1, 2, 3, 4], dtype=np.complex64) ring_buffer.push(add2) self.assertTrue(np.array_equal(add2, ring_buffer.pop(4))) self.assertTrue(ring_buffer.is_empty) add3 = np.array([1, 2], dtype=np.complex64) ring_buffer.push(add3) popped_item = ring_buffer.pop(1) self.assertTrue(np.array_equal(add3[0:1], popped_item), msg=popped_item) self.assertFalse(ring_buffer.is_empty) add4 = np.array([7, 8, 9, 10], dtype=np.complex64) ring_buffer.push(add4) self.assertFalse(ring_buffer.will_fit(1)) self.assertTrue(np.array_equal(np.concatenate((np.atleast_1d(add3[1]), add4)), ring_buffer.pop(5)))
def test_pop(self): ring_buffer = RingBuffer(size=5) add1 = np.array([1, 2, 3], dtype=np.complex64) ring_buffer.push(add1) self.assertTrue(np.array_equal(add1, ring_buffer.pop(40))) self.assertTrue(ring_buffer.is_empty) add2 = np.array([1, 2, 3, 4], dtype=np.complex64) ring_buffer.push(add2) self.assertTrue(np.array_equal(add2, ring_buffer.pop(4))) self.assertTrue(ring_buffer.is_empty) add3 = np.array([1, 2], dtype=np.complex64) ring_buffer.push(add3) popped_item = ring_buffer.pop(1) self.assertTrue(np.array_equal(add3[0:1], popped_item), msg=popped_item) self.assertFalse(ring_buffer.is_empty)
class ContinuousModulator(object): """ This class is used in continuous sending mode. You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially. This avoids running out of RAM for large amounts of messages. """ BUFFER_SIZE_MB = 100 WAIT_TIMEOUT = 0.1 def __init__(self, messages, modulators): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB * 10**6) // 8) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously) self.process.daemon = True @property def is_running(self): return self.process.is_alive() def start(self): self.abort.value = 0 try: self.process = Process(target=self.modulate_continuously) self.process.daemon = True self.process.start() except RuntimeError as e: logger.debug(str(e)) def stop(self, clear_buffer=True): self.abort.value = 1 if clear_buffer: self.ring_buffer.clear() if not self.process.is_alive(): return try: self.process.join(0.1) except RuntimeError as e: logger.debug(str(e)) if self.process.is_alive(): self.process.terminate() self.process.join() logger.debug("Stopped continuous modulation") def modulate_continuously(self): pos = 0 while True: start = self.current_message_index.value for i in range(start, len(self.messages)): if self.abort.value: return message = self.messages[i] self.current_message_index.value = i modulator = self.modulators[ message.modulator_indx] # type: Modulator modulator.modulate(start=pos, data=message.encoded_bits, pause=message.pause) while not self.ring_buffer.will_fit( len(modulator.modulated_samples)): if self.abort.value: return # Wait till there is space in buffer time.sleep(self.WAIT_TIMEOUT) self.ring_buffer.push(modulator.modulated_samples) pos += len(modulator.modulated_samples)
def __init__(self, backend_handler, name: str): self.__device = VirtualDevice(backend_handler=backend_handler, name=name, mode=Mode.send) self.ringbuffer = RingBuffer(int(constants.CONTINUOUS_BUFFER_SIZE_MB * 10 ** 6) // 8) self.__device.continuous_send_ring_buffer = self.ringbuffer self.__device.is_send_continuous = True self.__device.num_sending_repeats = 0
class ContinuousModulator(object): """ This class is used in continuous sending mode. You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially. This avoids running out of RAM for large amounts of messages. """ BUFFER_SIZE_MB = 100 WAIT_TIMEOUT = 0.1 def __init__(self, messages, modulators): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB*10**6)//8) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously) self.process.daemon = True @property def is_running(self): return self.process.is_alive() def start(self): self.abort.value = 0 try: self.process = Process(target=self.modulate_continuously) self.process.daemon = True self.process.start() except RuntimeError as e: logger.debug(str(e)) def stop(self, clear_buffer=True): self.abort.value = 1 if clear_buffer: self.ring_buffer.clear() if not self.process.is_alive(): return try: self.process.join(0.1) except RuntimeError as e: logger.debug(str(e)) if self.process.is_alive(): self.process.terminate() self.process.join() logger.debug("Stopped continuous modulation") def modulate_continuously(self): pos = 0 while True: start = self.current_message_index.value for i in range(start, len(self.messages)): if self.abort.value: return message = self.messages[i] self.current_message_index.value = i modulator = self.modulators[message.modulator_indx] # type: Modulator modulator.modulate(start=pos, data=message.encoded_bits, pause=message.pause) while not self.ring_buffer.will_fit(len(modulator.modulated_samples)): if self.abort.value: return # Wait till there is space in buffer time.sleep(self.WAIT_TIMEOUT) self.ring_buffer.push(modulator.modulated_samples) pos += len(modulator.modulated_samples)
def test_pop(self): ring_buffer = RingBuffer(size=5) add1 = np.array([1, 2, 3], dtype=np.complex64) ring_buffer.push(add1) self.assertTrue(np.array_equal(add1, ring_buffer.pop(40))) self.assertTrue(ring_buffer.is_empty) add2 = np.array([1, 2, 3, 4], dtype=np.complex64) ring_buffer.push(add2) self.assertTrue(np.array_equal(add2, ring_buffer.pop(4))) self.assertTrue(ring_buffer.is_empty) add3 = np.array([1, 2], dtype=np.complex64) ring_buffer.push(add3) popped_item = ring_buffer.pop(1) self.assertTrue(np.array_equal(add3[0:1], popped_item), msg=popped_item) self.assertFalse(ring_buffer.is_empty) add4 = np.array([7, 8, 9, 10], dtype=np.complex64) ring_buffer.push(add4) self.assertFalse(ring_buffer.will_fit(1)) self.assertTrue( np.array_equal(np.concatenate((np.atleast_1d(add3[1]), add4)), ring_buffer.pop(5)))
class ContinuousModulator(object): """ This class is used in continuous sending mode. You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially. This avoids running out of RAM for large amounts of messages. """ WAIT_TIMEOUT = 0.1 def __init__(self, messages, modulators, num_repeats=-1): """ :type messages: list of Message :type modulators: list of Modulator """ self.messages = messages self.modulators = modulators self.num_repeats = num_repeats # -1 or 0 = infinite self.ring_buffer = RingBuffer( int(settings.CONTINUOUS_BUFFER_SIZE_MB * 1e6) // 8, dtype=Modulator.get_dtype()) self.current_message_index = Value("L", 0) self.abort = Value("i", 0) self.process = Process(target=self.modulate_continuously, args=(self.num_repeats, ), daemon=True) @property def is_running(self): return self.process.is_alive() def start(self): self.abort.value = 0 try: self.process = Process(target=self.modulate_continuously, args=(self.num_repeats, ), daemon=True) self.process.start() except RuntimeError as e: logger.exception(e) def stop(self, clear_buffer=True): self.abort.value = 1 if self.process.is_alive(): try: self.process.join(1.5) except RuntimeError as e: logger.exception(e) self.process.terminate() if clear_buffer: self.ring_buffer.clear() logger.debug("Stopped continuous modulation") def modulate_continuously(self, num_repeats): rng = iter(int, 1) if num_repeats <= 0 else range( 0, num_repeats) # <= 0 = forever for _ in rng: if self.abort.value: return start = self.current_message_index.value for i in range(start, len(self.messages)): if self.abort.value: return message = self.messages[i] self.current_message_index.value = i modulator = self.modulators[ message.modulator_index] # type: Modulator modulated = modulator.modulate(start=0, data=message.encoded_bits, pause=message.pause) while not self.ring_buffer.will_fit(len(modulated)): if self.abort.value: return # Wait till there is space in buffer time.sleep(self.WAIT_TIMEOUT) self.ring_buffer.push(modulated) self.current_message_index.value = 0