Mercurial > hg > FederatedLinda
comparison src/fdl/IOHandler.java @ 40:046feb56a196 metacomm-worked **INVALID**
message logging
author | kono |
---|---|
date | Mon, 25 Aug 2008 14:38:18 +0900 |
parents | 81abceebc869 |
children | 96c63bc659d4 |
comparison
equal
deleted
inserted
replaced
39:81abceebc869 | 40:046feb56a196 |
---|---|
4 import java.nio.ByteBuffer; | 4 import java.nio.ByteBuffer; |
5 import java.nio.ByteOrder; | 5 import java.nio.ByteOrder; |
6 import java.nio.channels.ClosedChannelException; | 6 import java.nio.channels.ClosedChannelException; |
7 import java.nio.channels.SelectionKey; | 7 import java.nio.channels.SelectionKey; |
8 import java.nio.channels.SocketChannel; | 8 import java.nio.channels.SocketChannel; |
9 import java.util.logging.Level; | |
9 | 10 |
10 public class IOHandler implements TupleHandler { | 11 public class IOHandler implements TupleHandler { |
11 static final boolean debug = true; | 12 static final boolean debug = true; |
12 public TupleSpace tupleSpace; | 13 public TupleSpace tupleSpace; |
13 public SocketChannel ch; | 14 public SocketChannel ch; |
15 public FDLindaServ fds; | |
14 | 16 |
15 String remoteString; | 17 String remoteString; |
16 String localString; | 18 String localString; |
17 public int cnt = 0; | 19 public int cnt = 0; |
18 | 20 |
19 public IOHandler(TupleSpace tupleSpace,SocketChannel ch) { | 21 public IOHandler(FDLindaServ fds, TupleSpace tupleSpace,SocketChannel ch) { |
20 this.tupleSpace = tupleSpace; | 22 this.tupleSpace = tupleSpace; |
21 | 23 |
22 remoteString = PSX.getRemoteHostAndPort(ch); | 24 remoteString = PSX.getRemoteHostAndPort(ch); |
23 localString = PSX.getLocalHostAndPort(ch); | 25 localString = PSX.getLocalHostAndPort(ch); |
24 this.ch = ch; | 26 this.ch = ch; |
27 this.fds = fds; | |
25 } | 28 } |
26 | 29 |
27 public void handle(SelectionKey key) { | 30 public void handle(SelectionKey key) { |
28 // 書き込み可であれば,読み込みを行う | 31 // 書き込み可であれば,読み込みを行う |
29 if (key.isReadable()) { | 32 if (key.isReadable()) { |
30 try { | 33 try { |
31 SocketChannel channel = (SocketChannel)key.channel(); | 34 SocketChannel channel = (SocketChannel)key.channel(); |
32 if (ch!=channel) { | 35 if (ch!=channel) { |
33 System.err.println("Wrong socket on IOHandler"); | 36 fds.log(Level.INFO,"Wrong socket on IOHandler"); |
34 } | 37 } |
35 // 読み込み用のバッファの生成 | 38 // 読み込み用のバッファの生成 |
36 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); | 39 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); |
37 command.order(ByteOrder.BIG_ENDIAN); | 40 command.order(ByteOrder.BIG_ENDIAN); |
38 ByteBuffer data = PSX.receivePacket(channel,command); | 41 ByteBuffer data = PSX.receivePacket(channel,command); |
51 command.order(ByteOrder.BIG_ENDIAN); | 54 command.order(ByteOrder.BIG_ENDIAN); |
52 int mode = command.get(PSX.LINDA_MODE_OFFSET); | 55 int mode = command.get(PSX.LINDA_MODE_OFFSET); |
53 command.rewind(); | 56 command.rewind(); |
54 | 57 |
55 if (debug) { | 58 if (debug) { |
56 System.out.println("data from : "+key.channel()); | 59 fds.log(Level.INFO,"data from : "+key.channel()); |
57 } | 60 } |
58 if(mode == '!') { | 61 if(mode == '!') { |
59 tupleSpace.hook.closeHook(key); | 62 tupleSpace.hook.closeHook(key); |
60 } else if(mode == PSX.PSX_CHECK) { | 63 } else if(mode == PSX.PSX_CHECK) { |
61 tupleSpace.Check(key, command); | 64 tupleSpace.Check(key, command); |
65 tupleSpace.Wait_Rd(key, command, mode); | 68 tupleSpace.Wait_Rd(key, command, mode); |
66 } else if(mode == PSX.PSX_OUT) { | 69 } else if(mode == PSX.PSX_OUT) { |
67 tupleSpace.Out(key, command, data); | 70 tupleSpace.Out(key, command, data); |
68 } else { | 71 } else { |
69 tupleSpace.hook.closeHook(key); | 72 tupleSpace.hook.closeHook(key); |
70 System.err.println("Incorrect tuple operation"); | 73 fds.log(Level.SEVERE,"Incorrect tuple operation"); |
71 System.exit(1); | |
72 } | 74 } |
73 | 75 |
74 } | 76 } |
75 | 77 |
76 void Connection_Close(SelectionKey key) throws IOException { | 78 void Connection_Close(SelectionKey key) throws IOException { |
77 System.out.println("Connection closed by "+key.channel()); | 79 fds.log(Level.INFO,"Connection closed by "+key.channel()); |
78 SocketChannel channel = (SocketChannel)key.channel(); | 80 SocketChannel channel = (SocketChannel)key.channel(); |
79 key.cancel(); | 81 key.cancel(); |
80 channel.close(); | 82 channel.close(); |
81 } | 83 } |
82 | 84 |