view src/fdl/test/ComDebug_Client.java @ 93:a1d796c0e975 fuchita

Wait Read Tester
author one
date Tue, 25 May 2010 22:57:54 +0900
parents 046feb56a196
children
line wrap: on
line source

package fdl.test;


import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedList;

import fdl.FederatedLinda;
import fdl.PSX;
import fdl.PSXCallback;
import fdl.PSXLinda;

/*
 * それぞれのLinda Serverのmeta protocolに接続して、順々にモニタデータを
 * 受け取って表示する。
 */
public class ComDebug_Client {

	static int id;
	static final boolean debug = false;
	PSXCallback debugCallback ; 

	FederatedLinda fdl;
	PSXLinda psx;
	LinkedList<String> hosts = new LinkedList<String>();
	LinkedList<Integer> ports = new LinkedList<Integer>();
	LinkedList<PSXLinda> psxs = new LinkedList<PSXLinda>();

	public static void main(String[] args) {
		ComDebug_Client com = new ComDebug_Client();
		com.run(args);
	}

	public void run(String[] args) {
		int connect_num = 1;
		final String usages = "usage: ComDebug_Client [-h host -p port]*";
		//引数判定
		try {
			if (args.length < 2) {
				System.err.println(usages);
			}

			for (int i=0; i<args.length; ++i) {
				if("-h".equals(args[i])) {
					hosts.add(args[++i]);
					System.err.println("host = "+hosts.getLast());
				} else if("-p".equals(args[i])) {
					ports.add(Integer.parseInt(args[++i]));
					System.err.println("port = "+ports.getLast());
				} else {
					System.err.println(usages); return;
				}
			}
			if (hosts.size()!=ports.size()) {
				System.err.println("-h and -p pairs are always necessary.");
				System.exit(1);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}


		try {
			fdl = FederatedLinda.init();
			for(int i=0;i<hosts.size();i++) {
				String host = hosts.get(i);
				int port = ports.get(i);

				psx = fdl.open(host,port);
				psxs.add(psx);
				PSXCallback checkid = new PSXCallback() {
					public void callback(ByteBuffer reply) {
						int p = psxs.indexOf(psx);
						System.out.println("PSXReply("+p+") =>"+reply);
					}
				};
				psx.in(65535, checkid);

				System.out.println("COM_DEBUG Connected.["+host+":"+port+"]");
				psx.out(PSX.META_MONITOR, null);
				debugCallback = 
					new PSXCallback() {
					public void callback(ByteBuffer reply) {
						System.out.println("COM_DEBUG: "+PSX.getdataString(reply));
						psx.out(PSX.META_MONITOR, null);
						psx.in(PSX.META_MONITOR_DATA,debugCallback); 
					}
				};
				psx.out(PSX.META_MONITOR, null);
				psx.in(PSX.META_MONITOR_DATA,debugCallback); 

				connect_num++;
			}
			while(true) {
				fdl.sync(1000);
			}
		} catch (IOException nfex) {
			nfex.printStackTrace();
			System.out.println("Faild.");
			return;
		}
	}
}