annotate src/fdl/FDLindaServ.java @ 24:35375016b2f0 simple-test-passed

cleanup.
author kono
date Wed, 20 Aug 2008 10:18:05 +0900
parents 56e015e8f5dc
children 330fa49bc4fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
1
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
2 package fdl;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
3
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
4 import java.io.IOException;
4
2023d9b31af9 fix parameter
fuchita
parents: 3
diff changeset
5 import java.net.InetAddress;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
6 import java.net.InetSocketAddress;
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
7 import java.nio.channels.ClosedChannelException;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
8 import java.nio.channels.SelectionKey;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
9 import java.nio.channels.ServerSocketChannel;
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
10 import java.nio.channels.spi.AbstractSelector;
3
ae7e0e92c651 *** empty log message ***
fuchita
parents: 0
diff changeset
11 import java.nio.channels.spi.SelectorProvider;
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
12 import java.util.Iterator;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
13
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
14 public class FDLindaServ {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
15 static final int MAX_REQ = 1;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
16 static final int FAIL = (-1);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
17 static final int DEF_PORT = 10000;
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
18 public int port = DEF_PORT;
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
19 private AbstractSelector selector;
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
20 private ServerSocketChannel ssChannel;
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
21 public TupleSpace tupleSpace;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
22
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
23 public static void main(final String[] args) {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
24 final String usages = "usage: FDLindaServ [-p port]";
4
2023d9b31af9 fix parameter
fuchita
parents: 3
diff changeset
25
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
26 int port = DEF_PORT;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
27 //引数判定
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
28 try {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
29 for (int i=0; i<args.length; ++i) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30 if("-p".equals(args[i])) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 port = Integer.parseInt(args[++i]);
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
32 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
33 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
34 } catch (NumberFormatException e) {
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
35 System.err.println(usages);
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
36 return;
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
37 }
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
38 try {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
39 FDLindaServ serv;
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
40 serv = new FDLindaServ(port);
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
41 serv.mainLoop();
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
42 } catch (IOException e) {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
43 System.err.println("Server Communiation Problem.");
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
44 }
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
45 }
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
46
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
47 private void mainLoop() {
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
48 MetaLinda ml = new MetaLinda(tupleSpace, this);
21
fac6e0073b1a *** empty log message ***
kono
parents: 20
diff changeset
49 MetaEngine me = new NullMetaEngine(ml);
fac6e0073b1a *** empty log message ***
kono
parents: 20
diff changeset
50 // MetaEngine me = new MetaEngine(ml);
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
51 while(true) {
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
52 me.mainLoop();
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
53 }
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
54 }
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
55
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
56 public FDLindaServ(int port) throws IOException {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
57 this.port = port;
19
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 selector = SelectorProvider.provider().openSelector();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
60 //ソケット・チャネルを生成・設定
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
61 ssChannel = SelectorProvider.provider().openServerSocketChannel();
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
62 // getAllByName で、すべて取って、その上のすべてでselectする必要がある。
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
63 InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port);
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
64 ssChannel.socket().setReuseAddress(true);
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
65 ssChannel.socket().bind(address);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
66 ssChannel.configureBlocking(false);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
67 System.out.println("Server: litening at "+ssChannel);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
68 //セレクタにチャンネルを登録
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
69 tupleSpace = new TupleSpace();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
70 ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tupleSpace));
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
71
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
72
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
73 }
3
ae7e0e92c651 *** empty log message ***
fuchita
parents: 0
diff changeset
74
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
75 public void checkTuple() {
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
76 checkTuple(0);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
77 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
78
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
79 public void checkTuple(long timeout) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
80 // セレクタによる監視
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
81 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
82 if (selector.select(timeout)>0) {
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
83 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) {
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
84 SelectionKey s = it.next();
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
85 it.remove();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
86 TupleHandler handler = (TupleHandler)s.attachment();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
87 handler.handle(s);
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 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
90 } catch (ClosedChannelException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
91 // we have to do something...
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
92 } catch (IOException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
93 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
94 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
95 }