0
|
1
|
|
2 package fdl;
|
|
3
|
|
4 import java.io.IOException;
|
4
|
5 import java.net.InetAddress;
|
0
|
6 import java.net.InetSocketAddress;
|
4
|
7 //import java.nio.ByteOrder;
|
0
|
8 import java.nio.channels.SelectionKey;
|
|
9 import java.nio.channels.Selector;
|
|
10 import java.nio.channels.ServerSocketChannel;
|
3
|
11 import java.nio.channels.spi.SelectorProvider;
|
0
|
12 import java.util.Iterator;
|
|
13
|
|
14 public class FDLindaServ implements PSXQueueInterface {
|
|
15 static final int MAX_REQ = 1;
|
|
16 static final int FAIL = (-1);
|
|
17 static final int MAX_UAER = 4;
|
|
18 static final int MAX_TUPLE = 65536;
|
|
19 static final int DEF_PORT = 10000;
|
4
|
20 //public static final int TIMEOUT = 5*1000;
|
0
|
21 public static Tuple[] tuple_space;
|
|
22
|
3
|
23 @SuppressWarnings("unchecked")
|
0
|
24 public static void main(final String[] args) throws IOException {
|
|
25 @SuppressWarnings("unused")
|
|
26 final String usages = "usage: FDLindaServ [-p port]";
|
|
27 int port = DEF_PORT;
|
4
|
28 //バイトオーダー確認
|
|
29 //System.out.println(ByteOrder.nativeOrder().toString());
|
|
30
|
0
|
31 tuple_space = new Tuple[MAX_TUPLE];
|
4
|
32
|
0
|
33 //引数判定
|
|
34 try {
|
|
35 for (int i=0; i<args.length; ++i) {
|
|
36 if("-p".equals(args[i])) {
|
|
37 port = Integer.parseInt(args[++i]);
|
|
38 } else {
|
|
39 System.err.println(usages);
|
|
40 }
|
|
41 }
|
|
42 } catch (NumberFormatException e) {
|
|
43 e.printStackTrace();
|
|
44 }
|
4
|
45 //セレクタを生成
|
|
46 Selector selector = SelectorProvider.provider().openSelector();
|
|
47 try {
|
0
|
48 //ソケット・チャネルを生成・設定
|
3
|
49 ServerSocketChannel ssChannel = SelectorProvider.provider().openServerSocketChannel();
|
4
|
50 InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port);
|
|
51 //ssChannel.socket().bind(new InetSocketAddress(port));
|
|
52 ssChannel.socket().bind(address);
|
0
|
53 ssChannel.configureBlocking(false);
|
4
|
54 //ssChannel.socket().setReuseAddress(true);
|
0
|
55 System.out.println("Server: litening at "+ssChannel);
|
|
56 //セレクタにチャンネルを登録
|
3
|
57 //ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tuple_space));
|
|
58 //ssChannel.register(selector, ssChannel.validOps(), new AcceptHandler(tuple_space));
|
|
59 ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tuple_space));
|
0
|
60
|
|
61 // セレクタによる監視
|
3
|
62 while (selector.keys().size() > 0) {
|
|
63 @SuppressWarnings("unused")
|
4
|
64 int KeyCount = selector.select();
|
0
|
65 // Iteratorを用意
|
3
|
66 Iterator it = selector.selectedKeys().iterator();
|
0
|
67 while (it.hasNext()) {
|
|
68 // SelectionKeyを取り出す
|
|
69 SelectionKey selKey = (SelectionKey)it.next();
|
|
70
|
|
71 // 操作に対する処理が行われていると認識させるためにremoveする
|
|
72 it.remove();
|
3
|
73
|
0
|
74 TupleHandler handler = (TupleHandler)selKey.attachment();
|
|
75 handler.handle(selKey);
|
|
76 }
|
|
77
|
|
78 }
|
|
79 } catch (IOException exc) {
|
|
80 exc.printStackTrace();
|
4
|
81 } /*finally {
|
0
|
82 try {
|
|
83 for (SelectionKey key: selector.keys()) {
|
|
84 key.channel().close();
|
|
85 }
|
|
86 } catch(IOException ex) {
|
|
87 ex.printStackTrace();
|
4
|
88 }*/
|
|
89 //}
|
0
|
90 }
|
|
91 }
|