Skip to content

Commit a181f8b

Browse files
committed
Added broadcast to discover server
1 parent 6671af1 commit a181f8b

3 files changed

Lines changed: 127 additions & 5 deletions

File tree

.idea/misc.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

app/src/main/java/uk/co/digitme/machinemonitoring/MainActivity.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package uk.co.digitme.machinemonitoring;
22

3+
import static uk.co.digitme.machinemonitoring.Helpers.ServerDiscovery.findServer;
4+
35
import android.annotation.SuppressLint;
46
import android.content.Intent;
57
import android.content.SharedPreferences;
@@ -84,8 +86,9 @@ public void onSingleClick(View v) {
8486
@Override
8587
protected void onResume() {
8688
super.onResume();
87-
// On the first run, show the "set address" button immediately
89+
// On the first run, try to get server address immediately
8890
if (prefs.getBoolean("firstrun", true)) {
91+
discoverServer();
8992
mStatusText.setVisibility(View.INVISIBLE);
9093
mRetryButton.setVisibility(View.INVISIBLE);
9194
mSetAddressButton.setVisibility(View.VISIBLE);
@@ -101,11 +104,20 @@ protected void onResume() {
101104
}
102105
}
103106

107+
108+
private boolean discoverServer(){
109+
mStatusText.setText("Searching for OEE Server...");
110+
return findServer(getApplicationContext());
111+
}
112+
113+
104114
private void showError(String errorText) {
105-
mStatusText.setText(errorText);
106-
mStatusText.setVisibility(View.VISIBLE);
107-
mRetryButton.setVisibility(View.VISIBLE);
108-
mSetAddressButton.setVisibility(View.VISIBLE);
115+
if (!discoverServer()) {
116+
mStatusText.setText(errorText);
117+
mStatusText.setVisibility(View.VISIBLE);
118+
mRetryButton.setVisibility(View.VISIBLE);
119+
mSetAddressButton.setVisibility(View.VISIBLE);
120+
}
109121
}
110122

111123
/**

0 commit comments

Comments
 (0)