public MsgEvent call(MsgEvent me) { try { String callId = java.util.UUID.randomUUID().toString(); me.setParam( "callId-" + PluginEngine.region + "-" + PluginEngine.agent + "-" + PluginEngine.plugin, callId); PluginEngine.msgInQueue.offer(me); int count = 0; int timeout = 300; while (count < timeout) { if (PluginEngine.rpcMap.containsKey(callId)) { MsgEvent ce = null; synchronized (PluginEngine.rpcMap) { ce = PluginEngine.rpcMap.get(callId); PluginEngine.rpcMap.remove(callId); } return ce; } Thread.sleep(100); count++; } return null; } catch (Exception ex) { System.out.println("Controller : RPCCall : RPC failed " + ex.toString()); return null; } }
public List<MsgEvent> discover() { List<MsgEvent> discoveryList = null; // Find the server using UDP broadcast try { discoveryList = new ArrayList<MsgEvent>(); // Open a random port to send the package c = new DatagramSocket(); c.setBroadcast(true); byte[] sendData = "DISCOVER_FUIFSERVER_REQUEST".getBytes(); // Try the 255.255.255.255 first try { DatagramPacket sendPacket = new DatagramPacket( sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 32005); c.send(sendPacket); // System.out.println(getClass().getName() + ">>> Request packet sent to: 255.255.255.255 // (DEFAULT)"); } catch (Exception e) { } // Broadcast the message over all the network interfaces Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement(); if (networkInterface.isLoopback() || !networkInterface.isUp()) { continue; // Don't want to broadcast to the loopback interface } for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { InetAddress broadcast = interfaceAddress.getBroadcast(); if (broadcast == null) { continue; } // Send the broadcast package! try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 32005); c.send(sendPacket); } catch (Exception e) { } // System.out.println(getClass().getName() + ">>> Request packet sent to: " + // broadcast.getHostAddress() + "; Interface: " + networkInterface.getDisplayName()); } } // System.out.println(getClass().getName() + ">>> Done looping over all network interfaces. // Now waiting for a reply!"); // Wait for a response while (!c.isClosed()) { try { byte[] recvBuf = new byte[15000]; DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length); c.receive(receivePacket); // We have a response // System.out.println(getClass().getName() + ">>> Broadcast response from server: " + // receivePacket.getAddress().getHostAddress()); // Check if the message is correct // System.out.println(new String(receivePacket.getData())); String json = new String(receivePacket.getData()).trim(); // String response = "region=region0,agent=agent0,recaddr=" + // packet.getAddress().getHostAddress(); try { MsgEvent me = gson.fromJson(json, MsgEvent.class); if (me != null) { if (!me.getParam("clientip").equals(receivePacket.getAddress().getHostAddress())) { // System.out.println("SAME HOST"); // System.out.println(me.getParamsString() + // receivePacket.getAddress().getHostAddress()); me.setParam("serverip", receivePacket.getAddress().getHostAddress()); discoveryList.add(me); } } } catch (Exception ex) { System.out.println("in loop 0" + ex.toString()); } } catch (SocketException ex) { // eat message.. this should happen } catch (Exception ex) { System.out.println("in loop 1" + ex.toString()); } } // Close the port! // c.close(); // System.out.println("CODY : Dicsicer Client Worker Engned!"); } catch (Exception ex) { System.out.println("while not closed: " + ex.toString()); } return discoveryList; }