List of usage examples for java.net DatagramSocket setSoTimeout
public synchronized void setSoTimeout(int timeout) throws SocketException
From source file:vitro.vgw.wsiadapter.WSIAdapterCoap.java
/** * WSIAdapter interface//from w w w .j a va2 s . co m */ public List<Node> getAvailableNodeList() throws WSIAdapterException { logger.info("Getting available nodes..."); nodesList = new ArrayList<Node>(); List<String> wsnProxyList = new LinkedList<String>(); wsnProxyList.add(Network.WLAB_OFFICE_PROXY_ADDRESS); wsnProxyList.add(Network.WLAB_LAB_PROXY_ADDRESS); DatagramSocket serverSocket = null; DatagramSocket clientSocket = null; String cmdString = "route"; for (int i = 0; i < wsnProxyList.size(); i++) { try { serverSocket = new DatagramSocket(Constants.UDPSHELL_VGW_PORT); String hostProxyString = wsnProxyList.get(i); InetAddress hostProxy = InetAddress.getByName(hostProxyString); clientSocket = new DatagramSocket(); byte[] bufCmd = new byte[10]; bufCmd = cmdString.getBytes(); DatagramPacket outcomingPacket = new DatagramPacket(bufCmd, bufCmd.length, hostProxy, Constants.PROXY_UDPFORWARDER_PORT); clientSocket.send(outcomingPacket); boolean otherPackets = false; serverSocket.setSoTimeout(Constants.PROXY_RESPONSE_TIMEOUT); logger.info("Quering " + hostProxyString); try { byte[] bufAck = new byte[10]; DatagramPacket ackPacket = new DatagramPacket(bufAck, bufAck.length); serverSocket.receive(ackPacket); String ackString = new String(ackPacket.getData()).trim(); if (ackString.equals("ack")) { otherPackets = true; } } catch (SocketTimeoutException e) { logger.warn(e.getMessage()); } serverSocket.setSoTimeout(0); while (otherPackets) { try { byte[] bufIncoming = new byte[1000]; DatagramPacket incomingPacket = new DatagramPacket(bufIncoming, bufIncoming.length); serverSocket.receive(incomingPacket); String currentNodeIP = new String(incomingPacket.getData()).trim(); if (!currentNodeIP.equals("end")) { logger.info("Node: " + currentNodeIP); nodesList.add(new Node(currentNodeIP)); } else { otherPackets = false; logger.info("No other nodes from " + hostProxyString); } } catch (IOException e) { logger.error(e.getMessage()); } } } catch (UnknownHostException e) { logger.warn(e.getMessage() + " is not reachable."); } catch (SocketException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } finally { if (serverSocket != null) { serverSocket.close(); } if (clientSocket != null) { clientSocket.close(); } } } return nodesList; }
From source file:com.clustercontrol.agent.Agent.java
/** * ??awakeAgent?/* www. j a va 2 s . c o m*/ * Agent.properties???UDP?24005??????????(releaseLatch) * ????ReceiveTopic????Topic???? */ public void waitAwakeAgent() { final int BUFSIZE = 1; byte[] buf = new byte[BUFSIZE]; InetAddress cAddr; // ??IP int cPort; // ??? DatagramSocket sock = null; boolean flag = true; int port = 24005; int awakeDelay = 1000; try { String awakeDelayStr = AgentProperties.getProperty("awake.delay", Integer.toString(1000)); awakeDelay = Integer.parseInt(awakeDelayStr); m_log.info("awake.delay = " + awakeDelay + " msec"); } catch (NumberFormatException e) { m_log.error("awake.delay", e); } while (true) { /* * UDP???flag?true?? * ?????flag?false?????getTopic(releaseLatch)? * * UDP???????getTopic???????? * ?????? */ try { if (sock != null && port != awakePort) { sock.close(); sock = null; } if (sock == null || !sock.isBound()) { port = awakePort; sock = new DatagramSocket(port); sock.setSoTimeout(awakeDelay); } DatagramPacket recvPacket = new DatagramPacket(buf, BUFSIZE); sock.receive(recvPacket); cAddr = recvPacket.getAddress(); cPort = recvPacket.getPort(); flag = true; m_log.info("waitAwakeAgent (" + cAddr.getHostAddress() + " onPort=" + cPort + ") buf.length=" + buf.length); } catch (SocketTimeoutException e) { if (flag) { m_log.info("waitAwakeAgent packet end"); m_receiveTopic.releaseLatch(); flag = false; } } catch (Exception e) { String msg = "waitAwakeAgent port=" + awakePort + ", " + e.getClass().getSimpleName() + ", " + e.getMessage(); if (e instanceof BindException) { m_log.warn(msg); } else { m_log.warn(msg, e); } try { Thread.sleep(60 * 1000); } catch (InterruptedException e1) { m_log.warn(e1, e1); } } } }
From source file:org.opendaylight.lispflowmapping.integrationtest.MappingServiceIntegrationTest.java
License:asdf
private DatagramPacket receivePacket(DatagramSocket receivedSocket, int timeout) throws SocketTimeoutException { try {/*from w w w . j a v a 2s . c o m*/ byte[] buffer = new byte[4096]; DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length); LOG.trace("Waiting for packet from socket..."); receivedSocket.setSoTimeout(timeout); receivedSocket.receive(receivePacket); LOG.trace("Recieved packet from socket!"); return receivePacket; } catch (SocketTimeoutException ste) { throw ste; } catch (Throwable t) { fail(); return null; } }
From source file:vmTools.java
private String callOvnmanager(String node, String action) { String result = ""; DatagramSocket socket = null; int serverPort = 9999; DatagramPacket packet2Send;/* w w w . j av a2s . c om*/ 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 w w w . ja v a 2s.c om*/ 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; }