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;
|
|
13
|
|
14 public class REPSocketChannel<P> extends SelectableChannel{
|
|
15
|
|
16 private SocketChannel sc;
|
147
|
17 private REPPack<P> pack;
|
123
|
18
|
184
|
19 public REPSocketChannel(SocketChannel channel, REPPack<P> packer) {
|
123
|
20 sc = channel;
|
184
|
21 pack = packer;
|
123
|
22 }
|
|
23
|
|
24 @Override
|
|
25 public Object blockingLock() {
|
|
26 return sc.blockingLock();
|
|
27 }
|
|
28
|
|
29 @Override
|
|
30 public SelectableChannel configureBlocking(boolean block) throws IOException {
|
|
31 return sc.configureBlocking(block);
|
|
32 }
|
|
33
|
|
34 @Override
|
|
35 public boolean isBlocking() {
|
|
36 return sc.isBlocking();
|
|
37 }
|
|
38
|
|
39 @Override
|
|
40 public boolean isRegistered() {
|
|
41 return sc.isRegistered();
|
|
42 }
|
|
43
|
|
44 @Override
|
|
45 public SelectionKey keyFor(Selector sel) {
|
|
46 return sc.keyFor(sel);
|
|
47 }
|
|
48
|
|
49 @Override
|
|
50 public SelectorProvider provider() {
|
|
51 return sc.provider();
|
|
52 }
|
|
53
|
|
54 @Override
|
|
55 public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException {
|
|
56 return sc.register(sel, ops, att);
|
|
57 }
|
|
58
|
|
59 @Override
|
|
60 public int validOps() {
|
|
61 return sc.validOps();
|
|
62 }
|
|
63
|
|
64 @Override
|
|
65 protected void implCloseChannel() throws IOException {
|
|
66 sc.close();
|
|
67 }
|
136
|
68
|
|
69 public long read(ByteBuffer header) {
|
|
70 // TODO Auto-generated method stub
|
|
71 return 0;
|
|
72 }
|
|
73
|
|
74 public void write(ByteBuffer buffer) throws IOException {
|
|
75 // TODO Auto-generated method stub
|
|
76
|
|
77 }
|
|
78
|
|
79 public boolean finishConnect() {
|
|
80 // TODO Auto-generated method stub
|
|
81 return false;
|
|
82 }
|
137
|
83
|
|
84 public Socket socket() {
|
|
85 // TODO Auto-generated method stub
|
|
86 return null;
|
|
87 }
|
143
|
88
|
147
|
89 public P read() throws IOException{
|
184
|
90 return pack.unpackUConv(sc);
|
143
|
91 }
|
145
|
92 public boolean write(P p){
|
147
|
93 ByteBuffer bb = pack.packUConv(p);
|
|
94 try {
|
|
95 while (bb.remaining() > 0 ){
|
|
96 sc.write(bb);
|
|
97 }
|
|
98 return true;
|
|
99 } catch (IOException e) {
|
|
100 return false;
|
|
101 }
|
145
|
102 }
|
140
|
103
|
184
|
104 public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException {
|
140
|
105 if (REPServerSocketChannel.isSimulation) {
|
174
|
106 return new ChannelSimulator<T>();
|
140
|
107 } else {
|
184
|
108 REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer);
|
|
109 return rsc;
|
140
|
110 }
|
|
111 }
|
153
|
112
|
|
113 public boolean connect(SocketAddress semaIP) throws IOException {
|
|
114 return sc.connect(semaIP);
|
|
115 }
|
156
|
116
|
123
|
117
|
|
118
|
|
119 } |