Esp2Processor extends AbstractEspProcessor<Esp2Packet> { public void setProcessorListener(Esp2ProcessorListener listener) { this.listener = listener; } Esp2Processor(Esp2ComPortAdapter port); void setProcessorListener(Esp2ProcessorListener listener); static final long ESP2_RESPONSE_TIMEOUT; }
@Test public void testFragmentedResponse2() throws Exception { int numOfTelegrams = 30000; Deque<Byte> buffer = new LinkedList<Byte>(); Random rand = new Random(47); int payload = 0; for(int i = 0; i < numOfTelegrams; i++) { Esp2RPSTelegram telegram = new Esp2RPSTelegram(senderID, (byte)payload, (byte)0x00); ++payload; byte[] packetBytes = setPacketType( Esp2PacketHeader.PacketType.RRT_UNKNOWN, telegram.asByteArray() ); for(byte dataByte : packetBytes) { buffer.add(dataByte); } } while(buffer.size() > 0) { int size = Math.min(rand.nextInt(AbstractEsp2RadioTelegram.ESP2_RADIO_PACKET_LENGTH / 2), buffer.size()); byte[] data = new byte[size]; for(int i = 0; i < size; i++) { data[i] = buffer.removeFirst(); } mockPort.addDataToReturn(data); } RadioListener listener = new RadioListener(120, numOfTelegrams); processor.setProcessorListener(listener); processor.start(); List<EspRadioTelegram> receivedTelegrams = listener.waitForRadioTelegrams(); Assert.assertNotNull(receivedTelegrams); Assert.assertEquals(numOfTelegrams, receivedTelegrams.size()); int expectedPayload = 0; for(EspRadioTelegram telegram : receivedTelegrams) { byte receivedPayload = telegram.getPayload()[0]; Assert.assertTrue((byte)expectedPayload == receivedPayload); ++expectedPayload; } }
@Test public void testReceiveRadioTelegrams() throws Exception { List<EspRadioTelegram> expectedTelegrams = new ArrayList<EspRadioTelegram>(); expectedTelegrams.add(new Esp2RPSTelegram(senderID, (byte)0x11, (byte)0x22)); expectedTelegrams.add(new Esp21BSTelegram(senderID, (byte)0x33, (byte)0x44)); expectedTelegrams.add(new Esp24BSTelegram( senderID, new byte[] {(byte)0xFF, 0x01, 0x02, 0x03}, (byte)0x55) ); for(EspRadioTelegram telegram : expectedTelegrams) { byte[] telegramBytes = ((EspPacket) telegram).asByteArray(); mockPort.addDataToReturn( setPacketType(Esp2PacketHeader.PacketType.RRT_UNKNOWN, telegramBytes) ); } RadioListener listener = new RadioListener(2, expectedTelegrams.size()); processor.setProcessorListener(listener); processor.start(); List<EspRadioTelegram> receivedTelegrams = listener.waitForRadioTelegrams(); Assert.assertNotNull(receivedTelegrams); Assert.assertEquals(expectedTelegrams.size(), receivedTelegrams.size()); for(int index = 0; index < expectedTelegrams.size(); index++) { EspRadioTelegram expTele = expectedTelegrams.get(index); EspRadioTelegram rcvTele = receivedTelegrams.get(index); Assert.assertEquals(expTele.getRORG(), rcvTele.getRORG()); Assert.assertEquals(expTele.getSenderID(), rcvTele.getSenderID()); Assert.assertArrayEquals(expTele.getPayload(), rcvTele.getPayload()); Assert.assertEquals(expTele.getStatusByte(), rcvTele.getStatusByte()); } Assert.assertTrue(receivedTelegrams.get(0) instanceof Esp2RPSTelegram); Assert.assertTrue(receivedTelegrams.get(1) instanceof Esp21BSTelegram); Assert.assertTrue(receivedTelegrams.get(2) instanceof Esp24BSTelegram); }
@Test public void testReceiveAfterRestart() throws Exception { List<EspRadioTelegram> expectedTelegrams = new ArrayList<EspRadioTelegram>(); expectedTelegrams.add(new Esp2RPSTelegram(senderID, (byte)0x11, (byte)0x22)); expectedTelegrams.add(new Esp21BSTelegram(senderID, (byte)0x33, (byte)0x44)); expectedTelegrams.add(new Esp24BSTelegram( senderID, new byte[] {(byte)0xFF, 0x01, 0x02, 0x03}, (byte)0x55) ); for(EspRadioTelegram telegram : expectedTelegrams) { byte[] telegramBytes = ((EspPacket) telegram).asByteArray(); mockPort.addDataToReturn( setPacketType(Esp2PacketHeader.PacketType.RRT_UNKNOWN, telegramBytes) ); } RadioListener listener = new RadioListener(2, expectedTelegrams.size()); processor.setProcessorListener(listener); processor.start(); List<EspRadioTelegram> receivedTelegrams = listener.waitForRadioTelegrams(); processor.stop(); Assert.assertNotNull(receivedTelegrams); Assert.assertEquals(expectedTelegrams.size(), receivedTelegrams.size()); for(EspRadioTelegram telegram : expectedTelegrams) { byte[] telegramBytes = ((EspPacket) telegram).asByteArray(); mockPort.addDataToReturn( setPacketType(Esp2PacketHeader.PacketType.RRT_UNKNOWN, telegramBytes) ); } listener = new RadioListener(2, expectedTelegrams.size()); processor.setProcessorListener(listener); processor.start(); receivedTelegrams = listener.waitForRadioTelegrams(); processor.stop(); Assert.assertNotNull(receivedTelegrams); Assert.assertEquals(expectedTelegrams.size(), receivedTelegrams.size()); }