Skip to content

An attempt to improve Yun's discovery #2658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,32 @@ public NetworkDiscovery() {

@Override
public List<BoardPort> discovery() {
List<BoardPort> ports = clonePortsList();
Iterator<BoardPort> iterator = ports.iterator();
while (iterator.hasNext()) {
List<BoardPort> boardPorts = clonePortsList();
Iterator<BoardPort> boardPortIterator = boardPorts.iterator();
while (boardPortIterator.hasNext()) {
try {
BoardPort board = iterator.next();
if (!NetUtils.isReachable(InetAddress.getByName(board.getAddress()), Integer.parseInt(board.getPrefs().get("port")))) {
iterator.remove();
BoardPort board = boardPortIterator.next();

InetAddress inetAddress = InetAddress.getByName(board.getAddress());
int broadcastedPort = Integer.valueOf(board.getPrefs().get("port"));

List<Integer> ports = new LinkedList<Integer>();
ports.add(broadcastedPort);

//dirty code: allows non up to date yuns to be discovered. Newer yuns will broadcast port 22
if (broadcastedPort == 80) {
ports.add(0, 22);
}

boolean reachable = NetUtils.isReachable(inetAddress, ports);
if (!reachable) {
boardPortIterator.remove();
}
} catch (UnknownHostException e) {
iterator.remove();
boardPortIterator.remove();
}
}
return ports;
return boardPorts;
}

private List<BoardPort> clonePortsList() {
Expand Down
29 changes: 28 additions & 1 deletion arduino-core/src/processing/app/helpers/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,41 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;

public abstract class NetUtils {

private static boolean isReachableByEcho(InetAddress address) {
try {
return address.isReachable(100);
} catch (IOException e) {
return false;
}
}

public static boolean isReachable(InetAddress address, int port) {
return isReachable(address, Arrays.asList(port));
}

public static boolean isReachable(InetAddress address, List<Integer> ports) {
if (isReachableByEcho(address)) {
return true;
}

boolean reachable = false;
for (Integer port : ports) {
reachable = reachable || isPortOpen(address, port);
}

return reachable;
}

private static boolean isPortOpen(InetAddress address, int port) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(address, port), 100);
socket.connect(new InetSocketAddress(address, port), 300);
return true;
} catch (IOException e) {
return false;
Expand Down