annotate src/fdl/IOHandler.java @ 20:a0fd653d1121

Debug Client and Meta Engine for logging.
author kono
date Tue, 19 Aug 2008 06:26:20 +0900
parents 0243987383b7
children 56e015e8f5dc
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 import java.io.IOException;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
4 import java.net.InetAddress;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
5 import java.net.InetSocketAddress;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
6 import java.net.UnknownHostException;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
7 import java.nio.ByteBuffer;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
8 import java.nio.ByteOrder;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
9 import java.nio.channels.ClosedChannelException;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
10 import java.nio.channels.SelectionKey;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
11 import java.nio.channels.SocketChannel;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
12
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
13 public class IOHandler {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
14 static final boolean debug = false;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
15 public TupleSpace tupleSpace;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
16
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
17 String remoteString;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
18 String localString;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
19 public int cnt = 0;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
20
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
21 public IOHandler(TupleSpace tupleSpace,SelectionKey key) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
22 this.tupleSpace = tupleSpace;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
23
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
24 SocketChannel ch = (SocketChannel) key.channel();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
25 remoteString = getRemoteHostAndPort(ch);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
26 localString = getLocalHostAndPort(ch);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
27 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
28
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
29 public void handle(SelectionKey key) {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30 // 書き込み可であれば,読み込みを行う
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 if (key.isReadable()) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
32 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
33 read(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
34 } catch (ClosedChannelException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
35 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
36 } catch (IOException e) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
37 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
38 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
39 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
40 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
41
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
42 void read(SelectionKey key)
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
43 throws ClosedChannelException, IOException {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
44 SocketChannel channel = (SocketChannel)key.channel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
45
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
46 // 読み込み用のバッファの生成
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
47 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
48 command.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
49 command.clear();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
50
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
51 int readsize = PSX.LINDA_HEADER_SIZE;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
52 int count = 0;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
53
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
54 // 読み込み
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
55 while(readsize>0) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
56 if(debug){
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
57 System.out.print("reading command...");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
58 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
59 count = channel.read(command);
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
60 if(count < 0) throw new IOException();
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
61 readsize -= count;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
62 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
63 command.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
64
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
65 command.getInt(PSX.LINDA_PACKET_LENGTH_OFFSET);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
66 int datalen = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
67
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
68 ByteBuffer data = ByteBuffer.allocate(datalen);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
69 int read = datalen;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
70
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
71 if (debug) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
72 System.out.println("reading: " +datalen);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
73 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
74
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
75 data.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
76 data.clear();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
77 while(read>0) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
78 //System.out.print("reading2...");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
79 read -= channel.read(data);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
80 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
81 data.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
82
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
83 command.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
84 command.rewind();
15
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 5
diff changeset
85
aced4bfc15af add Meta Linda Interface for debugger.
kono
parents: 5
diff changeset
86 if (debug) {
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
87 PSX.printData(command);
19
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 // I believe we don't need this
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
90 //key.interestOps(key.interestOps() | SelectionKey.OP_READ );
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
91 assert((key.interestOps()& SelectionKey.OP_READ) !=0);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
92 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
93
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
94 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data, int len) throws IOException {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
95 command.order(ByteOrder.BIG_ENDIAN);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
96 int mode = command.get(PSX.LINDA_MODE_OFFSET);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
97 command.rewind();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
98
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
99 if (debug) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
100 System.out.println("data from : "+key.channel());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
101 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
102 if((mode == '!') || (len == 0)) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
103 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
104 } else if(mode == PSX.PSX_CHECK) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
105 tupleSpace.Check(key, command);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
106 }
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
107 else if(mode == PSX.PSX_IN || mode == PSX.PSX_RD){
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
108 tupleSpace.In_Rd(key, command, mode);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
109 } else if (mode == PSX.PSX_WAIT_RD) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
110 tupleSpace.Wait_Rd(key, command, mode);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
111 } else if(mode == PSX.PSX_OUT) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
112 tupleSpace.Out(key, command, data);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
113 } else {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
114 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
115 System.out.println("Incorrect tuple operation");
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
116 System.exit(1);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
117 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
118
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
119 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
120
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
121 void Connection_Close(SelectionKey key) throws IOException {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
122 System.out.println("Connection closed by "+key.channel());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
123 SocketChannel channel = (SocketChannel)key.channel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
124 key.cancel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
125 channel.close();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
126 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
127
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
128 private static String getRemoteHostAndPort(SocketChannel channel) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
129 String socketString = channel.socket().getRemoteSocketAddress().toString();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
130 String[] split = socketString.split("/");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
131 int length = split.length;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
132 String hostAndPort = split[length-1];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
133 split = hostAndPort.split(":");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
134 String host = split[0];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
135 String port = split[1];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
136 int portnum = Integer.parseInt(port);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
137 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
138 InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
139 host = address.getHostName().toString();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
140 return (host +":"+port);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
141 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
142 catch( UnknownHostException e ){
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
143 return hostAndPort;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
144 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
145 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
146
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
147 private static String getLocalHostAndPort(SocketChannel channel) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
148 String socketString = channel.socket().getLocalSocketAddress().toString();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
149 String[] split = socketString.split("/");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
150 int length = split.length;
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
151 String hostAndPort = split[length-1];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
152 split = hostAndPort.split(":");
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
153 String host = split[0];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
154 String port = split[1];
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
155 try {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
156 InetAddress localhost = InetAddress.getLocalHost();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
157 host = localhost.getHostName();
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
158 return (host +":"+port);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
159 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
160 catch( UnknownHostException e ){
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
161 return (host +":"+port);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
162 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
163 }
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
164
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
165 }