mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-11 05:54:16 +01:00
Implemented NetworkMonitor retry
This commit is contained in:
parent
6293c76bff
commit
861d6f4b06
@ -105,8 +105,9 @@ public abstract class AbstractMonitor extends JFrame implements MessageConsumer
|
|||||||
};
|
};
|
||||||
|
|
||||||
serialRates = new JComboBox();
|
serialRates = new JComboBox();
|
||||||
for (int i = 0; i < serialRateStrings.length; i++)
|
for (String rate : serialRateStrings) {
|
||||||
serialRates.addItem(serialRateStrings[i] + " " + _("baud"));
|
serialRates.addItem(rate + " " + _("baud"));
|
||||||
|
}
|
||||||
|
|
||||||
serialRates.setMaximumSize(serialRates.getMinimumSize());
|
serialRates.setMaximumSize(serialRates.getMinimumSize());
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package processing.app;
|
|||||||
import com.jcraft.jsch.*;
|
import com.jcraft.jsch.*;
|
||||||
import processing.app.debug.MessageSiphon;
|
import processing.app.debug.MessageSiphon;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -15,12 +16,15 @@ import static processing.app.I18n._;
|
|||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class NetworkMonitor extends AbstractMonitor {
|
public class NetworkMonitor extends AbstractMonitor {
|
||||||
|
|
||||||
|
private static final int MAX_CONNECTION_ATTEMPTS = 5;
|
||||||
|
|
||||||
private final String ipAddress;
|
private final String ipAddress;
|
||||||
|
|
||||||
private MessageSiphon inputConsumer;
|
private MessageSiphon inputConsumer;
|
||||||
private Session session;
|
private Session session;
|
||||||
private Channel channel;
|
private Channel channel;
|
||||||
private MessageSiphon errorConsumer;
|
private MessageSiphon errorConsumer;
|
||||||
|
private int connectionAttempts;
|
||||||
|
|
||||||
public NetworkMonitor(String port, Base base) {
|
public NetworkMonitor(String port, Base base) {
|
||||||
super(port);
|
super(port);
|
||||||
@ -56,6 +60,8 @@ public class NetworkMonitor extends AbstractMonitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void open() throws Exception {
|
public void open() throws Exception {
|
||||||
|
this.connectionAttempts = 0;
|
||||||
|
|
||||||
JSch jSch = new JSch();
|
JSch jSch = new JSch();
|
||||||
session = jSch.getSession("root", ipAddress, 22);
|
session = jSch.getSession("root", ipAddress, 22);
|
||||||
session.setPassword(Preferences.get(getAuthorizationKey()));
|
session.setPassword(Preferences.get(getAuthorizationKey()));
|
||||||
@ -63,6 +69,20 @@ public class NetworkMonitor extends AbstractMonitor {
|
|||||||
session.setUserInfo(new NoInteractionUserInfo());
|
session.setUserInfo(new NoInteractionUserInfo());
|
||||||
session.connect(30000);
|
session.connect(30000);
|
||||||
|
|
||||||
|
tryConnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tryConnect() throws JSchException, IOException {
|
||||||
|
connectionAttempts++;
|
||||||
|
|
||||||
|
if (connectionAttempts > 1) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
channel = session.openChannel("exec");
|
channel = session.openChannel("exec");
|
||||||
((ChannelExec) channel).setCommand("telnet localhost 6571");
|
((ChannelExec) channel).setCommand("telnet localhost 6571");
|
||||||
|
|
||||||
@ -73,14 +93,56 @@ public class NetworkMonitor extends AbstractMonitor {
|
|||||||
|
|
||||||
inputConsumer = new MessageSiphon(inputStream, this);
|
inputConsumer = new MessageSiphon(inputStream, this);
|
||||||
errorConsumer = new MessageSiphon(errStream, this);
|
errorConsumer = new MessageSiphon(errStream, this);
|
||||||
|
|
||||||
|
if (connectionAttempts > 1) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
if (channel.isConnected()) {
|
||||||
|
NetworkMonitor.this.message(_("Connected!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void message(String s) {
|
public synchronized void message(String s) {
|
||||||
if (s.contains("can't connect")) {
|
if (s.contains("can't connect")) {
|
||||||
s = _("Unable to connect: is the sketch using the bridge?");
|
while (!channel.isClosed()) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// ignore
|
||||||
}
|
}
|
||||||
super.message(s); //To change body of overridden methods use File | Settings | File Templates.
|
}
|
||||||
|
if (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
|
||||||
|
s = _("Unable to connect: retrying (" + connectionAttempts + ")...") + "\n";
|
||||||
|
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
NetworkMonitor.this.tryConnect();
|
||||||
|
} catch (JSchException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
s = _("Unable to connect: is the sketch using the bridge?") + "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.message(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user