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