31
31
32
32
import cc .arduino .packages .BoardPort ;
33
33
import cc .arduino .packages .Discovery ;
34
- import cc .arduino .packages .discoverers .network .BoardReachabilityFilter ;
35
- import cc .arduino .packages .discoverers .network .NetworkChecker ;
36
- import org .apache .commons .compress .utils .IOUtils ;
37
34
import processing .app .BaseNoGui ;
38
- import processing .app .zeroconf .jmdns .ArduinoDNSTaskStarter ;
39
35
40
36
import javax .jmdns .*;
41
- import javax .jmdns .impl .DNSTaskStarter ;
42
37
import java .io .IOException ;
43
38
import java .net .InetAddress ;
44
39
import java .util .*;
45
40
46
- public class NetworkDiscovery implements Discovery , ServiceListener , cc .arduino .packages .discoverers .network .NetworkTopologyListener {
47
-
48
- private static final int MAX_TIME_AWAITING_FOR_PACKAGES = 5000 ;
49
-
50
- private final List <BoardPort > boardPortsDiscoveredWithJmDNS ;
51
- private final Map <InetAddress , JmDNS > mappedJmDNSs ;
52
- private Timer networkCheckerTimer ;
53
- private Timer boardReachabilityFilterTimer ;
54
- private final List <BoardPort > reachableBoardPorts ;
55
-
56
- public NetworkDiscovery () {
57
- DNSTaskStarter .Factory .setClassDelegate (new ArduinoDNSTaskStarter ());
58
- this .boardPortsDiscoveredWithJmDNS = new LinkedList <>();
59
- this .mappedJmDNSs = new Hashtable <>();
60
- this .reachableBoardPorts = new LinkedList <>();
61
- }
41
+ import cc .arduino .packages .discoverers .network .BoardReachabilityFilter ;
62
42
63
- @ Override
64
- public List <BoardPort > listDiscoveredBoards () {
65
- synchronized (reachableBoardPorts ) {
66
- return new LinkedList <>(reachableBoardPorts );
67
- }
68
- }
43
+ public class NetworkDiscovery implements Discovery , ServiceListener {
69
44
70
- @ Override
71
- public List <BoardPort > listDiscoveredBoards (boolean complete ) {
72
- synchronized (reachableBoardPorts ) {
73
- return new LinkedList <>(reachableBoardPorts );
74
- }
75
- }
45
+ private static final int MAX_TIME_AWAITING_FOR_PACKAGES = 5000 ;
76
46
77
- public void setReachableBoardPorts (List <BoardPort > newReachableBoardPorts ) {
78
- synchronized (reachableBoardPorts ) {
79
- this .reachableBoardPorts .clear ();
80
- this .reachableBoardPorts .addAll (newReachableBoardPorts );
81
- }
82
- }
47
+ private final List <BoardPort > reachableBoardPorts = new LinkedList <>();
48
+ private final List <BoardPort > boardPortsDiscoveredWithJmDNS = new LinkedList <>();
49
+ private final Timer reachabilityTimer = new Timer ();
83
50
84
- public List < BoardPort > getBoardPortsDiscoveredWithJmDNS ( ) {
51
+ private void removeDuplicateBoards ( BoardPort newBoard ) {
85
52
synchronized (boardPortsDiscoveredWithJmDNS ) {
86
- return new LinkedList <>(boardPortsDiscoveredWithJmDNS );
53
+ Iterator <BoardPort > iterator = boardPortsDiscoveredWithJmDNS .iterator ();
54
+ while (iterator .hasNext ()) {
55
+ BoardPort board = iterator .next ();
56
+ if (newBoard .getAddress ().equals (board .getAddress ())) {
57
+ iterator .remove ();
58
+ }
59
+ }
87
60
}
88
61
}
89
62
90
- @ Override
91
- public void start () throws IOException {
92
- this .networkCheckerTimer = new Timer (NetworkChecker .class .getName ());
93
- new NetworkChecker (this , NetworkTopologyDiscovery .Factory .getInstance ()).start (networkCheckerTimer );
94
- this .boardReachabilityFilterTimer = new Timer (BoardReachabilityFilter .class .getName ());
95
- new BoardReachabilityFilter (this ).start (boardReachabilityFilterTimer );
96
- }
97
-
98
- @ Override
99
- public void stop () throws IOException {
100
- this .networkCheckerTimer .purge ();
101
- this .boardReachabilityFilterTimer .purge ();
102
- // we don't close each JmDNS instance as it's too slow
103
- }
104
-
105
63
@ Override
106
64
public void serviceAdded (ServiceEvent serviceEvent ) {
107
- String type = serviceEvent .getType ();
108
- String name = serviceEvent .getName ();
109
-
110
- JmDNS dns = serviceEvent .getDNS ();
111
-
112
- dns .requestServiceInfo (type , name );
113
- ServiceInfo serviceInfo = dns .getServiceInfo (type , name );
114
- if (serviceInfo != null ) {
115
- dns .requestServiceInfo (type , name );
116
- }
117
65
}
118
66
119
67
@ Override
@@ -126,11 +74,9 @@ public void serviceRemoved(ServiceEvent serviceEvent) {
126
74
127
75
@ Override
128
76
public void serviceResolved (ServiceEvent serviceEvent ) {
129
- int sleptFor = 0 ;
130
- while (BaseNoGui .packages == null && sleptFor <= MAX_TIME_AWAITING_FOR_PACKAGES ) {
77
+ while (BaseNoGui .packages == null ) {
131
78
try {
132
79
Thread .sleep (1000 );
133
- sleptFor += 1000 ;
134
80
} catch (InterruptedException e ) {
135
81
e .printStackTrace ();
136
82
}
@@ -151,7 +97,7 @@ public void serviceResolved(ServiceEvent serviceEvent) {
151
97
port .getPrefs ().put ("board" , board );
152
98
port .getPrefs ().put ("distro_version" , info .getPropertyString ("distro_version" ));
153
99
port .getPrefs ().put ("port" , "" + info .getPort ());
154
-
100
+
155
101
//Add additional fields to permit generic ota updates
156
102
//and make sure we do not intefere with Arduino boards
157
103
// define "ssh_upload=no" TXT property to use generic uploader
@@ -190,35 +136,50 @@ public void serviceResolved(ServiceEvent serviceEvent) {
190
136
}
191
137
}
192
138
193
- private void removeDuplicateBoards (BoardPort newBoard ) {
194
- synchronized (boardPortsDiscoveredWithJmDNS ) {
195
- Iterator <BoardPort > iterator = boardPortsDiscoveredWithJmDNS .iterator ();
196
- while (iterator .hasNext ()) {
197
- BoardPort board = iterator .next ();
198
- if (newBoard .getAddress ().equals (board .getAddress ())) {
199
- iterator .remove ();
200
- }
201
- }
139
+ public NetworkDiscovery () {
140
+ final JmDNS jmdns ;
141
+ try {
142
+ jmdns = JmDNS .create ();
143
+ jmdns .addServiceListener ("_arduino._tcp.local." , this );
144
+ } catch (IOException e ) {
145
+ e .printStackTrace ();
202
146
}
203
147
}
204
148
205
149
@ Override
206
- public void inetAddressAdded (InetAddress address ) {
207
- if (mappedJmDNSs .containsKey (address )) {
208
- return ;
209
- }
210
- try {
211
- JmDNS jmDNS = JmDNS .create (address );
212
- jmDNS .addServiceListener ("_arduino._tcp.local." , this );
213
- mappedJmDNSs .put (address , jmDNS );
214
- } catch (Exception e ) {
215
- e .printStackTrace ();
150
+ public void start () {
151
+ new BoardReachabilityFilter (this ).start (reachabilityTimer );
152
+ }
153
+
154
+ @ Override
155
+ public void stop () {
156
+ reachabilityTimer .cancel ();
157
+ }
158
+
159
+ @ Override
160
+ public List <BoardPort > listDiscoveredBoards () {
161
+ synchronized (reachableBoardPorts ) {
162
+ return new LinkedList <>(reachableBoardPorts );
216
163
}
217
164
}
218
165
219
166
@ Override
220
- public void inetAddressRemoved (InetAddress address ) {
221
- JmDNS jmDNS = mappedJmDNSs .remove (address );
222
- IOUtils .closeQuietly (jmDNS );
167
+ public List <BoardPort > listDiscoveredBoards (boolean complete ) {
168
+ synchronized (reachableBoardPorts ) {
169
+ return new LinkedList <>(reachableBoardPorts );
170
+ }
171
+ }
172
+
173
+ public void setReachableBoardPorts (List <BoardPort > newReachableBoardPorts ) {
174
+ synchronized (reachableBoardPorts ) {
175
+ this .reachableBoardPorts .clear ();
176
+ this .reachableBoardPorts .addAll (newReachableBoardPorts );
177
+ }
178
+ }
179
+
180
+ public List <BoardPort > getBoardPortsDiscoveredWithJmDNS () {
181
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
182
+ return new LinkedList <>(boardPortsDiscoveredWithJmDNS );
183
+ }
223
184
}
224
185
}
0 commit comments