Skip to content

Commit 944dcf1

Browse files
committed
TEST: jmdns hacking
1 parent a63ec15 commit 944dcf1

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

arduino-core/src/cc/arduino/packages/discoverers/NetworkDiscovery.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
4646

4747
private final List<BoardPort> reachableBoardPorts = new LinkedList<>();
4848
private final List<BoardPort> boardPortsDiscoveredWithJmDNS = new LinkedList<>();
49-
private final Timer reachabilityTimer = new Timer();
49+
private Timer reachabilityTimer;
50+
private JmDNS jmdns = null;
5051

5152
private void removeDuplicateBoards(BoardPort newBoard) {
5253
synchronized (boardPortsDiscoveredWithJmDNS) {
@@ -137,23 +138,31 @@ public void serviceResolved(ServiceEvent serviceEvent) {
137138
}
138139

139140
public NetworkDiscovery() {
140-
final JmDNS jmdns;
141+
142+
}
143+
144+
@Override
145+
public void start() {
141146
try {
142147
jmdns = JmDNS.create();
143148
jmdns.addServiceListener("_arduino._tcp.local.", this);
144149
} catch (IOException e) {
145150
e.printStackTrace();
146151
}
147-
}
148-
149-
@Override
150-
public void start() {
152+
reachabilityTimer = new Timer();
151153
new BoardReachabilityFilter(this).start(reachabilityTimer);
152154
}
153155

154156
@Override
155157
public void stop() {
158+
jmdns.unregisterAllServices();
159+
try {
160+
jmdns.close();
161+
} catch (IOException e) {
162+
e.printStackTrace();
163+
}
156164
reachabilityTimer.cancel();
165+
start();
157166
}
158167

159168
@Override

arduino-core/src/cc/arduino/packages/discoverers/network/BoardReachabilityFilter.java

+28
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,51 @@
3434
import processing.app.helpers.NetUtils;
3535

3636
import java.net.InetAddress;
37+
import java.net.NetworkInterface;
38+
import java.util.Enumeration;
3739
import java.net.UnknownHostException;
3840
import java.util.*;
3941

4042
public class BoardReachabilityFilter extends TimerTask {
4143

4244
private final NetworkDiscovery networkDiscovery;
45+
private Enumeration<NetworkInterface> staticNetworkInterfaces;
46+
private final List<String> staticNetworkInterfacesList = new LinkedList<>();
4347

4448
public BoardReachabilityFilter(NetworkDiscovery networkDiscovery) {
4549
this.networkDiscovery = networkDiscovery;
4650
}
4751

4852
public void start(Timer timer) {
53+
try {
54+
staticNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
55+
while (staticNetworkInterfaces.hasMoreElements()) {
56+
staticNetworkInterfacesList.add(staticNetworkInterfaces.nextElement().getInterfaceAddresses().toString());
57+
}
58+
} catch (Exception e) {
59+
e.printStackTrace();
60+
}
61+
4962
timer.schedule(this, 0, 5000);
5063
}
5164

5265
@Override
5366
public void run() {
67+
68+
try {
69+
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
70+
while (networkInterfaces.hasMoreElements()) {
71+
if (!staticNetworkInterfacesList.contains(networkInterfaces.nextElement().getInterfaceAddresses().toString())) {
72+
networkDiscovery.stop();
73+
staticNetworkInterfacesList.clear();
74+
System.out.println("IP changed, restarting jmdns");
75+
return;
76+
}
77+
}
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
5482
List<BoardPort> boardPorts = networkDiscovery.getBoardPortsDiscoveredWithJmDNS();
5583

5684
Iterator<BoardPort> boardPortIterator = boardPorts.iterator();

0 commit comments

Comments
 (0)