List of usage examples for java.net DatagramPacket getLength
public synchronized int getLength()
From source file:org.openhab.io.hueemulation.internal.HueEmulationUpnpServer.java
@Override public void run() { MulticastSocket recvSocket = null; // since jupnp shares port 1900, lets use a different port to send UDP packets on just to be safe. DatagramSocket sendSocket = null; byte[] buf = new byte[1000]; DatagramPacket recv = new DatagramPacket(buf, buf.length); while (running) { try {/*from ww w.j av a 2 s . c om*/ if (discoveryIp != null && discoveryIp.trim().length() > 0) { address = InetAddress.getByName(discoveryIp); } else { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface ni = interfaces.nextElement(); Enumeration<InetAddress> addresses = ni.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { address = addr; break; } } } } InetSocketAddress socketAddr = new InetSocketAddress(MULTI_ADDR, UPNP_PORT_RECV); recvSocket = new MulticastSocket(UPNP_PORT_RECV); recvSocket.joinGroup(socketAddr, NetworkInterface.getByInetAddress(address)); sendSocket = new DatagramSocket(); while (running) { recvSocket.receive(recv); if (recv.getLength() > 0) { String data = new String(recv.getData()); logger.trace("Got SSDP Discovery packet from {}:{}", recv.getAddress().getHostAddress(), recv.getPort()); if (data.startsWith("M-SEARCH")) { String msg = String.format(discoString, "http://" + address.getHostAddress().toString() + ":" + System.getProperty("org.osgi.service.http.port") + discoPath, usn); DatagramPacket response = new DatagramPacket(msg.getBytes(), msg.length(), recv.getAddress(), recv.getPort()); try { logger.trace("Sending to {} : {}", recv.getAddress().getHostAddress(), msg); sendSocket.send(response); } catch (IOException e) { logger.error("Could not send UPNP response", e); } } } } } catch (SocketException e) { logger.error("Socket error with UPNP server", e); } catch (IOException e) { logger.error("IO Error with UPNP server", e); } finally { IOUtils.closeQuietly(recvSocket); IOUtils.closeQuietly(sendSocket); if (running) { try { Thread.sleep(3000); } catch (InterruptedException e) { } } } } }
From source file:snapshotTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;/*from www. j a va 2s. c o m*/ DatagramPacket receivedPacket; InetAddress theServerAddress; byte[] outBuffer; byte[] inBuffer; inBuffer = new byte[8192]; outBuffer = new byte[8192]; try { HttpSession session = RuntimeAccess.getInstance().getSession(); String sessionUser = (String) session.getAttribute("User"); if (sessionUser == null) { sessionUser = "administrator"; } JSONObject joCmd = new JSONObject(); JSONObject joAction = new JSONObject(action); joCmd.put("sender", sessionUser); joCmd.put("target", "SNAPSHOT"); joCmd.put("node", node); joCmd.put("action", joAction); String output = joCmd.toString(); socket = new DatagramSocket(); String actionName = joAction.get("name").toString(); if (actionName.equals("create")) { socket.setSoTimeout(180000); } else { socket.setSoTimeout(60000); } InetAddress serverInet = InetAddress.getByName("localhost"); socket.connect(serverInet, serverPort); outBuffer = output.getBytes(); packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort); try { // send the data socket.send(packet2Send); receivedPacket = new DatagramPacket(inBuffer, inBuffer.length); socket.receive(receivedPacket); // the server response is... result = new String(receivedPacket.getData(), 0, receivedPacket.getLength()); session.setAttribute("LastActive", System.currentTimeMillis()); } catch (Exception excep) { String msg = excep.getMessage(); //String msg = excep.toString(); joCmd.remove("action"); joAction.put("result", "Error:" + msg); joCmd.put("action", joAction); result = joCmd.toString(); } socket.close(); } catch (Exception e) { log(ERROR, "callOvnmanager", e); return e.toString(); } return result; }
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();//from w w w .j a v a 2s .c o 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(); }
From source file:vmXmlFile.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;//from www . j a va2 s .c o m DatagramPacket receivedPacket; InetAddress theServerAddress; byte[] outBuffer; byte[] inBuffer; inBuffer = new byte[65536]; outBuffer = new byte[8192]; try { HttpSession session = RuntimeAccess.getInstance().getSession(); String sessionUser = (String) session.getAttribute("User"); if (sessionUser == null) { sessionUser = "administrator"; } JSONObject joCmd = new JSONObject(); JSONObject joAction = new JSONObject(action); joCmd.put("sender", sessionUser); joCmd.put("target", "VM"); joCmd.put("node", node); joCmd.put("action", joAction); String output = joCmd.toString(); socket = new DatagramSocket(); InetAddress serverInet = InetAddress.getByName("localhost"); socket.connect(serverInet, serverPort); outBuffer = output.getBytes(); packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort); // send the data socket.send(packet2Send); receivedPacket = new DatagramPacket(inBuffer, inBuffer.length); socket.receive(receivedPacket); // the server response is... result = new String(receivedPacket.getData(), 0, receivedPacket.getLength()); session.setAttribute("LastActive", System.currentTimeMillis()); socket.close(); } catch (Exception e) { log(ERROR, "callOvnmanager", e); return e.toString(); } return result; }
From source file:vmTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;/*from www . ja v a 2s .c o m*/ DatagramPacket receivedPacket; InetAddress theServerAddress; byte[] outBuffer; byte[] inBuffer; inBuffer = new byte[65536]; outBuffer = new byte[8192]; /* if (this.user == null) { this.user = "admin"; }*/ try { HttpSession session = RuntimeAccess.getInstance().getSession(); String sessionUser = (String) session.getAttribute("User"); if (sessionUser == null) { sessionUser = "administrator"; } JSONObject joCmd = new JSONObject(); JSONObject joAction = new JSONObject(action); joCmd.put("sender", sessionUser); joCmd.put("target", "VM"); joCmd.put("node", node); joCmd.put("action", joAction); String output = joCmd.toString(); socket = new DatagramSocket(); String actionName = joAction.get("name").toString(); /*if (actionName.equals("migrate")) { socket.setSoTimeout(600000); } else { socket.setSoTimeout(60000); }*/ socket.setSoTimeout(60000); InetAddress serverInet = InetAddress.getByName("localhost"); socket.connect(serverInet, serverPort); outBuffer = output.getBytes(); packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort); try { // send the data socket.send(packet2Send); receivedPacket = new DatagramPacket(inBuffer, inBuffer.length); socket.receive(receivedPacket); // the server response is... result = new String(receivedPacket.getData(), 0, receivedPacket.getLength()); session.setAttribute("LastActive", System.currentTimeMillis()); } catch (Exception excep) { String msg = excep.getMessage(); //String msg = excep.toString(); joCmd.remove("action"); joAction.put("result", "Error:" + msg); joCmd.put("action", joAction); result = joCmd.toString(); } socket.close(); } catch (Exception e) { log(ERROR, "callOvnmanager", e); return e.toString(); } return result; }
From source file:serverTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;//from ww w.ja v a 2 s . c o m DatagramPacket receivedPacket; InetAddress theServerAddress; byte[] outBuffer; byte[] inBuffer; //inBuffer = new byte[8192]; //outBuffer = new byte[8192]; inBuffer = new byte[65536]; outBuffer = new byte[8192]; try { HttpSession session = RuntimeAccess.getInstance().getSession(); String sessionUser = (String) session.getAttribute("User"); if (sessionUser == null) { sessionUser = "administrator"; } JSONObject joAction = new JSONObject(action); JSONObject joCmd = new JSONObject(); joCmd.put("sender", sessionUser); joCmd.put("target", "NODE"); joCmd.put("node", node); joCmd.put("action", joAction); String output = joCmd.toString(); socket = new DatagramSocket(); // set timeout String actionName = joAction.get("name").toString(); if (actionName.equals("add")) { socket.setSoTimeout(90000); } else if (actionName.equals("connect")) { socket.setSoTimeout(60000); } else { socket.setSoTimeout(60000); } InetAddress serverInet = InetAddress.getByName("localhost"); socket.connect(serverInet, serverPort); outBuffer = output.getBytes(); packet2Send = new DatagramPacket(outBuffer, outBuffer.length, serverInet, serverPort); receivedPacket = new DatagramPacket(inBuffer, inBuffer.length); try { // send the data socket.send(packet2Send); // receive reply socket.receive(receivedPacket); // the server reply is... result = new String(receivedPacket.getData(), 0, receivedPacket.getLength()); session.setAttribute("LastActive", System.currentTimeMillis()); } catch (Exception excep) { String msg = excep.getMessage(); //String msg = excep.toString(); joCmd.remove("action"); joAction.put("result", "Error:" + msg); joCmd.put("action", joAction); result = joCmd.toString(); } socket.close(); } catch (Exception e) { log(ERROR, "callOvnmanager", e); return e.toString(); } return result; }