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.ActionListener;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -30,14 +33,24 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
private Session session;
private Channel channel;
private int connectionAttempts;
private Socket socket;
private int remotePort;
private boolean useSSH;
public NetworkMonitor(BoardPort port) {
super(port);
remotePort = Integer.parseInt(port.getPrefs().get("port"));
useSSH = (remotePort == 22 || remotePort == 80);
onSendCommand(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
OutputStream out = channel.getOutputStream();
OutputStream out;
if(useSSH){
out = channel.getOutputStream();
} else {
out = socket.getOutputStream();
}
out.write(textField.getText().getBytes());
out.write('\n');
out.flush();
@ -62,89 +75,107 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
@Override
public void open() throws Exception {
super.open();
this.connectionAttempts = 0;
JSch jSch = new JSch();
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
session = sshClientSetupChain.setup(getBoardPort(), jSch);
if(useSSH){
this.connectionAttempts = 0;
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
session.connect(30000);
JSch jSch = new JSch();
SSHClientSetupChainRing sshClientSetupChain = new SSHConfigFileSetup(new SSHPwdSetup());
session = sshClientSetupChain.setup(getBoardPort(), jSch);
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
session.connect(30000);
}
tryConnect();
}
private void tryConnect() throws JSchException, IOException {
connectionAttempts++;
if(useSSH){
connectionAttempts++;
if (connectionAttempts > 1) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// 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 (connectionAttempts > 1) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// 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');
}
}
});
}
} 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
public synchronized void message(String s) {
if (s.contains("can't connect")) {
while (!channel.isClosed()) {
try {
Thread.sleep(100);
} 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();
}
if(useSSH){
if (s.contains("can't connect")) {
while (!channel.isClosed()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignore
}
});
} else {
s = "\n" + _("Unable to connect: is the sketch using the bridge?");
}
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 {
s = "\n" + _("Unable to connect: is the sketch using the bridge?");
}
}
}
super.message(s);
@ -154,15 +185,21 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
public void close() throws Exception {
super.close();
if (channel != null) {
inputConsumer.stop();
channel.disconnect();
textArea.setText("");
}
if(useSSH){
if (channel != null) {
inputConsumer.stop();
channel.disconnect();
textArea.setText("");
}
if (session != null) {
session.disconnect();
if (session != null) {
session.disconnect();
}
} else {
if (socket != null) {
socket.close();
textArea.setText("");
}
}
}
}