| 1 | package tmcsim.cadsimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.IOException; |
|---|
| 4 | import java.net.ServerSocket; |
|---|
| 5 | import java.net.Socket; |
|---|
| 6 | import java.net.SocketTimeoutException; |
|---|
| 7 | import java.util.logging.Level; |
|---|
| 8 | import java.util.logging.Logger; |
|---|
| 9 | |
|---|
| 10 | import tmcsim.common.SimulationException; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * CADSimulatorSocketHandler is used to accept socket connections from multiple |
|---|
| 15 | * CAD Clients. At initialization, this class binds a ServerSocket to a specific |
|---|
| 16 | * port, passed in as a parameter. This class is threaded, and when it is 'run', |
|---|
| 17 | * it begins accepting connections. When a connection is made, a Socket is created |
|---|
| 18 | * for data communication with that client and a CADSimulatorClient thread |
|---|
| 19 | * is spun with connection to that socket. This thread will continue to accept |
|---|
| 20 | * clients indefinitely. |
|---|
| 21 | * |
|---|
| 22 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 23 | * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.3 $ |
|---|
| 24 | */ |
|---|
| 25 | public class CADSimulatorSocketHandler extends Thread { |
|---|
| 26 | |
|---|
| 27 | /** Error logger. */ |
|---|
| 28 | private Logger socketLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 29 | |
|---|
| 30 | /** ServerSocket used to accept connections from clients. */ |
|---|
| 31 | private ServerSocket serverSocket; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Constructor. A ServerSocket is bound to the parameter port number. |
|---|
| 35 | * |
|---|
| 36 | * @param port Port number for ServerSocket binding. |
|---|
| 37 | * @throws SimulationException if an error occurs binding the ServerSocket. |
|---|
| 38 | */ |
|---|
| 39 | public CADSimulatorSocketHandler(Integer port) throws SimulationException { |
|---|
| 40 | |
|---|
| 41 | try { |
|---|
| 42 | serverSocket = new ServerSocket(port); |
|---|
| 43 | serverSocket.setSoTimeout(5000); //delay for accept timeout(milliseconds) |
|---|
| 44 | } catch (IOException ioe) { |
|---|
| 45 | throw new SimulationException(SimulationException.BINDING, ioe); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * While this thread is not interrupted, connection requests are continuously |
|---|
| 51 | * accepted from CAD Clients. When a connection is accepted, a Socket is created, |
|---|
| 52 | * passed into the CADSimulatorClient's constructor, and then that new |
|---|
| 53 | * Client thread is spun off, and the ServerSocket returns to accepting |
|---|
| 54 | * more clients. The CADSimulatorViewer is notified of a successful client |
|---|
| 55 | * conection after the Socket creating completes. If there is an error in |
|---|
| 56 | * establishing connection to a client, the ServerSocket will continue to |
|---|
| 57 | * accept connections. |
|---|
| 58 | */ |
|---|
| 59 | public void run() { |
|---|
| 60 | |
|---|
| 61 | Socket clientSocket; |
|---|
| 62 | |
|---|
| 63 | while (!isInterrupted()) { |
|---|
| 64 | try |
|---|
| 65 | { |
|---|
| 66 | clientSocket = serverSocket.accept(); |
|---|
| 67 | |
|---|
| 68 | CADClientConnector theTMClient = new CADClientConnector(clientSocket); |
|---|
| 69 | theTMClient.start(); |
|---|
| 70 | //CADSimulator.theViewer.connectClient(); |
|---|
| 71 | } |
|---|
| 72 | catch(SocketTimeoutException ste) {} |
|---|
| 73 | catch(Exception e) { |
|---|
| 74 | socketLogger.logp(Level.SEVERE, "CADSimulatorSocketHandler", "run", |
|---|
| 75 | "Exception in creating a connection to a remote CAD Client.", e); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | try { |
|---|
| 80 | serverSocket.close(); |
|---|
| 81 | } |
|---|
| 82 | catch (IOException ioe) { |
|---|
| 83 | socketLogger.logp(Level.SEVERE, "CADSimulatorSocketHandler", "run", |
|---|
| 84 | "Exception in closing socket.", ioe); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } |
|---|