1 /**********************************************************************
2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
10 **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
13 import java.io.InterruptedIOException;
14 import java.net.ServerSocket;
15 import java.net.Socket;
16 import java.net.SocketException;
18 import net.sourceforge.phpdt.monitor.core.IMonitor;
20 * The actual TCP/IP monitoring server. This is a thread that
21 * listens on a port and relays a call to another server.
23 public class AcceptThread {
24 protected IMonitor monitor;
26 protected boolean alive = true;
27 protected ServerSocket serverSocket;
29 protected Thread thread;
31 class ServerThread extends Thread {
33 * Actual running of the server proxy.
36 // create a new server socket
38 serverSocket = new ServerSocket(monitor.getLocalPort());
39 serverSocket.setSoTimeout(2000);
41 Trace.trace(Trace.FINEST, "Monitoring localhost:" + monitor.getLocalPort() + " -> " + monitor.getRemoteHost() + ":" + monitor.getRemotePort());
42 } catch (Exception e) {
43 Trace.trace(Trace.SEVERE, "Could not start monitoring");
49 // accept the connection from the client
50 Socket localSocket = serverSocket.accept();
52 // connect to the remote server
53 Socket remoteSocket = new Socket(monitor.getRemoteHost(), monitor.getRemotePort());
55 // relay the call through
56 ProtocolAdapter adapter = (ProtocolAdapter) monitor.getProtocolAdapter();
57 adapter.parse(monitor, localSocket, remoteSocket);
58 } catch (InterruptedIOException e) {
60 } catch (Exception e) {
62 Trace.trace(Trace.SEVERE, "Error while monitoring", e);
69 * ServerMonitorThread constructor comment.
71 public AcceptThread(IMonitor monitor) {
73 this.monitor = monitor;
76 public void startServer() {
79 thread = new ServerThread();
80 thread.setDaemon(true);
84 public boolean isRunning() {
85 return (thread != null);
89 * Correctly close the server socket and shut down the server.
91 public void stopServer() {
96 } catch (Exception e) {
97 Trace.trace(Trace.SEVERE, "Error stopping server", e);
102 * Returns true if this port is in use.
107 public static boolean isPortInUse(int port) {
108 ServerSocket s = null;
110 s = new ServerSocket(port);
111 } catch (SocketException e) {
113 } catch (Exception e) {
119 } catch (Exception e) { }