File tree 2 files changed +49
-9
lines changed
cc/arduino/packages/discoverers 2 files changed +49
-9
lines changed Original file line number Diff line number Diff line change @@ -58,19 +58,32 @@ public NetworkDiscovery() {
58
58
59
59
@ Override
60
60
public List <BoardPort > discovery () {
61
- List <BoardPort > ports = clonePortsList ();
62
- Iterator <BoardPort > iterator = ports .iterator ();
63
- while (iterator .hasNext ()) {
61
+ List <BoardPort > boardPorts = clonePortsList ();
62
+ Iterator <BoardPort > boardPortIterator = boardPorts .iterator ();
63
+ while (boardPortIterator .hasNext ()) {
64
64
try {
65
- BoardPort board = iterator .next ();
66
- if (!NetUtils .isReachable (InetAddress .getByName (board .getAddress ()), Integer .parseInt (board .getPrefs ().get ("port" )))) {
67
- iterator .remove ();
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 ();
68
81
}
69
82
} catch (UnknownHostException e ) {
70
- iterator .remove ();
83
+ boardPortIterator .remove ();
71
84
}
72
85
}
73
- return ports ;
86
+ return boardPorts ;
74
87
}
75
88
76
89
private List <BoardPort > clonePortsList () {
Original file line number Diff line number Diff line change 4
4
import java .net .InetAddress ;
5
5
import java .net .InetSocketAddress ;
6
6
import java .net .Socket ;
7
+ import java .util .Arrays ;
8
+ import java .util .List ;
7
9
8
10
public abstract class NetUtils {
9
11
12
+ private static boolean isReachableByEcho (InetAddress address ) {
13
+ try {
14
+ return address .isReachable (100 );
15
+ } catch (IOException e ) {
16
+ return false ;
17
+ }
18
+ }
19
+
10
20
public static boolean isReachable (InetAddress address , int port ) {
21
+ return isReachable (address , Arrays .asList (port ));
22
+ }
23
+
24
+ public static boolean isReachable (InetAddress address , List <Integer > ports ) {
25
+ if (isReachableByEcho (address )) {
26
+ return true ;
27
+ }
28
+
29
+ boolean reachable = false ;
30
+ for (Integer port : ports ) {
31
+ reachable = reachable || isPortOpen (address , port );
32
+ }
33
+
34
+ return reachable ;
35
+ }
36
+
37
+ private static boolean isPortOpen (InetAddress address , int port ) {
11
38
Socket socket = null ;
12
39
try {
13
40
socket = new Socket ();
14
- socket .connect (new InetSocketAddress (address , port ), 100 );
41
+ socket .connect (new InetSocketAddress (address , port ), 300 );
15
42
return true ;
16
43
} catch (IOException e ) {
17
44
return false ;
You can’t perform that action at this time.
0 commit comments