Mercurial > hg > FederatedLinda
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 |
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 { |
39 | 31 SocketChannel channel = (SocketChannel)key.channel(); |
32 if (ch!=channel) { | |
33 System.err.println("Wrong socket on IOHandler"); | |
34 } | |
35 // 読み込み用のバッファの生成 | |
36 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); | |
37 command.order(ByteOrder.BIG_ENDIAN); | |
38 ByteBuffer data = PSX.receivePacket(channel,command); | |
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 | 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 | 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 | 47 } |
48 } | |
49 | |
23 | 50 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException { |
33 | 51 command.order(ByteOrder.BIG_ENDIAN); |
17 | 52 int mode = command.get(PSX.LINDA_MODE_OFFSET); |
0 | 53 command.rewind(); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
54 |
0 | 55 if (debug) { |
56 System.out.println("data from : "+key.channel()); | |
57 } | |
23 | 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 | 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 | 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 | 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 | 68 } else { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
69 tupleSpace.hook.closeHook(key); |
33 | 70 System.err.println("Incorrect tuple operation"); |
0 | 71 System.exit(1); |
72 } | |
73 | |
74 } | |
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 | 77 System.out.println("Connection closed by "+key.channel()); |
78 SocketChannel channel = (SocketChannel)key.channel(); | |
79 key.cancel(); | |
80 channel.close(); | |
81 } | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
82 |
0 | 83 } |