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