Mercurial > hg > FederatedLinda
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 |
rev | line source |
---|---|
0 | 1 |
2 package fdl; | |
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 | 7 import java.nio.ByteBuffer; |
8 import java.nio.ByteOrder; | |
9 import java.nio.channels.ClosedChannelException; | |
10 import java.nio.channels.SelectionKey; | |
11 import java.nio.channels.SocketChannel; | |
12 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
13 public class IOHandler { |
0 | 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 | 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 | 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 | 30 // 書き込み可であれば,読み込みを行う |
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 | 39 } |
40 } | |
41 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
42 void read(SelectionKey key) |
0 | 43 throws ClosedChannelException, IOException { |
44 SocketChannel channel = (SocketChannel)key.channel(); | |
45 | |
46 // 読み込み用のバッファの生成 | |
17 | 47 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); |
0 | 48 command.order(ByteOrder.BIG_ENDIAN); |
49 command.clear(); | |
50 | |
17 | 51 int readsize = PSX.LINDA_HEADER_SIZE; |
0 | 52 int count = 0; |
53 | |
54 // 読み込み | |
55 while(readsize>0) { | |
56 if(debug){ | |
57 System.out.print("reading command..."); | |
58 } | |
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 | 61 readsize -= count; |
62 } | |
63 command.rewind(); | |
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 | 66 int datalen = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); |
0 | 67 |
68 ByteBuffer data = ByteBuffer.allocate(datalen); | |
69 int read = datalen; | |
70 | |
71 if (debug) { | |
72 System.out.println("reading: " +datalen); | |
73 } | |
74 | |
75 data.order(ByteOrder.BIG_ENDIAN); | |
76 data.clear(); | |
77 while(read>0) { | |
78 //System.out.print("reading2..."); | |
79 read -= channel.read(data); | |
80 } | |
81 data.rewind(); | |
82 | |
83 command.order(ByteOrder.BIG_ENDIAN); | |
84 command.rewind(); | |
15 | 85 |
86 if (debug) { | |
17 | 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 | 92 } |
93 | |
17 | 94 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data, int len) throws IOException { |
0 | 95 command.order(ByteOrder.BIG_ENDIAN); |
17 | 96 int mode = command.get(PSX.LINDA_MODE_OFFSET); |
0 | 97 command.rewind(); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
98 |
0 | 99 if (debug) { |
100 System.out.println("data from : "+key.channel()); | |
101 } | |
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 | 106 } |
17 | 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 | 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 | 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 | 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 | 116 System.exit(1); |
117 } | |
118 | |
119 } | |
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 | 122 System.out.println("Connection closed by "+key.channel()); |
123 SocketChannel channel = (SocketChannel)key.channel(); | |
124 key.cancel(); | |
125 channel.close(); | |
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 | 165 } |