Mercurial > hg > FederatedLinda
view src/fdl/AcceptHandler.java @ 17:609b288f47f9
*** empty log message ***
author | kono |
---|---|
date | Mon, 18 Aug 2008 07:28:29 +0900 |
parents | cccf34386cad |
children | 0243987383b7 |
line wrap: on
line source
package fdl; import java.io.IOException; //import java.net.InetAddress; //import java.net.InetSocketAddress; //import java.net.UnknownHostException; 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 { 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()); //初期生成 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++; 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); // 入出力用のハンドラを生成し,アタッチする // 監視する操作は読み込みのみ TupleSpace handler = new IOHandler(tuple_space); channel.register(key.selector(), SelectionKey.OP_READ, handler); } }