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;
16 import net.sourceforge.phpdt.monitor.core.IMonitor;
18 * The actual TCP/IP monitoring server. This is a thread that
19 * listens on a port and relays a call to another server.
21 public class AcceptThread {
22 protected IMonitor monitor;
24 protected boolean alive = true;
25 protected ServerSocket serverSocket;
27 protected Thread thread;
29 class ServerThread extends Thread {
31 * Actual running of the server proxy.
34 // create a new server socket
36 serverSocket = new ServerSocket(monitor.getLocalPort());
37 serverSocket.setSoTimeout(2000);
39 Trace.trace(Trace.FINEST, "Monitoring localhost:" + monitor.getLocalPort() + " -> " + monitor.getRemoteHost() + ":" + monitor.getRemotePort());
40 } catch (Exception e) {
41 Trace.trace(Trace.SEVERE, "Could not start monitoring");
47 // accept the connection from the client
48 Socket localSocket = serverSocket.accept();
50 // connect to the remote server
51 Socket remoteSocket = new Socket(monitor.getRemoteHost(), monitor.getRemotePort());
53 // relay the call through
54 ProtocolAdapter adapter = (ProtocolAdapter) monitor.getProtocolAdapter();
55 adapter.parse(monitor, localSocket, remoteSocket);
56 } catch (InterruptedIOException e) {
58 } catch (Exception e) {
60 Trace.trace(Trace.SEVERE, "Error while monitoring", e);
67 * ServerMonitorThread constructor comment.
69 public AcceptThread(IMonitor monitor) {
71 this.monitor = monitor;
74 public void startServer() {
77 thread = new ServerThread();
78 thread.setDaemon(true);
82 public boolean isRunning() {
83 return (thread != null);
87 * Correctly close the server socket and shut down the server.
89 public void stopServer() {
94 } catch (Exception e) {
95 Trace.trace(Trace.SEVERE, "Error stopping server", e);
100 * Returns true if this port is in use.
105 public static boolean isPortInUse(int port) {
106 ServerSocket s = null;
108 s = new ServerSocket(port);
109 } catch (SocketException e) {
111 } catch (Exception e) {
117 } catch (Exception e) { }