Mercurial > hg > FederatedLinda
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 |
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 |
17 | 14 public class FDLindaServ { |
0 | 15 static final int MAX_REQ = 1; |
16 static final int FAIL = (-1); | |
17 static final int DEF_PORT = 10000; | |
15 | 18 public int port = DEF_PORT; |
19 private AbstractSelector selector; | |
20 private ServerSocketChannel ssChannel; | |
20 | 21 public TupleSpace tupleSpace; |
0 | 22 |
15 | 23 public static void main(final String[] args) { |
0 | 24 final String usages = "usage: FDLindaServ [-p port]"; |
4 | 25 |
15 | 26 int port = DEF_PORT; |
0 | 27 //引数判定 |
28 try { | |
29 for (int i=0; i<args.length; ++i) { | |
30 if("-p".equals(args[i])) { | |
31 port = Integer.parseInt(args[++i]); | |
15 | 32 } |
0 | 33 } |
34 } catch (NumberFormatException e) { | |
15 | 35 System.err.println(usages); |
36 return; | |
37 } | |
38 try { | |
39 FDLindaServ serv; | |
40 serv = new FDLindaServ(port); | |
41 serv.mainLoop(); | |
42 } catch (IOException e) { | |
43 System.err.println("Server Communiation Problem."); | |
0 | 44 } |
15 | 45 } |
46 | |
47 private void mainLoop() { | |
20 | 48 MetaLinda ml = new MetaLinda(tupleSpace, this); |
21 | 49 MetaEngine me = new NullMetaEngine(ml); |
50 // MetaEngine me = new MetaEngine(ml); | |
15 | 51 while(true) { |
20 | 52 me.mainLoop(); |
15 | 53 } |
54 } | |
55 | |
56 public FDLindaServ(int port) throws IOException { | |
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 | 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 | 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 | 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 | 72 |
73 } | |
3 | 74 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
75 public void checkTuple() { |
22 | 76 checkTuple(0); |
0 | 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 | 83 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { |
84 SelectionKey s = it.next(); | |
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 | 95 } |