Mercurial > hg > FederatedLinda
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 |
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; | |
40 | 9 import java.util.logging.Level; |
0 | 10 |
22 | 11 public class IOHandler implements TupleHandler { |
23 | 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 | 15 public FDLindaServ fds; |
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 |
40 | 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 | 24 remoteString = PSX.getRemoteHostAndPort(ch); |
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 | 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 | 31 // 書き込み可であれば,読み込みを行う |
32 if (key.isReadable()) { | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
33 try { |
39 | 34 SocketChannel channel = (SocketChannel)key.channel(); |
40 | 35 if (ch!=channel) { |
36 fds.log(Level.INFO,"Wrong socket on IOHandler"); | |
39 | 37 } |
38 // 読み込み用のバッファの生成 | |
39 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); | |
40 command.order(ByteOrder.BIG_ENDIAN); | |
41 ByteBuffer data = PSX.receivePacket(channel,command); | |
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 | 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 | 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 | 50 } |
51 } | |
52 | |
23 | 53 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException { |
33 | 54 command.order(ByteOrder.BIG_ENDIAN); |
17 | 55 int mode = command.get(PSX.LINDA_MODE_OFFSET); |
0 | 56 command.rewind(); |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
57 |
0 | 58 if (debug) { |
40 | 59 fds.log(Level.INFO,"data from : "+key.channel()); |
0 | 60 } |
23 | 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 | 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 | 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 | 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 | 71 } else { |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
72 tupleSpace.hook.closeHook(key); |
40 | 73 fds.log(Level.SEVERE,"Incorrect tuple operation"); |
0 | 74 } |
75 | |
76 } | |
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 | 79 fds.log(Level.INFO,"Connection closed by "+key.channel()); |
0 | 80 SocketChannel channel = (SocketChannel)key.channel(); |
81 key.cancel(); | |
82 channel.close(); | |
83 } | |
19
0243987383b7
Meta Protocol Engine and sample implementation of event logger.
kono
parents:
17
diff
changeset
|
84 |
0 | 85 } |