Mercurial > hg > RemoteEditor > REPSessionManager
view test/channeltest/testSeMa.java @ 154:fafbaaa0abd0
*** empty log message ***
author | kono |
---|---|
date | Thu, 28 Aug 2008 15:33:02 +0900 |
parents | |
children | f0d80a64aba0 |
line wrap: on
line source
package test.channeltest; import java.io.IOException; import java.util.LinkedList; import pathfinder.mergetest.channels.*; public class testSeMa extends Thread{ int IP; boolean running=true; NetworkSimulator<String> ns; LinkedList<ChannelSimulator<String>> channels; public testSeMa(NetworkSimulator<String> _ns, String name, int ip){ super(name); IP = ip; ns = _ns; channels = new LinkedList<ChannelSimulator<String>>(); } public void init(){ } public void run() { SelectorSimulator<String> selector = new SelectorSimulator<String>(); ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector); scs.bind(IP); selector.register(scs, SelectionKeySimulator.OP_ACCEPT); ns.writeLog("SessionManager starts mainroutin.", 1); /* Main Loop */ while(running){ try { selector.select(); } catch (IOException e) { e.printStackTrace();} for(SelectionKeySimulator<String> key : selector.selectedKeys()){ if(key.isAcceptable()){ ns.writeLog(this, "gets acceptable channel", 1); ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); ChannelSimulator<String> channel = sc.accept(); selector.register(channel, SelectionKeySimulator.OP_READ); ns.writeLog(this, "accepts a client.", 1); }else if(key.isReadable()){ ns.writeLog(this, "gets readable channel", 1); //SelectableChannelSimulator<String> channel = key.channel(); ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel(); String packet = channel.read(); ns.writeLog(this, "receives String==> `"+packet+"\'", 1); channel.write("from SeMa"+this.getName()+": world"); } } } } }