view src/fdl/test/transfer/cluster/MetaProtocolEngine.java @ 59:a372992d0ac2

cluster plus date
author axmo
date Tue, 17 Feb 2009 17:24:37 +0900
parents c33abea8cddc
children 673085a02ccb
line wrap: on
line source

package fdl.test.transfer.cluster;

import java.io.IOException;
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 ByteBuffer data = ByteBuffer.allocate(10);
	private int chknum;
	
	public MetaProtocolEngine(MetaLinda ml, String host, int chknum) {
		this.host = host;
		this.fdlmeta = ml;
		this.chknum = chknum;
	}

	public void mainLoop(){
		psxget = fdlmeta;
		if(chknum == 1){
			try {
				sendData();
			} catch (IOException e) {
				e.printStackTrace();
			}
			}
		boolean connect = true;
		while(connect){
		try {
			initConnect();
			transfer();
			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(1024);
		send.putInt(12);
		send.flip();
		while(connectSend){
		psxget.out(id, send);
		psxget.sync(1);
		System.out.println("Send Data");
		connectSend = false;
		}
	}

	private void transfer() throws IOException {
		boolean running = true;
		PSXReply in = psxget.in(id);
		while (running) {
			if(in.ready()){
				data = in.getData();
				int i = data.getInt();
				data.rewind();
				System.out.println("transfer Data => "+i);
				//outしたbytebufferの変更をこれ以降やっちゃいけない
				psxsend.out(id,data);
				SimpleDateFormat DF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
				System.out.println(DF.format(new Date()));
				System.out.println("connect to => "+host);
				psxsend.sync();
				running = false;
				break;
			}
			fdlmeta.sync();
		}
	}

	
}