From 9c9c66b02d9dac650303ac1576afefe9cd0002c9 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 1 Jul 2015 22:41:24 +0300 Subject: [PATCH] add monitor connection to the advertised arduino port --- app/src/processing/app/NetworkMonitor.java | 199 ++++++++++++--------- 1 file changed, 118 insertions(+), 81 deletions(-) diff --git a/app/src/processing/app/NetworkMonitor.java b/app/src/processing/app/NetworkMonitor.java index 716c9f0fc..b7682c0a2 100644 --- a/app/src/processing/app/NetworkMonitor.java +++ b/app/src/processing/app/NetworkMonitor.java @@ -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); - - session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey()))); - session.connect(30000); + + if(useSSH){ + this.connectionAttempts = 0; + + 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); @@ -153,16 +184,22 @@ public class NetworkMonitor extends AbstractMonitor implements MessageConsumer { @Override public void close() throws Exception { super.close(); + + if(useSSH){ + if (channel != null) { + inputConsumer.stop(); + channel.disconnect(); + textArea.setText(""); + } - 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(""); + } } } - }