annotate src/fdl/IOHandler.java @ 71:0352536c33fa

(example: writer) get linda server addr from commandline arg.
author kazz@e065701.local
date Fri, 23 Oct 2009 14:11:07 +0900
parents 046feb56a196
children 96c63bc659d4
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;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
4 import java.nio.ByteBuffer;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
5 import java.nio.ByteOrder;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
6 import java.nio.channels.ClosedChannelException;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
7 import java.nio.channels.SelectionKey;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
8 import java.nio.channels.SocketChannel;
40
046feb56a196 message logging
kono
parents: 39
diff changeset
9 import java.util.logging.Level;
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
10
22
56e015e8f5dc Testing TestLindaServer
kono
parents: 19
diff changeset
11 public class IOHandler implements TupleHandler {
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
12 static final boolean debug = true;
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
13 public TupleSpace tupleSpace;
35
fe338d497c72 FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents: 33
diff changeset
14 public SocketChannel ch;
40
046feb56a196 message logging
kono
parents: 39
diff changeset
15 public FDLindaServ fds;
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
40
046feb56a196 message logging
kono
parents: 39
diff changeset
21 public IOHandler(FDLindaServ fds, TupleSpace tupleSpace,SocketChannel ch) {
19
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
24
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
24 remoteString = PSX.getRemoteHostAndPort(ch);
35375016b2f0 cleanup.
kono
parents: 23
diff changeset
25 localString = PSX.getLocalHostAndPort(ch);
35
fe338d497c72 FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents: 33
diff changeset
26 this.ch = ch;
40
046feb56a196 message logging
kono
parents: 39
diff changeset
27 this.fds = fds;
19
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
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
30 public void handle(SelectionKey key) {
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 // 書き込み可であれば,読み込みを行う
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
32 if (key.isReadable()) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
33 try {
39
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
34 SocketChannel channel = (SocketChannel)key.channel();
40
046feb56a196 message logging
kono
parents: 39
diff changeset
35 if (ch!=channel) {
046feb56a196 message logging
kono
parents: 39
diff changeset
36 fds.log(Level.INFO,"Wrong socket on IOHandler");
39
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
37 }
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
38 // 読み込み用のバッファの生成
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
39 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
40 command.order(ByteOrder.BIG_ENDIAN);
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
41 ByteBuffer data = PSX.receivePacket(channel,command);
81abceebc869 *** empty log message ***
kono
parents: 35
diff changeset
42 manager_run(key, command, data);
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
43 } catch (ClosedChannelException e) {
33
64071f8e2e0d *** empty log message ***
kono
parents: 31
diff changeset
44 key.cancel();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
45 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
46 } catch (IOException e) {
33
64071f8e2e0d *** empty log message ***
kono
parents: 31
diff changeset
47 key.cancel();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
48 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
49 }
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
50 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
51 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
52
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
53 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException {
33
64071f8e2e0d *** empty log message ***
kono
parents: 31
diff changeset
54 command.order(ByteOrder.BIG_ENDIAN);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
55 int mode = command.get(PSX.LINDA_MODE_OFFSET);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
56 command.rewind();
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
57
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
58 if (debug) {
40
046feb56a196 message logging
kono
parents: 39
diff changeset
59 fds.log(Level.INFO,"data from : "+key.channel());
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
60 }
23
b4fd7fb9135a Simple Test run.
kono
parents: 22
diff changeset
61 if(mode == '!') {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
62 tupleSpace.hook.closeHook(key);
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
63 } else if(mode == PSX.PSX_CHECK) {
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
64 tupleSpace.Check(key, command);
33
64071f8e2e0d *** empty log message ***
kono
parents: 31
diff changeset
65 } 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
66 tupleSpace.In_Rd(key, command, mode);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
67 } else if (mode == PSX.PSX_WAIT_RD) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
68 tupleSpace.Wait_Rd(key, command, mode);
17
609b288f47f9 *** empty log message ***
kono
parents: 16
diff changeset
69 } else if(mode == PSX.PSX_OUT) {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
70 tupleSpace.Out(key, command, data);
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
71 } else {
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
72 tupleSpace.hook.closeHook(key);
40
046feb56a196 message logging
kono
parents: 39
diff changeset
73 fds.log(Level.SEVERE,"Incorrect tuple operation");
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
74 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
75
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
76 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
77
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
78 void Connection_Close(SelectionKey key) throws IOException {
40
046feb56a196 message logging
kono
parents: 39
diff changeset
79 fds.log(Level.INFO,"Connection closed by "+key.channel());
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
80 SocketChannel channel = (SocketChannel)key.channel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
81 key.cancel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
82 channel.close();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
83 }
19
0243987383b7 Meta Protocol Engine and sample implementation of event logger.
kono
parents: 17
diff changeset
84
0
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
85 }