annotate rep/channel/REPSocketChannel.java @ 287:1ff8bfc0a99a test-editor

*** empty log message ***
author kono
date Sun, 28 Sep 2008 15:46:36 +0900
parents 4a02c7f26794
children ef00df38dd5d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
1 package rep.channel;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
2
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
137
1ea856259add *** empty log message ***
pin
parents: 136
diff changeset
4 import java.net.Socket;
153
4cfed12aa3aa *** empty log message ***
kono
parents: 147
diff changeset
5 import java.net.SocketAddress;
136
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
6 import java.nio.ByteBuffer;
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
7 import java.nio.channels.ClosedChannelException;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
8 import java.nio.channels.SelectableChannel;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
9 import java.nio.channels.SelectionKey;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
10 import java.nio.channels.Selector;
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
11 import java.nio.channels.SocketChannel;
255
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
12 import java.nio.channels.spi.AbstractSelector;
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
13 import java.nio.channels.spi.SelectorProvider;
208
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
14 import java.util.HashMap;
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
15
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
16 public class REPSocketChannel<P> extends SelectableChannel{
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
17
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
18 public SocketChannel sc;
147
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
19 private REPPack<P> pack;
208
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
20 static public HashMap<SelectableChannel,SelectableChannel> channels = new HashMap<SelectableChannel,SelectableChannel>();
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
21
184
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
22 public REPSocketChannel(SocketChannel channel, REPPack<P> packer) {
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
23 sc = channel;
184
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
24 pack = packer;
208
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
25 addChannel(sc,this);
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
26 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
27
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
28 public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) {
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
29 sc = (SocketChannel)channel;
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
30 pack = packer;
208
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
31 addChannel(sc,this);
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
32 }
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
33
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
34 public static void addChannel(SelectableChannel sc,SelectableChannel rc) {
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
35 channels.put(sc, rc);
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
36 }
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
37
208
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
38 public void close1() throws IOException {
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
39 removeChannel(sc);
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
40 sc.close();
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
41 }
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
42
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
43 public static void removeChannel(SelectableChannel sc) throws IOException {
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
44 if(channels.containsKey(sc)) channels.remove(sc);
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
45 }
302c4a0afab6 *** empty log message ***
kono
parents: 194
diff changeset
46
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
47 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
48 public Object blockingLock() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
49 return sc.blockingLock();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
50 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
51
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
52 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
53 public SelectableChannel configureBlocking(boolean block) throws IOException {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
54 return sc.configureBlocking(block);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
55 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
56
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
57 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
58 public boolean isBlocking() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
59 return sc.isBlocking();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
60 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
61
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
62 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
63 public boolean isRegistered() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
64 return sc.isRegistered();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
65 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
66
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
67 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
68 public SelectionKey keyFor(Selector sel) {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
69 return sc.keyFor(sel);
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
70 }
255
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
71
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
72 public SelectionKey keyFor(REPSelector<?> sel) {
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
73 return sc.keyFor(sel.selector);
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
74 }
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
75
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
76 public REPSelectionKey<P> keyFor1(REPSelector<P> sel) {
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
77 return new REPSelectionKey<P>(sc.keyFor(sel.selector),
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
78 new REPSelector<P>((AbstractSelector) sel.selector));
cc119b4b1e96 *** empty log message ***
kono
parents: 241
diff changeset
79 }
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
80
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
81 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
82 public SelectorProvider provider() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
83 return sc.provider();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
84 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
85
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
86
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
87 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
88 public int validOps() {
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
89 return sc.validOps();
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
90 }
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
91
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
92 @Override
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
93 protected void implCloseChannel() throws IOException {
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
94 close1();
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
95 }
136
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
96
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
97 public long read(ByteBuffer header) throws IOException {
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
98 return sc.read(header);
136
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
99 }
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
100
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
101 public void write(ByteBuffer buffer) throws IOException {
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
102 sc.write(buffer);
136
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
103
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
104 }
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
105
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
106 public boolean finishConnect() throws IOException {
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
107 return sc.finishConnect();
136
a4e2bceb9713 *** empty log message ***
pin
parents: 123
diff changeset
108 }
137
1ea856259add *** empty log message ***
pin
parents: 136
diff changeset
109
1ea856259add *** empty log message ***
pin
parents: 136
diff changeset
110 public Socket socket() {
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
111 return sc.socket();
137
1ea856259add *** empty log message ***
pin
parents: 136
diff changeset
112 }
143
785a3e8ea858 *** empty log message ***
kent
parents: 140
diff changeset
113
147
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
114 public P read() throws IOException{
184
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
115 return pack.unpackUConv(sc);
143
785a3e8ea858 *** empty log message ***
kent
parents: 140
diff changeset
116 }
266
4a02c7f26794 *** empty log message ***
kono
parents: 255
diff changeset
117
145
ec625f8f8d7c *** empty log message ***
kent
parents: 143
diff changeset
118 public boolean write(P p){
147
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
119 ByteBuffer bb = pack.packUConv(p);
211
44d502851c9e *** empty log message ***
kent
parents: 208
diff changeset
120 if (bb==null) return true;
147
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
121 try {
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
122 while (bb.remaining() > 0 ){
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
123 sc.write(bb);
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
124 }
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
125 return true;
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
126 } catch (IOException e) {
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
127 return false;
4ff68518e9ca *** empty log message ***
kent
parents: 145
diff changeset
128 }
145
ec625f8f8d7c *** empty log message ***
kent
parents: 143
diff changeset
129 }
140
01062be677e9 *** empty log message ***
kono
parents: 137
diff changeset
130
184
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
131 public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException {
140
01062be677e9 *** empty log message ***
kono
parents: 137
diff changeset
132 if (REPServerSocketChannel.isSimulation) {
174
72252e970a8b *** empty log message ***
kent
parents: 171
diff changeset
133 return new ChannelSimulator<T>();
140
01062be677e9 *** empty log message ***
kono
parents: 137
diff changeset
134 } else {
184
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
135 REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer);
3c82100cdadd *** empty log message ***
kent
parents: 174
diff changeset
136 return rsc;
140
01062be677e9 *** empty log message ***
kono
parents: 137
diff changeset
137 }
01062be677e9 *** empty log message ***
kono
parents: 137
diff changeset
138 }
153
4cfed12aa3aa *** empty log message ***
kono
parents: 147
diff changeset
139
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
140
153
4cfed12aa3aa *** empty log message ***
kono
parents: 147
diff changeset
141 public boolean connect(SocketAddress semaIP) throws IOException {
4cfed12aa3aa *** empty log message ***
kono
parents: 147
diff changeset
142 return sc.connect(semaIP);
4cfed12aa3aa *** empty log message ***
kono
parents: 147
diff changeset
143 }
156
31334767e65d *** empty log message ***
kono
parents: 153
diff changeset
144
240
168dd841be51 selected key Interator fix.
kono
parents: 234
diff changeset
145 public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
168dd841be51 selected key Interator fix.
kono
parents: 234
diff changeset
146 return sc.register(sel.selector, ops, att);
168dd841be51 selected key Interator fix.
kono
parents: 234
diff changeset
147 }
168dd841be51 selected key Interator fix.
kono
parents: 234
diff changeset
148
168dd841be51 selected key Interator fix.
kono
parents: 234
diff changeset
149
234
0498425202a4 made ChannelSimulator no extends SelectableChannelSimulator
kent
parents: 232
diff changeset
150
232
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
151 public SelectionKey register1(REPSelector<P> sel, int ops, Object att)
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
152 throws ClosedChannelException {
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
153 if(sel instanceof REPSelector) {
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
154 REPSelector<P> s = (REPSelector<P>)sel;
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
155 return sc.register(s.selector, ops,att);
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
156 }
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
157 return sc.register(sel, ops,att);
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
158 }
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
159
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
160 @SuppressWarnings("unchecked")
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
161 @Override
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
162 public SelectionKey register(Selector sel, int ops, Object att)
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
163 throws ClosedChannelException {
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
164 if(sel instanceof REPSelector) {
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
165 REPSelector<P> s = (REPSelector<P>)sel;
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
166 return sc.register(s.selector, ops,att);
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
167 }
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
168 return sc.register(sel, ops,att);
844c8f46584b *** empty log message ***
kono
parents: 225
diff changeset
169 }
194
be219ba8b39c *** empty log message ***
kono
parents: 184
diff changeset
170
123
5b1a0574b406 *** empty log message ***
pin
parents:
diff changeset
171 }