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 Timer reachabilityTimer ;
50
+ private JmmDNS jmdns = null ;
83
51
84
- public List < BoardPort > getBoardPortsDiscoveredWithJmDNS ( ) {
52
+ private void removeDuplicateBoards ( BoardPort newBoard ) {
85
53
synchronized (boardPortsDiscoveredWithJmDNS ) {
86
- return new LinkedList <>(boardPortsDiscoveredWithJmDNS );
54
+ Iterator <BoardPort > iterator = boardPortsDiscoveredWithJmDNS .iterator ();
55
+ while (iterator .hasNext ()) {
56
+ BoardPort board = iterator .next ();
57
+ if (newBoard .getAddress ().equals (board .getAddress ())) {
58
+ iterator .remove ();
59
+ }
60
+ }
87
61
}
88
62
}
89
63
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
64
@ Override
106
65
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
66
}
118
67
119
68
@ Override
@@ -126,11 +75,9 @@ public void serviceRemoved(ServiceEvent serviceEvent) {
126
75
127
76
@ Override
128
77
public void serviceResolved (ServiceEvent serviceEvent ) {
129
- int sleptFor = 0 ;
130
- while (BaseNoGui .packages == null && sleptFor <= MAX_TIME_AWAITING_FOR_PACKAGES ) {
78
+ while (BaseNoGui .packages == null ) {
131
79
try {
132
80
Thread .sleep (1000 );
133
- sleptFor += 1000 ;
134
81
} catch (InterruptedException e ) {
135
82
e .printStackTrace ();
136
83
}
@@ -151,7 +98,7 @@ public void serviceResolved(ServiceEvent serviceEvent) {
151
98
port .getPrefs ().put ("board" , board );
152
99
port .getPrefs ().put ("distro_version" , info .getPropertyString ("distro_version" ));
153
100
port .getPrefs ().put ("port" , "" + info .getPort ());
154
-
101
+
155
102
//Add additional fields to permit generic ota updates
156
103
//and make sure we do not intefere with Arduino boards
157
104
// define "ssh_upload=no" TXT property to use generic uploader
@@ -190,35 +137,56 @@ public void serviceResolved(ServiceEvent serviceEvent) {
190
137
}
191
138
}
192
139
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
- }
202
- }
140
+ public NetworkDiscovery () {
141
+
203
142
}
204
143
205
144
@ Override
206
- public void inetAddressAdded (InetAddress address ) {
207
- if (mappedJmDNSs .containsKey (address )) {
208
- return ;
209
- }
145
+ public void start () {
146
+ jmdns = JmmDNS .Factory .getInstance ();
147
+ jmdns .addServiceListener ("_arduino._tcp.local." , this );
148
+ reachabilityTimer = new Timer ();
149
+ new BoardReachabilityFilter (this ).start (reachabilityTimer );
150
+ }
151
+
152
+ @ Override
153
+ public void stop () {
154
+ jmdns .unregisterAllServices ();
155
+ // we don't close the JmmDNS instance as it's too slow
156
+ /*
210
157
try {
211
- JmDNS jmDNS = JmDNS .create (address );
212
- jmDNS .addServiceListener ("_arduino._tcp.local." , this );
213
- mappedJmDNSs .put (address , jmDNS );
214
- } catch (Exception e ) {
158
+ jmdns.close();
159
+ } catch (IOException e) {
215
160
e.printStackTrace();
216
161
}
162
+ */
163
+ reachabilityTimer .cancel ();
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 () {
168
+ synchronized (reachableBoardPorts ) {
169
+ return new LinkedList <>(reachableBoardPorts );
170
+ }
171
+ }
172
+
173
+ @ Override
174
+ public List <BoardPort > listDiscoveredBoards (boolean complete ) {
175
+ synchronized (reachableBoardPorts ) {
176
+ return new LinkedList <>(reachableBoardPorts );
177
+ }
178
+ }
179
+
180
+ public void setReachableBoardPorts (List <BoardPort > newReachableBoardPorts ) {
181
+ synchronized (reachableBoardPorts ) {
182
+ this .reachableBoardPorts .clear ();
183
+ this .reachableBoardPorts .addAll (newReachableBoardPorts );
184
+ }
185
+ }
186
+
187
+ public List <BoardPort > getBoardPortsDiscoveredWithJmDNS () {
188
+ synchronized (boardPortsDiscoveredWithJmDNS ) {
189
+ return new LinkedList <>(boardPortsDiscoveredWithJmDNS );
190
+ }
223
191
}
224
192
}
0 commit comments