1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

add monitor connection to the advertised arduino port

This commit is contained in:
John Doe
2015-07-01 22:41:24 +03:00
parent 99f0ea182e
commit 9c9c66b02d

View File

@ -15,6 +15,9 @@ 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.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -30,14 +33,24 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
private Session session; private Session session;
private Channel channel; private Channel channel;
private int connectionAttempts; private int connectionAttempts;
private Socket socket;
private int remotePort;
private boolean useSSH;
public NetworkMonitor(BoardPort port) { public NetworkMonitor(BoardPort port) {
super(port); super(port);
remotePort = Integer.parseInt(port.getPrefs().get("port"));
useSSH = (remotePort == 22 || remotePort == 80);
onSendCommand(new ActionListener() { onSendCommand(new ActionListener() {
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
try { try {
OutputStream out = channel.getOutputStream(); OutputStream out;
if(useSSH){
out = channel.getOutputStream();
} else {
out = socket.getOutputStream();
}
out.write(textField.getText().getBytes()); out.write(textField.getText().getBytes());
out.write('\n'); out.write('\n');
out.flush(); out.flush();
@ -62,89 +75,107 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
@Override @Override
public void open() throws Exception { public void open() throws Exception {
super.open(); super.open();
this.connectionAttempts = 0;
if(useSSH){
JSch jSch = new JSch(); this.connectionAttempts = 0;
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
session = sshClientSetupChain.setup(getBoardPort(), jSch); JSch jSch = new JSch();
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey()))); session = sshClientSetupChain.setup(getBoardPort(), jSch);
session.connect(30000);
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
session.connect(30000);
}
tryConnect(); tryConnect();
} }
private void tryConnect() throws JSchException, IOException { private void tryConnect() throws JSchException, IOException {
connectionAttempts++; if(useSSH){
connectionAttempts++;
if (connectionAttempts > 1) { if (connectionAttempts > 1) {
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// ignored // ignored
}
}
if (!session.isConnected()) {
return;
}
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("telnet localhost 6571");
InputStream inputStream = channel.getInputStream();
InputStream errStream = ((ChannelExec) channel).getErrStream();
channel.connect();
inputConsumer = new MessageSiphon(inputStream, this);
new MessageSiphon(errStream, this);
if (connectionAttempts > 1) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
if (channel.isConnected()) {
NetworkMonitor.this.message(_("connected!") + '\n');
}
} }
}
if (!session.isConnected()) {
return;
}
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("telnet localhost 6571");
}); InputStream inputStream = channel.getInputStream();
InputStream errStream = ((ChannelExec) channel).getErrStream();
channel.connect();
inputConsumer = new MessageSiphon(inputStream, this);
new MessageSiphon(errStream, this);
if (connectionAttempts > 1) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
if (channel.isConnected()) {
NetworkMonitor.this.message(_("connected!") + '\n');
}
}
});
}
} else {
socket = new Socket();
socket.connect(new InetSocketAddress(getBoardPort().getAddress(), remotePort), 1000);
socket.setTcpNoDelay(true);
inputConsumer = new MessageSiphon(socket.getInputStream(), this);
String password = PreferencesData.get(getAuthorizationKey());
if(password.length() > 0){
OutputStream out = socket.getOutputStream();
out.write(password.getBytes());
out.write('\n');
out.flush();
}
} }
} }
@Override @Override
public synchronized void message(String s) { public synchronized void message(String s) {
if (s.contains("can't connect")) { if(useSSH){
while (!channel.isClosed()) { if (s.contains("can't connect")) {
try { while (!channel.isClosed()) {
Thread.sleep(100); try {
} catch (InterruptedException e) { Thread.sleep(100);
// ignore } catch (InterruptedException e) {
} // ignore
}
if (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
s = "\n" + _("Unable to connect: retrying") + " (" + connectionAttempts + ")... ";
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
NetworkMonitor.this.tryConnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
}); }
} else { if (connectionAttempts < MAX_CONNECTION_ATTEMPTS) {
s = "\n" + _("Unable to connect: is the sketch using the bridge?"); s = "\n" + _("Unable to connect: retrying") + " (" + connectionAttempts + ")... ";
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
NetworkMonitor.this.tryConnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} else {
s = "\n" + _("Unable to connect: is the sketch using the bridge?");
}
} }
} }
super.message(s); super.message(s);
@ -153,16 +184,22 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
@Override @Override
public void close() throws Exception { public void close() throws Exception {
super.close(); super.close();
if(useSSH){
if (channel != null) {
inputConsumer.stop();
channel.disconnect();
textArea.setText("");
}
if (channel != null) { if (session != null) {
inputConsumer.stop(); session.disconnect();
channel.disconnect(); }
textArea.setText(""); } else {
} if (socket != null) {
socket.close();
if (session != null) { textArea.setText("");
session.disconnect(); }
} }
} }
} }