KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > nio > SelectionQueue


1 package org.sapia.ubik.net.nio;
2
3 import java.io.IOException JavaDoc;
4 import java.nio.channels.SelectionKey JavaDoc;
5 import java.nio.channels.Selector JavaDoc;
6
7 import org.sapia.ubik.util.Queue;
8
9 /**
10  * A utility class that holds a <code>Selector</code> and a
11  * <code>ChannelManager</code>. The selector thread is expected to block on
12  * this queue, waiting for <code>Cycle</code> instances to be added to it.
13  * Upon such an instance being added, the thread is notified and calls this
14  * instance's <code>register()</code> method.
15  *
16  * @see org.sapia.ubik.util.Queue#wasItemAdded()
17  * @see #register()
18  *
19  * @author Yanick Duchesne
20  *
21  * <dl>
22  * <dt><b>Copyright: </b>
23  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
24  * Source Software </a>. All Rights Reserved.</dd>
25  * </dt>
26  * <dt><b>License: </b>
27  * <dd>Read the license.txt file of the jar or visit the <a
28  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
29  * OSS web site</dd>
30  * </dt>
31  * </dl>
32  */

33 public class SelectionQueue extends Queue {
34
35   private Selector JavaDoc _selector;
36   private ChannelManager _manager;
37
38   /**
39    * @param manager
40    * a <code>ChannelManager</code>.
41    * @param selector
42    * a <code>Selector</code>.
43    */

44   public SelectionQueue(ChannelManager manager, Selector JavaDoc selector) {
45     _selector = selector;
46     _manager = manager;
47   }
48
49   /**
50    * This method expects <code>Cycle</code> instances.
51    *
52    * @see org.sapia.ubik.util.Queue#add(Object obj, boolean)
53    */

54   public void add(Object JavaDoc o, boolean notifyAll) {
55     super.add(o, notifyAll);
56     _selector.wakeup();
57   }
58
59   /**
60    * Internally registers the next <code>Cycle</code> in the queue with the
61    * <code>Selector</code> that this instance encapsulates.
62    *
63    * @throws IOException
64    * if a problem occurs.
65    */

66   public synchronized void register() throws IOException JavaDoc {
67     while(_items.size() > 0) {
68       Cycle cycle = (Cycle) _items.remove(0);
69       if(cycle.state() == Cycle.STATE_READ) {
70         _manager.register(cycle.getChannel(), _selector, SelectionKey.OP_READ,
71             cycle);
72       } else if(cycle.state() == Cycle.STATE_WRITE) {
73         _manager.register(cycle.getChannel(), _selector, SelectionKey.OP_WRITE,
74             cycle);
75       } else {
76         throw new IllegalStateException JavaDoc(
77             "Cycle state should be STATE_READ or STATE_WRITE; got "
78                 + cycle.state());
79       }
80     }
81     resetAddedFlag();
82   }
83
84   /**
85    * Closes this instance - internally calls the <code>destroy()</code> method
86    * of the pending <code>Cycle</code>s.
87    */

88   public synchronized void close() {
89     while(_items.size() > 0) {
90       Cycle cycle = (Cycle) _items.remove(0);
91       cycle.destroy();
92     }
93   }
94 }
95
Popular Tags