|
| 1 | +package uk.co.digitme.machinemonitoring.Helpers; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.StrictMode; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.DatagramPacket; |
| 9 | +import java.net.DatagramSocket; |
| 10 | +import java.net.InetAddress; |
| 11 | +import java.net.InterfaceAddress; |
| 12 | +import java.net.NetworkInterface; |
| 13 | +import java.net.SocketTimeoutException; |
| 14 | +import java.util.Enumeration; |
| 15 | + |
| 16 | +public class ServerDiscovery { |
| 17 | + |
| 18 | + private static final int DISCOVERY_TIMEOUT_MS = 10000; |
| 19 | + private static final String DISCOVERY_REQUEST_MESSAGE = "DISCOVER_OEE_SERVER_REQUEST"; |
| 20 | + private static final String DISCOVERY_RESPONSE_MESSAGE = "DISCOVER_OEE_SERVER_RESPONSE"; |
| 21 | + private static final String TAG = "ServerDiscovery"; |
| 22 | + |
| 23 | + public static boolean findServer(Context context) { |
| 24 | + //Find the server using UDP broadcast |
| 25 | + DbHelper dbHelper = new DbHelper(context); |
| 26 | + StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); |
| 27 | + StrictMode.setThreadPolicy(policy); |
| 28 | + String ip; |
| 29 | + |
| 30 | + |
| 31 | + try { |
| 32 | + //Open a random port to send the package |
| 33 | + DatagramSocket datagramSocket = new DatagramSocket(); |
| 34 | + datagramSocket.setBroadcast(true); |
| 35 | + datagramSocket.setSoTimeout(DISCOVERY_TIMEOUT_MS); |
| 36 | + |
| 37 | + byte[] sendData = DISCOVERY_REQUEST_MESSAGE.getBytes(); |
| 38 | + |
| 39 | + //Broadcast the message all over the network interfaces |
| 40 | + Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
| 41 | + while (interfaces.hasMoreElements()) { |
| 42 | + NetworkInterface networkInterface = interfaces.nextElement(); |
| 43 | + |
| 44 | + if (networkInterface.isLoopback() || !networkInterface.isUp()) { |
| 45 | + continue; //Don't want to broadcast to the loopback interface |
| 46 | + } |
| 47 | + |
| 48 | + for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) { |
| 49 | + InetAddress broadcast = interfaceAddress.getBroadcast(); |
| 50 | + if (broadcast == null) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + //Send the broadcast packet |
| 54 | + try { |
| 55 | + DatagramPacket sendPacket = new DatagramPacket( |
| 56 | + sendData, |
| 57 | + sendData.length, |
| 58 | + broadcast, |
| 59 | + 8090); |
| 60 | + datagramSocket.send(sendPacket); |
| 61 | + } catch (Exception e) { |
| 62 | + e.printStackTrace(); |
| 63 | + return false; |
| 64 | + } |
| 65 | + Log.v(TAG, |
| 66 | + "Request packet sent to: " + |
| 67 | + broadcast.getHostAddress() + |
| 68 | + "; Interface: " + |
| 69 | + networkInterface.getDisplayName()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Log.v(TAG, "Done looping over network interfaces. Waiting for a reply"); |
| 74 | + |
| 75 | + //Wait for a response |
| 76 | + byte[] receiveBuf = new byte[15000]; |
| 77 | + DatagramPacket receivePacket = new DatagramPacket(receiveBuf, receiveBuf.length); |
| 78 | + //Show a dialog if the connection times out |
| 79 | + try { |
| 80 | + datagramSocket.receive(receivePacket); |
| 81 | + } catch (SocketTimeoutException e) { |
| 82 | + |
| 83 | + Log.v(TAG, "Socket timeout. Exiting..."); |
| 84 | + datagramSocket.close(); |
| 85 | + return false; |
| 86 | + } |
| 87 | + //We have a response |
| 88 | + Log.v(TAG, "Broadcast response from server: " + receivePacket.getAddress().getHostAddress()); |
| 89 | + |
| 90 | + //Check if the message is correct |
| 91 | + String message = new String(receivePacket.getData()).trim(); |
| 92 | + if (message.equals(DISCOVERY_RESPONSE_MESSAGE)) { |
| 93 | + ip = receivePacket.getAddress().toString(); |
| 94 | + Log.v(TAG, "HOST IP IS " + ip); |
| 95 | + String address = "http://" + ip + ":80"; // todo support other ports |
| 96 | + //Save the ip as a preference |
| 97 | + dbHelper.saveServerAddress(address); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + datagramSocket.close(); |
| 102 | + } catch (IOException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + return false; |
| 105 | + } |
| 106 | + return false; |
| 107 | + |
| 108 | + } |
| 109 | +} |
0 commit comments