PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/quickserver-1.4.7/src/main/org/quickserver/net/server/ClientHandler.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 545 lines | 94 code | 72 blank | 379 comment | 0 complexity | 12017fded26e719c823b21a27c8761c8 MD5 | raw file
  1. /*
  2. * This file is part of the QuickServer library
  3. * Copyright (C) 2003-2005 QuickServer.org
  4. *
  5. * Use, modification, copying and distribution of this software is subject to
  6. * the terms and conditions of the GNU Lesser General Public License.
  7. * You should have received a copy of the GNU LGP License along with this
  8. * library; if not, you can download a copy from <http://www.quickserver.org/>.
  9. *
  10. * For questions, suggestions, bug-reports, enhancement-requests etc.
  11. * visit http://www.quickserver.org
  12. *
  13. */
  14. package org.quickserver.net.server;
  15. import java.io.BufferedInputStream;
  16. import java.io.BufferedOutputStream;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.net.InetAddress;
  26. import java.net.Socket;
  27. import java.net.SocketException;
  28. import java.nio.channels.ClosedChannelException;
  29. import java.nio.channels.SelectionKey;
  30. import java.nio.channels.SocketChannel;
  31. import java.security.KeyManagementException;
  32. import java.security.NoSuchAlgorithmException;
  33. import java.sql.Connection;
  34. import java.util.Date;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. import javax.net.ssl.SSLSocket;
  38. import org.quickserver.util.MyString;
  39. /**
  40. * Interface that represents client handle in QuickServer.
  41. * <p> This class is used by {@link QuickServer} to handle each new client
  42. * connected. This class is responsible to handle client sockets. It can operate
  43. * in both blocking mode and non-blocking mode (java nio) based on its
  44. * implementation.</p>
  45. * @author Akshathkumar Shetty
  46. */
  47. public interface ClientHandler extends Runnable {
  48. /**
  49. * Adds the ClientEvent.
  50. * @since 1.4.5
  51. */
  52. void addEvent(ClientEvent event);
  53. /**
  54. * Removes the ClientEvent.
  55. * @since 1.4.5
  56. */
  57. void removeEvent(ClientEvent event);
  58. void clean();
  59. /** Closes client socket associated. */
  60. void closeConnection();
  61. /**
  62. * Force the closing of the client by closing the associated socket.
  63. * @since 1.3.3
  64. */
  65. void forceClose() throws IOException;
  66. /**
  67. * Returns client SelectionKey associated, if any.
  68. * @since 1.4.5
  69. */
  70. Logger getAppLogger();
  71. /**
  72. *Returns the {@link java.io.BufferedInputStream} associated with
  73. * the Client being handled. Can be null if not available at the time of method call.
  74. * @see #getBufferedOutputStream
  75. * @since 1.4.6
  76. */
  77. BufferedInputStream getBufferedInputStream();
  78. /**
  79. * Returns the {@link java.io.BufferedOutputStream} associated with
  80. * the Client being handled. Can be null if not available at the time of method call.
  81. * @see #getBufferedInputStream
  82. * @since 1.4.6
  83. */
  84. BufferedOutputStream getBufferedOutputStream();
  85. /**
  86. * Returns the {@link java.io.BufferedReader} associated with
  87. * the Client being handled. Note that this is only available under blocking mode.
  88. * @see #getBufferedWriter
  89. */
  90. BufferedReader getBufferedReader();
  91. /**
  92. * Returns Charset to be used for String decoding and encoding..
  93. * @see #setCharset
  94. * @since 1.4.5
  95. */
  96. String getCharset();
  97. /**
  98. * Returns the date/time when the client socket was assigned to this
  99. * ClientHanlder. If no client is currently connected it will return
  100. * <code>null</code>
  101. * @since 1.3.1
  102. */
  103. Date getClientConnectedTime();
  104. /**
  105. * Returns the ClientData object associated with this ClientHandler,
  106. * if not set will return <code>null</code>
  107. * @see ClientData
  108. */
  109. ClientData getClientData();
  110. /**
  111. * Returns the communication logging flag.
  112. * @see #setCommunicationLogging
  113. * @since 1.3.2
  114. */
  115. boolean getCommunicationLogging();
  116. /**
  117. * Returns the {@link DataMode} of the ClientHandler for the
  118. * DataType.
  119. * @since 1.2
  120. */
  121. DataMode getDataMode(DataType dataType);
  122. /**
  123. * Returns cached socket host ip address.
  124. * @since 1.4.5
  125. */
  126. String getHostAddress();
  127. /**
  128. * Returns the {@link java.io.InputStream} associated with
  129. * the Client being handled.
  130. */
  131. InputStream getInputStream();
  132. /**
  133. * Returns the date/time when the client socket last sent a data to this
  134. * ClientHanlder. If no client is currently connected it will return
  135. * <code>null</code>
  136. * @since 1.3.3
  137. */
  138. Date getLastCommunicationTime();
  139. /**
  140. * Returns message to be displayed to the client when maximum
  141. * connection reaches.
  142. * @since 1.4.5
  143. */
  144. String getMaxConnectionMsg();
  145. /**
  146. * Returns the ClientHandler name
  147. * @since 1.4.6
  148. */
  149. String getName();
  150. /**
  151. * Returns the {@link java.io.ObjectInputStream} associated with
  152. * the Client being handled.
  153. * It will be <code>null</code> if no {@link ClientObjectHandler}
  154. * was set in {@link QuickServer}.
  155. * @see #getObjectOutputStream
  156. * @since 1.2
  157. */
  158. ObjectInputStream getObjectInputStream();
  159. /**
  160. * Returns the {@link java.io.ObjectOutputStream} associated with
  161. * the Client being handled.
  162. * It will be <code>null</code> if no {@link ClientObjectHandler}
  163. * was set in {@link QuickServer}.
  164. * @see #getObjectInputStream
  165. * @since 1.2
  166. */
  167. ObjectOutputStream getObjectOutputStream();
  168. /**
  169. * Returns the {@link java.io.OutputStream} associated with
  170. * the Client being handled.
  171. * @see #setOutputStream
  172. */
  173. OutputStream getOutputStream();
  174. /**
  175. * Returns client SelectionKey associated, if any.
  176. * @since 1.4.5
  177. */
  178. SelectionKey getSelectionKey();
  179. /**
  180. * Returns the QuickServer object that created it.
  181. */
  182. QuickServer getServer();
  183. /** Returns client socket associated. */
  184. Socket getSocket();
  185. /**
  186. * Returns client socket channel associated, if any.
  187. * @since 1.4.5
  188. */
  189. SocketChannel getSocketChannel();
  190. /**
  191. * Returns the Client socket timeout in milliseconds.
  192. * @see #setTimeout
  193. * @since 1.4.5
  194. */
  195. int getTimeout();
  196. /**
  197. * Associates the ClientHanlder with the client encapsulated by
  198. * <code>theClient</code>.
  199. * @param theClient object that encapsulates client socket
  200. * and its configuration details.
  201. */
  202. void handleClient(TheClient theClient);
  203. /**
  204. * Checks if this client has the event.
  205. * @since 1.4.5
  206. */
  207. boolean hasEvent(ClientEvent event);
  208. /**
  209. * Returns the ClientHandler detailed information.
  210. * If ClientData is present and is ClientIdentifiable will return ClientInfo else
  211. * it will return Clients InetAddress and port information.
  212. */
  213. String info();
  214. /**
  215. * Checks if the passed ClientEvent is the one next for
  216. * processing if a thread is allowed through this object.
  217. * @since 1.4.6
  218. */
  219. boolean isClientEventNext(ClientEvent clientEvent);
  220. /**
  221. * Checks if the client is closed.
  222. * @since 1.4.1
  223. */
  224. boolean isClosed();
  225. /**
  226. * Checks if the client is still connected.
  227. * @exception SocketException if Socket is not open.
  228. * @since 1.4.5
  229. */
  230. boolean isConnected() throws SocketException;
  231. /**
  232. * Checks if the client is still connected and if socket is open. This is same as isConnected()
  233. * but does not throw SocketException.
  234. * @since 1.4.6
  235. */
  236. boolean isOpen();
  237. /**
  238. * Returns flag indicating if the client is connected in secure mode
  239. * (SSL or TLS).
  240. * @return secure flag
  241. * @since 1.4.0
  242. */
  243. boolean isSecure();
  244. /**
  245. * Makes current Client connection to secure protocol based on the
  246. * secure configuration set to the server. This method will just call
  247. * <code>makeSecure(false, false, true, null)</code>.
  248. * @throws IOException
  249. * @throws NoSuchAlgorithmException
  250. * @throws KeyManagementException
  251. * @since 1.4.0
  252. */
  253. void makeSecure() throws IOException, NoSuchAlgorithmException, KeyManagementException;
  254. /**
  255. * Makes current Client connection to secure protocol.
  256. * @param useClientMode falg if the socket should start its first handshake in "client" mode.
  257. * @param needClientAuth flag if the clients must authenticate themselves.
  258. * @param autoClose close the underlying socket when this socket is closed
  259. * @param protocol the standard name of the requested protocol. If <code>null</code> will use the protocol set in secure configuration of the server.
  260. * @throws IOException
  261. * @throws NoSuchAlgorithmException
  262. * @throws KeyManagementException
  263. * @since 1.4.0
  264. */
  265. void makeSecure(boolean useClientMode, boolean needClientAuth, boolean autoClose, String protocol) throws IOException, NoSuchAlgorithmException, KeyManagementException;
  266. /**
  267. * Makes current Client connection to secure protocol.
  268. * This method will just call <code>makeSecure(false, false, true, protocol)</code>.
  269. * @throws IOException
  270. * @throws NoSuchAlgorithmException
  271. * @throws KeyManagementException
  272. * @since 1.4.0
  273. */
  274. void makeSecure(String protocol) throws IOException, NoSuchAlgorithmException, KeyManagementException;
  275. /**
  276. * Read the binary input. This will block till some data is
  277. * received from the stream. Allowed only when
  278. * <code>DataType.IN</code> is in <code>DataMode.BINARY</code> mode.
  279. * @return The data as a String
  280. * @since 1.4
  281. */
  282. byte[] readBinary() throws IOException;
  283. /**
  284. * Read the byte input. This will block till some data is
  285. * received from the stream. Allowed only when
  286. * <code>DataType.IN</code> is in <code>DataMode.BYTE</code> mode.
  287. * @return The data as a String
  288. * @since 1.3.2
  289. */
  290. String readBytes() throws IOException;
  291. /**
  292. * Register OP_READ with the SelectionKey associated with the channel. If SelectionKey is
  293. * not set then it registers the channel with the Selector.
  294. * @since 1.4.5
  295. */
  296. void registerForRead() throws IOException, ClosedChannelException;
  297. /**
  298. * Register OP_WRITE with the SelectionKey associated with the channel.
  299. * @since 1.4.5
  300. */
  301. void registerForWrite() throws IOException, ClosedChannelException;
  302. void run();
  303. /**
  304. * Send a binary data to the connected client.
  305. * If client is not connected it will just return.
  306. * @since 1.4
  307. * @exception IOException
  308. * if Socket IO Error or Socket was closed by the client.
  309. */
  310. void sendClientBinary(byte[] data) throws IOException;
  311. /**
  312. * Send a binary data to the connected client.
  313. * If client is not connected it will just return.
  314. * @since 1.4.5
  315. * @exception IOException
  316. * if Socket IO Error or Socket was closed by the client.
  317. */
  318. void sendClientBinary(byte[] data, int off, int len) throws IOException;
  319. /**
  320. * Send a String message to the connected client as a string of bytes.
  321. * If client is not connected it will just return.
  322. * @since 1.3.1
  323. * @exception IOException
  324. * if Socket IO Error or Socket was closed by the client.
  325. */
  326. void sendClientBytes(String msg) throws IOException;
  327. /**
  328. * Send a String message to the connected client
  329. * it adds a new line{\r\n} to the end of the string.
  330. * If client is not connected it will just return.
  331. * @exception IOException
  332. * if Socket IO Error or Socket was closed by the client.
  333. */
  334. void sendClientMsg(String msg) throws IOException;
  335. /**
  336. * Send a Object message to the connected client. The message Object
  337. * passed must be serializable. If client is not connected it
  338. * will just return.
  339. * @exception IOException if Socket IO Error or Socket was closed
  340. * by the client.
  341. * @exception IllegalStateException if DataType.OUT is not in
  342. * DataMode.OBJECT
  343. * @see #setDataMode
  344. * @since 1.2
  345. */
  346. void sendClientObject(Object msg) throws IOException;
  347. /**
  348. * Send a String message to the logger associated with
  349. * {@link QuickServer#getAppLogger} with Level.INFO as its level.
  350. */
  351. void sendSystemMsg(String msg);
  352. /**
  353. * Send a String message to the logger associated with
  354. * {@link QuickServer#getAppLogger}.
  355. * @since 1.2
  356. */
  357. void sendSystemMsg(String msg, Level level);
  358. /**
  359. * Sets the Charset to be used for String decoding and encoding.
  360. * @param charset to be used for String decoding and encoding
  361. * @see #getCharset
  362. * @since 1.4.5
  363. */
  364. void setCharset(String charset);
  365. /**
  366. * Sets the communication logging flag.
  367. * @see #getCommunicationLogging
  368. * @since 1.3.2
  369. */
  370. void setCommunicationLogging(boolean communicationLogging);
  371. /**
  372. * Sets the {@link DataMode} for the ClientHandler
  373. *
  374. * Note: When mode is DataMode.OBJECT and type is DataType.IN
  375. * this call will block until the client ObjectOutputStream has
  376. * written and flushes the header.
  377. * @since 1.2
  378. * @exception IOException if mode could not be changed.
  379. * @param dataMode mode of data exchange - String or Object.
  380. * @param dataType type of data for which mode has to be set.
  381. */
  382. void setDataMode(DataMode dataMode, DataType dataType) throws IOException;
  383. /**
  384. * Sets message to be displayed when maximum connection reaches.
  385. * @since 1.4.5
  386. */
  387. void setMaxConnectionMsg(String msg);
  388. /**
  389. * Set the {@link java.io.OutputStream} associated with
  390. * the Client being handled.
  391. * @since 1.1
  392. * @see #getOutputStream
  393. * @exception IOException if ObjectOutputStream could not be created.
  394. */
  395. void setOutputStream(OutputStream out) throws IOException;
  396. /**
  397. * Sets flag indicating if the client is connected in secure mode
  398. * (SSL or TLS).
  399. * @param secure
  400. * @since 1.4.0
  401. */
  402. void setSecure(boolean secure);
  403. /**
  404. * Sets client SelectionKey associated, if any.
  405. * @since 1.4.5
  406. */
  407. void setSelectionKey(SelectionKey selectionKey);
  408. /**
  409. * Returns client socket associated.
  410. * @since 1.4.0
  411. * @see #updateInputOutputStreams
  412. */
  413. void setSocket(Socket socket);
  414. /**
  415. * Sets client socket channel associated, if any.
  416. * @since 1.4.5
  417. */
  418. void setSocketChannel(SocketChannel socketChannel);
  419. /**
  420. * Sets the client socket's timeout.
  421. * @param time client socket timeout in milliseconds.
  422. * @see #getTimeout
  423. * @since 1.4.5
  424. */
  425. void setTimeout(int time);
  426. /**
  427. * Returns the ClientHandler information.
  428. * If ClientData is present and is ClientIdentifiable will return ClientInfo else
  429. * it will return Clients InetAddress and port information.
  430. */
  431. String toString();
  432. /**
  433. * Updates the InputStream and OutputStream for the ClientHandler for the
  434. * set Socket.
  435. * @since 1.4.0
  436. * @see #setSocket
  437. */
  438. void updateInputOutputStreams() throws IOException;
  439. /**
  440. * Updates the last communication time for this client
  441. * @since 1.3.3
  442. */
  443. void updateLastCommunicationTime();
  444. /**
  445. * Returns the {@link java.sql.Connection} object for the
  446. * DatabaseConnection that is identified by id passed. If id passed
  447. * does not match with any connection loaded by this class it will
  448. * return <code>null</code>.
  449. * This just calls <code>getServer().getDBPoolUtil().getConnection(id)</code>
  450. * @since 1.3
  451. * @deprecated as of v1.4.5 use <code>getServer().getDBPoolUtil().getConnection(id)</code>
  452. */
  453. Connection getConnection(String id) throws Exception;
  454. /**
  455. * Checks if the client is still connected.
  456. * @exception SocketException if Socket is not open.
  457. * @deprecated since 1.4.5 Use {@link #isConnected}
  458. */
  459. boolean isConected() throws SocketException;
  460. /**
  461. * Send a String message to the system output stream.
  462. * @param newline indicates if new line required at the end.
  463. * @deprecated Use {@link #sendSystemMsg(java.lang.String)},
  464. * since it uses Logging.
  465. */
  466. void sendSystemMsg(String msg, boolean newline);
  467. /**
  468. * Returns the {@link java.io.BufferedWriter} associated with
  469. * the Client being handled.
  470. * @deprecated since 1.4.5 use getOutputStream()
  471. */
  472. BufferedWriter getBufferedWriter();
  473. }