Source code: com/iborg/robo/client/RoboClientLoginProcessor.java
1 /*
2 * Copyright (C) 2002 iBorg Corporation. All Rights Reserved.
3 * Copyright (C) 2002 Boris Galinsky. All Rights Reserved.
4 *
5 * This file is part of the share system.
6 *
7 * The share system is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * See terms of license at gnu.org.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
20 package com.iborg.robo.client;
21 import java.io.*;
22 import java.net.*;
23 import java.util.zip.*;
24 import java.util.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.awt.image.*;
28
29 import com.iborg.hsocket.*;
30 import com.iborg.robo.*;
31 /**
32 *
33 * @author <a href="mailto:sanych@comcast.net">Boris Galinsky</a>.
34 * @version
35 */
36
37 public class RoboClientLoginProcessor extends Thread {
38 InputStream is;
39 OutputStream os;
40 RoboClient roboClient;
41 int maxPixels = -1;
42 int maxUpdateChunk = -1;
43
44
45 RoboClientLoginProcessor(ISocket s, RoboClient roboClient) {
46 try {
47 os= s.getOutputStream();
48 is = s.getInputStream();
49 } catch (Exception e) {
50 System.err.println(e);
51 }
52 this.roboClient = roboClient;
53 }
54
55 public void run() {
56
57 boolean loop = true;
58 while(loop) {
59 try {
60 int command = is.read();
61 switch(command) {
62 case RoboProtocol.REQUEST_LOGIN:
63 login();
64 break;
65 case RoboProtocol.LOGIN_SUCCESSFUL:
66 loginSuccesful();
67 loop = false;
68 break;
69 case RoboProtocol.LOGIN_FAILED:
70 loginFailed();
71 loop = false;
72 break;
73 default:
74 //System.out.println("unknown command " + command);
75 break;
76 }
77 } catch (Exception e) {
78 System.err.println(e);
79 break;
80 }
81 }
82 }
83
84 public void login() {
85 DataInputStream dis = new DataInputStream(is);
86 long loginMask = 0;
87 try {
88 loginMask = dis.readLong();
89 } catch (Exception e) {
90 }
91 // create dialog
92 final Dialog dialog = new Dialog(new Frame(), "Please enter the secret word", true);
93 TextField textField = new TextField(48);
94 textField.setEchoChar('*');
95 Button okButton = new Button("OK");
96 Button advancedButton = new Button("Advanced");
97
98 ActionListener actionListener = new ActionListener() {
99 public void actionPerformed(ActionEvent e) {
100 dialog.dispose();
101 }
102 };
103
104 ActionListener advancedListener = new ActionListener() {
105 public void actionPerformed(ActionEvent e) {
106 advancedSetupDialog();
107 }
108 };
109
110 okButton.addActionListener(actionListener);
111 textField.addActionListener(actionListener);
112 advancedButton.addActionListener(advancedListener);
113
114 dialog.addWindowListener(new WindowAdapter() {
115 public void windowClosing(WindowEvent e) {
116 dialog.dispose();
117 }
118 });
119
120 Panel panel = new Panel();
121 panel.setLayout(new FlowLayout());
122 panel.add(textField);
123 panel.add(okButton);
124 panel.add(advancedButton);
125 dialog.add(BorderLayout.CENTER, panel);
126 dialog.pack();
127
128 // position dialog
129 centerFrame(dialog);
130
131 dialog.show();
132
133 String secret = textField.getText();
134 try {
135 try {
136 //java.security.MessageDigest messageDigest = java.security.MessageDigest.getInstance("SHA-1");
137
138 Class classMessageDigest = Class.forName("java.security.MessageDigest");
139 Class [] getMethodParams = new Class[1];
140 getMethodParams[0] = String.class;
141 java.lang.reflect.Method method = classMessageDigest.getMethod("getInstance", getMethodParams);
142 Object [] methodParam = new Object[1];
143 methodParam[0] = "SHA-1";
144 Object messageDigest = method.invoke(null, methodParam);
145
146 String msg = secret + loginMask;
147 byte [] buffer = msg.getBytes();
148
149 // messageDigest.update(buffer);
150 getMethodParams[0] = buffer.getClass();
151 method = classMessageDigest.getMethod("update", getMethodParams);
152 methodParam[0] = buffer;
153 method.invoke(messageDigest, methodParam);
154
155 //byte[] digest = messageDigest.digest();
156 getMethodParams = null;
157 method = classMessageDigest.getMethod("digest", getMethodParams);
158 byte [] digest = (byte []) method.invoke(messageDigest, null);
159 sendLogin(RoboProtocol.LOGIN_MESSAGE_DIGEST, digest);
160 return;
161 } catch (Exception e) {
162 System.err.println(e);
163 }
164 sendLogin(RoboProtocol.LOGIN, secret.getBytes());
165 } catch (Exception e) {
166 e.printStackTrace(System.err);
167 sendLogin(RoboProtocol.LOGIN, secret.getBytes());
168 }
169
170 }
171
172 private static void centerFrame(Window w) {
173 Toolkit toolkit = Toolkit.getDefaultToolkit();
174 Dimension screenDimension = toolkit.getScreenSize();
175 w.setLocation((screenDimension.width - w.getSize().width)/2, (screenDimension.height - w.getSize().height)/2);
176 }
177
178 public void advancedSetupDialog() {
179 // create dialog
180 final Dialog dialog = new Dialog(new Frame(), "Advanced communication parameters", true);
181 final TextField maxPixelsField = new TextField(10);
182 final TextField maxUpdateChunkField = new TextField(10);
183 Button okButton = new Button("OK");
184 Button cancelButton = new Button("Cancel");
185
186
187 maxPixelsField.setText(Integer.toString(maxPixels));
188 maxUpdateChunkField.setText(Integer.toString(maxUpdateChunk));
189
190 ActionListener okListener = new ActionListener() {
191 public void actionPerformed(ActionEvent e) {
192 try {
193 maxPixels = Integer.parseInt(maxPixelsField.getText());
194 } catch (Exception e1) {
195 }
196 try {
197 maxUpdateChunk = Integer.parseInt(maxUpdateChunkField.getText());
198 } catch (Exception e1) {
199 }
200 dialog.dispose();
201 }
202 };
203
204 ActionListener cancelListener = new ActionListener() {
205 public void actionPerformed(ActionEvent e) {
206 dialog.dispose();
207 }
208 };
209
210 okButton.addActionListener(okListener);
211 maxPixelsField.addActionListener(okListener);
212 maxUpdateChunkField.addActionListener(okListener);
213 cancelButton.addActionListener(cancelListener);
214
215 dialog.addWindowListener(new WindowAdapter() {
216 public void windowClosing(WindowEvent e) {
217 dialog.dispose();
218 }
219 });
220
221 GridBagLayout gridBag = new GridBagLayout();
222 GridBagConstraints gbc = new GridBagConstraints();
223
224 Panel panel = new Panel();
225 panel.setLayout(gridBag);
226
227 Label label = new Label("Maximum Pixels in a Chunk");
228 gbc.fill = GridBagConstraints.BOTH;
229 gbc.weightx = 1;
230 gridBag.setConstraints(label,gbc);
231 panel.add(label);
232
233 gbc.gridwidth = GridBagConstraints.REMAINDER;
234 gridBag.setConstraints(maxPixelsField,gbc);
235 panel.add(maxPixelsField);
236
237 label = new Label("Maximum Transmission Size");
238 gbc.fill = GridBagConstraints.NONE;
239 gbc.gridwidth = 1;
240 gridBag.setConstraints(label,gbc);
241 panel.add(label);
242
243 gbc.gridwidth = GridBagConstraints.REMAINDER;
244 gridBag.setConstraints(maxUpdateChunkField,gbc);
245 panel.add(maxUpdateChunkField);
246
247 dialog.add(BorderLayout.CENTER, panel);
248
249 panel = new Panel();
250 panel.setLayout(new FlowLayout());
251 panel.add(okButton);
252 panel.add(cancelButton);
253 dialog.add(BorderLayout.SOUTH, panel);
254 dialog.pack();
255
256 centerFrame(dialog);
257 dialog.show();
258
259
260 System.out.println("action " + maxUpdateChunk + " " + maxPixels);
261
262 }
263
264
265
266 private synchronized void sendLogin(int command, byte [] buffer) {
267 try {
268 os.write(command);
269 DataOutputStream dos = new DataOutputStream(os);
270 dos.writeInt(buffer.length);
271 dos.write(buffer);
272 os.flush();
273 } catch (Exception e) {
274 }
275 }
276
277 private void loginSuccesful() {
278 if(maxPixels != -1 || maxUpdateChunk != -1) {
279 try {
280 os.write(RoboProtocol.SCREEN_SET_COMMUNICATION_PARAMETERS);
281 DataOutputStream dos = new DataOutputStream(os);
282 dos.writeInt(maxPixels);
283 dos.writeInt(maxUpdateChunk);
284 os.flush();
285 } catch (Exception e) {
286 }
287 }
288 roboClient.startCapture();
289 }
290
291 private void loginFailed() {
292 // create dialog
293 final Dialog dialog = new Dialog(new Frame(), "Login failed", true);
294 Label explanation = new Label("You failed to login. Application is terminating...");
295 Button button = new Button("OK");
296
297 ActionListener actionListener = new ActionListener() {
298 public void actionPerformed(ActionEvent e) {
299 dialog.dispose();
300 }
301 };
302
303 button.addActionListener(actionListener);
304
305 dialog.addWindowListener(new WindowAdapter() {
306 public void windowClosing(WindowEvent e) {
307 dialog.dispose();
308 }
309 });
310
311 Panel panel = new Panel();
312 panel.setLayout(new FlowLayout());
313 panel.add(explanation);
314 panel.add(button);
315 dialog.add(BorderLayout.CENTER, panel);
316 dialog.pack();
317
318 // position dialog
319 Toolkit toolkit = Toolkit.getDefaultToolkit();
320 Dimension screenDimension = toolkit.getScreenSize();
321 dialog.setLocation((screenDimension.width - dialog.getSize().width)/2, (screenDimension.height - dialog.getSize().height)/2);
322
323 dialog.show();
324
325 roboClient.stop();
326 }
327
328 }
329