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