PageRenderTime 252ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ojc-core/ftpbc/ftpbcimpl/src/org/apache/commons/net/DatagramSocketClient.java

https://bitbucket.org/rsaqc/openesb-components
Java | 273 lines | 79 code | 33 blank | 161 comment | 2 complexity | ff74b905a1845e447ae411e627289220 MD5 | raw file
  1. /*
  2. * Copyright 2001-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net;
  17. import java.net.DatagramSocket;
  18. import java.net.InetAddress;
  19. import java.net.SocketException;
  20. /***
  21. * The DatagramSocketClient provides the basic operations that are required
  22. * of client objects accessing datagram sockets. It is meant to be
  23. * subclassed to avoid having to rewrite the same code over and over again
  24. * to open a socket, close a socket, set timeouts, etc. Of special note
  25. * is the {@link #setDatagramSocketFactory setDatagramSocketFactory }
  26. * method, which allows you to control the type of DatagramSocket the
  27. * DatagramSocketClient creates for network communications. This is
  28. * especially useful for adding things like proxy support as well as better
  29. * support for applets. For
  30. * example, you could create a
  31. * {@link org.apache.commons.net.DatagramSocketFactory}
  32. * that
  33. * requests browser security capabilities before creating a socket.
  34. * All classes derived from DatagramSocketClient should use the
  35. * {@link #_socketFactory_ _socketFactory_ } member variable to
  36. * create DatagramSocket instances rather than instantiating
  37. * them by directly invoking a constructor. By honoring this contract
  38. * you guarantee that a user will always be able to provide his own
  39. * Socket implementations by substituting his own SocketFactory.
  40. * <p>
  41. * <p>
  42. * @author Daniel F. Savarese
  43. * @see DatagramSocketFactory
  44. ***/
  45. public abstract class DatagramSocketClient
  46. {
  47. /***
  48. * The default DatagramSocketFactory shared by all DatagramSocketClient
  49. * instances.
  50. ***/
  51. private static final DatagramSocketFactory __DEFAULT_SOCKET_FACTORY =
  52. new DefaultDatagramSocketFactory();
  53. /*** The timeout to use after opening a socket. ***/
  54. protected int _timeout_;
  55. /*** The datagram socket used for the connection. ***/
  56. protected DatagramSocket _socket_;
  57. /***
  58. * A status variable indicating if the client's socket is currently open.
  59. ***/
  60. protected boolean _isOpen_;
  61. /*** The datagram socket's DatagramSocketFactory. ***/
  62. protected DatagramSocketFactory _socketFactory_;
  63. /***
  64. * Default constructor for DatagramSocketClient. Initializes
  65. * _socket_ to null, _timeout_ to 0, and _isOpen_ to false.
  66. ***/
  67. public DatagramSocketClient()
  68. {
  69. _socket_ = null;
  70. _timeout_ = 0;
  71. _isOpen_ = false;
  72. _socketFactory_ = __DEFAULT_SOCKET_FACTORY;
  73. }
  74. /***
  75. * Opens a DatagramSocket on the local host at the first available port.
  76. * Also sets the timeout on the socket to the default timeout set
  77. * by {@link #setDefaultTimeout setDefaultTimeout() }.
  78. * <p>
  79. * _isOpen_ is set to true after calling this method and _socket_
  80. * is set to the newly opened socket.
  81. * <p>
  82. * @exception SocketException If the socket could not be opened or the
  83. * timeout could not be set.
  84. ***/
  85. public void open() throws SocketException
  86. {
  87. _socket_ = _socketFactory_.createDatagramSocket();
  88. _socket_.setSoTimeout(_timeout_);
  89. _isOpen_ = true;
  90. }
  91. /***
  92. * Opens a DatagramSocket on the local host at a specified port.
  93. * Also sets the timeout on the socket to the default timeout set
  94. * by {@link #setDefaultTimeout setDefaultTimeout() }.
  95. * <p>
  96. * _isOpen_ is set to true after calling this method and _socket_
  97. * is set to the newly opened socket.
  98. * <p>
  99. * @param port The port to use for the socket.
  100. * @exception SocketException If the socket could not be opened or the
  101. * timeout could not be set.
  102. ***/
  103. public void open(int port) throws SocketException
  104. {
  105. _socket_ = _socketFactory_.createDatagramSocket(port);
  106. _socket_.setSoTimeout(_timeout_);
  107. _isOpen_ = true;
  108. }
  109. /***
  110. * Opens a DatagramSocket at the specified address on the local host
  111. * at a specified port.
  112. * Also sets the timeout on the socket to the default timeout set
  113. * by {@link #setDefaultTimeout setDefaultTimeout() }.
  114. * <p>
  115. * _isOpen_ is set to true after calling this method and _socket_
  116. * is set to the newly opened socket.
  117. * <p>
  118. * @param port The port to use for the socket.
  119. * @param laddr The local address to use.
  120. * @exception SocketException If the socket could not be opened or the
  121. * timeout could not be set.
  122. ***/
  123. public void open(int port, InetAddress laddr) throws SocketException
  124. {
  125. _socket_ = _socketFactory_.createDatagramSocket(port, laddr);
  126. _socket_.setSoTimeout(_timeout_);
  127. _isOpen_ = true;
  128. }
  129. /***
  130. * Closes the DatagramSocket used for the connection.
  131. * You should call this method after you've finished using the class
  132. * instance and also before you call {@link #open open() }
  133. * again. _isOpen_ is set to false and _socket_ is set to null.
  134. * If you call this method when the client socket is not open,
  135. * a NullPointerException is thrown.
  136. ***/
  137. public void close()
  138. {
  139. _socket_.close();
  140. _socket_ = null;
  141. _isOpen_ = false;
  142. }
  143. /***
  144. * Returns true if the client has a currently open socket.
  145. * <p>
  146. * @return True if the client has a curerntly open socket, false otherwise.
  147. ***/
  148. public boolean isOpen()
  149. {
  150. return _isOpen_;
  151. }
  152. /***
  153. * Set the default timeout in milliseconds to use when opening a socket.
  154. * After a call to open, the timeout for the socket is set using this value.
  155. * This method should be used prior to a call to {@link #open open()}
  156. * and should not be confused with {@link #setSoTimeout setSoTimeout()}
  157. * which operates on the currently open socket. _timeout_ contains
  158. * the new timeout value.
  159. * <p>
  160. * @param timeout The timeout in milliseconds to use for the datagram socket
  161. * connection.
  162. ***/
  163. public void setDefaultTimeout(int timeout)
  164. {
  165. _timeout_ = timeout;
  166. }
  167. /***
  168. * Returns the default timeout in milliseconds that is used when
  169. * opening a socket.
  170. * <p>
  171. * @return The default timeout in milliseconds that is used when
  172. * opening a socket.
  173. ***/
  174. public int getDefaultTimeout()
  175. {
  176. return _timeout_;
  177. }
  178. /***
  179. * Set the timeout in milliseconds of a currently open connection.
  180. * Only call this method after a connection has been opened
  181. * by {@link #open open()}.
  182. * <p>
  183. * @param timeout The timeout in milliseconds to use for the currently
  184. * open datagram socket connection.
  185. ***/
  186. public void setSoTimeout(int timeout) throws SocketException
  187. {
  188. _socket_.setSoTimeout(timeout);
  189. }
  190. /***
  191. * Returns the timeout in milliseconds of the currently opened socket.
  192. * If you call this method when the client socket is not open,
  193. * a NullPointerException is thrown.
  194. * <p>
  195. * @return The timeout in milliseconds of the currently opened socket.
  196. ***/
  197. public int getSoTimeout() throws SocketException
  198. {
  199. return _socket_.getSoTimeout();
  200. }
  201. /***
  202. * Returns the port number of the open socket on the local host used
  203. * for the connection. If you call this method when the client socket
  204. * is not open, a NullPointerException is thrown.
  205. * <p>
  206. * @return The port number of the open socket on the local host used
  207. * for the connection.
  208. ***/
  209. public int getLocalPort()
  210. {
  211. return _socket_.getLocalPort();
  212. }
  213. /***
  214. * Returns the local address to which the client's socket is bound.
  215. * If you call this method when the client socket is not open, a
  216. * NullPointerException is thrown.
  217. * <p>
  218. * @return The local address to which the client's socket is bound.
  219. ***/
  220. public InetAddress getLocalAddress()
  221. {
  222. return _socket_.getLocalAddress();
  223. }
  224. /***
  225. * Sets the DatagramSocketFactory used by the DatagramSocketClient
  226. * to open DatagramSockets. If the factory value is null, then a default
  227. * factory is used (only do this to reset the factory after having
  228. * previously altered it).
  229. * <p>
  230. * @param factory The new DatagramSocketFactory the DatagramSocketClient
  231. * should use.
  232. ***/
  233. public void setDatagramSocketFactory(DatagramSocketFactory factory)
  234. {
  235. if (factory == null)
  236. _socketFactory_ = __DEFAULT_SOCKET_FACTORY;
  237. else
  238. _socketFactory_ = factory;
  239. }
  240. }