}
}
+ public void shutdown() throws IOException {
+ try {
+ channel.socket().shutdownInput();
+ channel.socket().shutdownOutput();
+ } catch(IOException e) {
+ throw new IOException(e.toString());
+ }
+ }
+
public void close() throws IOException {
try {
channel.close();
addr = InetAddress.getByName("0.0.0.0");
}
} catch (UnknownHostException e) {
- System.out.println(e.toString());
- System.exit(-1);
+ throw new Exception(e.toString());
}
try {
try {
fd = new SocketDescriptor();
} catch (java.lang.Exception e) {
- System.out.println(e.toString());
- System.exit(-1);
+ throw new SocketException(e.toString());
}
fd.setChannel(new_sock);
TcpSocket s = new TcpSocket(fd);
}
public void shutdown() throws Exception {
+ super.shutdown();
try {
- close();
- super.shutdown();
+ ((SocketDescriptor)getFd()).shutdown();
} catch (IOException e) {
throw new Exception(e.toString());
}
public class Exception extends RuntimeException {
public Exception(String s) {
super(s);
- System.out.println(s);
}
}
String name = sock.getPeerEndpoint();
vlog.info("Accepted connection from "+name);
} else {
- if (vncServerName != null) {
+ if (vncServerName != null &&
+ !viewer.alwaysShowServerDialog.getValue()) {
serverHost = Hostname.getHost(vncServerName);
serverPort = Hostname.getPort(vncServerName);
} else {
ServerDialog dlg = new ServerDialog(options, vncServerName, this);
if (!dlg.showDialog() || dlg.server.getSelectedItem().equals("")) {
- if (viewer.firstApplet) {
- System.exit(1);
- } else {
- viewer.stop();
- return;
- }
+ vlog.info("No server name specified!");
+ close();
+ return;
}
vncServerName = (String)dlg.server.getSelectedItem();
serverHost = Hostname.getHost(vncServerName);
if (viewport != null)
viewport.dispose();
viewport = null;
- if (viewer.firstApplet) {
- System.exit(1);
- } else {
- close();
- viewer.stop();
- }
}
// blockCallback() is called when reading from the socket would block.
////////////////////////////////////////////////////////////////////
// The following methods are all called from the GUI thread
- // close() closes the socket, thus waking up the RFB thread.
+ // close() shuts down the socket, thus waking up the RFB thread.
public void close() {
+ deleteWindow();
shuttingDown = true;
try {
- sock.shutdown();
+ if (sock != null)
+ sock.shutdown();
} catch (java.lang.Exception e) {
throw new Exception(e.toString());
}
// shuttingDown is set by the GUI thread and only ever tested by the RFB
// thread after the window has been destroyed.
- boolean shuttingDown;
+ boolean shuttingDown = false;
// reading and writing int and boolean is atomic in java, so no
// synchronization of the following flags is needed:
public void actionPerformed(ActionEvent ev) {
if (actionMatch(ev, exit)) {
- cc.deleteWindow();
+ cc.close();
} else if (actionMatch(ev, fullScreen)) {
cc.toggleFullScreen();
} else if (actionMatch(ev, restore)) {
setTitle("VNC Viewer : Connection Details");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
- if (cc.viewer.firstApplet && cc.viewport == null) {
- System.exit(1);
+ if (cc.viewer.nViewers == 1) {
+ cc.viewer.exit(1);
} else {
ok = false;
endDialog();
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
- cc.deleteWindow();
+ cc.close();
}
});
addComponentListener(new ComponentAdapter() {
public static void main(String[] argv) {
setLookAndFeel();
VncViewer viewer = new VncViewer(argv);
- viewer.firstApplet = true;
- viewer.stop = false;
viewer.start();
}
if (argv[i].equalsIgnoreCase("-tunnel") || argv[i].equalsIgnoreCase("-via")) {
if (!tunnel.createTunnel(argv.length, argv, i))
- System.exit(1);
+ exit(1);
if (argv[i].equalsIgnoreCase("-via")) i++;
continue;
}
" \t%R remote TCP port number.\n"+
"\n");
System.err.print(propertiesString);
+ // Technically, we shouldn't use System.exit here but if there is a parameter
+ // error then the problem is in the index/html file anyway.
System.exit(1);
}
public VncViewer() {
applet = true;
- firstApplet = true;
}
public static void newViewer(VncViewer oldViewer, Socket sock, boolean close) {
VncViewer viewer = new VncViewer();
viewer.applet = oldViewer.applet;
- viewer.firstApplet = (close) ? true : false;
viewer.sock = sock;
viewer.start();
if (close)
- oldViewer.stop();
+ oldViewer.exit(0);
}
public static void newViewer(VncViewer oldViewer, Socket sock) {
public void start() {
vlog.debug("start called");
getTimestamp();
- nViewers++;
- if (applet && firstApplet) {
+ if (applet && nViewers == 0) {
alwaysShowServerDialog.setParam(true);
Configuration.global().readAppletParams(this);
String host = getCodeBase().getHost();
: ("::"+port)));
}
}
+ nViewers++;
thread = new Thread(this);
thread.start();
}
- public void stop() {
- stop = true;
+ public void exit(int n) {
+ nViewers--;
+ if (nViewers > 0)
+ return;
+ if (applet) {
+ destroy();
+ } else {
+ System.exit(n);
+ }
}
public void paint(Graphics g) {
listener = new TcpListener(null, port);
} catch (java.lang.Exception e) {
System.out.println(e.toString());
- System.exit(1);
+ exit(1);
}
vlog.info("Listening on port "+port);
try {
cc = new CConn(this, sock, vncServerName.getValue());
- while (!stop)
+ while (!cc.shuttingDown)
cc.processMsg();
- if (nViewers > 1) {
- cc = null;
- return;
- }
- } catch (EndOfStream e) {
- vlog.info(e.toString());
} catch (java.lang.Exception e) {
- if (cc != null) cc.deleteWindow();
if (cc == null || !cc.shuttingDown) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
e.toString(),
"VNC Viewer : Error",
JOptionPane.ERROR_MESSAGE);
+ } else {
+ if (!cc.shuttingDown)
+ vlog.info(e.toString());
+ cc = null;
}
}
- if (cc != null) cc.deleteWindow();
- nViewers--;
- if (!applet && nViewers == 0) {
- System.exit(0);
- }
+ exit(0);
}
BoolParameter useLocalCursor
Thread thread;
Socket sock;
- boolean applet, firstApplet, stop;
+ boolean applet;
Image logo;
static int nViewers;
static LogWriter vlog = new LogWriter("main");