1 package fr.ove.utils;
2
3 import java.io.*;
4 import java.net.*;
5
6 /***
7 * A connection object.
8 * Opens a socket to the specified host on the specified port number.
9 */
10 public class Connection implements ConnectionConfigurable {
11 /***
12 * The socket of the connection
13 */
14 private Socket socket;
15
16 /***
17 * The input stream to read from the socket.
18 */
19 private InputStream inputStream = null;
20
21 /***
22 * The output stream to write into the socket.
23 */
24 private OutputStream outputStream = null;
25
26 /***
27 * The host name to connect.
28 */
29 private String hostName;
30
31 /***
32 * The port number.
33 */
34 private int portNumber;
35
36 /***
37 * The default constructor.
38 * The default host name is "localhost" and the default port number is 6666.
39 */
40 public Connection() {
41 this("localhost", 6666);
42 }
43
44 /***
45 * The Constructor.
46 * @param hostName the host name.
47 * @param portNumber the port number.
48 */
49 public Connection(String hostName, int portNumber) {
50 this.hostName = hostName;
51 this.portNumber = portNumber;
52 }
53
54 /***
55 * Sets the host name for the connection to create.
56 * @param name the host name.
57 */
58 public void setHostName(String name) {
59 hostName = name;
60 }
61
62 /***
63 * Returns the host name for the connection to create.
64 */
65 public String getHostName() {
66 return hostName;
67 }
68
69 /***
70 * Sets the port number for the connexion to create.
71 * @param number the port number.
72 */
73 public void setPortNumber(int number) {
74 portNumber = number;
75 }
76
77 /***
78 * Returns the port number for the connexion to create.
79 */
80 public int getPortNumber() {
81 return portNumber;
82 }
83
84 /***
85 * Opens the connection.
86 */
87 public void open() throws IOException {
88 socket = new Socket(InetAddress.getByName(hostName), portNumber);
89 inputStream = socket.getInputStream();
90 outputStream = socket.getOutputStream();
91 }
92
93 /***
94 * Opens the connection through the specified socket.
95 * @param socket the specified socket.
96 */
97 public void open(Socket socket) throws IOException {
98 this.socket = socket;
99 hostName = socket.getInetAddress().getHostName();
100 portNumber = socket.getPort();
101 inputStream = socket.getInputStream();
102 outputStream = socket.getOutputStream();
103 }
104
105 /***
106 * Checks wether the connection is opened.
107 */
108 public boolean isOpened() {
109 return ((outputStream != null) && (inputStream != null));
110 }
111
112 /***
113 * Closes the connection.
114 */
115 public void close() throws IOException {
116 outputStream.close();
117 inputStream.close();
118 outputStream = null;
119 inputStream = null;
120 }
121
122 /***
123 * Gets the input stream to read from the socket.
124 */
125 public InputStream getInputStream() {
126 return inputStream;
127 }
128
129 /***
130 * Gets the output stream to write into the socket.
131 */
132 public OutputStream getOutputStream() {
133 return outputStream;
134 }
135 }