view src/fdl/AcceptHandler.java @ 90:9cdc24bae625

ring test
author one
date Sat, 13 Feb 2010 04:16:15 +0900
parents 5336bafaaf48
children
line wrap: on
line source


package fdl;
import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;

public class AcceptHandler implements TupleHandler {
	
	public TupleSpace tupleSpace;
	public ServerSocketChannel ss;
	private FDLindaServ fds;
	
	public AcceptHandler(FDLindaServ fds,ServerSocketChannel ss, TupleSpace tupleSpace) {
    	// 読みこんだデータを格納するためのリストの初期化
        this.tupleSpace = tupleSpace;
        this.ss = ss;
        this.fds = fds;
    }
	
	public void handle(SelectionKey key) 
                throws ClosedChannelException, IOException {
        ServerSocketChannel serverChannel
        	= (ServerSocketChannel)key.channel();

        if (ss!=serverChannel) {
		fds.log(Level.SEVERE,"Wrong server socket channel.");
        }
        // アクセプト処理
        SocketChannel channel = serverChannel.accept();
        channel.configureBlocking(false);
        //channel.socket().setTcpNoDelay(true);
        fds.log(Level.INFO,"Server: accepted "+channel.socket());

        // TCP N Delay を ServerSocketChannel のオプションに指定する
        // setTcpNoDelay(boolean on) 
        tupleSpace.newUser();

        // 入出力用のハンドラを生成し,アタッチする
        // 監視する操作は読み込みのみ        
        channel.register(key.selector(),
                         SelectionKey.OP_READ,
                         new IOHandler(fds, tupleSpace,channel));
	}

}