1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-30 11:24:12 +01:00

IDE network discovery now reacts to new networks: it monitors available IP addresses and, when one is added, it starts listening to bonjours coming from it

This commit is contained in:
Federico Fissore 2013-06-25 16:13:14 +02:00
parent 37825c1711
commit c2897c4a3f
5 changed files with 105 additions and 23 deletions

View File

@ -23,7 +23,7 @@ public interface Discovery {
/**
* Stop discovery service
*/
public void stop();
public void stop() throws Exception;
/**
* Return the list of discovered ports.

View File

@ -1,12 +1,12 @@
package cc.arduino.packages;
import static processing.app.I18n._;
import cc.arduino.packages.discoverers.NetworkDiscovery;
import cc.arduino.packages.discoverers.SerialDiscovery;
import java.util.ArrayList;
import java.util.List;
import cc.arduino.packages.discoverers.NetworkDiscovery;
import cc.arduino.packages.discoverers.SerialDiscovery;
import static processing.app.I18n._;
public class DiscoveryManager {
@ -25,7 +25,7 @@ public class DiscoveryManager {
e.printStackTrace();
}
}
Runtime.getRuntime().addShutdownHook(closeHook);
}
@ -39,8 +39,13 @@ public class DiscoveryManager {
private Thread closeHook = new Thread(new Runnable() {
@Override
public void run() {
for (Discovery d : discoverers)
d.stop();
for (Discovery d : discoverers) {
try {
d.stop();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
});
}

View File

@ -2,6 +2,7 @@ package cc.arduino.packages.discoverers;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import cc.arduino.packages.discoverers.network.NetworkChecker;
import processing.app.Base;
import processing.app.helpers.PreferencesMap;
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
@ -12,17 +13,18 @@ import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
public class NetworkDiscovery implements Discovery, ServiceListener {
public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino.packages.discoverers.network.NetworkTopologyListener {
private Timer timer;
private List<BoardPort> ports;
private final Map<InetAddress, JmDNS> mappedJmDNSs;
public NetworkDiscovery() {
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
this.ports = new ArrayList<BoardPort>();
this.mappedJmDNSs = new Hashtable<InetAddress, JmDNS>();
}
@Override
@ -56,21 +58,14 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
@Override
public void start() throws IOException {
for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance().getInetAddresses()) {
JmDNS jmDNS = JmDNS.create(addr);
jmDNS.addServiceListener("_arduino._tcp.local.", this);
}
this.timer = new Timer(this.getClass().getName() + " timer");
new NetworkChecker(this, NetworkTopologyDiscovery.Factory.getInstance()).start(timer);
}
@Override
public void stop() {
// Removed cleanup: is extremely slow on closing
// try {
// jmDNS.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
public void stop() throws IOException {
timer.purge();
// we don't close each JmDNS instance as it's too slow
}
@Override
@ -130,4 +125,29 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
}
}
@Override
public void inetAddressAdded(InetAddress address) {
if (mappedJmDNSs.containsKey(address)) {
return;
}
try {
JmDNS jmDNS = JmDNS.create(address);
jmDNS.addServiceListener("_arduino._tcp.local.", this);
mappedJmDNSs.put(address, jmDNS);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void inetAddressRemoved(InetAddress address) {
JmDNS jmDNS = mappedJmDNSs.remove(address);
if (jmDNS != null) {
try {
jmDNS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,46 @@
package cc.arduino.packages.discoverers.network;
import javax.jmdns.NetworkTopologyDiscovery;
import java.net.InetAddress;
import java.util.*;
public class NetworkChecker extends TimerTask {
private final NetworkTopologyListener topologyListener;
private final NetworkTopologyDiscovery topology;
private Set<InetAddress> knownAddresses;
public NetworkChecker(NetworkTopologyListener topologyListener, NetworkTopologyDiscovery topology) {
super();
this.topologyListener = topologyListener;
this.topology = topology;
this.knownAddresses = Collections.synchronizedSet(new HashSet<InetAddress>());
}
public void start(Timer timer) {
timer.schedule(this, 0, 3000);
}
@Override
public void run() {
try {
InetAddress[] curentAddresses = topology.getInetAddresses();
Set<InetAddress> current = new HashSet<InetAddress>(curentAddresses.length);
for (InetAddress address : curentAddresses) {
current.add(address);
if (!knownAddresses.contains(address)) {
topologyListener.inetAddressAdded(address);
}
}
for (InetAddress address : knownAddresses) {
if (!current.contains(address)) {
topologyListener.inetAddressRemoved(address);
}
}
knownAddresses = current;
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,11 @@
package cc.arduino.packages.discoverers.network;
import java.net.InetAddress;
public interface NetworkTopologyListener {
void inetAddressAdded(InetAddress address);
void inetAddressRemoved(InetAddress address);
}