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