view src/fdl/test/TestWaitRd.java @ 94:d962eecaf9f5 fuchita

Test done.
author one
date Tue, 25 May 2010 23:08:08 +0900
parents
children 96c63bc659d4
line wrap: on
line source

package fdl.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

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


public class TestWaitRd {

	public FederatedLinda fdl;
	public FDLindaServ fds;
	public static final int PORT = 10000;

	class Server implements Runnable {
		public void run() {
			String[] args = {"-p",Integer.toString(PORT)};
			FDLindaServ.main(args);
		}
	}

	class Client implements Runnable {
		public void run() {
			String[] args = {};
			sleep(2000);
			main(args);
		}
		public synchronized void sleep(int time) {
			try {
				wait(time);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		public void main(String[] arg) {
			try {
				PSXLinda psx = openLinda();
				ByteBuffer data = sendData(psx,1,0);

				psx.waitRd(1, new PSXCallback() {	public void callback(ByteBuffer reply) {read_wait(reply,"First read");} });
				psx.sync(1);
				sendData(psx,1,1);
				psx.sync(1);
				

				sleep(1000);

				psx.waitRd(1, new PSXCallback() {	public void callback(ByteBuffer reply) {read_wait(reply,"2nd read");} });
				psx.sync(1);
				sendData(psx,1,2);
				psx.sync(1);
				sleep(1000);


				for(int i=3;i<10;i++) {
					final int j = i;
					psx.waitRd(1, new PSXCallback() {public void callback(ByteBuffer reply) {read_wait(reply,""+j+"th read");} });
					psx.sync(1);
					sendData(psx,1,i);
					psx.sync(1);
				}
				sleep(1000);
				
				data.clear();
				psx.out(PSX.META_STOP, data);
				psx.sync(1);

			} catch (IOException e) {
				System.err.println("Communication failure.");
			}
		}
		
		public void read_wait(ByteBuffer r, String mesg) {
			System.out.println(mesg);
			System.out.println(r.getInt());
			System.out.println("");
		}
		
		private PSXLinda openLinda() throws IOException {
			FederatedLinda fdl;
			PSXLinda psx;
			PSXReply r;
			String host;
			InetSocketAddress localAddress;
			int port = PORT;
			try {
				localAddress = new InetSocketAddress(InetAddress.getLocalHost(), port);
				host = localAddress.getHostName();
			} catch (UnknownHostException e) {
				host = "localhost";
			}
			fdl = FederatedLinda.init();
			psx = fdl.open(host,port);
			r = psx.in(65535);
			fdl.sync(1);
			System.out.println("Connected.");
			int cnt=0;
			while(!r.ready()) {
				// psx.sync(1000);
				psx.sync();
				System.out.println("Waiting...."+(cnt++));
			}
			print_id(r);
			return psx;
		}
		
		private ByteBuffer sendData(PSXLinda psx,int id, int n) {
			ByteBuffer data = ByteBuffer.allocate(10);
			data.putInt(n);
			data.flip();
			psx.out(id,data);
			return data;
		}


		public void print_id (PSXReply ans) throws IOException {
			ByteBuffer r = ans.getData();
			System.out.print("ID = ");
			System.out.write(r.array());
			System.out.println("");
		}
	}

	public static void main(String[] arg) throws InterruptedException {
		TestWaitRd me = new TestWaitRd();
		me.test1();
	}

	public void test1() throws InterruptedException {
		Server s = new Server();
		Client c = new Client();
		Thread s1 = new Thread(s);
		Thread c1 = new Thread(c);
		s1.start();
		c1.start();
		s1.join();
		c1.join();
		
	}
}