Mercurial > hg > FederatedLinda
view src/fdl/FDLindaServ.java @ 22:56e015e8f5dc
Testing TestLindaServer
author | kono |
---|---|
date | Tue, 19 Aug 2008 16:02:48 +0900 |
parents | fac6e0073b1a |
children | 35375016b2f0 |
line wrap: on
line source
package fdl; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.ServerSocketChannel; import java.nio.channels.spi.AbstractSelector; import java.nio.channels.spi.SelectorProvider; import java.util.Iterator; public class FDLindaServ { static final int MAX_REQ = 1; static final int FAIL = (-1); static final int DEF_PORT = 10000; //public static final int TIMEOUT = 5*1000; public int port = DEF_PORT; private AbstractSelector selector; private ServerSocketChannel ssChannel; public TupleSpace tupleSpace; 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() { MetaLinda ml = new MetaLinda(tupleSpace, this); MetaEngine me = new NullMetaEngine(ml); // MetaEngine me = new MetaEngine(ml); while(true) { me.mainLoop(); } } public FDLindaServ(int port) throws IOException { this.port = port; //セレクタを生成 selector = SelectorProvider.provider().openSelector(); //ソケット・チャネルを生成・設定 ssChannel = SelectorProvider.provider().openServerSocketChannel(); // getAllByName で、すべて取って、その上のすべてでselectする必要がある。 InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port); ssChannel.socket().setReuseAddress(true); ssChannel.socket().bind(address); ssChannel.configureBlocking(false); System.out.println("Server: litening at "+ssChannel); //セレクタにチャンネルを登録 tupleSpace = new TupleSpace(); ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(tupleSpace)); } public void checkTuple() { checkTuple(0); } public void checkTuple(long timeout) { // セレクタによる監視 try { if (selector.select(timeout)>0) { for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) { SelectionKey s = it.next(); it.remove(); TupleHandler handler = (TupleHandler)s.attachment(); handler.handle(s); } } } catch (ClosedChannelException e) { // we have to do something... } catch (IOException e) { } } }