1
1
package processing .app ;
2
2
3
+ import com .jcraft .jsch .*;
3
4
import processing .app .debug .MessageSiphon ;
4
5
5
6
import java .awt .event .ActionEvent ;
6
7
import java .awt .event .ActionListener ;
7
8
import java .io .IOException ;
9
+ import java .io .InputStream ;
8
10
import java .io .OutputStream ;
9
- import java .net .InetSocketAddress ;
10
- import java .net .Socket ;
11
11
import java .util .regex .Matcher ;
12
12
13
+ import static processing .app .I18n ._ ;
14
+
13
15
@ SuppressWarnings ("serial" )
14
16
public class NetworkMonitor extends AbstractMonitor {
15
17
16
18
private final String ipAddress ;
17
19
18
- private Socket socket ;
19
- private MessageSiphon consumer ;
20
+ private MessageSiphon inputConsumer ;
21
+ private Session session ;
22
+ private Channel channel ;
23
+ private MessageSiphon errorConsumer ;
20
24
21
25
public NetworkMonitor (String port , Base base ) {
22
26
super (port );
@@ -28,7 +32,7 @@ public NetworkMonitor(String port, Base base) {
28
32
onSendCommand (new ActionListener () {
29
33
public void actionPerformed (ActionEvent event ) {
30
34
try {
31
- OutputStream out = socket .getOutputStream ();
35
+ OutputStream out = channel .getOutputStream ();
32
36
out .write (textField .getText ().getBytes ());
33
37
out .write ('\n' );
34
38
out .flush ();
@@ -41,24 +45,81 @@ public void actionPerformed(ActionEvent event) {
41
45
}
42
46
43
47
@ Override
44
- public void open () throws IOException {
45
- try {
46
- socket = new Socket ();
47
- socket .connect (new InetSocketAddress (ipAddress , 6571 ), 5000 );
48
- consumer = new MessageSiphon (socket .getInputStream (), this );
49
- return ;
50
- } catch (IOException e ) {
51
- socket = null ;
52
- throw e ;
48
+ public boolean requiresAuthorization () {
49
+ return true ;
50
+ }
51
+
52
+ @ Override
53
+ public String getAuthorizationKey () {
54
+ return "runtime.pwd." + ipAddress ;
55
+ }
56
+
57
+ @ Override
58
+ public void open () throws Exception {
59
+ JSch jSch = new JSch ();
60
+ session = jSch .getSession ("root" , ipAddress , 22 );
61
+ session .setPassword (Preferences .get (getAuthorizationKey ()));
62
+
63
+ session .setUserInfo (new NoInteractionUserInfo ());
64
+ session .connect (30000 );
65
+
66
+ channel = session .openChannel ("exec" );
67
+ ((ChannelExec ) channel ).setCommand ("telnet localhost 6571" );
68
+
69
+ InputStream inputStream = channel .getInputStream ();
70
+ InputStream errStream = ((ChannelExec ) channel ).getErrStream ();
71
+
72
+ channel .connect ();
73
+
74
+ inputConsumer = new MessageSiphon (inputStream , this );
75
+ errorConsumer = new MessageSiphon (errStream , this );
76
+ }
77
+
78
+ @ Override
79
+ public void message (String s ) {
80
+ if (s .contains ("can't connect" )) {
81
+ s = _ ("Unable to connect: is the sketch using the bridge?" );
53
82
}
83
+ super .message (s ); //To change body of overridden methods use File | Settings | File Templates.
54
84
}
55
85
56
86
@ Override
57
- public void close () throws IOException {
58
- if (socket != null ) {
59
- consumer .stop ();
60
- socket . close ();
87
+ public void close () throws Exception {
88
+ if (channel != null ) {
89
+ inputConsumer .stop ();
90
+ channel . disconnect ();
61
91
textArea .setText ("" );
62
92
}
93
+
94
+ if (session != null ) {
95
+ session .disconnect ();
96
+ }
97
+ }
98
+
99
+ public static class NoInteractionUserInfo implements UserInfo {
100
+
101
+ public String getPassword () {
102
+ return null ;
103
+ }
104
+
105
+ public boolean promptYesNo (String str ) {
106
+ return true ;
107
+ }
108
+
109
+ public String getPassphrase () {
110
+ return null ;
111
+ }
112
+
113
+ public boolean promptPassphrase (String message ) {
114
+ return false ;
115
+ }
116
+
117
+ public boolean promptPassword (String message ) {
118
+ return false ;
119
+ }
120
+
121
+ public void showMessage (String message ) {
122
+ }
123
+
63
124
}
64
125
}
0 commit comments