29
29
30
30
package cc .arduino .packages .discoverers ;
31
31
32
- //import cc.arduino.packages.BoardPort;
33
- //import cc.arduino.packages.Discovery;
34
32
import java .io .IOException ;
35
33
import java .net .InetAddress ;
36
34
import java .util .HashSet ;
46
44
import javax .jmdns .ServiceListener ;
47
45
import javax .jmdns .impl .DNSTaskStarter ;
48
46
49
- import cc .arduino .packages .discoverers .network .NetworkChecker ;
50
- import processing .app .zeroconf .jmdns .ArduinoDNSTaskStarter ;
51
-
52
- public class NetworkDiscovery
53
- implements ServiceListener , cc .arduino .packages .discoverers .network .NetworkTopologyListener {
47
+ public class NetworkDiscovery implements ServiceListener {
54
48
55
49
private class bonour {
56
50
public String address ;
@@ -79,35 +73,41 @@ public String getLabel() {
79
73
80
74
}
81
75
82
- private Timer timer ;
83
- private final HashSet <bonour > myComPorts ; // well not really com ports but
84
- // we treat them like com ports
85
- private final Map <InetAddress , JmDNS > mappedJmDNSs ;
76
+ private static Timer timer = new Timer ("Network discovery timer" ); //$NON-NLS-1$ ;
77
+ private static final HashSet <bonour > myComPorts = new HashSet <>(); // well
78
+ // not
79
+ // really
80
+ // com
81
+ // ports
82
+ // but
83
+ // we treat them like com ports
84
+ private final static Map <InetAddress , JmDNS > mappedJmDNSs = new Hashtable <>();
85
+ private static NetworkDiscovery me = null ;
86
86
87
- public NetworkDiscovery () {
87
+ private NetworkDiscovery () {
88
88
DNSTaskStarter .Factory .setClassDelegate (new ArduinoDNSTaskStarter ());
89
- this .myComPorts = new HashSet <>();
90
- this .mappedJmDNSs = new Hashtable <>();
91
89
}
92
90
93
- public String [] getList () {
94
- String [] ret = new String [this . myComPorts .size ()];
91
+ public static String [] getList () {
92
+ String [] ret = new String [myComPorts .size ()];
95
93
int curPort = 0 ;
96
- Iterator <bonour > iterator = this . myComPorts .iterator ();
94
+ Iterator <bonour > iterator = myComPorts .iterator ();
97
95
while (iterator .hasNext ()) {
98
96
bonour board = iterator .next ();
99
97
ret [curPort ++] = board .getLabel ();
100
98
}
101
99
return ret ;
102
100
}
103
101
104
- public void start () {
105
- this .timer = new Timer (this .getClass ().getName () + " timer" ); //$NON-NLS-1$
106
- new NetworkChecker (this , NetworkTopologyDiscovery .Factory .getInstance ()).start (this .timer );
102
+ public static void start () {
103
+ if (me == null ) {
104
+ me = new NetworkDiscovery ();
105
+ }
106
+ new NetworkChecker (NetworkTopologyDiscovery .Factory .getInstance ()).start (timer );
107
107
}
108
108
109
- public void stop () {
110
- this . timer .purge ();
109
+ public static void stop () {
110
+ timer .purge ();
111
111
// we don't close each JmDNS instance as it's too slow
112
112
}
113
113
@@ -163,13 +163,13 @@ public void serviceResolved(ServiceEvent serviceEvent) {
163
163
164
164
synchronized (this ) {
165
165
removeBoardswithSameAdress (newItem );
166
- this . myComPorts .add (newItem );
166
+ myComPorts .add (newItem );
167
167
}
168
168
}
169
169
}
170
170
171
- private void removeBoardswithSameAdress (bonour newBoard ) {
172
- Iterator <bonour > iterator = this . myComPorts .iterator ();
171
+ private static void removeBoardswithSameAdress (bonour newBoard ) {
172
+ Iterator <bonour > iterator = myComPorts .iterator ();
173
173
while (iterator .hasNext ()) {
174
174
bonour board = iterator .next ();
175
175
if (newBoard .address .equals (board .address )) {
@@ -178,8 +178,8 @@ private void removeBoardswithSameAdress(bonour newBoard) {
178
178
}
179
179
}
180
180
181
- private void removeBoardswithSameName (String name ) {
182
- Iterator <bonour > iterator = this . myComPorts .iterator ();
181
+ private static void removeBoardswithSameName (String name ) {
182
+ Iterator <bonour > iterator = myComPorts .iterator ();
183
183
while (iterator .hasNext ()) {
184
184
bonour board = iterator .next ();
185
185
if (name .equals (board .name )) {
@@ -189,24 +189,22 @@ private void removeBoardswithSameName(String name) {
189
189
}
190
190
191
191
@ SuppressWarnings ("resource" )
192
- @ Override
193
- public void inetAddressAdded (InetAddress address ) {
194
- if (this .mappedJmDNSs .containsKey (address )) {
192
+ public static void inetAddressAdded (InetAddress address ) {
193
+ if (mappedJmDNSs .containsKey (address )) {
195
194
return ;
196
195
}
197
196
try {
198
197
JmDNS jmDNS = JmDNS .create (address );
199
- jmDNS .addServiceListener ("_arduino._tcp.local." , this ); //$NON-NLS-1$
200
- this . mappedJmDNSs .put (address , jmDNS );
198
+ jmDNS .addServiceListener ("_arduino._tcp.local." , me ); //$NON-NLS-1$
199
+ mappedJmDNSs .put (address , jmDNS );
201
200
} catch (Exception e ) {
202
201
e .printStackTrace ();
203
202
}
204
203
}
205
204
206
205
@ SuppressWarnings ("resource" )
207
- @ Override
208
- public void inetAddressRemoved (InetAddress address ) {
209
- JmDNS jmDNS = this .mappedJmDNSs .remove (address );
206
+ public static void inetAddressRemoved (InetAddress address ) {
207
+ JmDNS jmDNS = mappedJmDNSs .remove (address );
210
208
if (jmDNS != null ) {
211
209
try {
212
210
jmDNS .close ();
@@ -216,8 +214,8 @@ public void inetAddressRemoved(InetAddress address) {
216
214
}
217
215
}
218
216
219
- private bonour getBoardByName (String name ) {
220
- Iterator <bonour > iterator = this . myComPorts .iterator ();
217
+ private static bonour getBoardByName (String name ) {
218
+ Iterator <bonour > iterator = myComPorts .iterator ();
221
219
while (iterator .hasNext ()) {
222
220
bonour board = iterator .next ();
223
221
if (name .equals (board .name )) {
@@ -227,35 +225,35 @@ private bonour getBoardByName(String name) {
227
225
return null ;
228
226
}
229
227
230
- public String getAddress (String name ) {
228
+ public static String getAddress (String name ) {
231
229
bonour board = getBoardByName (name );
232
230
if (board == null )
233
231
return null ;
234
232
return board .address ;
235
233
}
236
234
237
- public String getPort (String name ) {
235
+ public static String getPort (String name ) {
238
236
bonour board = getBoardByName (name );
239
237
if (board == null )
240
238
return null ;
241
239
return board .port ;
242
240
}
243
241
244
- public boolean hasAuth (String name ) {
242
+ public static boolean hasAuth (String name ) {
245
243
bonour board = getBoardByName (name );
246
244
if (board == null )
247
245
return false ;
248
246
return board .auth_upload ;
249
247
}
250
248
251
- public boolean isSSH (String name ) {
249
+ public static boolean isSSH (String name ) {
252
250
bonour board = getBoardByName (name );
253
251
if (board == null )
254
252
return false ;
255
253
return board .ssh_upload ;
256
254
}
257
255
258
- public boolean needstcpCheck (String name ) {
256
+ public static boolean needstcpCheck (String name ) {
259
257
bonour board = getBoardByName (name );
260
258
if (board == null )
261
259
return false ;
0 commit comments