PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java

https://github.com/apache/httpcore
Java | 241 lines | 41 code | 30 blank | 170 comment | 0 complexity | baf74e1c1dbe90f86e9da847c3637f8d MD5 | raw file
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. * ====================================================================
  20. *
  21. * This software consists of voluntary contributions made by many
  22. * individuals on behalf of the Apache Software Foundation. For more
  23. * information on the Apache Software Foundation, please see
  24. * <http://www.apache.org/>.
  25. *
  26. */
  27. package org.apache.hc.core5.reactor;
  28. import java.net.SocketAddress;
  29. import java.nio.channels.ByteChannel;
  30. import java.util.concurrent.locks.Lock;
  31. import org.apache.hc.core5.annotation.Internal;
  32. import org.apache.hc.core5.http.SocketModalCloseable;
  33. import org.apache.hc.core5.util.Identifiable;
  34. import org.apache.hc.core5.util.Timeout;
  35. /**
  36. * IOSession interface represents a sequence of logically related data exchanges
  37. * between two end points.
  38. * <p>
  39. * The channel associated with implementations of this interface can be used to
  40. * read data from and write data to the session.
  41. * <p>
  42. * I/O sessions are not bound to an execution thread, therefore one cannot use
  43. * the context of the thread to store a session's state. All details about
  44. * a particular session must be stored within the session itself, usually
  45. * using execution context associated with it.
  46. * <p>
  47. * Implementations of this interface are expected to be threading safe.
  48. *
  49. * @since 4.0
  50. */
  51. @Internal
  52. public interface IOSession extends ByteChannel, SocketModalCloseable, Identifiable {
  53. /**
  54. * This enum represents a set of states I/O session transitions through
  55. * during its life-span.
  56. */
  57. enum Status {
  58. ACTIVE,
  59. CLOSING,
  60. CLOSED
  61. }
  62. /**
  63. * Returns event handler associated with the session.
  64. *
  65. * @since 5.0
  66. */
  67. IOEventHandler getHandler();
  68. /**
  69. * Upgrades event handler associated with the session.
  70. *
  71. * @since 5.0
  72. */
  73. void upgrade(IOEventHandler handler);
  74. /**
  75. * Returns session lock that should be used by I/O event handlers
  76. * to synchronize access to the session.
  77. *
  78. * @since 5.0
  79. */
  80. Lock getLock();
  81. /**
  82. * Inserts {@link Command} at the end of the command queue.
  83. *
  84. * @since 5.0
  85. */
  86. void enqueue(Command command, Command.Priority priority);
  87. /**
  88. * Tests if there enqueued commands pending execution.
  89. *
  90. * @since 5.0
  91. */
  92. boolean hasCommands();
  93. /**
  94. * Removes first {@link Command} from the command queue if available.
  95. *
  96. * @since 5.0
  97. */
  98. Command poll();
  99. /**
  100. * Returns the underlying I/O channel associated with this session.
  101. *
  102. * @return the I/O channel.
  103. */
  104. ByteChannel channel();
  105. /**
  106. * Returns address of the remote peer.
  107. *
  108. * @return socket address.
  109. */
  110. SocketAddress getRemoteAddress();
  111. /**
  112. * Returns local address.
  113. *
  114. * @return socket address.
  115. */
  116. SocketAddress getLocalAddress();
  117. /**
  118. * Returns mask of I/O evens this session declared interest in.
  119. *
  120. * @return I/O event mask.
  121. */
  122. int getEventMask();
  123. /**
  124. * Declares interest in I/O event notifications by setting the event mask
  125. * associated with the session
  126. *
  127. * @param ops new I/O event mask.
  128. */
  129. void setEventMask(int ops);
  130. /**
  131. * Declares interest in a particular I/O event type by updating the event
  132. * mask associated with the session.
  133. *
  134. * @param op I/O event type.
  135. */
  136. void setEvent(int op);
  137. /**
  138. * Clears interest in a particular I/O event type by updating the event
  139. * mask associated with the session.
  140. *
  141. * @param op I/O event type.
  142. */
  143. void clearEvent(int op);
  144. /**
  145. * Terminates the session gracefully and closes the underlying I/O channel.
  146. * This method ensures that session termination handshake, such as the one
  147. * used by the SSL/TLS protocol, is correctly carried out.
  148. */
  149. @Override
  150. void close();
  151. /**
  152. * Returns status of the session:
  153. * <p>
  154. * {@link Status#ACTIVE}: session is active.
  155. * <p>
  156. * {@link Status#CLOSING}: session is being closed.
  157. * <p>
  158. * {@link Status#CLOSED}: session has been terminated.
  159. *
  160. * @return session status.
  161. */
  162. Status getStatus();
  163. /**
  164. * Returns value of the socket timeout in milliseconds. The value of
  165. * {@code 0} signifies the session cannot time out.
  166. *
  167. * @return socket timeout.
  168. */
  169. @Override
  170. Timeout getSocketTimeout();
  171. /**
  172. * Sets value of the socket timeout in milliseconds. The value of
  173. * {@code 0} signifies the session cannot time out.
  174. * <p>
  175. * Please note this operation may affect the last event time.
  176. *
  177. * @see #getLastEventTime()
  178. *
  179. * @param timeout socket timeout.
  180. */
  181. @Override
  182. void setSocketTimeout(Timeout timeout);
  183. /**
  184. * Returns timestamp of the last read event.
  185. *
  186. * @return timestamp.
  187. */
  188. long getLastReadTime();
  189. /**
  190. * Returns timestamp of the last write event.
  191. *
  192. * @return timestamp.
  193. */
  194. long getLastWriteTime();
  195. /**
  196. * Returns timestamp of the last I/O event including socket timeout reset.
  197. *
  198. * @see #getSocketTimeout()
  199. *
  200. * @return timestamp.
  201. */
  202. long getLastEventTime();
  203. /**
  204. * Updates the timestamp of the last read event
  205. */
  206. void updateReadTime();
  207. /**
  208. * Updates the timestamp of the last write event
  209. */
  210. void updateWriteTime();
  211. }