List of usage examples for java.net DatagramPacket getOffset
public synchronized int getOffset()
From source file:org.springframework.integration.ip.udp.DatagramPacketSendingHandlerTests.java
@Test public void verifySend() throws Exception { final int testPort = SocketUtils.findAvailableUdpSocket(); byte[] buffer = new byte[8]; final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length); final CountDownLatch latch = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(new Runnable() { public void run() { try { DatagramSocket socket = new DatagramSocket(testPort); socket.receive(receivedPacket); latch.countDown();//from w ww.j a va 2 s.c om socket.close(); } catch (Exception e) { e.printStackTrace(); } } }); Thread.sleep(1000); UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", testPort); String payload = "foo"; handler.handleMessage(MessageBuilder.withPayload(payload).build()); assertTrue(latch.await(3000, TimeUnit.MILLISECONDS)); byte[] src = receivedPacket.getData(); int length = receivedPacket.getLength(); int offset = receivedPacket.getOffset(); byte[] dest = new byte[length]; System.arraycopy(src, offset, dest, 0, length); assertEquals(payload, new String(dest)); handler.shutDown(); }
From source file:org.springframework.integration.ip.udp.DatagramPacketSendingHandlerTests.java
@Test public void verifySendWithAck() throws Exception { final List<Integer> openPorts = SocketUtils.findAvailableUdpSockets(SocketUtils.getRandomSeedPort(), 2); final int testPort = openPorts.get(0); final int ackPort = openPorts.get(1); byte[] buffer = new byte[1000]; final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); UnicastSendingMessageHandler handler = new UnicastSendingMessageHandler("localhost", testPort, true, true, "localhost", ackPort, 5000); handler.afterPropertiesSet();/* w ww . j a v a2s . co m*/ Executors.newSingleThreadExecutor().execute(new Runnable() { public void run() { try { DatagramSocket socket = new DatagramSocket(testPort); latch1.countDown(); socket.receive(receivedPacket); socket.close(); DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper(); mapper.setAcknowledge(true); mapper.setLengthCheck(true); Message<byte[]> message = mapper.toMessage(receivedPacket); Object id = message.getHeaders().get(IpHeaders.ACK_ID); byte[] ack = id.toString().getBytes(); DatagramPacket ackPack = new DatagramPacket(ack, ack.length, new InetSocketAddress("localHost", ackPort)); DatagramSocket out = new DatagramSocket(); out.send(ackPack); out.close(); latch2.countDown(); } catch (Exception e) { e.printStackTrace(); } } }); latch1.await(3000, TimeUnit.MILLISECONDS); String payload = "foobar"; handler.handleMessage(MessageBuilder.withPayload(payload).build()); assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS)); byte[] src = receivedPacket.getData(); int length = receivedPacket.getLength(); int offset = receivedPacket.getOffset(); byte[] dest = new byte[6]; System.arraycopy(src, offset + length - 6, dest, 0, 6); assertEquals(payload, new String(dest)); handler.shutDown(); }