annotate src/fdl/AcceptHandler.java @ 3:ae7e0e92c651

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