Mercurial > hg > FederatedLinda
view src/fdl/FDLindaServ.java @ 16:cccf34386cad
*** empty log message ***
author | kono |
---|---|
date | Mon, 18 Aug 2008 06:17:54 +0900 |
parents | aced4bfc15af |
children | 609b288f47f9 |
line wrap: on
line source
package fdl; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; //import java.nio.ByteOrder; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.spi.AbstractSelector; import java.nio.channels.spi.SelectorProvider; public class FDLindaServ implements PSXQueueInterface { static final int MAX_REQ = 1; static final int FAIL = (-1); static final int MAX_UAER = 4; static final int MAX_TUPLE = 65536; static final int DEF_PORT = 10000; //public static final int TIMEOUT = 5*1000; public Tuple[] tuple_space; public int port = DEF_PORT; private AbstractSelector selector; private ServerSocketChannel ssChannel; public static void main(final String[] args) { final String usages = "usage: FDLindaServ [-p port]"; //バイトオーダー確認 //System.out.println(ByteOrder.nativeOrder().toString()); int port = DEF_PORT; //引数判定 try { for (int i=0; i<args.length; ++i) { if("-p".equals(args[i])) { port = Integer.parseInt(args[++i]); } } } catch (NumberFormatException e) { System.err.println(usages); return; } try { FDLindaServ serv; serv = new FDLindaServ(port); serv.mainLoop(); } catch (IOException e) { System.err.println("Server Communiation Problem."); } } private void mainLoop() { while(true) { checkTuple(selector); } } public FDLindaServ(int port) throws IOException { this.port = port; tuple_space = new Tuple[MAX_TUPLE]; //セレクタを生成 selector = SelectorProvider.provider().openSelector(); //ソケット・チャネルを生成・設定 ssChannel = SelectorProvider.provider().openServerSocketChannel(); InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port); //ssChannel.socket().bind(new InetSocketAddress(port)); ssChannel.socket().bind(address); ssChannel.configureBlocking(false); //ssChannel.socket().setReuseAddress(true); System.out.println("Server: litening at "+ssChannel); //セレクタにチャンネルを登録 //ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tuple_space)); //ssChannel.register(selector, ssChannel.validOps(), new AcceptHandler(tuple_space)); ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tuple_space)); } public void checkTuple(Selector selector) { // セレクタによる監視 try { if (selector.select()>0) { for(SelectionKey s:selector.selectedKeys()) { TupleHandler handler = (TupleHandler)s.attachment(); handler.handle(s); } } } catch (ClosedChannelException e) { // we have to do something... } catch (IOException e) { } } }