65
|
1 package com.glavsoft.viewer;
|
|
2
|
|
3 import com.glavsoft.rfb.protocol.ProtocolSettings;
|
|
4 import com.glavsoft.viewer.cli.Parser;
|
|
5 import com.glavsoft.viewer.swing.ConnectionParams;
|
|
6 import com.glavsoft.viewer.swing.ParametersHandler;
|
|
7 import com.glavsoft.viewer.swing.SwingConnectionWorkerFactory;
|
130
|
8
|
65
|
9 import javax.swing.*;
|
130
|
10
|
65
|
11 import java.awt.*;
|
|
12 import java.awt.event.WindowEvent;
|
|
13 import java.awt.event.WindowListener;
|
|
14 import java.io.IOException;
|
|
15 import java.io.InputStream;
|
|
16 import java.net.Socket;
|
|
17 import java.util.jar.Attributes;
|
|
18 import java.util.jar.Manifest;
|
|
19 import java.util.logging.*;
|
|
20
|
|
21 import jp.ac.u_ryukyu.treevnc.CreateConnectionParam;
|
|
22 import jp.ac.u_ryukyu.treevnc.MyRfbProto;
|
|
23 import jp.ac.u_ryukyu.treevnc.client.MyRfbProtoClient;
|
174
|
24 import jp.ac.u_ryukyu.treevnc.server.TreeManagement;
|
65
|
25
|
153
|
26 public class CuiViewer implements Runnable, WindowListener, ViewerInterface {
|
65
|
27
|
|
28 private Logger logger;
|
|
29 private int paramsMask;
|
|
30 private boolean allowAppletInteractiveConnections;
|
|
31
|
|
32 public final ConnectionParams connectionParams;
|
|
33 protected String passwordFromParams;
|
|
34 boolean isSeparateFrame = true;
|
|
35 protected boolean isApplet = true;
|
|
36 private final ProtocolSettings settings;
|
|
37 protected UiSettings uiSettings;
|
|
38 private ConnectionPresenter connectionPresenter;
|
|
39 protected MyRfbProto myRfb;
|
|
40 private boolean cuiVersion;
|
|
41
|
|
42 public static void main(String[] args) {
|
|
43 Parser parser = new Parser();
|
|
44 ParametersHandler.completeParserOptions(parser);
|
|
45
|
|
46 parser.parse(args);
|
|
47 if (parser.isSet(ParametersHandler.ARG_HELP)) {
|
|
48 printUsage(parser.optionsUsage());
|
|
49 System.exit(0);
|
|
50 }
|
|
51 CuiViewer viewer = new CuiViewer(parser);
|
|
52 SwingUtilities.invokeLater(viewer);
|
|
53 }
|
|
54
|
|
55 public static void printUsage(String additional) {
|
|
56 System.out
|
|
57 .println("Usage: java -jar (progfilename) [hostname [port_number]] [Options]?n"
|
|
58 + " or?n"
|
|
59 + " java -jar (progfilename) [Options]?n"
|
|
60 + " or?n java -jar (progfilename) -help?n to view this help?n?n"
|
|
61 + "Where Options are:?n"
|
|
62 + additional
|
|
63 + "?nOptions format: -optionName=optionValue. Ex. -host=localhost -port=5900 -viewonly=yes?n"
|
|
64 + "Both option name and option value are case insensitive.");
|
|
65 }
|
|
66
|
|
67 public CuiViewer() {
|
|
68 logger = Logger.getLogger(getClass().getName());
|
|
69 connectionParams = new ConnectionParams();
|
|
70 settings = ProtocolSettings.getDefaultSettings();
|
|
71 uiSettings = new UiSettings();
|
|
72 }
|
|
73
|
|
74 private CuiViewer(Parser parser) {
|
|
75 this();
|
|
76 setLoggingLevel(parser.isSet(ParametersHandler.ARG_VERBOSE) ? Level.FINE
|
|
77 : parser.isSet(ParametersHandler.ARG_VERBOSE_MORE) ? Level.FINER
|
|
78 : Level.INFO);
|
|
79
|
|
80 paramsMask = ParametersHandler.completeSettingsFromCLI(parser,
|
|
81 connectionParams, settings, uiSettings);
|
|
82 passwordFromParams = parser.getValueFor(ParametersHandler.ARG_PASSWORD);
|
|
83 logger.info("TightVNC Viewer version " + ver());
|
|
84 isApplet = false;
|
|
85 }
|
|
86
|
|
87 private void setLoggingLevel(Level levelToSet) {
|
|
88 final Logger appLogger = Logger.getLogger("com.glavsoft");
|
|
89 appLogger.setLevel(levelToSet);
|
|
90 ConsoleHandler ch = null;
|
|
91 for (Handler h : appLogger.getHandlers()) {
|
|
92 if (h instanceof ConsoleHandler) {
|
|
93 ch = (ConsoleHandler) h;
|
|
94 break;
|
|
95 }
|
|
96 }
|
|
97 if (null == ch) {
|
|
98 ch = new ConsoleHandler();
|
|
99 appLogger.addHandler(ch);
|
|
100 }
|
|
101 // ch.setFormatter(new SimpleFormatter());
|
|
102 ch.setLevel(levelToSet);
|
|
103 }
|
|
104
|
|
105 @Override
|
|
106 public void windowClosing(WindowEvent e) {
|
|
107 if (e != null && e.getComponent() != null) {
|
|
108 final Window w = e.getWindow();
|
|
109 if (w != null) {
|
|
110 w.setVisible(false);
|
|
111 w.dispose();
|
|
112 }
|
|
113 }
|
|
114 closeApp();
|
|
115 }
|
|
116
|
|
117 /**
|
|
118 * Closes App(lication) or stops App(let).
|
|
119 */
|
|
120 public void closeApp() {
|
|
121 /* nop */
|
|
122 }
|
|
123
|
|
124 private boolean checkJsch() {
|
|
125 try {
|
|
126 Class.forName("com.jcraft.jsch.JSch");
|
|
127 return true;
|
|
128 } catch (ClassNotFoundException e) {
|
|
129 return false;
|
|
130 }
|
|
131 }
|
|
132
|
|
133 @Override
|
|
134 public void run() {
|
|
135 final boolean hasJsch = checkJsch();
|
|
136 final boolean allowInteractive = allowAppletInteractiveConnections
|
|
137 || !isApplet;
|
|
138 connectionPresenter = new ConnectionPresenter(hasJsch, allowInteractive);
|
|
139 connectionPresenter.addModel("ConnectionParamsModel", connectionParams);
|
|
140
|
|
141
|
|
142 /*
|
|
143 * SwingViewerWindowFactory viewerWindowFactory = new
|
|
144 * SwingViewerWindowFactory( isSeparateFrame, isApplet, this);
|
|
145 *
|
|
146 * connectionPresenter.setConnectionWorkerFactory(new
|
|
147 * SwingConnectionWorkerFactory( connectionView.getFrame(),
|
|
148 * passwordFromParams, connectionPresenter, viewerWindowFactory,
|
|
149 * myRfb));
|
|
150 */
|
|
151
|
|
152
|
|
153 connectionPresenter.setConnectionWorkerFactory(
|
|
154 new SwingConnectionWorkerFactory(null, passwordFromParams, connectionPresenter, null, myRfb));
|
|
155 connectionPresenter.setCuiVersion(true);
|
|
156 connectionPresenter.startConnection(settings, uiSettings, paramsMask);
|
|
157 }
|
|
158
|
|
159 @Override
|
|
160 public void windowOpened(WindowEvent e) { /* nop */
|
|
161 }
|
|
162
|
|
163 @Override
|
|
164 public void windowClosed(WindowEvent e) { /* nop */
|
|
165 }
|
|
166
|
|
167 @Override
|
|
168 public void windowIconified(WindowEvent e) { /* nop */
|
|
169 }
|
|
170
|
|
171 @Override
|
|
172 public void windowDeiconified(WindowEvent e) { /* nop */
|
|
173 }
|
|
174
|
|
175 @Override
|
|
176 public void windowActivated(WindowEvent e) { /* nop */
|
|
177 }
|
|
178
|
|
179 @Override
|
|
180 public void windowDeactivated(WindowEvent e) { /* nop */
|
|
181 }
|
|
182
|
|
183 public static String ver() {
|
|
184 final InputStream mfStream = Viewer.class.getClassLoader()
|
|
185 .getResourceAsStream("META-INF/MANIFEST.MF");
|
|
186 if (null == mfStream) {
|
|
187 System.out.println("No Manifest file found.");
|
|
188 return "-1";
|
|
189 }
|
|
190 try {
|
|
191 Manifest mf = new Manifest();
|
|
192 mf.read(mfStream);
|
|
193 Attributes atts = mf.getMainAttributes();
|
|
194 return atts.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
|
|
195 } catch (IOException e) {
|
|
196 return "-2";
|
|
197 }
|
|
198 }
|
|
199
|
|
200 public void setSocket(Socket soc) {
|
|
201 setConnectionParam(soc.getInetAddress().getHostAddress(), soc.getPort());
|
|
202 // Thread accThread = new Thread(new AcceptThread(myRfb,
|
|
203 // soc.getPort()));
|
|
204 // accThread.start();
|
|
205 }
|
|
206
|
|
207 public void setOpenPort(int parseInt) {
|
|
208 }
|
|
209
|
|
210 public void setTeminationType(boolean b) {
|
|
211 myRfb.setTerminationType(b);
|
|
212 }
|
|
213
|
148
|
214 public void startTreeViewer() {
|
65
|
215 CuiViewer viewer = new CuiViewer();
|
|
216 MyRfbProtoClient rfb = new MyRfbProtoClient();
|
|
217 CreateConnectionParam cp = new CreateConnectionParam(rfb);
|
153
|
218 cp.runTreeVncCommandListener();
|
130
|
219 try {
|
|
220 cp.findTreeVncRoot();
|
|
221 } catch (InterruptedException e) {
|
|
222 }
|
65
|
223 cp.createConnectionParam(viewer);
|
|
224 rfb.setViewer(viewer);
|
|
225 viewer.myRfb = rfb;
|
|
226 SwingUtilities.invokeLater(viewer);
|
|
227 }
|
|
228
|
148
|
229 public void startTreeViewer(String hostName, boolean cui) {
|
65
|
230 CuiViewer viewer = new CuiViewer();
|
|
231 viewer.cuiVersion = cui;
|
|
232 MyRfbProtoClient rfb = new MyRfbProtoClient();
|
|
233 CreateConnectionParam cp = new CreateConnectionParam(rfb);
|
|
234 cp.setHostName(hostName);
|
153
|
235 cp.runTreeVncCommandListener();
|
65
|
236 cp.createConnectionParam(viewer);
|
|
237 rfb.setViewer(viewer);
|
|
238 rfb.setCuiVersion(true);
|
|
239 viewer.myRfb = rfb;
|
|
240 SwingUtilities.invokeLater(viewer);
|
|
241 }
|
|
242
|
|
243 public void setConnectionParam(String hostName, int port) {
|
|
244 connectionParams.setHostName(hostName);
|
|
245 connectionParams.setPortNumber(port);
|
|
246 }
|
|
247
|
|
248 public void setIsTreeVNC(boolean flag) {
|
|
249 }
|
|
250
|
|
251 public MyRfbProto getRfb() {
|
|
252 return myRfb;
|
|
253 }
|
|
254
|
|
255 public boolean getCuiVersion() {
|
|
256 return cuiVersion;
|
|
257 }
|
68
|
258
|
|
259 public void setCuiVersion(boolean flag) {
|
|
260 // nop
|
|
261 }
|
130
|
262
|
|
263 @Override
|
|
264 public void createRootSelectionPanel() {
|
|
265 }
|
174
|
266
|
|
267 @Override
|
|
268 public void connectToParenet(int port, String hostname) throws IOException {
|
|
269 setTeminationType(false);
|
|
270 closeApp();
|
|
271 setConnectionParam(hostname, port);
|
|
272 run();
|
|
273 }
|
|
274
|
|
275 @Override
|
|
276 public void changeVNCServer(String newHostName, int i, int j, short id) {
|
|
277
|
|
278 }
|
|
279
|
|
280 @Override
|
|
281 public void initRoot(String hostName, MyRfbProto myRfb) {
|
|
282
|
|
283 }
|
|
284
|
|
285 @Override
|
|
286 public void inhelitClients(ViewerInterface vncProxyService, String hostName) {
|
|
287
|
|
288 }
|
|
289
|
|
290 @Override
|
|
291 public void setTreeManager(TreeManagement treeManager) {
|
|
292
|
|
293 }
|
|
294
|
|
295 @Override
|
|
296 public void initRootViewer(String hostName) {
|
|
297
|
|
298 }
|
|
299
|
65
|
300 }
|