1. /*
  2. * @(#)SelectableChannel.java 1.34 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels;
  8. import java.io.IOException;
  9. import java.nio.channels.spi.AbstractInterruptibleChannel;
  10. import java.nio.channels.spi.SelectorProvider;
  11. /**
  12. * A channel that can be multiplexed via a {@link Selector}.
  13. *
  14. * <p> In order to be used with a selector, an instance of this class must
  15. * first be <i>registered</i> via the {@link #register(Selector,int,Object)
  16. * register} method. This method returns a new {@link SelectionKey} object
  17. * that represents the channel's registration with the selector.
  18. *
  19. * <p> Once registered with a selector, a channel remains registered until it
  20. * is <i>deregistered</i>. This involves deallocating whatever resources were
  21. * allocated to the channel by the selector.
  22. *
  23. * <p> A channel cannot be deregistered directly; instead, the key representing
  24. * its registration must be <i>cancelled</i>. Cancelling a key requests that
  25. * the channel be deregistered during the selector's next selection operation.
  26. * A key may be cancelled explicitly by invoking its {@link
  27. * SelectionKey#cancel() cancel} method. All of a channel's keys are cancelled
  28. * implicitly when the channel is closed, whether by invoking its {@link
  29. * Channel#close close} method or by interrupting a thread blocked in an I/O
  30. * operation upon the channel.
  31. *
  32. * <p> If the selector itself is closed then the channel will be deregistered,
  33. * and the key representing its registration will be invalidated, without
  34. * further delay.
  35. *
  36. * <p> A channel may be registered at most once with any particular selector.
  37. *
  38. * <p> Whether or not a channel is registered with one or more selectors may be
  39. * determined by invoking the {@link #isRegistered isRegistered} method.
  40. *
  41. * <p> Selectable channels are safe for use by multiple concurrent
  42. * threads. </p>
  43. *
  44. *
  45. * <a name="bm">
  46. * <h4>Blocking mode</h4>
  47. *
  48. * A selectable channel is either in <i>blocking</i> mode or in
  49. * <i>non-blocking</i> mode. In blocking mode, every I/O operation invoked
  50. * upon the channel will block until it completes. In non-blocking mode an I/O
  51. * operation will never block and may transfer fewer bytes than were requested
  52. * or possibly no bytes at all. The blocking mode of a selectable channel may
  53. * be determined by invoking its {@link #isBlocking isBlocking} method.
  54. *
  55. * <p> Newly-created selectable channels are always in blocking mode.
  56. * Non-blocking mode is most useful in conjunction with selector-based
  57. * multiplexing. A channel must be placed into non-blocking mode before being
  58. * registered with a selector, and may not be returned to blocking mode until
  59. * it has been deregistered.
  60. *
  61. *
  62. * @author Mark Reinhold
  63. * @author JSR-51 Expert Group
  64. * @version 1.34, 03/12/19
  65. * @since 1.4
  66. *
  67. * @see SelectionKey
  68. * @see Selector
  69. */
  70. public abstract class SelectableChannel
  71. extends AbstractInterruptibleChannel
  72. implements Channel
  73. {
  74. /**
  75. * Initializes a new instance of this class.
  76. */
  77. protected SelectableChannel() { }
  78. /**
  79. * Returns the provider that created this channel.
  80. *
  81. * @return The provider that created this channel
  82. */
  83. public abstract SelectorProvider provider();
  84. /**
  85. * Returns an <a href="SelectionKey.html#opsets">operation set</a>
  86. * identifying this channel's supported operations. The bits that are set
  87. * in this integer value denote exactly the operations that are valid for
  88. * this channel. This method always returns the same value for a given
  89. * concrete channel class. </p>
  90. *
  91. * @return The valid-operation set
  92. */
  93. public abstract int validOps();
  94. // Internal state:
  95. // keySet, may be empty but is never null, typ. a tiny array
  96. // boolean isRegistered, protected by key set
  97. // regLock, lock object to prevent duplicate registrations
  98. // boolean isBlocking, protected by regLock
  99. /**
  100. * Tells whether or not this channel is currently registered with any
  101. * selectors. A newly-created channel is not registered.
  102. *
  103. * <p> Due to the inherent delay between key cancellation and channel
  104. * deregistration, a channel may remain registered for some time after all
  105. * of its keys have been cancelled. A channel may also remain registered
  106. * for some time after it is closed. </p>
  107. *
  108. * @return <tt>true</tt> if, and only if, this channel is registered
  109. */
  110. public abstract boolean isRegistered();
  111. //
  112. // sync(keySet) { return isRegistered; }
  113. /**
  114. * Retrieves the key representing the channel's registration with the given
  115. * selector. </p>
  116. *
  117. * @return The key returned when this channel was last registered with the
  118. * given selector, or <tt>null</tt> if this channel is not
  119. * currently registered with that selector
  120. */
  121. public abstract SelectionKey keyFor(Selector sel);
  122. //
  123. // sync(keySet) { return findKey(sel); }
  124. /**
  125. * Registers this channel with the given selector, returning a selection
  126. * key.
  127. *
  128. * <p> If this channel is currently registered with the given selector then
  129. * the selection key representing that registration is returned. The key's
  130. * interest set will have been changed to <tt>ops</tt>, as if by invoking
  131. * the {@link SelectionKey#interestOps(int) interestOps(int)} method. If
  132. * the <tt>att</tt> argument is not <tt>null</tt> then the key's attachment
  133. * will have been set to that value. A {@link CancelledKeyException} will
  134. * be thrown if the key has already been cancelled.
  135. *
  136. * <p> Otherwise this channel has not yet been registered with the given
  137. * selector, so it is registered and the resulting new key is returned.
  138. * The key's initial interest set will be <tt>ops</tt> and its attachment
  139. * will be <tt>att</tt>.
  140. *
  141. * <p> This method may be invoked at any time. If this method is invoked
  142. * while another invocation of this method or of the {@link
  143. * #configureBlocking(boolean) configureBlocking} method is in progress
  144. * then it will first block until the other operation is complete. This
  145. * method will then synchronize on the selector's key set and therefore may
  146. * block if invoked concurrently with another registration or selection
  147. * operation involving the same selector. </p>
  148. *
  149. * <p> If this channel is closed while this operation is in progress then
  150. * the key returned by this method will have been cancelled and will
  151. * therefore be invalid. </p>
  152. *
  153. * @param sel
  154. * The selector with which this channel is to be registered
  155. *
  156. * @param ops
  157. * The interest set for the resulting key
  158. *
  159. * @param att
  160. * The attachment for the resulting key; may be <tt>null</tt>
  161. *
  162. * @throws ClosedChannelException
  163. * If this channel is closed
  164. *
  165. * @throws IllegalBlockingModeException
  166. * If this channel is in blocking mode
  167. *
  168. * @throws IllegalSelectorException
  169. * If this channel was not created by the same provider
  170. * as the given selector
  171. *
  172. * @throws CancelledKeyException
  173. * If this channel is currently registered with the given selector
  174. * but the corresponding key has already been cancelled
  175. *
  176. * @throws IllegalArgumentException
  177. * If a bit in the <tt>ops</tt> set does not correspond to an
  178. * operation that is supported by this channel, that is, if
  179. * <tt>set & ~validOps() != 0</tt>
  180. *
  181. * @return A key representing the registration of this channel with
  182. * the given selector
  183. */
  184. public abstract SelectionKey register(Selector sel, int ops, Object att)
  185. throws ClosedChannelException;
  186. //
  187. // sync(regLock) {
  188. // sync(keySet) { look for selector }
  189. // if (channel found) { set interest ops -- may block in selector;
  190. // return key; }
  191. // create new key -- may block somewhere in selector;
  192. // sync(keySet) { add key; }
  193. // attach(attachment);
  194. // return key;
  195. // }
  196. /**
  197. * Registers this channel with the given selector, returning a selection
  198. * key.
  199. *
  200. * <p> An invocation of this convenience method of the form
  201. *
  202. * <blockquote><tt>sc.register(sel, ops)</tt></blockquote>
  203. *
  204. * behaves in exactly the same way as the invocation
  205. *
  206. * <blockquote><tt>sc.{@link
  207. * #register(java.nio.channels.Selector,int,java.lang.Object)
  208. * register}(sel, ops, null)</tt></blockquote>
  209. *
  210. * @param sel
  211. * The selector with which this channel is to be registered
  212. *
  213. * @param ops
  214. * The interest set for the resulting key
  215. *
  216. * @throws ClosedChannelException
  217. * If this channel is closed
  218. *
  219. * @throws IllegalBlockingModeException
  220. * If this channel is in blocking mode
  221. *
  222. * @throws IllegalSelectorException
  223. * If this channel was not created by the same provider
  224. * as the given selector
  225. *
  226. * @throws CancelledKeyException
  227. * If this channel is currently registered with the given selector
  228. * but the corresponding key has already been cancelled
  229. *
  230. * @throws IllegalArgumentException
  231. * If a bit in <tt>ops</tt> does not correspond to an operation
  232. * that is supported by this channel, that is, if <tt>set &
  233. * ~validOps() != 0</tt>
  234. *
  235. * @return A key representing the registration of this channel with
  236. * the given selector
  237. */
  238. public final SelectionKey register(Selector sel, int ops)
  239. throws ClosedChannelException
  240. {
  241. return register(sel, ops, null);
  242. }
  243. /**
  244. * Adjusts this channel's blocking mode.
  245. *
  246. * <p> If this channel is registered with one or more selectors then an
  247. * attempt to place it into blocking mode will cause an {@link
  248. * IllegalBlockingModeException} to be thrown.
  249. *
  250. * <p> This method may be invoked at any time. The new blocking mode will
  251. * only affect I/O operations that are initiated after this method returns.
  252. * For some implementations this may require blocking until all pending I/O
  253. * operations are complete.
  254. *
  255. * <p> If this method is invoked while another invocation of this method or
  256. * of the {@link #register(Selector, int) register} method is in progress
  257. * then it will first block until the other operation is complete. </p>
  258. *
  259. * @param block If <tt>true</tt> then this channel will be placed in
  260. * blocking mode; if <tt>false</tt> then it will be placed
  261. * non-blocking mode
  262. *
  263. * @return This selectable channel
  264. *
  265. * @throws ClosedChannelException
  266. * If this channel is closed
  267. *
  268. * @throws IllegalBlockingModeException
  269. * If <tt>block</tt> is <tt>true</tt> and this channel is
  270. * registered with one or more selectors
  271. *
  272. * @throws IOException
  273. * If an I/O error occurs
  274. */
  275. public abstract SelectableChannel configureBlocking(boolean block)
  276. throws IOException;
  277. //
  278. // sync(regLock) {
  279. // sync(keySet) { throw IBME if block && isRegistered; }
  280. // change mode;
  281. // }
  282. /**
  283. * Tells whether or not every I/O operation on this channel will block
  284. * until it completes. A newly-created channel is always in blocking mode.
  285. *
  286. * <p> If this channel is closed then the value returned by this method is
  287. * not specified. </p>
  288. *
  289. * @return <tt>true</tt> if, and only if, this channel is in blocking mode
  290. */
  291. public abstract boolean isBlocking();
  292. /**
  293. * Retrieves the object upon which the {@link #configureBlocking
  294. * configureBlocking} and {@link #register register} methods synchronize.
  295. * This is often useful in the implementation of adaptors that require a
  296. * specific blocking mode to be maintained for a short period of time.
  297. * </p>
  298. *
  299. * @return The blocking-mode lock object
  300. */
  301. public abstract Object blockingLock();
  302. }