changeset 159:ecab03b50e08

*** empty log message ***
author kono
date Thu, 28 Aug 2008 16:38:35 +0900
parents 5cc8cd48bded
children 6a5974dd0368
files test/channeltest/testSeMaSlave.java
diffstat 1 files changed, 50 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/test/channeltest/testSeMaSlave.java	Thu Aug 28 16:36:18 2008 +0900
+++ b/test/channeltest/testSeMaSlave.java	Thu Aug 28 16:38:35 2008 +0900
@@ -4,8 +4,13 @@
 import java.net.SocketAddress;
 import java.util.LinkedList;
 import java.net.InetSocketAddress;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
 
 import rep.channel.REPLogger;
+import rep.channel.REPSelector;
+import rep.channel.REPServerSocketChannel;
+import rep.channel.REPSocketChannel;
 
 public class testSeMaSlave extends Thread{
 
@@ -26,54 +31,60 @@
 	}
 
 	public void run() {
-		SelectorSimulator<String> selector = new SelectorSimulator<String>();
+		REPSelector selector;
+		REPSocketChannel<String> masterCH ;
+		REPServerSocketChannel<String> scs = new REPServerSocketChannel<String>();
+		try {
+			selector = REPSelector.create();
+			masterCH = connectToMaster(selector);
+			scs.socket().bind(ownIP);
+			selector.register(scs, SelectionKey.OP_ACCEPT,null);
+			selector.register(masterCH, SelectionKey.OP_READ,null);
 
-		ChannelSimulator<String> masterCH = connectToMaster(selector);
-		ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector);
-		scs.bind(ownIP);
-		
-		selector.register(scs, SelectionKeySimulator.OP_ACCEPT);
-		selector.register(masterCH, SelectionKeySimulator.OP_READ);
-		ns.writeLog("SessionManager starts mainroutin.", 1);
+
+			ns.writeLog("SessionManager starts mainroutin.", 1);
 
-		/* Main Loop */
-		while(running){
+			/* Main Loop */
+			while(running){
 
-			try { selector.select(); }
-			catch (IOException e) { e.printStackTrace();}
+				selector.select(); 
 
-			for(SelectionKeySimulator<String> key : selector.selectedKeys()){
+				for(SelectionKey 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);
-					ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel();
-					String packet = channel.read();
-					if (channel==masterCH){
-						ns.writeLog(this, "receives String from master ==> `"+packet+"\'", 1);
-						for (ClientInfo ci: cis){
-							if (!packet.matches(ci.str)) continue;
-							ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'");
+					if(key.isAcceptable()){
+						ns.writeLog(this, "gets acceptable channel", 1);
+						REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel();
+						REPSocketChannel<String> 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);
+						REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel();
+						String packet = channel.read();
+						if (channel==masterCH){
+							ns.writeLog(this, "receives String from master ==> `"+packet+"\'", 1);
+							for (ClientInfo ci: cis){
+								if (!packet.matches(ci.str)) continue;
+								ci.channel.write("from "+this.getName()+": loopback packet from master ==>`"+packet+"\'");
+							}
+						}else{
+							ns.writeLog(this, "receives String==> `"+packet+"\'", 1);
+							//channel.write("from "+this.getName()+": slave");
+							masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor");
+							cis.add(new ClientInfo(packet, channel));
 						}
-					}else{
-						ns.writeLog(this, "receives String==> `"+packet+"\'", 1);
-						//channel.write("from "+this.getName()+": slave");
-						masterCH.write("from "+this.getName()+": receive String==>`"+packet+"\' from editor");
-						cis.add(new ClientInfo(packet, channel));
 					}
 				}
 			}
+		} catch (IOException e1) {
+			e1.printStackTrace();
 		}
-		
+
 	}
-	private ChannelSimulator<String> connectToMaster(SelectorSimulator<String> _selector) {
-		ChannelSimulator<String> channel = new ChannelSimulator<String>(ns, _selector);
+	
+	private REPSocketChannel<String> connectToMaster(Selector _selector) throws IOException {
+		REPSocketChannel<String> channel = REPSocketChannel.<String>create();
 		ns.writeLog(this, "is connecting to masterSeMa whose ip is"+masterIP, 1);
 		while(!channel.connect(masterIP)){
 			ns.writeLog(this, "SeMa not listen to socket yet, wait", 1);
@@ -86,8 +97,8 @@
 }
 class ClientInfo{
 	String str;
-	ChannelSimulator<String> channel;
-	ClientInfo(String _str, ChannelSimulator<String> _channel){
+	REPSocketChannel<String> channel;
+	ClientInfo(String _str, REPSocketChannel<String> _channel){
 		str = _str;
 		channel = _channel;
 	}