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