Mercurial > hg > FederatedLinda
annotate src/fdl/FDLindaServ.java @ 19:0243987383b7
Meta Protocol Engine and sample implementation of event logger.
ComDebug_Client needs fixes.
author | kono |
---|---|
date | Tue, 19 Aug 2008 05:33:32 +0900 |
parents | 609b288f47f9 |
children | a0fd653d1121 |
rev | line source |
---|---|
0 | 1 |
2 package fdl; | |
3 | |
4 import java.io.IOException; | |
4 | 5 import java.net.InetAddress; |
0 | 6 import java.net.InetSocketAddress; |
15 | 7 import java.nio.channels.ClosedChannelException; |
0 | 8 import java.nio.channels.SelectionKey; |
9 import java.nio.channels.ServerSocketChannel; | |
15 | 10 import java.nio.channels.spi.AbstractSelector; |
3 | 11 import java.nio.channels.spi.SelectorProvider; |
0 | 12 |
17 | 13 public class FDLindaServ { |
0 | 14 static final int MAX_REQ = 1; |
15 static final int FAIL = (-1); | |
16 static final int DEF_PORT = 10000; | |
4 | 17 //public static final int TIMEOUT = 5*1000; |
15 | 18 public int port = DEF_PORT; |
19 private AbstractSelector selector; | |
20 private ServerSocketChannel ssChannel; | |
0 | 21 |
15 | 22 public static void main(final String[] args) { |
0 | 23 final String usages = "usage: FDLindaServ [-p port]"; |
4 | 24 //バイトオーダー確認 |
25 //System.out.println(ByteOrder.nativeOrder().toString()); | |
26 | |
15 | 27 int port = DEF_PORT; |
0 | 28 //引数判定 |
29 try { | |
30 for (int i=0; i<args.length; ++i) { | |
31 if("-p".equals(args[i])) { | |
32 port = Integer.parseInt(args[++i]); | |
15 | 33 } |
0 | 34 } |
35 } catch (NumberFormatException e) { | |
15 | 36 System.err.println(usages); |
37 return; | |
38 } | |
39 try { | |
40 FDLindaServ serv; | |
41 serv = new FDLindaServ(port); | |
42 serv.mainLoop(); | |
43 } catch (IOException e) { | |
44 System.err.println("Server Communiation Problem."); | |
0 | 45 } |
15 | 46 } |
47 | |
48 private void mainLoop() { | |
49 while(true) { | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
50 checkTuple(); |
15 | 51 } |
52 } | |
53 | |
54 public FDLindaServ(int port) throws IOException { | |
55 this.port = port; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
56 //セレクタを生成 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
57 selector = SelectorProvider.provider().openSelector(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
58 //ソケット・チャネルを生成・設定 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
59 ssChannel = SelectorProvider.provider().openServerSocketChannel(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
60 InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
61 ssChannel.socket().bind(address); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
62 ssChannel.configureBlocking(false); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
63 //ssChannel.socket().setReuseAddress(true); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
64 System.out.println("Server: litening at "+ssChannel); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
65 //セレクタにチャンネルを登録 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
66 TupleSpace tupleSpace = new TupleSpace(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
67 ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tupleSpace)); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
68 |
15 | 69 |
70 } | |
3 | 71 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
72 public void checkTuple() { |
15 | 73 // セレクタによる監視 |
74 try { | |
75 if (selector.select()>0) { | |
76 for(SelectionKey s:selector.selectedKeys()) { | |
77 TupleHandler handler = (TupleHandler)s.attachment(); | |
78 handler.handle(s); | |
79 } | |
80 } | |
81 } catch (ClosedChannelException e) { | |
82 // we have to do something... | |
83 } catch (IOException e) { | |
84 } | |
0 | 85 } |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
86 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
87 public void checkTuple(long timeout) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
88 // セレクタによる監視 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
89 try { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
90 if (selector.select(timeout)>0) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
91 for(SelectionKey s:selector.selectedKeys()) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
92 TupleHandler handler = (TupleHandler)s.attachment(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
93 handler.handle(s); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
94 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
95 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
96 } catch (ClosedChannelException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
97 // we have to do something... |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
98 } catch (IOException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
99 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
100 } |
0 | 101 } |