Mercurial > hg > FederatedLinda
annotate src/fdl/FDLindaServ.java @ 65:cc860e8beb8f
merge
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 26 Feb 2009 08:46:29 +0900 |
parents | e56d1f7af5ce b342dc9b52eb |
children | 82a292aa41ad |
rev | line source |
---|---|
0 | 1 |
2 package fdl; | |
3 | |
4 import java.io.IOException; | |
5 import java.net.InetSocketAddress; | |
45 | 6 import java.net.SocketException; |
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; |
40 | 13 import java.util.logging.Level; |
0 | 14 |
63 | 15 import fdl.test.transfer.cluster.MetaProtocolEngine; |
53 | 16 |
25 | 17 /** |
18 * @author kono | |
19 * | |
20 */ | |
17 | 21 public class FDLindaServ { |
0 | 22 static final int MAX_REQ = 1; |
23 static final int FAIL = (-1); | |
24 static final int DEF_PORT = 10000; | |
15 | 25 public int port = DEF_PORT; |
39 | 26 AbstractSelector selector; |
15 | 27 private ServerSocketChannel ssChannel; |
20 | 28 public TupleSpace tupleSpace; |
26 | 29 public MetaEngine me; |
0 | 30 |
15 | 31 public static void main(final String[] args) { |
0 | 32 final String usages = "usage: FDLindaServ [-p port]"; |
4 | 33 |
15 | 34 int port = DEF_PORT; |
0 | 35 //引数判定 |
36 try { | |
37 for (int i=0; i<args.length; ++i) { | |
38 if("-p".equals(args[i])) { | |
39 port = Integer.parseInt(args[++i]); | |
15 | 40 } |
0 | 41 } |
42 } catch (NumberFormatException e) { | |
15 | 43 System.err.println(usages); |
44 return; | |
45 } | |
46 try { | |
47 FDLindaServ serv; | |
48 serv = new FDLindaServ(port); | |
49 serv.mainLoop(); | |
50 } catch (IOException e) { | |
51 System.err.println("Server Communiation Problem."); | |
0 | 52 } |
15 | 53 } |
54 | |
53 | 55 public void mainLoop() { |
20 | 56 MetaLinda ml = new MetaLinda(tupleSpace, this); |
53 | 57 me = new NullMetaEngine(ml); |
58 //me = new MetaLogEngine(ml); | |
26 | 59 me.mainLoop(); |
15 | 60 } |
61 | |
62 public FDLindaServ(int port) throws IOException { | |
63 this.port = port; | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
64 //セレクタを生成 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
65 selector = SelectorProvider.provider().openSelector(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
66 //ソケット・チャネルを生成・設定 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
67 ssChannel = SelectorProvider.provider().openServerSocketChannel(); |
22 | 68 ssChannel.socket().setReuseAddress(true); |
45 | 69 // this should work for IPv6/IPv4 dual stack |
70 // check this using netstat -an result tcp46. | |
71 try { | |
72 InetSocketAddress address = new InetSocketAddress("::", port); | |
73 ssChannel.socket().bind(address); | |
74 } catch (SocketException e) { | |
75 // for some bad IPv6 implementation | |
76 ssChannel.socket().bind(new InetSocketAddress(port)); | |
77 } | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
78 ssChannel.configureBlocking(false); |
40 | 79 this.log(Level.INFO,"Server: litening at "+ssChannel); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
80 //セレクタにチャンネルを登録 |
40 | 81 tupleSpace = new TupleSpace(this); |
82 ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(this, ssChannel,tupleSpace)); | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
83 |
15 | 84 |
85 } | |
3 | 86 |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
87 public void checkTuple() { |
22 | 88 checkTuple(0); |
0 | 89 } |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
90 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
91 public void checkTuple(long timeout) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
92 // セレクタによる監視 |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
93 try { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
94 if (selector.select(timeout)>0) { |
30 | 95 // this does not work because #it.remove() is not called. |
96 // for(SelectionKey s:selector.selectedKeys()) { | |
97 // TupleHandler handler = (TupleHandler)s.attachment(); | |
98 // handler.handle(s); | |
99 // } | |
22 | 100 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { |
101 SelectionKey s = it.next(); | |
102 it.remove(); | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
103 TupleHandler handler = (TupleHandler)s.attachment(); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
104 handler.handle(s); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
105 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
106 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
107 } catch (ClosedChannelException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
108 // we have to do something... |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
109 } catch (IOException e) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
110 } |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
111 } |
40 | 112 |
113 | |
114 public void log(Level level,String msg) { | |
115 System.err.println(msg); | |
116 if (level==Level.SEVERE) | |
117 new IOException().setStackTrace(null); | |
118 } | |
0 | 119 } |