| 1 | package tmcsim.client; |
|---|
| 2 | |
|---|
| 3 | import java.io.IOException; |
|---|
| 4 | import java.io.InputStream; |
|---|
| 5 | import java.io.OutputStream; |
|---|
| 6 | import java.net.InetSocketAddress; |
|---|
| 7 | import java.net.Socket; |
|---|
| 8 | |
|---|
| 9 | import tmcsim.common.SimulationException; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * CADClientSocket contains the Socket used to communication between the CAD |
|---|
| 14 | * Client and the CAD Simulator. This class uses the host and port parameter |
|---|
| 15 | * values to connect to the CAD Simulator's ServerSocket. I/O streams are |
|---|
| 16 | * created from this connection to transfer all data between applications. |
|---|
| 17 | * |
|---|
| 18 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 19 | * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.3 $ |
|---|
| 20 | */ |
|---|
| 21 | class CADClientSocket { |
|---|
| 22 | |
|---|
| 23 | /** Socket used to communicate to the CAD Simulator. */ |
|---|
| 24 | private Socket clientSocket; |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Constructor accepting the hostname and port for the CAD Simulator's socket. |
|---|
| 28 | * |
|---|
| 29 | * @param newhost Target host name for CAD Simulator. |
|---|
| 30 | * @param newport Target port for CAD Simulator. |
|---|
| 31 | * @throws SimulationException if there is an exception in connecting to the CAD Simulator. |
|---|
| 32 | */ |
|---|
| 33 | CADClientSocket(String newhost, int newport) throws SimulationException { |
|---|
| 34 | |
|---|
| 35 | try { |
|---|
| 36 | clientSocket = new Socket(); |
|---|
| 37 | clientSocket.connect(new InetSocketAddress(newhost, newport), 5000); |
|---|
| 38 | } |
|---|
| 39 | catch (IOException ioe) { |
|---|
| 40 | throw new SimulationException(SimulationException.CAD_SIM_CONNECT, ioe); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Returns a reference to the input stream associated with this socket. |
|---|
| 47 | * |
|---|
| 48 | * @return InputStream Input Stream for socket. |
|---|
| 49 | * @throws SimulationException if there is an exception in opening the input stream. |
|---|
| 50 | */ |
|---|
| 51 | public InputStream getInputStream() throws SimulationException { |
|---|
| 52 | try { |
|---|
| 53 | return clientSocket.getInputStream(); |
|---|
| 54 | } |
|---|
| 55 | catch (IOException ioe) { |
|---|
| 56 | throw new SimulationException(SimulationException.CAD_SIM_COMM, ioe); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Returns a reference to the output stream associated with this socket. |
|---|
| 62 | * |
|---|
| 63 | * @return OutputStream Output Stream for socket. |
|---|
| 64 | * @throws SimulationException if there is an exception in opening the output stream. |
|---|
| 65 | */ |
|---|
| 66 | public OutputStream getOutputStream() throws SimulationException { |
|---|
| 67 | try { |
|---|
| 68 | return clientSocket.getOutputStream(); |
|---|
| 69 | } |
|---|
| 70 | catch (IOException ioe) { |
|---|
| 71 | throw new SimulationException(SimulationException.CAD_SIM_COMM, ioe); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Closes the Socket. |
|---|
| 77 | * |
|---|
| 78 | * @throws SimulationException if there is an exception in closing the socket. |
|---|
| 79 | */ |
|---|
| 80 | public void closeSocket() throws SimulationException { |
|---|
| 81 | try { |
|---|
| 82 | clientSocket.close(); |
|---|
| 83 | } |
|---|
| 84 | catch (IOException ioe) { |
|---|
| 85 | throw new SimulationException(SimulationException.CAD_SIM_COMM, ioe); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|