Mercurial > hg > RemoteEditor > REPSessionManager
view test/channeltest/testSeMa.java @ 172:a776ec9ed848
*** empty log message ***
author | kono |
---|---|
date | Thu, 28 Aug 2008 18:57:17 +0900 |
parents | 690182302c05 |
children | 72252e970a8b |
line wrap: on
line source
package test.channeltest; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.nio.channels.SelectionKey; import java.util.LinkedList; import rep.channel.REPLogger; import rep.channel.REPSelector; import rep.channel.REPServerSocketChannel; import rep.channel.REPSocketChannel; public class testSeMa extends Thread{ SocketAddress IP; boolean running=true; REPLogger ns; LinkedList<REPSocketChannel<String>> channels; public testSeMa(String name, String host, int port){ super(name); IP = new InetSocketAddress(host,port); ns = testNetworkSimulator.ns; channels = new LinkedList<REPSocketChannel<String>>(); } public void init(){ } @SuppressWarnings("unchecked") public void run() { REPSelector selector=null; REPServerSocketChannel<String> scs; try { selector = REPSelector.create(); scs = REPServerSocketChannel.<String>open(); scs.socket().setReuseAddress(true); scs.socket().bind(IP); scs.configureBlocking(false); selector.register(scs, SelectionKey.OP_ACCEPT, null); } catch (IOException e1) { return; } ns.writeLog("SessionManager starts mainroutin.", 1); /* Main Loop */ while(running){ try { selector.select(); for(SelectionKey key : selector.selectedKeys()){ if(key.isAcceptable()){ ns.writeLog(this, "gets acceptable channel", 1); REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel(); REPSocketChannel<String> channel; channel = sc.accept1(); selector.register(channel, SelectionKey.OP_READ, null); ns.writeLog(this, "accepts a client.", 1); }else if(key.isReadable()){ ns.writeLog(this, "gets readable channel", 1); //SelectableChannelSimulator<String> channel = key.channel(); REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel(); String packet; packet = channel.read(); ns.writeLog(this, "receives String==> `"+packet+"\'", 1); channel.write("from SeMa"+this.getName()+": world"); } } } catch (IOException e) { e.printStackTrace();} } } }