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
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;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
5 import java.net.InetSocketAddress;
45
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
6 import java.net.SocketException;
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;
40
046feb56a196 message logging
kono
parents: 39
diff changeset
13 import java.util.logging.Level;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
14
63
b342dc9b52eb add document
axmo
parents: 53
diff changeset
15 import fdl.test.transfer.cluster.MetaProtocolEngine;
53
629b6cfbd37f metaprotocol ring
axmo
parents: 45
diff changeset
16
25
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
17 /**
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
18 * @author kono
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
19 *
330fa49bc4fd *** empty log message ***
kono
parents: 24
diff changeset
20 */
17
609b288f47f9 *** empty log message ***
kono
parents: 15
diff changeset
21 public class FDLindaServ {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
22 static final int MAX_REQ = 1;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
23 static final int FAIL = (-1);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
24 static final int DEF_PORT = 10000;
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
25 public int port = DEF_PORT;
39
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
26 AbstractSelector selector;
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
27 private ServerSocketChannel ssChannel;
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
28 public TupleSpace tupleSpace;
26
d7d70edc9c7c META_STOP worked.
kono
parents: 25
diff changeset
29 public MetaEngine me;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
31 public static void main(final String[] args) {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
32 final String usages = "usage: FDLindaServ [-p port]";
4
2023d9b31af9 fix parameter
fuchita
parents: 3
diff changeset
33
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
34 int port = DEF_PORT;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
35 //引数判定
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
36 try {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
37 for (int i=0; i<args.length; ++i) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
38 if("-p".equals(args[i])) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
39 port = Integer.parseInt(args[++i]);
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
40 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
41 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
42 } catch (NumberFormatException e) {
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
43 System.err.println(usages);
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
44 return;
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 try {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
47 FDLindaServ serv;
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
48 serv = new FDLindaServ(port);
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
49 serv.mainLoop();
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
50 } catch (IOException e) {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
51 System.err.println("Server Communiation Problem.");
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
52 }
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
53
629b6cfbd37f metaprotocol ring
axmo
parents: 45
diff changeset
55 public void mainLoop() {
20
a0fd653d1121 Debug Client and Meta Engine for logging.
kono
parents: 19
diff changeset
56 MetaLinda ml = new MetaLinda(tupleSpace, this);
53
629b6cfbd37f metaprotocol ring
axmo
parents: 45
diff changeset
57 me = new NullMetaEngine(ml);
629b6cfbd37f metaprotocol ring
axmo
parents: 45
diff changeset
58 //me = new MetaLogEngine(ml);
26
d7d70edc9c7c META_STOP worked.
kono
parents: 25
diff changeset
59 me.mainLoop();
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
60 }
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
61
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
62 public FDLindaServ(int port) throws IOException {
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
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
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
68 ssChannel.socket().setReuseAddress(true);
45
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
69 // this should work for IPv6/IPv4 dual stack
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
70 // check this using netstat -an result tcp46.
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
71 try {
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
72 InetSocketAddress address = new InetSocketAddress("::", port);
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
73 ssChannel.socket().bind(address);
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
74 } catch (SocketException e) {
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
75 // for some bad IPv6 implementation
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
76 ssChannel.socket().bind(new InetSocketAddress(port));
9a9414141346 *** empty log message ***
kono
parents: 44
diff changeset
77 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
78 ssChannel.configureBlocking(false);
40
046feb56a196 message logging
kono
parents: 39
diff changeset
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
046feb56a196 message logging
kono
parents: 39
diff changeset
81 tupleSpace = new TupleSpace(this);
046feb56a196 message logging
kono
parents: 39
diff changeset
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
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
84
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 4
diff changeset
85 }
3
ae7e0e92c651 *** empty log message ***
fuchita
parents: 0
diff changeset
86
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
87 public void checkTuple() {
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
88 checkTuple(0);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
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
fca6eec8016f *** empty log message ***
kono
parents: 26
diff changeset
95 // this does not work because #it.remove() is not called.
fca6eec8016f *** empty log message ***
kono
parents: 26
diff changeset
96 // for(SelectionKey s:selector.selectedKeys()) {
fca6eec8016f *** empty log message ***
kono
parents: 26
diff changeset
97 // TupleHandler handler = (TupleHandler)s.attachment();
fca6eec8016f *** empty log message ***
kono
parents: 26
diff changeset
98 // handler.handle(s);
fca6eec8016f *** empty log message ***
kono
parents: 26
diff changeset
99 // }
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
100 for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) {
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
101 SelectionKey s = it.next();
56e015e8f5dc Testing TestLindaServer
kono
parents: 21
diff changeset
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
046feb56a196 message logging
kono
parents: 39
diff changeset
112
046feb56a196 message logging
kono
parents: 39
diff changeset
113
046feb56a196 message logging
kono
parents: 39
diff changeset
114 public void log(Level level,String msg) {
046feb56a196 message logging
kono
parents: 39
diff changeset
115 System.err.println(msg);
046feb56a196 message logging
kono
parents: 39
diff changeset
116 if (level==Level.SEVERE)
046feb56a196 message logging
kono
parents: 39
diff changeset
117 new IOException().setStackTrace(null);
046feb56a196 message logging
kono
parents: 39
diff changeset
118 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
119 }