-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathNetworkMonitor.java
169 lines (137 loc) · 4.2 KB
/
NetworkMonitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package processing.app;
import cc.arduino.packages.BoardPort;
import cc.arduino.packages.ssh.NoInteractionUserInfo;
import cc.arduino.packages.ssh.SSHClientSetupChainRing;
import cc.arduino.packages.ssh.SSHConfigFileSetup;
import cc.arduino.packages.ssh.SSHPwdSetup;
import com.jcraft.jsch.*;
import processing.app.debug.MessageConsumer;
import processing.app.debug.MessageSiphon;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static processing.app.I18n.tr;
@SuppressWarnings("serial")
public class NetworkMonitor extends AbstractTextMonitor implements MessageConsumer {
private static final int MAX_CONNECTION_ATTEMPTS = 5;
private MessageSiphon inputConsumer;
private Session session;
private Channel channel;
private int connectionAttempts;
public NetworkMonitor(BoardPort port) {
super(port);
onSendCommand(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
OutputStream out = channel.getOutputStream();
out.write(textField.getText().getBytes());
out.write('\n');
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
textField.setText("");
}
});
}
@Override
public boolean requiresAuthorization() {
return true;
}
@Override
public String getAuthorizationKey() {
return "runtime.pwd." + getBoardPort().getAddress();
}
@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.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setUserInfo(new NoInteractionUserInfo(PreferencesData.get(getAuthorizationKey())));
session.connect(30000);
tryConnect();
}
private void tryConnect() throws JSchException, IOException {
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(tr("connected!") + '\n');
}
}
});
}
}
@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" + tr("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" + tr("Unable to connect: is the sketch using the bridge?");
}
}
super.message(s);
}
@Override
public void close() throws Exception {
super.close();
if (channel != null) {
inputConsumer.stop();
channel.disconnect();
textArea.setText("");
}
if (session != null) {
session.disconnect();
}
}
}