comparison src/fdl/FDLindaServ.java @ 4:2023d9b31af9

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