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