+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1init99f4f39

master
1.2 KiB50 linesraw
1package clocks;
2import java.util.*;
3public class VectorClock {
4    public int[] v;
5    int myId;
6    int N;
7    public VectorClock(int numProc, int id) {
8        myId = id;
9        N = numProc;
10        v = new int[numProc];
11        Arrays.fill(v,0);
12        v[myId] = 1;
13    }
14    public VectorClock(VectorClock vc) { 
15        myId = vc.myId; N = vc.N; v = new int[N];
16        for(int i=0;i<N;i+=1)
17            v[i] = vc.v[i];
18    }
19
20    public void tick() {
21        v[myId]++;
22    }
23    public void sendAction() { //include the vector in the message
24        v[myId]++;
25    }
26    public void receiveAction(int[] sentValue) {
27        for (int i = 0; i < N; i++)
28            v[i] = Math.max(v[i], sentValue[i]);
29        v[myId]++;
30    }
31    public int getValue(int i) {
32        return v[i];
33    }
34    public String toString(){
35        return Arrays.toString(v);
36    }
37    public boolean isLessThan(VectorClock vc){
38        for(int i=0; i< N;i++){
39            if(v[i] >vc.v[i]) return false;
40        }
41        return true;
42    }
43    public boolean isGreaterThan(VectorClock vc){
44         return vc.isLessThan(this);
45    }
46    public boolean isIncomparableWith(VectorClock vc){
47      return (!isLessThan(vc) && !isGreaterThan(vc));
48    }
49
50}