view src/fdl/test/transfer/cluster/MetaProtocolEngine.java @ 64:3e051060d845

fix senddata
author axmo
date Thu, 26 Feb 2009 08:22:08 +0900
parents def989051b0c
children 82a292aa41ad
line wrap: on
line source

package fdl.test.transfer.cluster;

import java.io.*;
//import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;


import fdl.MetaEngine;
import fdl.MetaLinda;
import fdl.PSXLinda;
import fdl.PSXReply;

public class MetaProtocolEngine implements MetaEngine {
	private int id = 10;
	
	private PSXLinda psxget;
	MetaLinda fdlmeta;
	String host;
	int port = 10000;
	private PSXLinda psxsend;
	private int chknum;
	
	SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

	private int bufsize;

	private String time;
	private long timelong;
	private String start;
	private String txt;
	private String transfer;
	private long transferlong;
	
	public MetaProtocolEngine(MetaLinda ml, String host, int chknum, int bufsize, String txt) {
		this.host = host;
		this.fdlmeta = ml;
		this.chknum = chknum;
		this.bufsize = bufsize;
		this.txt = txt;
	}

	public void mainLoop(){
		psxget = fdlmeta;
		if(chknum == 1){
			try {
				sendData();
				start = DF.format(new Date());
				System.out.println("Start Time => "+start);
			} catch (IOException e) {
				e.printStackTrace();
			}
			}
		boolean connect = true;
		while(connect){
		try {
			initConnect();
			transfer = DF.format(new Date());
			transferlong = new Date().getTime();
			System.out.println("transfer Time => "+transfer);
			for(int l=0; l<1000; l++){
			transfer();
			}
			dataChk();
			write();
			connect = false;
		} catch (IOException e) {
			try {
				Thread.sleep(40);
			} catch (InterruptedException e1) {
			}
		}
		}
	}

	private void initConnect(){
		boolean connectpsx = true;
		while(connectpsx){
		try {
			psxsend = fdlmeta.open(host,port);
			connectpsx = false;
		} catch (IOException e) {
			try {
				Thread.sleep(40);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
		}
	}
	
	private void sendData() throws IOException{
		boolean connectSend = true;
		ByteBuffer send = ByteBuffer.allocate(bufsize);
		byte[] fdata = new byte[bufsize];
//		fdata = InetAddress.getLocalHost().getAddress();
		for(int n=0; n < fdata.length; n++ ){
			fdata[n] = 1;
		}
		send.put(fdata);
		send.flip();
		while(connectSend){
		psxget.out(id, send);
		psxget.sync(1);
		System.out.println("Send Data");
		connectSend = false;
		}
	}

	public void transfer() throws IOException {
		ByteBuffer data = ByteBuffer.allocate(bufsize);
		boolean running = true;
		PSXReply in = psxget.in(id);
		while (running) {
			if(in.ready()){
				data = in.getData();
				psxsend.out(id,data);
				System.out.println("connect to => "+host);
				psxsend.sync(1);
				running = false;
				break;
			}
			fdlmeta.sync();
		}
	}
	
	public void dataChk() throws IOException{
		fdlmeta.sync(1);
		boolean running2 = true;
//		ByteBuffer data2 = ByteBuffer.allocate(bufsize);
		PSXReply in2 = psxget.in(id);
//		byte[] fdata2 = new byte[bufsize];
//		byte[] fdata3 = new byte[bufsize];
//		fdata = InetAddress.getLocalHost().getAddress();
//		for(int n=0; n < fdata2.length; n++ ){
//			fdata2[n] = 1;
//		}
		while (running2) {
			psxget.sync(1);
			if(in2.ready()) {
//				data2 = in2.getData();
//				data2.get(fdata3);
//				if (fdata2 == fdata3){
				time = DF.format(new Date());
				timelong = new Date().getTime();
				System.out.println("Around Time => "+time);
//				}
			running2 = false;
			break;
			}
		}
	}
	
	
	public 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();
	}
}