31
31
32
32
import cc .arduino .packages .BoardPort ;
33
33
import cc .arduino .packages .Discovery ;
34
+ import cc .arduino .packages .discoverers .network .BoardReachabilityFilter ;
34
35
import cc .arduino .packages .discoverers .network .NetworkChecker ;
35
36
import processing .app .BaseNoGui ;
36
- import processing .app .helpers .NetUtils ;
37
37
import processing .app .helpers .PreferencesMap ;
38
38
import processing .app .zeroconf .jmdns .ArduinoDNSTaskStarter ;
39
39
40
40
import javax .jmdns .*;
41
41
import javax .jmdns .impl .DNSTaskStarter ;
42
42
import java .io .IOException ;
43
43
import java .net .InetAddress ;
44
- import java .net .UnknownHostException ;
45
44
import java .util .*;
46
45
47
46
public class NetworkDiscovery implements Discovery , ServiceListener , cc .arduino .packages .discoverers .network .NetworkTopologyListener {
48
47
49
- private Timer timer ;
50
- private final List <BoardPort > ports ;
48
+ private final List <BoardPort > boardPortsDiscoveredWithJmDNS ;
51
49
private final Map <InetAddress , JmDNS > mappedJmDNSs ;
50
+ private Timer networkCheckerTimer ;
51
+ private Timer boardReachabilityFilterTimer ;
52
+ private final List <BoardPort > reachableBoardPorts ;
52
53
53
54
public NetworkDiscovery () {
54
55
DNSTaskStarter .Factory .setClassDelegate (new ArduinoDNSTaskStarter ());
55
- this .ports = new ArrayList <BoardPort >();
56
+ this .boardPortsDiscoveredWithJmDNS = new LinkedList <BoardPort >();
56
57
this .mappedJmDNSs = new Hashtable <InetAddress , JmDNS >();
58
+ this .reachableBoardPorts = new LinkedList <BoardPort >();
57
59
}
58
60
59
61
@ Override
60
- public List <BoardPort > discovery () {
61
- List <BoardPort > boardPorts = clonePortsList ();
62
- Iterator <BoardPort > boardPortIterator = boardPorts .iterator ();
63
- while (boardPortIterator .hasNext ()) {
64
- try {
65
- BoardPort board = boardPortIterator .next ();
66
-
67
- InetAddress inetAddress = InetAddress .getByName (board .getAddress ());
68
- int broadcastedPort = Integer .valueOf (board .getPrefs ().get ("port" ));
69
-
70
- List <Integer > ports = new LinkedList <Integer >();
71
- ports .add (broadcastedPort );
72
-
73
- //dirty code: allows non up to date yuns to be discovered. Newer yuns will broadcast port 22
74
- if (broadcastedPort == 80 ) {
75
- ports .add (0 , 22 );
76
- }
77
-
78
- boolean reachable = NetUtils .isReachable (inetAddress , ports );
79
- if (!reachable ) {
80
- boardPortIterator .remove ();
81
- }
82
- } catch (UnknownHostException e ) {
83
- boardPortIterator .remove ();
84
- }
62
+ public List <BoardPort > listDiscoveredBoards () {
63
+ synchronized (reachableBoardPorts ) {
64
+ return new LinkedList <BoardPort >(reachableBoardPorts );
85
65
}
86
- return boardPorts ;
87
66
}
88
67
89
- private List <BoardPort > clonePortsList () {
90
- synchronized (this ) {
91
- return new ArrayList <BoardPort >(this .ports );
68
+ public void setReachableBoardPorts (List <BoardPort > newReachableBoardPorts ) {
69
+ synchronized (reachableBoardPorts ) {
70
+ this .reachableBoardPorts .clear ();
71
+ this .reachableBoardPorts .addAll (newReachableBoardPorts );
92
72
}
93
73
}
94
74
95
- @ Override
96
- public void setPreferences (PreferencesMap options ) {
75
+ public List <BoardPort > getBoardPortsDiscoveredWithJmDNS () {
76
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
77
+ return new LinkedList <BoardPort >(boardPortsDiscoveredWithJmDNS );
78
+ }
97
79
}
98
80
99
81
@ Override
100
82
public void start () throws IOException {
101
- this .timer = new Timer (this .getClass ().getName () + " timer" );
102
- new NetworkChecker (this , NetworkTopologyDiscovery .Factory .getInstance ()).start (timer );
83
+ this .networkCheckerTimer = new Timer (NetworkChecker .class .getName ());
84
+ new NetworkChecker (this , NetworkTopologyDiscovery .Factory .getInstance ()).start (networkCheckerTimer );
85
+ this .boardReachabilityFilterTimer = new Timer (BoardReachabilityFilter .class .getName ());
86
+ new BoardReachabilityFilter (this ).start (boardReachabilityFilterTimer );
103
87
}
104
88
105
89
@ Override
106
90
public void stop () throws IOException {
107
- timer .purge ();
91
+ this .networkCheckerTimer .purge ();
92
+ this .boardReachabilityFilterTimer .purge ();
108
93
// we don't close each JmDNS instance as it's too slow
109
94
}
110
95
@@ -125,10 +110,11 @@ public void serviceAdded(ServiceEvent serviceEvent) {
125
110
@ Override
126
111
public void serviceRemoved (ServiceEvent serviceEvent ) {
127
112
String name = serviceEvent .getName ();
128
- synchronized (this ) {
129
- for (BoardPort port : ports ) {
130
- if (port .getBoardName ().equals (name ))
131
- ports .remove (port );
113
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
114
+ for (BoardPort port : boardPortsDiscoveredWithJmDNS ) {
115
+ if (port .getBoardName ().equals (name )) {
116
+ boardPortsDiscoveredWithJmDNS .remove (port );
117
+ }
132
118
}
133
119
}
134
120
}
@@ -147,10 +133,9 @@ public void serviceResolved(ServiceEvent serviceEvent) {
147
133
board = info .getPropertyString ("board" );
148
134
prefs .put ("board" , board );
149
135
prefs .put ("distro_version" , info .getPropertyString ("distro_version" ));
136
+ prefs .put ("port" , "" + info .getPort ());
150
137
}
151
138
152
- prefs .put ("port" , "" + info .getPort ());
153
-
154
139
String label = name + " at " + address ;
155
140
if (board != null ) {
156
141
String boardName = BaseNoGui .getPlatform ().resolveDeviceByBoardID (BaseNoGui .packages , board );
@@ -166,19 +151,21 @@ public void serviceResolved(ServiceEvent serviceEvent) {
166
151
port .setPrefs (prefs );
167
152
port .setLabel (label );
168
153
169
- synchronized (this ) {
154
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
170
155
removeDuplicateBoards (port );
171
- ports .add (port );
156
+ boardPortsDiscoveredWithJmDNS .add (port );
172
157
}
173
158
}
174
159
}
175
160
176
161
private void removeDuplicateBoards (BoardPort newBoard ) {
177
- Iterator <BoardPort > iterator = ports .iterator ();
178
- while (iterator .hasNext ()) {
179
- BoardPort board = iterator .next ();
180
- if (newBoard .getAddress ().equals (board .getAddress ())) {
181
- iterator .remove ();
162
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
163
+ Iterator <BoardPort > iterator = boardPortsDiscoveredWithJmDNS .iterator ();
164
+ while (iterator .hasNext ()) {
165
+ BoardPort board = iterator .next ();
166
+ if (newBoard .getAddress ().equals (board .getAddress ())) {
167
+ iterator .remove ();
168
+ }
182
169
}
183
170
}
184
171
}
0 commit comments