Mercurial > hg > RemoteEditor > REPSessionManager
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 |
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); | |
194 | 36 } |
37 | |
208 | 38 public void close1() throws IOException { |
39 removeChannel(sc); | |
40 sc.close(); | |
41 } | |
42 | |
43 public static void removeChannel(SelectableChannel sc) throws IOException { | |
44 if(channels.containsKey(sc)) channels.remove(sc); | |
45 } | |
46 | |
123 | 47 @Override |
48 public Object blockingLock() { | |
49 return sc.blockingLock(); | |
50 } | |
51 | |
52 @Override | |
53 public SelectableChannel configureBlocking(boolean block) throws IOException { | |
54 return sc.configureBlocking(block); | |
55 } | |
56 | |
57 @Override | |
58 public boolean isBlocking() { | |
59 return sc.isBlocking(); | |
60 } | |
61 | |
62 @Override | |
63 public boolean isRegistered() { | |
64 return sc.isRegistered(); | |
65 } | |
66 | |
67 @Override | |
68 public SelectionKey keyFor(Selector sel) { | |
69 return sc.keyFor(sel); | |
70 } | |
255 | 71 |
72 public SelectionKey keyFor(REPSelector<?> sel) { | |
73 return sc.keyFor(sel.selector); | |
74 } | |
75 | |
76 public REPSelectionKey<P> keyFor1(REPSelector<P> sel) { | |
77 return new REPSelectionKey<P>(sc.keyFor(sel.selector), | |
78 new REPSelector<P>((AbstractSelector) sel.selector)); | |
79 } | |
123 | 80 |
81 @Override | |
82 public SelectorProvider provider() { | |
83 return sc.provider(); | |
84 } | |
85 | |
194 | 86 |
123 | 87 @Override |
88 public int validOps() { | |
89 return sc.validOps(); | |
90 } | |
91 | |
92 @Override | |
93 protected void implCloseChannel() throws IOException { | |
266 | 94 close1(); |
123 | 95 } |
136 | 96 |
266 | 97 public long read(ByteBuffer header) throws IOException { |
98 return sc.read(header); | |
136 | 99 } |
100 | |
101 public void write(ByteBuffer buffer) throws IOException { | |
266 | 102 sc.write(buffer); |
136 | 103 |
104 } | |
105 | |
266 | 106 public boolean finishConnect() throws IOException { |
107 return sc.finishConnect(); | |
136 | 108 } |
137 | 109 |
110 public Socket socket() { | |
266 | 111 return sc.socket(); |
137 | 112 } |
143 | 113 |
147 | 114 public P read() throws IOException{ |
184 | 115 return pack.unpackUConv(sc); |
143 | 116 } |
266 | 117 |
145 | 118 public boolean write(P p){ |
147 | 119 ByteBuffer bb = pack.packUConv(p); |
211 | 120 if (bb==null) return true; |
147 | 121 try { |
122 while (bb.remaining() > 0 ){ | |
123 sc.write(bb); | |
124 } | |
125 return true; | |
126 } catch (IOException e) { | |
127 return false; | |
128 } | |
145 | 129 } |
140 | 130 |
184 | 131 public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException { |
140 | 132 if (REPServerSocketChannel.isSimulation) { |
174 | 133 return new ChannelSimulator<T>(); |
140 | 134 } else { |
184 | 135 REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer); |
136 return rsc; | |
140 | 137 } |
138 } | |
153 | 139 |
194 | 140 |
153 | 141 public boolean connect(SocketAddress semaIP) throws IOException { |
142 return sc.connect(semaIP); | |
143 } | |
156 | 144 |
240 | 145 public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { |
146 return sc.register(sel.selector, ops, att); | |
147 } | |
148 | |
149 | |
234
0498425202a4
made ChannelSimulator no extends SelectableChannelSimulator
kent
parents:
232
diff
changeset
|
150 |
232 | 151 public SelectionKey register1(REPSelector<P> sel, int ops, Object att) |
152 throws ClosedChannelException { | |
153 if(sel instanceof REPSelector) { | |
154 REPSelector<P> s = (REPSelector<P>)sel; | |
155 return sc.register(s.selector, ops,att); | |
156 } | |
157 return sc.register(sel, ops,att); | |
158 } | |
159 | |
160 @SuppressWarnings("unchecked") | |
161 @Override | |
162 public SelectionKey register(Selector sel, int ops, Object att) | |
163 throws ClosedChannelException { | |
164 if(sel instanceof REPSelector) { | |
165 REPSelector<P> s = (REPSelector<P>)sel; | |
166 return sc.register(s.selector, ops,att); | |
167 } | |
168 return sc.register(sel, ops,att); | |
169 } | |
194 | 170 |
123 | 171 } |