view src/fdl/old/test/transfer/cluster/ProtocolEngine.java @ 114:3b877c9a44f5

gather old packages
author kazz
date Mon, 11 Oct 2010 14:29:37 +0900
parents src/fdl/test/transfer/cluster/ProtocolEngine.java@4d8c688b1d3c
children
line wrap: on
line source

package fdl.old.test.transfer.cluster;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

import fdl.FederatedLinda;
import fdl.PSXLinda;
import fdl.PSXReply;


public class ProtocolEngine extends Thread{
	static int id = 10;
	static FederatedLinda fdl;
	static PSXLinda getpsx;
	static PSXLinda sendpsx;
	static int port = 10000;
	String getHost = null;
	String sendHost = null;
	private int chknum;
	private int bufsize;
	private String txt;
	SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
	private String time;
	private long timelong;
	private String start;
	private String transfer;
	private long transferlong;
	
	public ProtocolEngine(String gethost, String sendhost, int chknum, int bufsize, String txt) {
		this.getHost = gethost;
		this.sendHost = sendhost;
		this.chknum = chknum;
		this.bufsize = bufsize;
		this.txt = txt;
	}

	public void run(){
		fdl = FederatedLinda.init();
		try {
			getpsx = fdl.open(getHost,port);
			System.out.println("Connect Host1");
			sendpsx = fdl.open(sendHost,port);
			System.out.println("Connect Host2");
			// Host1にデータを送信する。
			if(chknum == 1){
			testSend(getpsx);
			start = DF.format(new Date());
			System.out.println("Start Time => "+start);
			}
			// psxにデータを用意
			transfer(getpsx,sendpsx);
			dataChk();
			write();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void write() throws IOException {
		long result = (timelong - transferlong);
		String diff = String.valueOf(result);
		File read = new File("resultTime"+bufsize+"_"+txt+".txt");
		read.createNewFile();
		FileWriter filewrite = new FileWriter(read);
		if (start != "null"){
		filewrite.write("ServerStart => "+start+"\r\n");
		}
		if (transfer != "null") {
		filewrite.write("TransferTime => "+transfer+"\r\n");
		}
		if (time != "null"){
		filewrite.write("AroundTime => "+time+"\r\n");
		}
		if (diff != "null"){
		filewrite.write("DiffTime => "+diff+"\r\n");
		}
		filewrite.close();
	}

	private void dataChk() throws IOException {
		fdl.sync(1);
		boolean running2 = true;
		ByteBuffer data3 = ByteBuffer.allocate(bufsize);
		PSXReply in2 = getpsx.in(id);
		int localhost2 = InetAddress.getLocalHost().hashCode();
		while (running2) {
			getpsx.sync(1);
			if(in2.ready()) {
			data3 = in2.getData();
			int i = data3.getInt();
			if (i == localhost2){
				time = DF.format(new Date());
				timelong = new Date().getTime();
				System.out.println("Around Time => "+time);
			}
			running2 = false;
			break;
			}
		}
		
	}

	private void testSend(PSXLinda psx2) throws IOException {
		boolean connectSend = true;
		ByteBuffer send = ByteBuffer.allocate(bufsize);
		int localhost = InetAddress.getLocalHost().hashCode();
		send.putInt(localhost);
		send.flip();
		while(connectSend){
		psx2.out(id, send);
		psx2.sync(1);
		System.out.println("Send Data");
		connectSend = false;
		}
	}
	

	private void transfer(PSXLinda getpsx, PSXLinda sendpsx) throws IOException {
		ByteBuffer data2 = ByteBuffer.allocate(bufsize);
		PSXReply in = getpsx.in(id);
		boolean running = true;
		while (running) {
			if(in.ready()){
				//psx1にデータを書き出し
				data2 = in.getData();
				sendpsx.out(id,data2);
				//runningフラグをfalseする
				running = false;
				transfer = DF.format(new Date());
				transferlong = new Date().getTime();
				System.out.println("transfer Time => "+transfer);
				System.out.println("connect to => "+sendHost);
				sendpsx.sync(1);
				break;
			}
			fdl.sync();
		}
		
	}

}