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