StateManager.java 2.8 KB
Newer Older
P
pjsousa@gmail.com 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Copyright (c) 2007-2009 Alysson Bessani, Eduardo Alchieri, Paulo Sousa, and the authors indicated in the @author tags
 * 
 * This file is part of SMaRt.
 * 
 * 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.
 * 
 * SMaRt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 * 
 * 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.statemanagment;

21 22
import java.util.HashSet;
import java.util.Hashtable;
P
pjsousa@gmail.com 已提交
23 24 25 26

/**
 * TODO: Não sei se esta classe sera usada. Para já, deixo ficar
 * 
27
 * @author Jo�o Sousa
P
pjsousa@gmail.com 已提交
28 29 30 31
 */
public class StateManager {

    private StateLog log;
32 33
    private HashSet<Message> messages = null;
    private int f;
34
    private int lastEid;
35 36 37 38 39 40

    public StateManager(int k, int f) {

        this.log = new StateLog(k);
        messages = new HashSet<Message>();
        this.f = f;
41
        this.lastEid = -1;
42 43 44 45 46 47 48 49 50 51
    }

    public void addReplica(int sender, int eid) {
        messages.add(new Message(sender, eid));
    }

    public void emptyReplicas() {
        messages.clear();
    }

52 53 54 55 56 57 58 59 60 61 62 63 64
    public void emptyReplicas(int eid) {
        for (Message m : messages)
            if (m.eid <= eid) messages.remove(m);
    }
    
    public void setLastEID(int eid) {
        lastEid = eid;
    }

    public int getLastEID() {
        return lastEid;
    }

65
    public boolean moreThenF(int eid) {
P
pjsousa@gmail.com 已提交
66

67 68
        int count = 0;
        HashSet<Integer> replicasCounted = new HashSet<Integer>();
P
pjsousa@gmail.com 已提交
69

70 71 72 73 74 75 76 77
        for (Message m : messages) {
            if (m.eid == eid && !replicasCounted.contains(m.sender)) {
                replicasCounted.add(m.sender);
                count++;
            }
        }

        return count > f;
P
pjsousa@gmail.com 已提交
78 79
    }

80 81
    public StateLog getLog() {
        return log;
P
pjsousa@gmail.com 已提交
82
    }
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    private class Message {

        private int sender;
        private int eid;

        Message(int sender, int eid) {
            this.sender = sender;
            this.eid = eid;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Message) {
                Message m = (Message) obj;
                return (m.eid == this.eid && m.sender == this.sender);
            }
            return false;
        }

        @Override
        public int hashCode() {
            int hash = 1;
            hash = hash * 31 + this.sender;
            hash = hash * 31 + this.eid;
            return hash;
        }
110
    }
P
pjsousa@gmail.com 已提交
111
}