# HG changeset patch # User one # Date 1274796488 -32400 # Node ID d962eecaf9f5a9f43146adbf36ef006acccc5201 # Parent a1d796c0e975b0fcb0edb1c5beac834dafdaa166 Test done. diff -r a1d796c0e975 -r d962eecaf9f5 src/fdl/test/TestWaitRd.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/TestWaitRd.java Tue May 25 23:08:08 2010 +0900 @@ -0,0 +1,149 @@ +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(); + + } +}