Mercurial > hg > FederatedLinda
annotate src/fdl/FDLindaServ.java @ 35:fe338d497c72 meta-comdebug-wokred
FederatedLinda was static singleton. It does not work on Thread based test.
author | kono |
---|---|
date | Sun, 24 Aug 2008 19:07:28 +0900 |
parents | fca6eec8016f |
children | 81abceebc869 |
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; |
22 | 12 import java.util.Iterator; |
0 | 13 |
25 | 14 /** |
15 * @author kono | |
16 * | |
17 */ | |
17 | 18 public class FDLindaServ { |
0 | 19 static final int MAX_REQ = 1; |
20 static final int FAIL = (-1); | |
21 static final int DEF_PORT = 10000; | |
15 | 22 public int port = DEF_PORT; |
23 private AbstractSelector selector; | |
24 private ServerSocketChannel ssChannel; | |
20 | 25 public TupleSpace tupleSpace; |
26 | 26 public MetaEngine me; |
0 | 27 |
15 | 28 public static void main(final String[] args) { |
0 | 29 final String usages = "usage: FDLindaServ [-p port]"; |
4 | 30 |
15 | 31 int port = DEF_PORT; |
0 | 32 //引数判定 |
33 try { | |
34 for (int i=0; i<args.length; ++i) { | |
35 if("-p".equals(args[i])) { | |
36 port = Integer.parseInt(args[++i]); | |
15 | 37 } |
0 | 38 } |
39 } catch (NumberFormatException e) { | |
15 | 40 System.err.println(usages); |
41 return; | |
42 } | |
43 try { | |
44 FDLindaServ serv; | |
45 serv = new FDLindaServ(port); | |
46 serv.mainLoop(); | |
47 } catch (IOException e) { | |
48 System.err.println("Server Communiation Problem."); | |
0 | 49 } |
15 | 50 } |
51 | |
52 private void mainLoop() { | |
20 | 53 MetaLinda ml = new MetaLinda(tupleSpace, this); |
26 | 54 // me = new NullMetaEngine(ml); |
55 me = new MetaLogEngine(ml); | |
56 me.mainLoop(); | |
15 | 57 } |
58 | |
59 public FDLindaServ(int port) throws IOException { | |
60 this.port = port; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
61 //セレクタを生成 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
62 selector = SelectorProvider.provider().openSelector(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
63 //ソケット・チャネルを生成・設定 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
64 ssChannel = SelectorProvider.provider().openServerSocketChannel(); |
22 | 65 // getAllByName で、すべて取って、その上のすべてでselectする必要がある。 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
66 InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port); |
22 | 67 ssChannel.socket().setReuseAddress(true); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
68 ssChannel.socket().bind(address); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
69 ssChannel.configureBlocking(false); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
70 System.out.println("Server: litening at "+ssChannel); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
71 //セレクタにチャンネルを登録 |
20 | 72 tupleSpace = new TupleSpace(); |
35
fe338d497c72
FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents:
30
diff
changeset
|
73 ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(ssChannel,tupleSpace)); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
74 |
15 | 75 |
76 } | |
3 | 77 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
78 public void checkTuple() { |
22 | 79 checkTuple(0); |
0 | 80 } |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
81 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
82 public void checkTuple(long timeout) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
83 // セレクタによる監視 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
84 try { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
85 if (selector.select(timeout)>0) { |
30 | 86 // this does not work because #it.remove() is not called. |
87 // for(SelectionKey s:selector.selectedKeys()) { | |
88 // TupleHandler handler = (TupleHandler)s.attachment(); | |
89 // handler.handle(s); | |
90 // } | |
22 | 91 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { |
92 SelectionKey s = it.next(); | |
93 it.remove(); | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
94 TupleHandler handler = (TupleHandler)s.attachment(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
95 handler.handle(s); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
96 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
97 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
98 } catch (ClosedChannelException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
99 // we have to do something... |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
100 } catch (IOException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
101 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
102 } |
0 | 103 } |