Mercurial > hg > RemoteEditor > Eclipse
comparison src/rep/channel/REPSelectionKey.java @ 193:3133040ee4f4
(no commit message)
author | one |
---|---|
date | Wed, 31 Dec 2008 15:06:22 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
192:c921022bf494 | 193:3133040ee4f4 |
---|---|
1 package rep.channel; | |
2 | |
3 import java.io.IOException; | |
4 import java.nio.channels.SelectableChannel; | |
5 import java.nio.channels.SelectionKey; | |
6 import java.nio.channels.Selector; | |
7 import java.nio.channels.ServerSocketChannel; | |
8 import java.nio.channels.SocketChannel; | |
9 | |
10 | |
11 public class REPSelectionKey<P> extends SelectionKey { | |
12 SelectionKey key; | |
13 private REPSelector<P> selector; | |
14 | |
15 public REPSelectionKey() { | |
16 | |
17 } | |
18 | |
19 public REPSelectionKey(SelectionKey key,REPSelector<P>s) { | |
20 this.key = key; | |
21 this.selector = s; | |
22 attach(key.attachment()); | |
23 } | |
24 | |
25 @Override | |
26 public void cancel() { | |
27 key.cancel(); | |
28 } | |
29 | |
30 @Override | |
31 public SelectableChannel channel() { | |
32 if (REPServerSocketChannel.isSimulation) return key.channel(); | |
33 SelectableChannel sc = key.channel(); | |
34 SelectableChannel rsc = REPSocketChannel.channels.get(sc); | |
35 return rsc; | |
36 } | |
37 | |
38 @SuppressWarnings("unchecked") | |
39 public REPSocketChannel<P> channel1() { | |
40 assert (!REPServerSocketChannel.isSimulation); | |
41 SelectableChannel sc = key.channel(); | |
42 REPSocketChannel<P> rsc = (REPSocketChannel<P>) REPSocketChannel.channels.get(sc); | |
43 return rsc; | |
44 } | |
45 | |
46 @SuppressWarnings("unchecked") | |
47 public REPServerSocketChannel<P> serverSocketChannel() { | |
48 assert (!REPServerSocketChannel.isSimulation); | |
49 SelectableChannel sc = key.channel(); | |
50 REPServerSocketChannel<P> rsc = (REPServerSocketChannel<P>) REPSocketChannel.channels.get(sc); | |
51 return rsc; | |
52 } | |
53 | |
54 @Override | |
55 public int interestOps() { | |
56 return key.interestOps(); | |
57 } | |
58 | |
59 @Override | |
60 public SelectionKey interestOps(int ops) { | |
61 return key.interestOps(ops); | |
62 } | |
63 | |
64 @Override | |
65 public boolean isValid() { | |
66 return key.isValid(); | |
67 } | |
68 | |
69 @Override | |
70 public int readyOps() { | |
71 return key.readyOps(); | |
72 } | |
73 | |
74 @Override | |
75 public Selector selector() { | |
76 return selector; | |
77 } | |
78 | |
79 @SuppressWarnings("unchecked") | |
80 public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException { | |
81 assert(!REPServerSocketChannel.isSimulation); | |
82 if (!key.isAcceptable()) throw new IOException(); | |
83 ServerSocketChannel ssc = (ServerSocketChannel)key.channel(); | |
84 if (ssc==null) return null; | |
85 SocketChannel ss = (SocketChannel)ssc.accept(); | |
86 //System.err.println("Accept in SelectionKey "+ss); | |
87 if (ss==null) return null; | |
88 return new REPSocketChannel(ss, pack); | |
89 } | |
90 | |
91 | |
92 } |