def __MessageCycle(self, comp): source = sb.MessageSource(messageId='test_message', messageFormat={ 'item_float': 'float', 'item_string': 'string' }) source.connect(comp) source.start() # As a pre-condition, there should have been no messages yet self.assertEqual(comp._get_started(), False) self.assertEqual(comp.received_messages, []) source.sendMessage({'item_float': 0.0, 'item_string': 'first'}) # Wait until the component has time to process the message timeout = time.time() + 1.0 while len(comp.received_messages) == 0 and time.time() < timeout: time.sleep(0.1) self.assertEqual(len(comp.received_messages), 1) self.assertEqual(comp.received_messages[0][:14], "test_message,0") comp.start() self.assertEqual(comp._get_started(), True) self.assertEqual(len(comp.received_messages), 1) source.sendMessage({'item_float': 1.0, 'item_string': 'first'}) # Wait until the component has time to process the message timeout = time.time() + 1.0 while len(comp.received_messages) == 1 and time.time() < timeout: time.sleep(0.1) self.assertEqual(len(comp.received_messages), 2) self.assertEqual(comp.received_messages[1][:14], "test_message,1") comp.stop() self.assertEqual(comp._get_started(), False) self.assertEqual(len(comp.received_messages), 2) source.sendMessage({'item_float': 2.0, 'item_string': 'first'}) # Wait until the component has time to process the message timeout = time.time() + 1.0 while len(comp.received_messages) == 2 and time.time() < timeout: time.sleep(0.1) self.assertEqual(len(comp.received_messages), 3) self.assertEqual(comp.received_messages[2][:14], "test_message,2")
def forward_event_to_waveform(domain, evt_chan, wave, port, msg_id): """Forwards message on an event channel to specified waveform input port Parameters ---------- domain : str The name of the active domain. evt_chan : str The name of the event channel (on the specified domain) wave : str The name of the waveform on the domain port : str The name of the port on the waveform to forward messages. """ # access the event channel and waveform port dom = redhawk.attach(domain) e_chan = DT.find_event_channel_from_domain(domain, evt_chan) wfm = DT.find_waveform_from_domain(domain, wave) port_inst = wfm.getPort(port) # setup message source and connect to waveform port # FIXME: there can be multiple messages on an event channnel. # 1) should this filter messages based on this msg id? # 2) Do I need a separate message source per msg_id? msg_src = sb.MessageSource(msg_id) msg_port = msg_src.getPort("msgOut") msg_port.connectPort(port_inst, "conn_" + str(uuid.uuid1())) msg_src.start() # ---------------------- setup to forward messages --------------------- # setup the forwarder f = Forwarder(msg_src) # subscribe to channel with the callback in forwarder sub = Subscriber(dom, channel_name=evt_chan, dataArrivedCB=f.forward) # ----------- run forwarding until user hits enter -------------------- raw_input("Hit enter to exit") msg_src.releaseObject()
def test_MessagingJava(self): src = sb.MessageSource('foo') c = sb.launch('msg_through_java') src.connect(c) snk = sb.MessageSink('foo', Foo, self.callback) c.connect(snk) sb.start() src.sendMessage({'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}) _begin = time.time() _now = time.time() while _now - _begin < 5: if self.rcv_msg != None: break _now = time.time() self.assertEquals(self.rcv_msg.a, 'A') self.assertEquals(self.rcv_msg.b, 'B') self.assertEquals(self.rcv_msg.c, 'C') self.rcv_msg = None src.sendMessage({'a': 'A', 'b': 'B', 'c': 'C'}) while _now - _begin < 5: if self.rcv_msg != None: break _now = time.time() self.assertEquals(self.rcv_msg.a, 'A') self.assertEquals(self.rcv_msg.b, 'B') self.assertEquals(self.rcv_msg.c, 'C') self.rcv_msg = None src.sendMessage({'a': 'A', 'c': 'C'}) while _now - _begin < 5: if self.rcv_msg != None: break _now = time.time() self.assertEquals(self.rcv_msg.a, 'A') self.assertEquals(self.rcv_msg.b, None) self.assertEquals(self.rcv_msg.c, 'C') self.rcv_msg = None
def send_message(msg, wvfm, port_name, domain="REDHAWK_DEV", msg_id="default"): """Connect to waveform message in port and send message 1. Connects to a domain. 2. Searches for wavefor and gets input message port. 3. Send message Parameters ---------- msg : dict The message to transmit wvfm : str The name of the waveform to connect to. port_name : str The name of the port (on the specified wvfm) domain : str Name of the domain to attach and search for the waveform. msg_id : str The id of the message. """ wvfm = find_waveform_from_domain(domain, wvfm) if wvfm: port = wvfm.getPort(port_name) msg_src = sb.MessageSource(msg_id) msg_port = msg_src.getPort("msgOut") #msg_port.connectPort(port, "conn_" + str(uuid.uuid1())) msg_src.connectPort(port, "conn_" + str(uuid.uuid1())) msg_src.start() msg_src.sendMessage(msg) msg_src.releaseObject() else: raise RuntimeWarning("Cannot find waveform on domain")
# when the cut that I request is complete, this callback will receive the message def responseCallback(id_, message_response): print 'Got this response:', id_, message_response fp = dom.fileMgr.open(message_response.file_location, True) data = fp.read(fp.sizeOf()) fp.close() dom.fileMgr.remove(message_response.file_location) print '========= got data of length:', len(data) # request a cut from 15 seconds ago. Make the cut 3 seconds long. The cut is center at 1.01MHz and is 50 kHz wide request = { 'cut_request::time_begin': now - 15, 'cut_request::time_end': now - 12, 'cut_request::freq_begin': 985000, 'cut_request::freq_end': 1035000, 'cut_request::request_id': 'hello' } print '... sending request for a cut:', request src = sb.MessageSource('cut_request') src.connect(control, providesPortName='request') snk = sb.MessageSink('cut_response', CutResponse, responseCallback) control.connect(snk, usesPortName='response') sb.start() src.sendMessage(request) time.sleep(2) archiver.releaseObject() sample_write.releaseObject()