# HG changeset patch # User suikwasha # Date 1350658091 -32400 # Node ID d24bcb819032b35b6d390231dd3063836e506182 # Parent 38a110b13db1103ff730a5f300fe29ca5ec918e3 trying to add Selector diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRoberts.java --- a/src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRoberts.java Fri Oct 19 00:05:41 2012 +0900 +++ b/src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRoberts.java Fri Oct 19 23:48:11 2012 +0900 @@ -15,7 +15,7 @@ { LinkedList algoList = new LinkedList(); - for(long num = 0;num <= 40;num ++){ + for(long num = 0;num <= 1000;num ++){ algoList.add(new ChangRobertsAlgorithm(num)); } diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRobertsAlgorithm.java --- a/src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRobertsAlgorithm.java Fri Oct 19 00:05:41 2012 +0900 +++ b/src/main/java/suikwasha/distributedalgorithm/algorithms/cr/ChangRobertsAlgorithm.java Fri Oct 19 23:48:11 2012 +0900 @@ -36,17 +36,17 @@ public void execute(Context _c) { Iterator ports = _c.getPorts().iterator(); - Port receiving = ports.next(); - Port sending = ports.next(); + Port in = ports.next(); + Port out = ports.next(); max = num; Message newMessage = new Message(toByteBuffer(false,max)); - sending.send(newMessage); + out.send(newMessage); while(true){ Message receivedMessage = null; try{ - receivedMessage = receiving.blockingReceive(); + receivedMessage = in.blockingReceive(); }catch(InterruptedException _e){ _e.printStackTrace(); } @@ -57,20 +57,20 @@ if(flagMax == (byte)1){ // maximum value detected. newMessage = receivedMessage.newMessage(toByteBuffer(true,value)); - sending.send(newMessage); + out.send(newMessage); //stop return; }else{ if(value == max){ newMessage = receivedMessage.newMessage(toByteBuffer(true,max)); - sending.send(newMessage); + out.send(newMessage); //stop return; } if(value > max){ max = value; newMessage = receivedMessage.newMessage(toByteBuffer(false,max)); - sending.send(newMessage); + out.send(newMessage); } } } diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/algorithms/krs/KorachRotemSantoro.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/suikwasha/distributedalgorithm/algorithms/krs/KorachRotemSantoro.java Fri Oct 19 23:48:11 2012 +0900 @@ -0,0 +1,32 @@ +package suikwasha.distributedalgorithm.algorithms.krs; + +import java.util.LinkedList; + +import suikwasha.distributedalgorithm.framework.Algorithm; +import suikwasha.distributedalgorithm.link.ReliableLinkBuilder; +import suikwasha.distributedalgorithm.machines.SimpleMachineBuilder; +import suikwasha.distributedalgorithm.simulator.Simulator; +import suikwasha.distributedalgorithm.simulator.Summary; +import suikwasha.distributedalgorithm.topologies.RingTopologyBuilder; + +public class KorachRotemSantoro +{ + public static void main(String _args[]) throws InterruptedException + { + LinkedList algoList = new LinkedList(); + + long num = 0; + + for(num = 0;num <= 10;num ++){ + algoList.add(new KorachRotemSantoroAlgorithm(num,false,true)); + } + + ReliableLinkBuilder linkBuilder = new ReliableLinkBuilder(); + SimpleMachineBuilder machineBuilder = new SimpleMachineBuilder(); + RingTopologyBuilder ringBuilder = new RingTopologyBuilder(); + + Simulator sim = new Simulator(algoList,linkBuilder,machineBuilder,ringBuilder); + Summary sum = sim.startSimulation(); + sum.print(); + } +} diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/algorithms/krs/KorachRotemSantoroAlgorithm.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/suikwasha/distributedalgorithm/algorithms/krs/KorachRotemSantoroAlgorithm.java Fri Oct 19 23:48:11 2012 +0900 @@ -0,0 +1,152 @@ +package suikwasha.distributedalgorithm.algorithms.krs; + +import java.nio.ByteBuffer; +import java.util.Iterator; + +import fj.P; +import fj.P2; + +import suikwasha.distributedalgorithm.framework.Algorithm; +import suikwasha.distributedalgorithm.framework.Context; +import suikwasha.distributedalgorithm.framework.Message; +import suikwasha.distributedalgorithm.framework.Port; + +public class KorachRotemSantoroAlgorithm implements Algorithm +{ + private final boolean isInitProcess; + private final boolean direction; + private final long num; + private long max; + + public static final int MESSAGE_SIZE = 1 + 8; // size of (byte + long) in java + + public KorachRotemSantoroAlgorithm(long _num,boolean _direction,boolean _isInitProcess) + { + num = _num; + max = -1; + direction = _direction; + isInitProcess = _isInitProcess; + } + + /* + * message format. + * flag | long value + * 0xFF | 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF + */ + public static ByteBuffer s(boolean _flagMax,long _value) + { + ByteBuffer b = ByteBuffer.allocate(MESSAGE_SIZE); + b.put((_flagMax) ? (byte)1 : (byte)0); + b.putLong(_value); + b.rewind(); + return b; + } + + public P2 RECV(Port... ports) + { + while(true){ + for(Port p : ports){ + Message message = p.tryReceive(); + if(message != null){ + return P.p(message,p); + } + try{ + Thread.sleep(1); + }catch(Exception _e){ + _e.printStackTrace(); + } + } + } + + } + + public void execute(Context _c) + { + Iterator ports = _c.getPorts().iterator(); + Port left,right; + if(direction){ + left = ports.next(); + right = ports.next(); + }else{ + right = ports.next(); + left = ports.next(); + } + + Message newMessage,receivedMessage; + + if(isInitProcess){ + /* + * initial process starts from here. + */ + max = num; + right.send(new Message(s(false,max))); + }else{ + /* + * waiting process starts from here. + */ + + P2 ret = RECV(right,left); + receivedMessage = ret._1(); + Port receivedPort = ret._2(); + + ByteBuffer b = receivedMessage.getMessage(); + long value = b.getLong(1); // see message format above + max = Math.max(num,value); + + newMessage = receivedMessage.newMessage(s(false,max)); + if(receivedPort == right){ + left.send(newMessage); + }else{ + right.send(newMessage); + } + } + + /* + * label : L in text book. + */ + while(true){ + P2 ret = RECV(right,left); + + receivedMessage = ret._1(); + Port x = ret._2(); + + ByteBuffer b = receivedMessage.getMessage(); + byte flagMax = b.get(); + long value = b.getLong(); + if(max == value){ + newMessage = receivedMessage.newMessage(s(true,max)); + if(x == right){ + left.send(newMessage); + }else{ + right.send(newMessage); + } + + // stop; + return; + } + + if(flagMax == 1){ + max = value; + newMessage = receivedMessage.newMessage(s(true,max)); + if(x == right){ + left.send(newMessage); + }else{ + right.send(newMessage); + } + + // stop; + return; + } + + if(value > max){ + max = value; + newMessage = receivedMessage.newMessage(s(false,max)); + if(x == right){ + left.send(newMessage); + }else{ + right.send(newMessage); + } + } + } + } +} diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/algorithms/peterson/Perterson.java --- a/src/main/java/suikwasha/distributedalgorithm/algorithms/peterson/Perterson.java Fri Oct 19 00:05:41 2012 +0900 +++ b/src/main/java/suikwasha/distributedalgorithm/algorithms/peterson/Perterson.java Fri Oct 19 23:48:11 2012 +0900 @@ -15,7 +15,7 @@ { LinkedList algoList = new LinkedList(); - for(long num = 0;num <= 10;num ++){ + for(long num = 0;num <= 1000;num ++){ algoList.add(new PertersonAlgorithm(num)); } diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/framework/Port.java --- a/src/main/java/suikwasha/distributedalgorithm/framework/Port.java Fri Oct 19 00:05:41 2012 +0900 +++ b/src/main/java/suikwasha/distributedalgorithm/framework/Port.java Fri Oct 19 23:48:11 2012 +0900 @@ -3,6 +3,9 @@ public interface Port { public void send(Message _mes); + public boolean isReady(); public Message blockingReceive() throws InterruptedException; public Message tryReceive(); + + public void register(Selector _s); } diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/framework/Selector.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/suikwasha/distributedalgorithm/framework/Selector.java Fri Oct 19 23:48:11 2012 +0900 @@ -0,0 +1,42 @@ +package suikwasha.distributedalgorithm.framework; + +import java.util.LinkedList; +import java.util.concurrent.SynchronousQueue; + +public class Selector +{ + private final SynchronousQueue queue; + private final LinkedList watchList; + + public Selector() + { + queue = new SynchronousQueue(); + watchList = new LinkedList(); + } + + public void signal(Port _p) + { + queue.offer(_p); + } + + public void register(Port _p) + { + watchList.add(_p); + } + + public Port select() throws InterruptedException + { + Port availablePort; + int length = watchList.size(); + for(int i = 0;i < length;i ++){ + availablePort = watchList.poll(); + if(availablePort.isReady()){ + watchList.addLast(availablePort); + return availablePort; + } + watchList.addLast(availablePort); + } + + return queue.take(); + } +} diff -r 38a110b13db1 -r d24bcb819032 src/main/java/suikwasha/distributedalgorithm/simulator/Summary.java --- a/src/main/java/suikwasha/distributedalgorithm/simulator/Summary.java Fri Oct 19 00:05:41 2012 +0900 +++ b/src/main/java/suikwasha/distributedalgorithm/simulator/Summary.java Fri Oct 19 23:48:11 2012 +0900 @@ -60,7 +60,7 @@ { System.out.println("Summary:"); System.out.println(String.format("MessageComplexity :\t\t%d",messageComplexity.get())); - System.out.println(String.format("MessageBitComplexity :\t\t%d",messageComplexity.get())); + System.out.println(String.format("MessageBitComplexity :\t\t%d",bitComplexity.get())); System.out.println(String.format("TimeComplexity :\t\t%d",longestMessageChain.get().getMessageCount())); } } diff -r 38a110b13db1 -r d24bcb819032 target/classes/META-INF/maven/suikwasha/distributedalgorithm/pom.properties --- a/target/classes/META-INF/maven/suikwasha/distributedalgorithm/pom.properties Fri Oct 19 00:05:41 2012 +0900 +++ b/target/classes/META-INF/maven/suikwasha/distributedalgorithm/pom.properties Fri Oct 19 23:48:11 2012 +0900 @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Fri Oct 19 00:04:33 JST 2012 +#Fri Oct 19 23:47:17 JST 2012 version=0.0.1-SNAPSHOT groupId=suikwasha m2e.projectName=distributedalgorithm