diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fdl/AcceptHandler.java	Thu Feb 07 14:21:30 2008 +0900
@@ -0,0 +1,77 @@
+
+package fdl;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+
+public class AcceptHandler implements TupleHandler, PSXQueueInterface {
+	//public Hashtable<Integer, Tuple> tuple_space;
+	public Tuple[] tuple_space;
+	
+	public static int user = 0;
+	public byte userchar[] = new byte[2];
+	public final int MAX_TUPLE = TupleHandler.MAX_TUPLE;
+	public Tuple tmpTuple;
+	
+	public AcceptHandler(Tuple[] _tuple_space) {
+    	// 読みこんだデータを格納するためのリストの初期化
+        tuple_space = _tuple_space;        
+    }
+	
+	public void handle(SelectionKey key) 
+                throws ClosedChannelException, IOException {
+        ServerSocketChannel serverChannel
+            = (ServerSocketChannel)key.channel();
+        
+        // アクセプト処理
+        SocketChannel channel = serverChannel.accept();
+        channel.configureBlocking(false);
+        System.out.println("Server: accepted "+channel.socket().getInetAddress());
+
+
+        //初期生成        
+        if((tmpTuple = tuple_space[MAX_TUPLE-1]) == null) {
+        	tmpTuple = tuple_space[MAX_TUPLE-1] = new Tuple();
+        	tmpTuple.next = null;
+        } else {
+        	while(tmpTuple.next != null) tmpTuple = tmpTuple.next;
+        	tmpTuple.next = new Tuple();
+        	tmpTuple = tmpTuple.next;
+        	tmpTuple.next = null;
+        }
+        
+        user++;
+
+        //data set
+        //ByteBuffer data = ByteBuffer.allocate(SHORT_SIZE);
+        //data.rewind();
+        //data.putShort((short) (user));
+        
+        ByteBuffer data = ByteBuffer.allocate(2);
+        data.clear();
+        userchar[0] = (byte) (user/10 + '0');
+        userchar[1] = (byte) (user%10 + '0');
+
+        data.put(userchar[0]);
+        data.put(userchar[1]);
+        
+        data.rewind();
+        tmpTuple.setData(data);
+        //Tuple
+        int id = MAX_TUPLE-1;
+        tmpTuple.setTuple('o', id, 0, data.limit(), data);
+
+
+        System.out.println("Server: assign id "+user);
+
+        // 入出力用のハンドラを生成し,アタッチする
+        // 監視する操作は読み込みのみ        
+        IOParam handler = new IOHandler(tuple_space);
+        channel.register(key.selector(),
+                         SelectionKey.OP_READ,
+                         handler);
+    }
+}