def send(self, session, params=None): """send(dest) **Process** - Process for sending outgoing G3Frames. This will query the out_queue for frames to be sent to lyrebird. This will try to regulate how fast it sends frames such that the delay between when the frames are sent, and the timestamp of the frames are fixed. """ self._send_running = True first_frame_time = None stream_start_time = None sender = core.G3NetworkSender(hostname='*', port=params['dest'], max_queue_size=1000) sender.Process(self.fp.config_frame()) session.set_status('running') while session.status in ['starting', 'running']: f = self.out_queue.get(block=True) t = f['timestamp'].time / core.G3Units.s now = time.time() if first_frame_time is None: first_frame_time = t stream_start_time = now this_frame_time = stream_start_time + ( t - first_frame_time) + self.delay res = sleep_while_running(this_frame_time - now, session) sender.Process(f) return True, "Stopped send process"
def __init__(self, agent, port=4536, num_chans=528): """ OCS Agent to simulate data streaming without connection to a smurf. """ self.agent = agent self.log = agent.log self.port = port self.writer = core.G3NetworkSender(hostname="*", port=self.port) self.is_streaming = False self.channels = [StreamChannel(0, 1) for i in range(num_chans)]
def __init__(self, agent, port=4536): """ OCS Agent to simulate data streaming without connection to a smurf. """ self.port = port self.agent = agent self.log = agent.log self.is_streaming = False self.freq = 100 # [Hz] self.writer = core.G3NetworkSender(hostname="*", port=self.port) # This does not close the port when streaming stops! # Only when the agent is quit! self.keys = ["{}".format(i) for i in range(4096)] self.channels = { k: { 'type': 'const', 'val': 0, 'stdev': 0.05, } for k in self.keys }
def start_background_streamer(self, session, params=None): """start_background_streamer(params=None) Process to run streaming process. A data stream is started automatically. It can be stopped and started by the start and stop tasks. Either way keep alive flow control frames are being sent. Parameters ---------- frame_rate : float, optional Frequency [Hz] at which G3Frames are sent over the network. Defaults to 1 frame pers sec. sample_rate : float, optional Sample rate [Hz] for each channel. Defaults to 10 Hz. """ if params is None: params = {} self.writer = core.G3NetworkSender(hostname=self.target_host, port=self.port) frame_rate = params.get('frame_rate', 1.) sample_rate = params.get('sample_rate', 10.) frame_num = 0 self.running_in_background = True # Control flags FIFO stack to keep Writer single threaded self.flags = deque([FlowControl.START]) while self.running_in_background: # Send START frame if next(iter(self.flags), None) is FlowControl.START: self._set_stream_on() # sends start flowcontrol self.is_streaming = True self.flags.popleft() print("stream running in background") self.log.debug("control flags: {f}", f=self.flags) # Send keep alive flow control frame f = core.G3Frame(core.G3FrameType.none) f['sostream_flowcontrol'] = FlowControl.ALIVE.value self.writer.Process(f) if self.is_streaming: frame_start = time.time() time.sleep(1. / frame_rate) frame_stop = time.time() times = np.arange(frame_start, frame_stop, 1. / sample_rate) f = core.G3Frame(core.G3FrameType.Scan) f['session_id'] = 0 f['frame_num'] = frame_num f['sostream_id'] = self.stream_id f['data'] = core.G3TimestreamMap() for i, chan in enumerate(self.channels): ts = core.G3Timestream([chan.read() for t in times]) ts.start = core.G3Time(frame_start * core.G3Units.sec) ts.stop = core.G3Time(frame_stop * core.G3Units.sec) f['data'][f"r{i:04}"] = ts self.writer.Process(f) self.log.info("Writing frame...") frame_num += 1 # Send END frame if next(iter(self.flags), None) is FlowControl.END: self._send_end_flowcontrol_frame() self._send_cleanse_flowcontrol_frame() self.is_streaming = False self.flags.popleft() else: # Don't send keep alive frames too quickly time.sleep(1) # Shutdown streamer if next(iter(self.flags), None) is SHUTDOWN: self.running_in_background = False self.flags.popleft() # Teardown writer self.writer.Close() self.writer = None return True, "Finished streaming"
def networksender(): networksender = core.G3NetworkSender(hostname="*", port=4536) return networksender
frames = [] for i in range(0, 20): f = core.G3Frame() f['Sequence'] = i f['Data'] = core.G3Timestream(numpy.zeros(100000)) frames.append(f) print('Port: ', port) child = os.fork() if child != 0: # Parent print('Parent') send = core.G3NetworkSender(hostname='*', port=port) time.sleep(1) # XXX: how to signal that the remote end is ready? print('Sending') for f in frames: send(f) send(core.G3Frame(core.G3FrameType.EndProcessing)) pid, status = os.wait() print('Child Status: ', status) if status == 0: print('OK') sys.exit(status) else: # Child print('Child')