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