annotate src/rep/channel/REPSocketChannel.java @ 193:3133040ee4f4

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