annotate rep/channel/REPSelectionKey.java @ 225:e173411a2499

*** empty log message ***
author kent
date Sun, 31 Aug 2008 13:32:15 +0900
parents 6cd4aab9fea3
children 168dd841be51
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
1 package rep.channel;
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
2
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
3 import java.io.IOException;
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
4 import java.nio.channels.SelectableChannel;
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
5 import java.nio.channels.SelectionKey;
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
6 import java.nio.channels.Selector;
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
7 import java.nio.channels.ServerSocketChannel;
203
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
8 import java.nio.channels.SocketChannel;
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
9
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
10
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
11 public class REPSelectionKey<P> extends SelectionKey {
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
12 SelectionKey key;
219
13e0520470dc *** empty log message ***
kono
parents: 217
diff changeset
13 private REPSelector<P> selector;
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
14
221
6cd4aab9fea3 *** empty log message ***
kono
parents: 219
diff changeset
15 public REPSelectionKey() {
6cd4aab9fea3 *** empty log message ***
kono
parents: 219
diff changeset
16
6cd4aab9fea3 *** empty log message ***
kono
parents: 219
diff changeset
17 }
6cd4aab9fea3 *** empty log message ***
kono
parents: 219
diff changeset
18
219
13e0520470dc *** empty log message ***
kono
parents: 217
diff changeset
19 public REPSelectionKey(SelectionKey key,REPSelector<P>s) {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
20 this.key = key;
219
13e0520470dc *** empty log message ***
kono
parents: 217
diff changeset
21 this.selector = s;
214
ed43dc6ccb07 *** empty log message ***
kent
parents: 208
diff changeset
22 attach(key.attachment());
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
23 }
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
24
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
25 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
26 public void cancel() {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
27 key.cancel();
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
28 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
29
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
30 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
31 public SelectableChannel channel() {
217
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
32 if (REPServerSocketChannel.isSimulation) return key.channel();
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
33 if (key.isAcceptable()) {
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
34 SelectableChannel sc = key.channel();
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
35 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
36 return rsc;
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
37 } else if (key.isReadable()) {
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
38 SelectableChannel sc = key.channel();
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
39 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
40 return rsc;
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
41 }
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
42 return null;
4deaaaa6354e *** empty log message ***
kono
parents: 214
diff changeset
43
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
44 }
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
45
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
46 public SelectableChannel channel(REPPack<P>packer) {
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
47 if (REPServerSocketChannel.isSimulation) return key.channel();
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
48 if (key.isAcceptable()) {
208
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
49 SelectableChannel sc = key.channel();
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
50 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
51 if (rsc!=null) return rsc;
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
52 return new REPServerSocketChannel<P>(key.channel(),packer);
203
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
53 } else if (key.isReadable()) {
208
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
54 SelectableChannel sc = key.channel();
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
55 SelectableChannel rsc = REPSocketChannel.channels.get(sc);
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
56 if (rsc!=null) return rsc;
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
57 return new REPSocketChannel<P>(key.channel(),packer);
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
58 }
203
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
59 return null;
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
60 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
61
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
62 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
63 public int interestOps() {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
64 return key.interestOps();
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
65 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
66
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
67 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
68 public SelectionKey interestOps(int ops) {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
69 return key.interestOps(ops);
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
70 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
71
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
72 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
73 public boolean isValid() {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
74 return key.isValid();
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
75 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
76
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
77 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
78 public int readyOps() {
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
79 return key.readyOps();
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
80 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
81
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
82 @Override
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
83 public Selector selector() {
219
13e0520470dc *** empty log message ***
kono
parents: 217
diff changeset
84 return selector;
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
85 }
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
86
208
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
87 @SuppressWarnings("unchecked")
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
88 public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException {
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
89 assert(!REPServerSocketChannel.isSimulation);
203
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
90 if (!key.isAcceptable()) throw new IOException();
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
91 ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
92 if (ssc==null) return null;
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
93 SocketChannel ss = (SocketChannel)ssc.accept();
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
94 if (ss==null) return null;
208
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
95 REPSocketChannel<P> rsc = (REPSocketChannel<P>)REPSocketChannel.channels.get(ss);
302c4a0afab6 *** empty log message ***
kono
parents: 203
diff changeset
96 if (rsc!=null) return rsc;
203
4c0a94836357 *** empty log message ***
kono
parents: 194
diff changeset
97 return new REPSocketChannel<P>(ss,pack);
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
98 }
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
99
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
100
184
3c82100cdadd *** empty log message ***
kent
parents:
diff changeset
101 }