changeset 62:4d8c688b1d3c

object ring for cluster add time
author axmo
date Mon, 23 Feb 2009 20:09:32 +0900
parents def989051b0c
children b342dc9b52eb
files src/fdl/test/transfer/cluster/HostCheck.java src/fdl/test/transfer/cluster/ProtocolEngine.java src/fdl/test/transfer/cluster/Server.java
diffstat 3 files changed, 199 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/fdl/test/transfer/cluster/HostCheck.java	Thu Feb 19 23:05:04 2009 +0900
+++ b/src/fdl/test/transfer/cluster/HostCheck.java	Mon Feb 23 20:09:32 2009 +0900
@@ -4,13 +4,31 @@
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
-//import java.net.*;
+import java.net.*;
 
 public class HostCheck {
+	private static String bufstring;
+	private static String txt;
+	private static Integer ring;
+	private static int bufsize;
+	private static String nextHost;
+	private static int chknum;
+
 	public static void main(String[] args) {
-//		String localhost = null;
+		String localhost = null;
 		BufferedReader br = null;
-		String hostTable[] = new String[10];
+		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("引数指定の誤り:未知の引数が指定されました");
+            }
+		}
+		String hostTable[] = new String[ring+1];
+		bufsize = Integer.parseInt(bufstring);
 		try {
 			br = new BufferedReader(new FileReader("nodelist"));
 			int i = 0;
@@ -19,20 +37,28 @@
 				hostTable[i] = line;
 				i++;
 			}
-			System.out.println("ホストは");
+			localhost = InetAddress.getLocalHost().getHostName();
 			for (int j=0; j<hostTable.length; j++){
-				System.out.println(hostTable[j]);
+				if(localhost.equals(hostTable[j])){
+				nextHost = hostTable[++j];
+				if(j == ring) {
+					nextHost = hostTable[0];
+				}
+			}
 			}
-			System.out.println("です");
-//			localhost = InetAddress.getLocalHost().getHostName();
+			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();
 		}
-//		if (localhost == hostTable[0]){
-		ProtocolEngine pe = new ProtocolEngine(hostTable[0], hostTable[1]);
+		ProtocolEngine pe = new ProtocolEngine(localhost, nextHost,chknum, bufsize, txt);
 		pe.start();
-//		}
 	}
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fdl/test/transfer/cluster/ProtocolEngine.java	Mon Feb 23 20:09:32 2009 +0900
@@ -0,0 +1,147 @@
+package fdl.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();
+		}
+		
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fdl/test/transfer/cluster/Server.java	Mon Feb 23 20:09:32 2009 +0900
@@ -0,0 +1,16 @@
+package fdl.test.transfer.cluster;
+
+import fdl.FDLindaServ;
+
+public class Server extends Thread{
+	int port;
+
+	public Server() {
+	}
+
+	public void run(){
+		String[] args = {"-p",Integer.toString(port)};
+		FDLindaServ.main(args);
+	}
+
+}
\ No newline at end of file