view src/fdl/test/transfer/cluster/FDLServWithSendMeta.java @ 97:0ea086f0e96f fuchita

main loop modification, for easy meta engine addition. add comments.
author one
date Wed, 26 May 2010 10:49:50 +0900
parents 5336bafaaf48
children
line wrap: on
line source

package fdl.test.transfer.cluster;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import fdl.FDLindaServ;
import fdl.MetaLinda;


public class FDLServWithSendMeta extends FDLindaServ {

	public FDLServWithSendMeta(int port) throws IOException {
		super(port);
	}
	
	static String nextHost = null;
	private static int localport = 10000;
	static int chknum;
	private static String bufstring;
	private static int bufsize;
	private static String txt;
	private static int ring;
	
	@Override public void mainLoop() {
		MetaLinda ml = new MetaLinda(tupleSpace, this);
		MetaProtocolEngine mpe = new MetaProtocolEngine(ml, nextHost, chknum, bufsize, txt);
		mpe.mainLoop(ml);
	}
	
	public static void main(String[] args){
		for (int k=0; k<args.length; ++k) {
			if("-bufsize".equals(args[k])){
				bufstring = args[++k];
			} else if("-ring".equals(args[k])){
				txt = args[++k];
				ring = Integer.valueOf(txt);
			} else {
				System.err.println("引数指定の誤り:未知の引数が指定されました");
            }
		}
		bufsize = Integer.parseInt(bufstring);
		try {
			chkhost();
			FDLServWithSendMeta serv = new FDLServWithSendMeta(localport);
			serv.mainLoop();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void chkhost() throws UnknownHostException{
		String localhost = null;
		BufferedReader br = null;
		String hostTable[] = new String[ring+1];
		try {
			br = new BufferedReader(new FileReader("nodelist"));
			int i = 0;
			while (br.ready()) {
				String line = br.readLine();
				hostTable[i] = line;
				i++;
			}
			localhost = InetAddress.getLocalHost().getHostName();
			for (int j=0; j<hostTable.length; j++) {
				if(localhost.equals(hostTable[j])){
					nextHost = hostTable[++j];
					if(j == ring) {
						nextHost = hostTable[0];
					}
				}
			}
			
			if(localhost.equals(hostTable[0])) {
				chknum = 1;
			}
				
			System.out.println("localhost:"+localhost);
			System.out.println("nexthost:"+nextHost);
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
	
}