Mercurial > hg > FederatedLinda
changeset 50:0025a8582d5f
Federated Linda Ring three
author | axmo |
---|---|
date | Fri, 12 Dec 2008 19:38:40 +0900 |
parents | 282d42692403 |
children | 454a5376232f |
files | src/fdl/test/transfer/three/ProtocolEngine.java src/fdl/test/transfer/three/Server.java src/fdl/test/transfer/three/TestGet.java src/fdl/test/transfer/three/TestSend.java src/fdl/test/transfer/three/TestTransfer.java |
diffstat | 5 files changed, 256 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/transfer/three/ProtocolEngine.java Fri Dec 12 19:38:40 2008 +0900 @@ -0,0 +1,82 @@ +package fdl.test.transfer.three; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class ProtocolEngine extends Thread{ + private PSXLinda psx; + private String name; + FederatedLinda fdl1; + String host = "127.0.0.1"; + int port1; + int port2; + private int id = 10; + private PSXLinda psx1; + private boolean running = true; + private ByteBuffer data2 = ByteBuffer.allocate(10); + private int count = 1; + + public ProtocolEngine(String string, int port1, int port2) { + this.name = string; + this.port1 = port1; + this.port2 = port2; + } + + public void run(){ + fdl1 = FederatedLinda.init(); + boolean connect = true; + while(connect){ + try { + initConnect(); + transfer(); + connect = false; + } catch (IOException e) { + try { + Thread.sleep(40); + } catch (InterruptedException e1) { + } + } + } + } + + private void initConnect() throws IOException { + System.out.println(name); + psx = fdl1.open(host,port1); + System.out.println("Connect "+port1); + psx1 = fdl1.open(host,port2); + System.out.println("Connect "+port2); + } + + private void transfer() throws IOException { + PSXReply in = psx.in(id); + System.out.println("PSXReply "+port1 +": "+port2 +": "); + while (running) { + if(in.ready()){ + data2 = in.getData(); + int i = data2.getInt(); + data2.rewind(); + //outしたbytebufferの変更をこれ以降やっちゃいけない + psx1.out(id,data2); + + System.out.println("IntData0 "+port1 +": "+port2 +": " +i); + running = false; + System.out.println("IntData1 "+port1 +": "+port2 +": " +i); + fdl1.sync(0); + if (count++>3) { + running = false; + break; + } + System.out.println("IntData2 "+port1 +": "+port2 +": " +i); + break; + } + fdl1.sync(1); + } + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/transfer/three/Server.java Fri Dec 12 19:38:40 2008 +0900 @@ -0,0 +1,20 @@ +package fdl.test.transfer.three; + +import fdl.FDLindaServ; + +public class Server extends Thread { + int port; + private String name; + + public Server(String string, int i) { + port = i; + name = string; + } + + public void run(){ + String[] args = {"-p",Integer.toString(port)}; + System.out.println(name+"start"); + FDLindaServ.main(args); + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/transfer/three/TestGet.java Fri Dec 12 19:38:40 2008 +0900 @@ -0,0 +1,55 @@ +package fdl.test.transfer.three; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + + +public class TestGet extends Thread { + PSXLinda psxget; + FederatedLinda fdlget; + private int port; + String host = "127.0.0.1"; + private ByteBuffer data = ByteBuffer.allocate(10); + + public TestGet(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + fdlget = FederatedLinda.init(); + boolean connecttest = true; + while(connecttest){ + try { + getData(id); + i = data.getInt(); + System.out.println(i); + connecttest = false; + } catch (IOException e) { + e.printStackTrace(); + connecttest = true; + } + } + } + + private void getData(int id) throws IOException { + psxget = fdlget.open(host, port); + PSXReply get = psxget.in(id); + boolean running = false; + while(running){ + if(get.ready()){ + data = get.getData(); + running = false; + fdlget.sync(0); + break; + } + fdlget.sync(); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/transfer/three/TestSend.java Fri Dec 12 19:38:40 2008 +0900 @@ -0,0 +1,48 @@ +package fdl.test.transfer.three; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.FederatedLinda; +import fdl.PSXLinda; + +public class TestSend extends Thread { + PSXLinda psxsend; + FederatedLinda fdlsend; + private int port; + + public TestSend(int port1) { + this.port = port1; + } + + public void run(){ + int id = 10; + int i; + String host = "127.0.0.1"; + ByteBuffer send = ByteBuffer.allocate(1024); + send.putInt(12); + send.flip(); + i = send.getInt(); + send.rewind(); + fdlsend = FederatedLinda.init(); + boolean connect = true; + while(connect) { + try{ + psxsend = fdlsend.open(host,port); + System.out.println("Set Data = " +i); + System.out.println("Connect Host1"); + psxsend.out(id, send); + System.out.println("regist Que"); + fdlsend.sync(1); + System.out.println("Send Data"); + connect = false; + }catch (IOException e) { + try { + Thread.sleep(20); + } catch (InterruptedException e1) { + } + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/transfer/three/TestTransfer.java Fri Dec 12 19:38:40 2008 +0900 @@ -0,0 +1,51 @@ +package fdl.test.transfer.three; + +import static org.junit.Assert.*; +import org.junit.Test; + + +public class TestTransfer { + Server[] servers; + ProtocolEngine[] engines; + final int serverCount = 3; + final int startPort = 10011; + @Test + public void testTransfer() { + int port = startPort; + servers = new Server[serverCount]; + engines = new ProtocolEngine[serverCount]; + + for (int i = 0; i < serverCount; i++) { + servers[i] = new Server("Server"+(i+1),port+i); + servers[i].start(); + } + + TestSend send = new TestSend(startPort); + send.start(); + try{ + send.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + for (int i = 0; i < serverCount; i++) { + engines[i] = new ProtocolEngine("ProtocolEngine "+(i+1) +":port " +(port+i) +":port "+(netporot(port+i)),port+i,netporot(port+i)); + engines[i].start(); + } + try { + for (ProtocolEngine e: engines){ + e.join(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + assertEquals(1,1); + } + private int netporot(int i) { + i++; + if(i >= startPort+serverCount) { + return startPort; + } + return i; + } +}