Mercurial > hg > Members > shoshi > distributedalgorithm
diff src/main/java/suikwasha/distributedalgorithm/link/ReliableLinkBuilder.java @ 0:38a110b13db1
added SimpleDistributedAlgorithmFramework.
added NaiveAlgorithm
added ChangRobertsAlgorithm
added PertersonAlgorithm
author | suikwasha |
---|---|
date | Fri, 19 Oct 2012 00:05:41 +0900 |
parents | |
children | 8e1f63faa2fd |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/suikwasha/distributedalgorithm/link/ReliableLinkBuilder.java Fri Oct 19 00:05:41 2012 +0900 @@ -0,0 +1,67 @@ +package suikwasha.distributedalgorithm.link; + +import java.util.concurrent.LinkedBlockingQueue; + +import suikwasha.distributedalgorithm.framework.Link; +import suikwasha.distributedalgorithm.framework.LinkBuilder; +import suikwasha.distributedalgorithm.framework.Message; +import suikwasha.distributedalgorithm.framework.Port; + +public class ReliableLinkBuilder implements LinkBuilder +{ + public Link build() + { + return new LinkImpl(); + } + + private static class LinkImpl implements Link + { + private final Port port1,port2; + + public LinkImpl() + { + LinkedBlockingQueue<Message> queue1 = new LinkedBlockingQueue<Message>(); + LinkedBlockingQueue<Message> queue2 = new LinkedBlockingQueue<Message>(); + + port1 = new PortImpl(queue1,queue2); + port2 = new PortImpl(queue2,queue1); + } + + public Port getPort1() + { + return port1; + } + + public Port getPort2() + { + return port2; + } + } + + private static class PortImpl implements Port + { + private final LinkedBlockingQueue<Message> in; + private final LinkedBlockingQueue<Message> out; + + public PortImpl(LinkedBlockingQueue<Message> _in,LinkedBlockingQueue<Message> _out) + { + in = _in; + out = _out; + } + + public void send(Message _mes) + { + out.add(_mes); + } + + public Message blockingReceive() throws InterruptedException + { + return in.take(); + } + + public Message tryReceive() + { + return in.poll(); + } + } +}