193
|
1 package rep.channel;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.ServerSocket;
|
|
5 import java.nio.channels.ClosedChannelException;
|
|
6 import java.nio.channels.SelectableChannel;
|
|
7 import java.nio.channels.SelectionKey;
|
|
8 import java.nio.channels.Selector;
|
|
9 import java.nio.channels.ServerSocketChannel;
|
|
10 import java.nio.channels.SocketChannel;
|
|
11 import java.nio.channels.spi.SelectorProvider;
|
|
12
|
|
13 /*
|
|
14 * シミュレーションでは inheritance のServerChannelSimulator を生成、
|
|
15 * リアルコミュニケーションでは 自身を生成、内部にもつ ServerSocketChannelを扱う
|
|
16 */
|
|
17 public class REPServerSocketChannel<P> extends SelectableChannel {
|
|
18
|
|
19 public static boolean isSimulation=false;
|
|
20 private ServerSocketChannel ssc;
|
|
21 private REPPack<P> packer;
|
|
22
|
|
23 public REPServerSocketChannel() {
|
|
24
|
|
25 }
|
|
26
|
|
27 public static <T> REPServerSocketChannel<T> open(REPPack<T> packer) throws IOException{
|
|
28 if(isSimulation){
|
|
29 return new ServerChannelSimulator<T>();
|
|
30 }else{
|
|
31 return new REPServerSocketChannel<T>(ServerSocketChannel.open(), packer);
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public static <T> REPServerSocketChannel<T> open(SelectableChannel c,REPPack<T> packer) throws IOException{
|
|
36 assert(!isSimulation);
|
|
37 return new REPServerSocketChannel<T>((ServerSocketChannel)c, packer);
|
|
38 }
|
|
39
|
|
40 public REPServerSocketChannel(ServerSocketChannel open, REPPack<P> packer) {
|
|
41 ssc = open;
|
|
42 this.packer = packer;
|
|
43 REPSocketChannel.addChannel(ssc,this);
|
|
44 }
|
|
45
|
|
46 public void close1() throws IOException {
|
|
47 REPSocketChannel.removeChannel(ssc);
|
|
48 ssc.close();
|
|
49 }
|
|
50
|
|
51 public REPServerSocketChannel(SelectableChannel channel, REPPack<P> packer) {
|
|
52 this.ssc = (ServerSocketChannel)channel;
|
|
53 this.packer = packer;
|
|
54 REPSocketChannel.addChannel(ssc,this);
|
|
55 }
|
|
56
|
|
57 public REPSocketChannel<P> accept1() throws IOException {
|
|
58 return new REPSocketChannel<P>(ssc.accept(), packer);
|
|
59 }
|
|
60
|
|
61 public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
|
|
62 assert(!isSimulation);
|
|
63 if(sel!=null)
|
|
64 return sel.register(ssc, ops, att);
|
|
65 else
|
|
66 return null;
|
|
67 }
|
|
68
|
|
69 public SocketChannel accept() throws IOException {
|
|
70 return accept1().sc;
|
|
71 }
|
|
72
|
|
73
|
|
74 public ServerSocket socket() {
|
|
75 return ssc.socket();
|
|
76 }
|
|
77
|
|
78 public SelectableChannel configureBlocking(boolean block) throws IOException
|
|
79 {
|
|
80 ssc.configureBlocking(block);
|
|
81 return this;
|
|
82 }
|
|
83
|
|
84 @Override
|
|
85 public Object blockingLock() {
|
|
86 // TODO Auto-generated method stub
|
|
87 return null;
|
|
88 }
|
|
89
|
|
90 @Override
|
|
91 public boolean isBlocking() {
|
|
92 // TODO Auto-generated method stub
|
|
93 return false;
|
|
94 }
|
|
95
|
|
96 @Override
|
|
97 public boolean isRegistered() {
|
|
98 // TODO Auto-generated method stub
|
|
99 return false;
|
|
100 }
|
|
101
|
|
102 @Override
|
|
103 public SelectionKey keyFor(Selector sel) {
|
|
104 // TODO Auto-generated method stub
|
|
105 return null;
|
|
106 }
|
|
107
|
|
108 @Override
|
|
109 public SelectorProvider provider() {
|
|
110 // TODO Auto-generated method stub
|
|
111 return null;
|
|
112 }
|
|
113
|
|
114 @Override
|
|
115 public SelectionKey register(Selector sel, int ops, Object att)
|
|
116 throws ClosedChannelException {
|
|
117 // TODO Auto-generated method stub
|
|
118 return null;
|
|
119 }
|
|
120
|
|
121 @Override
|
|
122 public int validOps() {
|
|
123 // TODO Auto-generated method stub
|
|
124 return 0;
|
|
125 }
|
|
126
|
|
127 @Override
|
|
128 protected void implCloseChannel() throws IOException {
|
|
129 // TODO Auto-generated method stub
|
|
130
|
|
131 }
|
|
132
|
|
133
|
|
134 }
|