StateManager.java 2.5 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    private HashSet<Message> messages = null;
    private int f;

    public StateManager(int k, int f) {

        this.log = new StateLog(k);
        messages = new HashSet<Message>();
        this.f = f;
    }

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

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

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

52 53
        int count = 0;
        HashSet<Integer> replicasCounted = new HashSet<Integer>();
P
pjsousa@gmail.com 已提交
54

55 56 57 58 59 60 61 62
        for (Message m : messages) {
            if (m.eid == eid && !replicasCounted.contains(m.sender)) {
                replicasCounted.add(m.sender);
                count++;
            }
        }

        return count > f;
P
pjsousa@gmail.com 已提交
63 64
    }

65 66
    public StateLog getLog() {
        return log;
P
pjsousa@gmail.com 已提交
67
    }
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    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;
        }
95
    }
P
pjsousa@gmail.com 已提交
96
}