annotate src/fdl/IOHandler.java @ 0:083a0b5e12cc

Apply Debug Interface version start
author fuchita
date Thu, 07 Feb 2008 14:21:30 +0900
parents
children b49e593b2502
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 import java.util.Hashtable;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
10 import java.util.LinkedList;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
11
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
12
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
13 public class IOHandler extends IOParam {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
14 static final boolean debug = false;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
15 public Tuple[] tuple_space;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
16 public ComDebug com_debug;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
17 //public static Hashtable<String, Integer> com_Loggingtable = new Hashtable<String, Integer>();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
18 //public static LinkedList<SocketChannel> reportCh_list = new LinkedList<SocketChannel>();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
19
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
20 public IOHandler(Tuple[] _tuple_space) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
21 super(_tuple_space);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
22 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
23
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
24 public void handle(SelectionKey key)
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
25 throws ClosedChannelException, IOException {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
26 // 書き込み可であれば,読み込みを行う
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
27 if (key.isReadable()) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
28 read(key);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
29 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
30
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
31 // 書き込み可であれば,書き込みを行う
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
32 /*if (key.isWritable() && key.isValid()) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
33 write(key);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
34 }*/
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
35 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
36
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
37 private void read(SelectionKey key)
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
38 throws ClosedChannelException, IOException {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
39 SocketChannel channel = (SocketChannel)key.channel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
40
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
41 // 読み込み用のバッファの生成
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
42 ByteBuffer command = ByteBuffer.allocate(LINDA_HEADER_SIZE);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
43 command.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
44 command.clear();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
45
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
46 int readsize = LINDA_HEADER_SIZE;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
47 int count = 0;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
48
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
49 // 読み込み
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
50 while(readsize>0) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
51 if(debug){
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
52 System.out.print("reading command...");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
53 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
54 count = channel.read(command);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
55
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
56 if(count < 0) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
57 System.out.println("Connection closed by "+key.channel());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
58 //System.out.println("Close channel on EOF; channel: "+channel);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
59 key.cancel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
60 channel.close();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
61 readsize = -1;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
62 return;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
63 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
64 readsize -= count;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
65 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
66 command.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
67
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
68 int len = command.getInt(LINDA_PACKET_LENGTH_OFFSET);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
69 int datalen = command.getInt(LINDA_DATA_LENGTH_OFFSET);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
70
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
71 ByteBuffer data = ByteBuffer.allocate(datalen);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
72 int read = datalen;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
73
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
74 if (debug) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
75 System.out.println("reading: " +datalen);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
76 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
77
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
78 data.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
79 data.clear();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
80 while(read>0) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
81 //System.out.print("reading2...");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
82 read -= channel.read(data);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
83 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
84 data.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
85
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
86 /*
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
87 static final int LINDA_PACKET_LENGTH_OFFSET =0;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
88 static final int LINDA_MODE_OFFSET =0+4;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
89 static final int LINDA_ID_OFFSET =1+4;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
90 static final int LINDA_SEQ_OFFSET =3+4;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
91 static final int LINDA_DATA_LENGTH_OFFSET =7+4;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
92 static final int LINDA_HEADER_SIZE =12+4;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
93 */
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
94 command.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
95 command.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
96
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
97 /*** print data ***/
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
98 char id = (char)command.getShort(LINDA_ID_OFFSET);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
99 System.out.println("LENGTH:"+command.getInt(LINDA_PACKET_LENGTH_OFFSET)+" "+
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
100 "MODE:"+(char)command.get(LINDA_MODE_OFFSET)+" "+
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
101 "ID:"+(int)id+" "+
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
102 "SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" ");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
103 //"SEQ:"+command.getInt(LINDA_SEQ_OFFSET)+" "+
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
104 //"DATA LENGTH:"+command.getInt(LINDA_DATA_LENGTH_OFFSET)+" ");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
105 //System.out.println("DATA:"+data);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
106 //データ処理
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
107 command.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
108 manager_run(key, command, data, len);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
109
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
110 key.interestOps(key.interestOps() | SelectionKey.OP_READ );
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
111 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
112
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
113
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
114 public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data, int len) throws IOException {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
115 command.order(ByteOrder.BIG_ENDIAN);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
116 int mode = command.get(LINDA_MODE_OFFSET);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
117 int id = command.get(LINDA_ID_OFFSET);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
118 command.rewind();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
119
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
120 com_debug = new ComDebug();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
121 Hashtable<String, Integer> com_Loggingtable = ComDebug.Com_Hashtable;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
122 LinkedList<SocketChannel> reportCh_list = ComDebug.Report_Channellist;
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
123
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
124 if (debug) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
125 System.out.println("data from : "+key.channel());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
126 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
127 if((mode == '!') || (len == 0)) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
128 Connection_Close(key);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
129 }else if(id > PRIVILEGED_ID && id < MAX_TUPLE-1){
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
130 ComDebug.addChannel(key, reportCh_list);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
131 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
132 else if(mode == PSX_CHECK) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
133 Check(key, command);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
134 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
135 else if(mode == PSX_IN || mode == PSX_RD){
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
136 In_Rd(key, command, mode);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
137 } else if (mode == PSX_WAIT_RD) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
138 Wait_Rd(key, command, mode);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
139 } else if(mode == PSX_OUT) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
140 Out(command, data);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
141 } else {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
142 System.out.println("Uncorrect buffer");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
143 System.exit(1);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
144 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
145 //DEBUG用カウンタ
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
146 ComDebug.Com_inc(key, com_Loggingtable, mode);
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
147 System.out.println("Com_Debug:");
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
148 System.out.println(com_Loggingtable.toString());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
149 //DEBUG用レポート
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
150 ComDebug.Report(reportCh_list, command, com_Loggingtable.toString());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
151
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
152 if (key.interestOps()
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
153 != (SelectionKey.OP_READ)) {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
154 // 読み込み操作に対する監視を行う
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
155 key.interestOps(SelectionKey.OP_READ );
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
156 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
157
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
158 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
159
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
160 private void Connection_Close(SelectionKey key) throws IOException {
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
161 System.out.println("Connection closed by "+key.channel());
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
162 SocketChannel channel = (SocketChannel)key.channel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
163 key.cancel();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
164 channel.close();
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
165 }
083a0b5e12cc Apply Debug Interface version start
fuchita
parents:
diff changeset
166 }