annotate src/fdl/IOHandler.java @ 39:81abceebc869

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