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

NetworkDiscovery is a bit more responsive when boards go OFFLINE

This commit is contained in:
Federico Fissore 2013-06-14 16:43:05 +02:00
parent a7b5f23556
commit cca59f7e91

View File

@ -1,33 +1,52 @@
package cc.arduino.packages.discoverers;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.jmdns.JmDNS;
import javax.jmdns.NetworkTopologyDiscovery;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
import javax.jmdns.ServiceListener;
import javax.jmdns.impl.DNSTaskStarter;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import processing.app.Base;
import processing.app.helpers.PreferencesMap;
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.Discovery;
import javax.jmdns.*;
import javax.jmdns.impl.DNSTaskStarter;
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;
public class NetworkDiscovery implements Discovery, ServiceListener {
private JmDNS jmDNS;
private List<BoardPort> ports = new CopyOnWriteArrayList<BoardPort>();
private List<BoardPort> ports;
public NetworkDiscovery() {
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
this.ports = new ArrayList<BoardPort>();
}
@Override
public List<BoardPort> discovery() {
List<BoardPort> ports = clonePortsList();
Iterator<BoardPort> iterator = ports.iterator();
while (iterator.hasNext()) {
try {
InetAddress address = Inet4Address.getByName(iterator.next().getAddress());
if (!address.isReachable(100)) {
iterator.remove();
}
} catch (UnknownHostException e) {
iterator.remove();
} catch (IOException e) {
iterator.remove();
}
}
return ports;
}
private List<BoardPort> clonePortsList() {
synchronized (this) {
return new ArrayList<BoardPort>(ports);
return new ArrayList<BoardPort>(this.ports);
}
}
@ -37,10 +56,8 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
@Override
public void start() throws IOException {
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance()
.getInetAddresses()) {
jmDNS = JmDNS.create(addr);
for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance().getInetAddresses()) {
JmDNS jmDNS = JmDNS.create(addr);
jmDNS.addServiceListener("_arduino._tcp.local.", this);
}
}
@ -65,8 +82,9 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
dns.requestServiceInfo(type, name);
ServiceInfo serviceInfo = dns.getServiceInfo(type, name);
if (serviceInfo != null)
if (serviceInfo != null) {
dns.requestServiceInfo(type, name);
}
}
@Override
@ -97,11 +115,10 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
String label = name + " at " + address;
if (board != null) {
String boardName = Base.getPlatform()
.resolveDeviceByBoardID(Base.packages, board);
String boardName = Base.getPlatform().resolveDeviceByBoardID(Base.packages, board);
label += " (" + boardName + ")";
}
BoardPort port = new BoardPort();
port.setAddress(address);
port.setBoardName(name);