46 |
{ |
{ |
47 |
int registered; |
int registered; |
48 |
boolean blocking = true; |
boolean blocking = true; |
49 |
Object LOCK = new Object(); |
Object LOCK = new Object (); |
50 |
SelectorProvider sprovider; |
SelectorProvider provider; |
|
|
|
51 |
List keys; |
List keys; |
52 |
|
|
53 |
protected abstract void implCloseSelectableChannel(); |
/** |
54 |
protected abstract void implConfigureBlocking(boolean block); |
* Initializes the channel |
55 |
|
*/ |
56 |
protected AbstractSelectableChannel(SelectorProvider provider) |
protected AbstractSelectableChannel (SelectorProvider provider) |
57 |
{ |
{ |
58 |
|
this.provider = provider; |
59 |
} |
} |
60 |
|
|
61 |
public final Object blockingLock() |
/** |
62 |
|
* Retrieves the object upon which the configureBlocking and register |
63 |
|
* methods synchronize. |
64 |
|
*/ |
65 |
|
public final Object blockingLock () |
66 |
{ |
{ |
67 |
return LOCK; |
return LOCK; |
|
//Retrieves the object upon which the configureBlocking and register methods synchronize. |
|
68 |
} |
} |
69 |
|
|
70 |
public final SelectableChannel configureBlocking(boolean block) |
/** |
71 |
|
* Adjusts this channel's blocking mode. |
72 |
|
*/ |
73 |
|
public final SelectableChannel configureBlocking (boolean block) |
74 |
{ |
{ |
75 |
synchronized(LOCK) |
synchronized (LOCK) |
76 |
{ |
{ |
77 |
blocking = true; |
blocking = true; |
78 |
implConfigureBlocking(block); |
implConfigureBlocking (block); |
79 |
} |
} |
80 |
|
|
|
// Adjusts this channel's blocking mode. |
|
81 |
return this; |
return this; |
82 |
} |
} |
83 |
|
|
84 |
protected final void implCloseChannel() |
/** |
85 |
{ |
* Closes this channel. |
86 |
// Closes this channel. |
*/ |
87 |
implCloseSelectableChannel(); |
protected final void implCloseChannel () |
88 |
} |
{ |
89 |
|
implCloseSelectableChannel (); |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
|
* Closes this selectable channel. |
94 |
|
*/ |
95 |
|
protected abstract void implCloseSelectableChannel (); |
96 |
|
|
97 |
|
/** |
98 |
|
* Adjusts this channel's blocking mode. |
99 |
|
*/ |
100 |
|
protected abstract void implConfigureBlocking (boolean block); |
101 |
|
|
102 |
|
/** |
103 |
|
* Tells whether or not every I/O operation on this channel will block |
104 |
|
* until it completes. |
105 |
|
*/ |
106 |
public final boolean isBlocking() |
public final boolean isBlocking() |
107 |
{ |
{ |
108 |
return blocking; |
return blocking; |
|
//Tells whether or not every I/O operation on this channel will block until it completes. |
|
109 |
} |
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Tells whether or not this channel is currently registered with |
113 |
|
* any selectors. |
114 |
|
*/ |
115 |
public final boolean isRegistered() |
public final boolean isRegistered() |
116 |
{ |
{ |
|
//Tells whether or not this channel is currently registered with any selectors. |
|
117 |
return registered > 0; |
return registered > 0; |
118 |
} |
} |
119 |
|
|
120 |
public final SelectionKey keyFor(Selector sel) |
/** |
121 |
|
* Retrieves the key representing the channel's registration with the |
122 |
|
* given selector. |
123 |
|
*/ |
124 |
|
public final SelectionKey keyFor(Selector selector) |
125 |
{ |
{ |
126 |
//Retrieves the key representing the channel's registration with the given selector. |
try |
127 |
try { |
{ |
128 |
return register(sel, 0, null); |
return register (selector, 0, null); |
129 |
} catch (Exception e) { |
} |
130 |
return null; |
catch (Exception e) |
131 |
} |
{ |
132 |
|
return null; |
133 |
|
} |
134 |
} |
} |
135 |
|
|
136 |
public final SelectorProvider provider() |
/** |
137 |
|
* Returns the provider that created this channel. |
138 |
|
*/ |
139 |
|
public final SelectorProvider provider () |
140 |
{ |
{ |
141 |
// Returns the provider that created this channel. |
return provider; |
|
return sprovider; |
|
142 |
} |
} |
143 |
|
|
144 |
private SelectionKey locate(Selector sel) |
private SelectionKey locate (Selector selector) |
145 |
{ |
{ |
146 |
if (keys == null) |
if (keys == null) |
147 |
return null; |
return null; |
148 |
|
|
149 |
SelectionKey k = null; |
SelectionKey k = null; |
150 |
ListIterator it = keys.listIterator(); |
ListIterator it = keys.listIterator (); |
151 |
while (it.hasNext()) |
|
152 |
|
while (it.hasNext ()) |
153 |
{ |
{ |
154 |
k = (SelectionKey) it.next(); |
k = (SelectionKey) it.next (); |
155 |
if (k.selector() == sel) |
if (k.selector () == selector) |
156 |
{ |
{ |
157 |
return k; |
return k; |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
return k; |
return k; |
162 |
} |
} |
163 |
|
|
164 |
private void add(SelectionKey k) |
private void add (SelectionKey key) |
165 |
{ |
{ |
166 |
if (keys == null) |
if (keys == null) |
167 |
keys = new LinkedList(); |
keys = new LinkedList (); |
168 |
keys.add(k); |
|
169 |
|
keys.add (key); |
170 |
} |
} |
171 |
|
|
172 |
public final SelectionKey register(Selector selin, int ops, Object att) throws ClosedChannelException |
/** |
173 |
|
* Registers this channel with the given selector, returning a selection key. |
174 |
|
*/ |
175 |
|
public final SelectionKey register (Selector selin, int ops, Object att) |
176 |
|
throws ClosedChannelException |
177 |
{ |
{ |
178 |
if (!isOpen()) |
if (!isOpen ()) |
179 |
{ |
throw new ClosedChannelException(); |
|
System.out.println("not open, throwing exception"); |
|
|
throw new ClosedChannelException(); |
|
|
} |
|
|
|
|
|
System.out.println("Registers this channel with the given selector, returning a selection key."); |
|
180 |
|
|
181 |
SelectionKey k = null; |
SelectionKey k = null; |
182 |
AbstractSelector sel = (AbstractSelector) selin; |
AbstractSelector selector = (AbstractSelector) selin; |
183 |
|
|
184 |
synchronized (LOCK) |
synchronized (LOCK) |
185 |
{ |
{ |
186 |
k = locate(sel); |
k = locate (selector); |
187 |
|
|
188 |
if (k != null) |
if (k != null) |
189 |
{ |
{ |
190 |
k.attach(att); |
k.attach (att); |
191 |
} |
} |
192 |
else |
else |
193 |
{ |
{ |
194 |
k = sel.register(this, ops, att); |
k = selector.register (this, ops, att); |
195 |
|
|
196 |
if (k != null) |
if (k != null) |
197 |
{ |
add (k); |
|
add(k); |
|
|
} |
|
198 |
} |
} |
199 |
} |
} |
200 |
|
|