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,6 +75,8 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
@Override
public void open() throws Exception {
super.open();
if(useSSH){
this.connectionAttempts = 0;
JSch jSch = new JSch();
@ -70,11 +85,13 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
session.connect(30000);
}
tryConnect();
}
private void tryConnect() throws JSchException, IOException {
if(useSSH){
connectionAttempts++;
if (connectionAttempts > 1) {
@ -98,7 +115,6 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
inputConsumer = new MessageSiphon(inputStream, this);
new MessageSiphon(errStream, this);
if (connectionAttempts > 1) {
SwingUtilities.invokeLater(new Runnable() {
@ -116,10 +132,24 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
});
}
} 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(useSSH){
if (s.contains("can't connect")) {
while (!channel.isClosed()) {
try {
@ -147,6 +177,7 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
s = "\n" + _("Unable to connect: is the sketch using the bridge?");
}
}
}
super.message(s);
}
@ -154,6 +185,7 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
public void close() throws Exception {
super.close();
if(useSSH){
if (channel != null) {
inputConsumer.stop();
channel.disconnect();
@ -163,6 +195,11 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer {
if (session != null) {
session.disconnect();
}
} else {
if (socket != null) {
socket.close();
textArea.setText("");
}
}
}
}