comparison src/fdl/AcceptHandler.java @ 0:083a0b5e12cc

Apply Debug Interface version start
author fuchita
date Thu, 07 Feb 2008 14:21:30 +0900
parents
children ae7e0e92c651
comparison
equal deleted inserted replaced
-1:000000000000 0:083a0b5e12cc
1
2 package fdl;
3 import java.io.IOException;
4 import java.nio.ByteBuffer;
5 import java.nio.channels.ClosedChannelException;
6 import java.nio.channels.SelectionKey;
7 import java.nio.channels.ServerSocketChannel;
8 import java.nio.channels.SocketChannel;
9
10 public class AcceptHandler implements TupleHandler, PSXQueueInterface {
11 //public Hashtable<Integer, Tuple> tuple_space;
12 public Tuple[] tuple_space;
13
14 public static int user = 0;
15 public byte userchar[] = new byte[2];
16 public final int MAX_TUPLE = TupleHandler.MAX_TUPLE;
17 public Tuple tmpTuple;
18
19 public AcceptHandler(Tuple[] _tuple_space) {
20 // 読みこんだデータを格納するためのリストの初期化
21 tuple_space = _tuple_space;
22 }
23
24 public void handle(SelectionKey key)
25 throws ClosedChannelException, IOException {
26 ServerSocketChannel serverChannel
27 = (ServerSocketChannel)key.channel();
28
29 // アクセプト処理
30 SocketChannel channel = serverChannel.accept();
31 channel.configureBlocking(false);
32 System.out.println("Server: accepted "+channel.socket().getInetAddress());
33
34
35 //初期生成
36 if((tmpTuple = tuple_space[MAX_TUPLE-1]) == null) {
37 tmpTuple = tuple_space[MAX_TUPLE-1] = new Tuple();
38 tmpTuple.next = null;
39 } else {
40 while(tmpTuple.next != null) tmpTuple = tmpTuple.next;
41 tmpTuple.next = new Tuple();
42 tmpTuple = tmpTuple.next;
43 tmpTuple.next = null;
44 }
45
46 user++;
47
48 //data set
49 //ByteBuffer data = ByteBuffer.allocate(SHORT_SIZE);
50 //data.rewind();
51 //data.putShort((short) (user));
52
53 ByteBuffer data = ByteBuffer.allocate(2);
54 data.clear();
55 userchar[0] = (byte) (user/10 + '0');
56 userchar[1] = (byte) (user%10 + '0');
57
58 data.put(userchar[0]);
59 data.put(userchar[1]);
60
61 data.rewind();
62 tmpTuple.setData(data);
63 //Tuple
64 int id = MAX_TUPLE-1;
65 tmpTuple.setTuple('o', id, 0, data.limit(), data);
66
67
68 System.out.println("Server: assign id "+user);
69
70 // 入出力用のハンドラを生成し,アタッチする
71 // 監視する操作は読み込みのみ
72 IOParam handler = new IOHandler(tuple_space);
73 channel.register(key.selector(),
74 SelectionKey.OP_READ,
75 handler);
76 }
77 }