Consensus.java 3.5 KB
Newer Older
P
pjsousa@gmail.com 已提交
1 2
/**
 * Copyright (c) 2007-2009 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags
3
 *
P
pjsousa@gmail.com 已提交
4
 * This file is part of SMaRt.
5
 *
P
pjsousa@gmail.com 已提交
6 7 8 9
 * SMaRt is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10
 *
P
pjsousa@gmail.com 已提交
11 12
 * SMaRt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
P
pjsousa@gmail.com 已提交
14
 * GNU General Public License for more details.
15
 *
P
pjsousa@gmail.com 已提交
16 17 18 19
 * You should have received a copy of the GNU General Public License along with SMaRt.  If not, see <http://www.gnu.org/licenses/>.
 */
package navigators.smart.paxosatwar;

20 21
import navigators.smart.paxosatwar.executionmanager.Round;
import navigators.smart.tom.core.messages.TOMMessage;
P
pjsousa@gmail.com 已提交
22 23 24
import navigators.smart.tom.util.Logger;

/**
25 26 27 28 29 30 31
 *
 * This class represents a Consensus Instance.
 *
 * @param <E> Type of the decided Object
 *
 * @author unkown
 * @author Christian Spann <christian.spann at uni-ulm.de>
P
pjsousa@gmail.com 已提交
32
 */
33
public class Consensus {
P
pjsousa@gmail.com 已提交
34 35

    private int eid; // execution ID
36
    private Round decisionRound = null;
P
pjsousa@gmail.com 已提交
37
    private byte[] decision = null; // decided value
38
    private TOMMessage[] deserializedDecision = null; // decided value (deserialized)
B
bessani@gmail.com 已提交
39 40 41 42 43

    //for benchmarking
    public TOMMessage firstMessageProposed = null;
    public int batchSize = 0;

P
pjsousa@gmail.com 已提交
44 45 46 47 48 49 50

    /**
     * Creates a new instance of Consensus
     * @param proposer The proposer role of PaW algorithm
     * @param eid The execution ID for this consensus
     * @param startTime The consensus start time
     */
B
bessani@gmail.com 已提交
51
    public Consensus(int eid) {
P
pjsousa@gmail.com 已提交
52 53 54
        this.eid = eid;
    }

55 56 57
    public void decided(Round round) {
        //synchronized (sync) {
            //this.decision = decision;
58
            this.decisionRound = round;
59 60
            //sync.notifyAll();
        //}
P
pjsousa@gmail.com 已提交
61 62
    }

63
    public Round getDecisionRound() {
64
        return decisionRound;
P
pjsousa@gmail.com 已提交
65 66 67 68 69 70 71
    }

    /**
     * Sets the decided value
     * @return Decided Value
     */
    public byte[] getDecision() {
72 73 74 75
        //synchronized (sync) {  //TODO is this sync needed? cspann
            while (decision == null) {
                waitForPropose(); // Eduardo: deve ter um waitForDecision separando (agora funciona pq é só um sleep)
                decision = decisionRound.propValue;
76 77
            }
            return decision;
78
        //}
79
    }
80

81 82 83
    public TOMMessage[] getDeserializedDecision() {
        //synchronized (sync) {
            while (deserializedDecision == null) {
84
                waitForPropose();
85
                deserializedDecision = decisionRound.deserializedPropValue;
86
            }
87
        //}
P
pjsousa@gmail.com 已提交
88 89 90
        return deserializedDecision;
    }

91 92 93 94 95 96 97 98 99 100 101 102
    /**public void setDeserialisedDecision(TOMMessage[] deserialised) {

        //System.out.println("Chamou o setDeserialisedDecision......");
        //synchronized (sync) {
        if(this.deserializedDecision == null){
            this.deserializedDecision = deserialised;
        }
            //sync.notifyAll();
        //}

    }*/

P
pjsousa@gmail.com 已提交
103
    /**
104 105
     * The Execution ID for this consensus
     * @return Execution ID for this consensus
P
pjsousa@gmail.com 已提交
106
     */
107 108
    public int getId() {
        return eid;
P
pjsousa@gmail.com 已提交
109
    }
B
bessani@gmail.com 已提交
110

P
pjsousa@gmail.com 已提交
111
    private void waitForPropose() {
112 113 114 115 116
        while(decisionRound.deserializedPropValue == null) {
            try{
                Logger.println("waiting for propose for "+eid);
                Thread.sleep(1);
            }catch(InterruptedException ie) {}
P
pjsousa@gmail.com 已提交
117 118
        }
    }
119

120
}