view test/channeltest/testSeMaSlave.java @ 159:ecab03b50e08

*** empty log message ***
author kono
date Thu, 28 Aug 2008 16:38:35 +0900
parents f0d80a64aba0
children 5b4be02e7243
line wrap: on
line source

package test.channeltest;

import java.io.IOException;
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{

	SocketAddress ownIP;
	SocketAddress masterIP;
	boolean running=true;
	REPLogger ns = new REPLogger();
	LinkedList<ClientInfo> cis;
	
	public testSeMaSlave(String name, String oname,int oport, String mname,int  mport){
		super(name);
		ownIP = new InetSocketAddress(oname,oport);
		masterIP = new InetSocketAddress(mname,mport);
		cis = new LinkedList<ClientInfo>();
	}
	public void init(){
		
	}

	public void run() {
		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);


			ns.writeLog("SessionManager starts mainroutin.", 1);

			/* Main Loop */
			while(running){

				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 = 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));
						}
					}
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}
	
	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);
			Thread.yield();
		}
		ns.writeLog(this, "connecting was successful.", 1);

		return channel;
	}
}
class ClientInfo{
	String str;
	REPSocketChannel<String> channel;
	ClientInfo(String _str, REPSocketChannel<String> _channel){
		str = _str;
		channel = _channel;
	}
}