Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/channel/REPSocketChannel.java @ 241:1e5bf172b4d6
*** empty log message ***
author | kono |
---|---|
date | Sun, 31 Aug 2008 20:41:34 +0900 |
parents | 168dd841be51 |
children | cc119b4b1e96 |
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; | |
12 import java.nio.channels.spi.SelectorProvider; | |
208 | 13 import java.util.HashMap; |
123 | 14 |
15 public class REPSocketChannel<P> extends SelectableChannel{ | |
16 | |
194 | 17 public SocketChannel sc; |
147 | 18 private REPPack<P> pack; |
208 | 19 static public HashMap<SelectableChannel,SelectableChannel> channels = new HashMap<SelectableChannel,SelectableChannel>(); |
123 | 20 |
184 | 21 public REPSocketChannel(SocketChannel channel, REPPack<P> packer) { |
123 | 22 sc = channel; |
184 | 23 pack = packer; |
208 | 24 addChannel(sc,this); |
123 | 25 } |
26 | |
194 | 27 public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) { |
28 sc = (SocketChannel)channel; | |
29 pack = packer; | |
208 | 30 addChannel(sc,this); |
31 } | |
32 | |
33 public static void addChannel(SelectableChannel sc,SelectableChannel rc) { | |
34 channels.put(sc, rc); | |
241 | 35 //System.err.println("add Channel "+channels); |
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 } | |
71 | |
72 @Override | |
73 public SelectorProvider provider() { | |
74 return sc.provider(); | |
75 } | |
76 | |
194 | 77 |
123 | 78 @Override |
79 public int validOps() { | |
80 return sc.validOps(); | |
81 } | |
82 | |
83 @Override | |
84 protected void implCloseChannel() throws IOException { | |
85 sc.close(); | |
86 } | |
136 | 87 |
88 public long read(ByteBuffer header) { | |
89 // TODO Auto-generated method stub | |
90 return 0; | |
91 } | |
92 | |
93 public void write(ByteBuffer buffer) throws IOException { | |
94 // TODO Auto-generated method stub | |
95 | |
96 } | |
97 | |
98 public boolean finishConnect() { | |
99 // TODO Auto-generated method stub | |
100 return false; | |
101 } | |
137 | 102 |
103 public Socket socket() { | |
104 // TODO Auto-generated method stub | |
105 return null; | |
106 } | |
143 | 107 |
147 | 108 public P read() throws IOException{ |
184 | 109 return pack.unpackUConv(sc); |
143 | 110 } |
145 | 111 public boolean write(P p){ |
147 | 112 ByteBuffer bb = pack.packUConv(p); |
211 | 113 if (bb==null) return true; |
147 | 114 try { |
115 while (bb.remaining() > 0 ){ | |
116 sc.write(bb); | |
117 } | |
118 return true; | |
119 } catch (IOException e) { | |
120 return false; | |
121 } | |
145 | 122 } |
140 | 123 |
184 | 124 public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException { |
140 | 125 if (REPServerSocketChannel.isSimulation) { |
174 | 126 return new ChannelSimulator<T>(); |
140 | 127 } else { |
184 | 128 REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer); |
129 return rsc; | |
140 | 130 } |
131 } | |
153 | 132 |
194 | 133 |
153 | 134 public boolean connect(SocketAddress semaIP) throws IOException { |
135 return sc.connect(semaIP); | |
136 } | |
156 | 137 |
240 | 138 public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { |
139 return sc.register(sel.selector, ops, att); | |
140 } | |
141 | |
142 | |
234
0498425202a4
made ChannelSimulator no extends SelectableChannelSimulator
kent
parents:
232
diff
changeset
|
143 |
232 | 144 public SelectionKey register1(REPSelector<P> sel, int ops, Object att) |
145 throws ClosedChannelException { | |
146 if(sel instanceof REPSelector) { | |
147 REPSelector<P> s = (REPSelector<P>)sel; | |
148 return sc.register(s.selector, ops,att); | |
149 } | |
150 return sc.register(sel, ops,att); | |
151 } | |
152 | |
153 @SuppressWarnings("unchecked") | |
154 @Override | |
155 public SelectionKey register(Selector sel, int ops, Object att) | |
156 throws ClosedChannelException { | |
157 if(sel instanceof REPSelector) { | |
158 REPSelector<P> s = (REPSelector<P>)sel; | |
159 return sc.register(s.selector, ops,att); | |
160 } | |
161 return sc.register(sel, ops,att); | |
162 } | |
194 | 163 |
123 | 164 } |