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:
parent
a7b5f23556
commit
cca59f7e91
@ -1,33 +1,52 @@
|
|||||||
package cc.arduino.packages.discoverers;
|
package cc.arduino.packages.discoverers;
|
||||||
|
|
||||||
import java.io.IOException;
|
import cc.arduino.packages.BoardPort;
|
||||||
import java.net.InetAddress;
|
import cc.arduino.packages.Discovery;
|
||||||
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 processing.app.Base;
|
import processing.app.Base;
|
||||||
import processing.app.helpers.PreferencesMap;
|
import processing.app.helpers.PreferencesMap;
|
||||||
import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter;
|
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 {
|
public class NetworkDiscovery implements Discovery, ServiceListener {
|
||||||
|
|
||||||
private JmDNS jmDNS;
|
private List<BoardPort> ports;
|
||||||
private List<BoardPort> ports = new CopyOnWriteArrayList<BoardPort>();
|
|
||||||
|
public NetworkDiscovery() {
|
||||||
|
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
|
||||||
|
this.ports = new ArrayList<BoardPort>();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BoardPort> discovery() {
|
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) {
|
synchronized (this) {
|
||||||
return new ArrayList<BoardPort>(ports);
|
return new ArrayList<BoardPort>(this.ports);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,10 +56,8 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter());
|
for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance().getInetAddresses()) {
|
||||||
for (InetAddress addr : NetworkTopologyDiscovery.Factory.getInstance()
|
JmDNS jmDNS = JmDNS.create(addr);
|
||||||
.getInetAddresses()) {
|
|
||||||
jmDNS = JmDNS.create(addr);
|
|
||||||
jmDNS.addServiceListener("_arduino._tcp.local.", this);
|
jmDNS.addServiceListener("_arduino._tcp.local.", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,9 +82,10 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
|
|||||||
|
|
||||||
dns.requestServiceInfo(type, name);
|
dns.requestServiceInfo(type, name);
|
||||||
ServiceInfo serviceInfo = dns.getServiceInfo(type, name);
|
ServiceInfo serviceInfo = dns.getServiceInfo(type, name);
|
||||||
if (serviceInfo != null)
|
if (serviceInfo != null) {
|
||||||
dns.requestServiceInfo(type, name);
|
dns.requestServiceInfo(type, name);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void serviceRemoved(ServiceEvent serviceEvent) {
|
public void serviceRemoved(ServiceEvent serviceEvent) {
|
||||||
@ -97,8 +115,7 @@ public class NetworkDiscovery implements Discovery, ServiceListener {
|
|||||||
|
|
||||||
String label = name + " at " + address;
|
String label = name + " at " + address;
|
||||||
if (board != null) {
|
if (board != null) {
|
||||||
String boardName = Base.getPlatform()
|
String boardName = Base.getPlatform().resolveDeviceByBoardID(Base.packages, board);
|
||||||
.resolveDeviceByBoardID(Base.packages, board);
|
|
||||||
label += " (" + boardName + ")";
|
label += " (" + boardName + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user