def input_msg(self, msg): if self.count_packets: self.ctr += 1 pdu = pmt.cdr(msg) b0 = pmt.u8vector_ref(pdu, 0) b1 = pmt.u8vector_ref(pdu, 1) b2 = pmt.u8vector_ref(pdu, 2) b3 = pmt.u8vector_ref(pdu, 3) if self.textfile: self.textfile.write("PACKET meta:" + str(pmt.car(msg)) + "\n") self.textfile.write("\tfirst bytes:") for i in range(4): self.textfile.write(str(pmt.u8vector_ref(pdu, i)) + "\t") self.textfile.write("\n") new_seq_nr = (b0 << 0) + (b1 << 8) + (b2 << 16) + (b3 << 24) if new_seq_nr > self.seq_numbers[0]: if self.first_seqnr is None: self.first_seqnr = new_seq_nr self.unique_msg_ctr += 1 # if new_seq_nr - self.seq_numbers[0] > 1: # print "\t missed", new_seq_nr - self.seq_numbers[0] - 1, "frame(s)!" # print "new seq #:", new_seq_nr self.seq_numbers.appendleft(new_seq_nr) self.PER = 1 - len(self.seq_numbers) / float( self.seq_numbers[0] - self.seq_numbers[-1] + 1)
def test_004(self): # Test that the TCP server can stream PDUs <= the MTU size. port = str(random.Random().randint(0, 30000) + 10000) mtu = 10000 srcdata = tuple(x % 256 for x in range(mtu)) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_send = blocks.socket_pdu("TCP_SERVER", "localhost", port, mtu) self.pdu_recv = blocks.socket_pdu("TCP_CLIENT", "localhost", port, mtu) self.pdu_sink = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.pdu_sink, "store") self.tb.start() time.sleep(1) self.tb.stop() self.tb.wait() received = self.pdu_sink.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(mtu): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def test_002(self): # Send a PDU through a pair of UDP sockets port = str(random.Random().randint(0, 30000) + 10000) srcdata = (0x64, 0x6f, 0x67, 0x65) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_recv = blocks.socket_pdu("UDP_SERVER", "localhost", port) self.pdu_send = blocks.socket_pdu("UDP_CLIENT", "localhost", port) self.dbg = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.dbg, "store") self.tb.start() time.sleep(1) self.tb.stop() self.tb.wait() self.pdu_send = None self.pdu_recv = None received = self.dbg.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(4): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def test_001(self): #test complementary operation of framer & deframer #want to frame some random data that has enough consecutive bits to #stuff at least a few bits npkts = 20 src_data = [0xFE, 0xDA, 0xAC, 0x29, 0x7F, 0xA2, 0x90, 0x0F, 0xF8] frame = digital.hdlc_framer_pb("wat") corr = digital.correlate_access_code_tag_bb("01111110", 0, "frame") deframe = digital.hdlc_deframer_bp("frame", 32, 500) debug = blocks.message_debug() self.tb.connect(frame, corr, deframe) self.tb.msg_connect(deframe, "out", debug, "store") self.tb.start() msg = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(src_data),src_data)) for i in xrange(npkts): frame.to_basic_block()._post(pmt.intern("in"), msg) sleep(0.2) self.tb.stop() self.tb.wait() rxmsg = debug.get_message(0) result_len = pmt.blob_length(pmt.cdr(rxmsg)) msg_data = [] for j in xrange(result_len): msg_data.append(pmt.u8vector_ref(pmt.cdr(rxmsg), j)) self.assertEqual(src_data, msg_data)
def test_002 (self): # Send a PDU through a pair of UDP sockets port = str(random.Random().randint(0, 30000) + 10000) srcdata = (0x64, 0x6f, 0x67, 0x65) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_recv = blocks.socket_pdu("UDP_SERVER", "localhost", port) self.pdu_send = blocks.socket_pdu("UDP_CLIENT", "localhost", port) self.dbg = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.dbg, "store") self.tb.start () time.sleep(1) self.tb.stop() self.tb.wait() self.pdu_send = None self.pdu_recv = None received = self.dbg.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(4): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def test_004 (self): # Test that the TCP server can stream PDUs <= the MTU size. port = str(random.Random().randint(0, 30000) + 10000) mtu = 10000 srcdata = tuple(x % 256 for x in range(mtu)) data = pmt.init_u8vector(srcdata.__len__(), srcdata) pdu_msg = pmt.cons(pmt.PMT_NIL, data) self.pdu_source = blocks.message_strobe(pdu_msg, 500) self.pdu_send = blocks.socket_pdu("TCP_SERVER", "localhost", port, mtu) self.pdu_recv = blocks.socket_pdu("TCP_CLIENT", "localhost", port, mtu) self.pdu_sink = blocks.message_debug() self.tb.msg_connect(self.pdu_source, "strobe", self.pdu_send, "pdus") self.tb.msg_connect(self.pdu_recv, "pdus", self.pdu_sink, "store") self.tb.start() time.sleep(1) self.tb.stop() self.tb.wait() received = self.pdu_sink.get_message(0) received_data = pmt.cdr(received) msg_data = [] for i in range(mtu): msg_data.append(pmt.u8vector_ref(received_data, i)) self.assertEqual(srcdata, tuple(msg_data))
def test_001(self): #test complementary operation of framer & deframer #want to frame some random data that has enough consecutive bits to #stuff at least a few bits npkts = 20 src_data = [0xFE, 0xDA, 0xAC, 0x29, 0x7F, 0xA2, 0x90, 0x0F, 0xF8] frame = digital.hdlc_framer_pb("wat") corr = digital.correlate_access_code_tag_bb("01111110", 0, "frame") deframe = digital.hdlc_deframer_bp("frame", 32, 500) debug = blocks.message_debug() self.tb.connect(frame, corr, deframe) self.tb.msg_connect(deframe, "out", debug, "store") self.tb.start() msg = pmt.cons(pmt.PMT_NIL, pmt.init_u8vector(len(src_data), src_data)) for i in xrange(npkts): frame.to_basic_block()._post(pmt.intern("in"), msg) sleep(0.2) self.tb.stop() self.tb.wait() rxmsg = debug.get_message(0) result_len = pmt.blob_length(pmt.cdr(rxmsg)) msg_data = [] for j in xrange(result_len): msg_data.append(pmt.u8vector_ref(pmt.cdr(rxmsg), j)) self.assertEqual(src_data, msg_data)
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = pdu.pdu_to_tagged_stream(gr.types.byte_t) snk3 = pdu.tagged_stream_to_pdu(gr.types.byte_t) snk2 = blocks.vector_sink_b() snk = blocks.tag_debug(1, "test") snk.set_display(False) dbg = blocks.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() # system port is defined automatically self.assertEqual(pmt.length(pi), 1) self.assertEqual(pmt.length(po), 1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") # make our reference and message pmts port = pmt.intern("pdus") msg = pmt.cons(pmt.PMT_NIL, pmt.make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) src.to_basic_block()._post( pmt.intern("system"), pmt.cons(pmt.intern("done"), pmt.from_long(1))) self.tb.start() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.cdr(result_msg) # pmt.print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in range(16): msg_data.append(pmt.u8vector_ref(msg_vec, i)) actual_data = 16 * [ 0xFF, ] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def print_msg(self, msg): """ Print the rx'd message. """ meta = pmt.to_python(pmt.car(msg)) vect = pmt.cdr(msg) msg_str = ''.join([ chr(pmt.u8vector_ref(vect, i)) for i in range(pmt.length(vect)) ]).strip() if len(msg_str): print 'Incoming message: ', msg_str print meta
def test_000(self): # Just run some data through and make sure it doesn't puke. src_data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) src = blocks.pdu_to_tagged_stream(blocks.byte_t) snk3 = blocks.tagged_stream_to_pdu(blocks.byte_t) snk2 = blocks.vector_sink_b() snk = blocks.tag_debug(1, "test") snk.set_display(False) dbg = blocks.message_debug() # Test that the right number of ports exist. pi = snk3.message_ports_in() po = snk3.message_ports_out() self.assertEqual(pmt.length(pi), 0) self.assertEqual(pmt.length(po), 1) self.tb.connect(src, snk) self.tb.connect(src, snk2) self.tb.connect(src, snk3) self.tb.msg_connect(snk3, "pdus", dbg, "store") self.tb.start() # make our reference and message pmts port = pmt.intern("pdus") msg = pmt.cons( pmt.PMT_NIL, pmt.make_u8vector(16, 0xFF)) # post the message src.to_basic_block()._post(port, msg) # eww, what's that smell? while dbg.num_messages() < 1: time.sleep(0.1) self.tb.stop() self.tb.wait() # Get the vector of data from the vector sink result_data = snk2.data() # Get the vector of data from the message sink # Convert the message PMT as a pair into its vector result_msg = dbg.get_message(0) msg_vec = pmt.cdr(result_msg) #pmt.print(msg_vec) # Convert the PMT vector into a Python list msg_data = [] for i in xrange(16): msg_data.append(pmt.u8vector_ref(msg_vec, i)) actual_data = 16*[0xFF,] self.assertEqual(actual_data, list(result_data)) self.assertEqual(actual_data, msg_data)
def _handle_packet(self, pdu): field = pmt.car(pdu) lengthP = pmt.to_python( pmt.dict_ref(field, pmt.intern('dataLength'), pmt.from_long(0))) + 4 packetTemp = pmt.cdr(pdu) packetList = [] packet = pdu for index in range(lengthP): item = list(bin(pmt.u8vector_ref(packetTemp, index))[2:].zfill(8)) packetList.extend(item) lengthP = len(packetList) packetList = self._transform_data(packetList, lengthP) packetList.extend(list(self._endString)) iterator = iter(packetList) byteList = [ int("".join(sli), 2) for sli in zip(*iter([iterator] * SYMBOL_SIZE)) ] data_pdu = self._byteList_to_pmtvector(byteList) packet = pmt.cons(pmt.make_dict(), data_pdu) self.message_port_pub(pmt.intern('packet_out'), packet)