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 }
|
|
80
|
|
81 @Override
|
|
82 public int validOps() {
|
|
83 return sc.validOps();
|
|
84 }
|
|
85
|
|
86 @Override
|
|
87 protected void implCloseChannel() throws IOException {
|
|
88 sc.close();
|
|
89 }
|
136
|
90
|
|
91 public long read(ByteBuffer header) {
|
|
92 // TODO Auto-generated method stub
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 public void write(ByteBuffer buffer) throws IOException {
|
|
97 // TODO Auto-generated method stub
|
|
98
|
|
99 }
|
|
100
|
|
101 public boolean finishConnect() {
|
|
102 // TODO Auto-generated method stub
|
|
103 return false;
|
|
104 }
|
137
|
105
|
|
106 public Socket socket() {
|
|
107 // TODO Auto-generated method stub
|
|
108 return null;
|
|
109 }
|
143
|
110
|
147
|
111 public P read() throws IOException{
|
184
|
112 return pack.unpackUConv(sc);
|
143
|
113 }
|
145
|
114 public boolean write(P p){
|
147
|
115 ByteBuffer bb = pack.packUConv(p);
|
|
116 try {
|
|
117 while (bb.remaining() > 0 ){
|
|
118 sc.write(bb);
|
|
119 }
|
|
120 return true;
|
|
121 } catch (IOException e) {
|
|
122 return false;
|
|
123 }
|
145
|
124 }
|
140
|
125
|
184
|
126 public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException {
|
140
|
127 if (REPServerSocketChannel.isSimulation) {
|
174
|
128 return new ChannelSimulator<T>();
|
140
|
129 } else {
|
184
|
130 REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer);
|
|
131 return rsc;
|
140
|
132 }
|
|
133 }
|
153
|
134
|
194
|
135
|
153
|
136 public boolean connect(SocketAddress semaIP) throws IOException {
|
|
137 return sc.connect(semaIP);
|
|
138 }
|
156
|
139
|
194
|
140 @Override
|
|
141 public SelectionKey register(Selector sel, int ops, Object att)
|
|
142 throws ClosedChannelException {
|
|
143 assert(false);
|
|
144 return null;
|
|
145 }
|
|
146
|
123
|
147
|
|
148
|
|
149 } |