Mercurial > hg > FederatedLinda
annotate src/fdl/IOHandler.java @ 37:2a366abc3f1f
*** empty log message ***
author | kono |
---|---|
date | Sun, 24 Aug 2008 21:21:40 +0900 |
parents | fe338d497c72 |
children | 81abceebc869 |
rev | line source |
---|---|
0 | 1 |
2 package fdl; | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 import java.nio.ByteOrder; | |
6 import java.nio.channels.ClosedChannelException; | |
7 import java.nio.channels.SelectionKey; | |
8 import java.nio.channels.SocketChannel; | |
9 | |
22 | 10 public class IOHandler implements TupleHandler { |
23 | 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 | 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 | 18 |
22 | 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 | 22 remoteString = PSX.getRemoteHostAndPort(ch); |
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 | 28 // 書き込み可であれば,読み込みを行う |
29 if (key.isReadable()) { | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
30 try { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
31 read(key); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
32 } catch (ClosedChannelException e) { |
33 | 33 key.cancel(); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
34 tupleSpace.hook.closeHook(key); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
35 } catch (IOException e) { |
33 | 36 key.cancel(); |
19
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(); | |
35
fe338d497c72
FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents:
33
diff
changeset
|
45 if (ch!=channel) { |
fe338d497c72
FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents:
33
diff
changeset
|
46 System.err.println("Wrong socket on IOHandler"); |
fe338d497c72
FederatedLinda was static singleton. It does not work on Thread based test.
kono
parents:
33
diff
changeset
|
47 } |
0 | 48 |
49 // 読み込み用のバッファの生成 | |
17 | 50 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); |
0 | 51 command.order(ByteOrder.BIG_ENDIAN); |
52 | |
33 | 53 ByteBuffer data = PSX.receivePacket(channel, command); |
15 | 54 |
55 if (debug) { | |
25 | 56 PSX.printData("IOHandler:",command); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
57 } |
23 | 58 manager_run(key, command, data); |
24 | 59 // assert((key.interestOps()& SelectionKey.OP_READ) !=0); |
0 | 60 } |
61 | |
23 | 62 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException { |
33 | 63 command.order(ByteOrder.BIG_ENDIAN); |
17 | 64 int mode = command.get(PSX.LINDA_MODE_OFFSET); |
0 | 65 command.rewind(); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
66 |
0 | 67 if (debug) { |
68 System.out.println("data from : "+key.channel()); | |
69 } | |
23 | 70 if(mode == '!') { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
71 tupleSpace.hook.closeHook(key); |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
72 } else if(mode == PSX.PSX_CHECK) { |
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
73 tupleSpace.Check(key, command); |
33 | 74 } 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
|
75 tupleSpace.In_Rd(key, command, mode); |
17 | 76 } else if (mode == PSX.PSX_WAIT_RD) { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
77 tupleSpace.Wait_Rd(key, command, mode); |
17 | 78 } else if(mode == PSX.PSX_OUT) { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
79 tupleSpace.Out(key, command, data); |
0 | 80 } else { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
81 tupleSpace.hook.closeHook(key); |
33 | 82 System.err.println("Incorrect tuple operation"); |
0 | 83 System.exit(1); |
84 } | |
85 | |
86 } | |
87 | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
88 void Connection_Close(SelectionKey key) throws IOException { |
0 | 89 System.out.println("Connection closed by "+key.channel()); |
90 SocketChannel channel = (SocketChannel)key.channel(); | |
91 key.cancel(); | |
92 channel.close(); | |
93 } | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
94 |
0 | 95 } |