inital plugin from webtools project
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.monitor.core / src / net / sourceforge / phpdt / monitor / core / internal / AcceptThread.java
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  **********************************************************************/
11 package net.sourceforge.phpdt.monitor.core.internal;
12
13 import java.io.InterruptedIOException;
14 import java.net.*;
15
16 import net.sourceforge.phpdt.monitor.core.IMonitor;
17 /**
18  * The actual TCP/IP monitoring server. This is a thread that
19  * listens on a port and relays a call to another server.
20  */
21 public class AcceptThread {
22         protected IMonitor monitor;
23
24         protected boolean alive = true;
25         protected ServerSocket serverSocket;
26         
27         protected Thread thread;
28         
29         class ServerThread extends Thread {
30                 /**
31                  * Actual running of the server proxy.
32                  */
33            public void run() {
34                    // create a new server socket
35                    try {
36                            serverSocket = new ServerSocket(monitor.getLocalPort());
37                            serverSocket.setSoTimeout(2000);
38                            
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");
42                            return;
43                    }
44                 
45                    while (alive) {
46                            try {
47                                    // accept the connection from the client
48                                    Socket localSocket = serverSocket.accept();
49                 
50                                    // connect to the remote server
51                                    Socket remoteSocket = new Socket(monitor.getRemoteHost(), monitor.getRemotePort());
52                 
53                                    // relay the call through
54                                    ProtocolAdapter adapter = (ProtocolAdapter) monitor.getProtocolAdapter();
55                                    adapter.parse(monitor, localSocket, remoteSocket);
56                            } catch (InterruptedIOException e) {
57                                 // do nothing
58                            } catch (Exception e) {
59                                    if (alive)
60                                                 Trace.trace(Trace.SEVERE, "Error while monitoring", e);
61                            }
62                    }
63            }
64         }
65
66         /**
67          * ServerMonitorThread constructor comment.
68          */
69         public AcceptThread(IMonitor monitor) {
70                 super();
71                 this.monitor = monitor;
72         }
73         
74         public void startServer() {
75                 if (thread != null)
76                         return;
77                 thread = new ServerThread();
78                 thread.setDaemon(true);
79                 thread.start();
80         }
81
82         public boolean isRunning() {
83                 return (thread != null);
84         }
85
86         /**
87          * Correctly close the server socket and shut down the server.
88          */
89         public void stopServer() {
90                 try {
91                         alive = false;
92                         thread = null;
93                         serverSocket.close();
94                 } catch (Exception e) {
95                         Trace.trace(Trace.SEVERE, "Error stopping server", e);
96                 }
97         }
98         
99         /**
100          * Returns true if this port is in use.
101          *
102          * @return boolean
103          * @param port int
104          */
105         public static boolean isPortInUse(int port) {
106                 ServerSocket s = null;
107                 try {
108                         s = new ServerSocket(port);
109                 } catch (SocketException e) {
110                         return true;
111                 } catch (Exception e) {
112                         return true;
113                 } finally {
114                         if (s != null) {
115                                 try {
116                                         s.close();
117                                 } catch (Exception e) { }
118                         }
119                 }
120
121                 return false;
122         }
123 }