3131
3232import cc .arduino .packages .BoardPort ;
3333import 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 ;
3734import processing .app .BaseNoGui ;
38- import processing .app .zeroconf .jmdns .ArduinoDNSTaskStarter ;
3935
4036import javax .jmdns .*;
41- import javax .jmdns .impl .DNSTaskStarter ;
4237import java .io .IOException ;
4338import java .net .InetAddress ;
4439import java .util .*;
4540
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 ;
6242
63- @ Override
64- public List <BoardPort > listDiscoveredBoards () {
65- synchronized (reachableBoardPorts ) {
66- return new LinkedList <>(reachableBoardPorts );
67- }
68- }
43+ public class NetworkDiscovery implements Discovery , ServiceListener {
6944
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 ;
7646
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 ;
8351
84- public List < BoardPort > getBoardPortsDiscoveredWithJmDNS ( ) {
52+ private void removeDuplicateBoards ( BoardPort newBoard ) {
8553 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+ }
8761 }
8862 }
8963
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-
10564 @ Override
10665 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- }
11766 }
11867
11968 @ Override
@@ -126,11 +75,9 @@ public void serviceRemoved(ServiceEvent serviceEvent) {
12675
12776 @ Override
12877 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 ) {
13179 try {
13280 Thread .sleep (1000 );
133- sleptFor += 1000 ;
13481 } catch (InterruptedException e ) {
13582 e .printStackTrace ();
13683 }
@@ -151,7 +98,7 @@ public void serviceResolved(ServiceEvent serviceEvent) {
15198 port .getPrefs ().put ("board" , board );
15299 port .getPrefs ().put ("distro_version" , info .getPropertyString ("distro_version" ));
153100 port .getPrefs ().put ("port" , "" + info .getPort ());
154-
101+
155102 //Add additional fields to permit generic ota updates
156103 //and make sure we do not intefere with Arduino boards
157104 // define "ssh_upload=no" TXT property to use generic uploader
@@ -190,35 +137,56 @@ public void serviceResolved(ServiceEvent serviceEvent) {
190137 }
191138 }
192139
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+
203142 }
204143
205144 @ 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+ /*
210157 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) {
215160 e.printStackTrace();
216161 }
162+ */
163+ reachabilityTimer .cancel ();
217164 }
218165
219166 @ 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+ }
223191 }
224192}
0 commit comments