StateManager.java 4.9 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
import java.util.Arrays;
22 23
import java.util.HashSet;
import java.util.Hashtable;
P
pjsousa@gmail.com 已提交
24 25 26 27

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

    private StateLog log;
33 34
    private HashSet<SenderEid> senderEids = null;
    private HashSet<SenderState> senderStates = null;
35
    private int f;
36
    private int lastEid;
37
    private int waitingEid;
38 39 40 41

    public StateManager(int k, int f) {

        this.log = new StateLog(k);
42 43
        senderEids = new HashSet<SenderEid>();
        senderStates = new HashSet<SenderState>();
44
        this.f = f;
45
        this.lastEid = -1;
46
        this.waitingEid = -1;
47 48
    }

49 50
    public void addEID(int sender, int eid) {
        senderEids.add(new SenderEid(sender, eid));
51 52
    }

53 54
    public void emptyEIDs() {
        senderEids.clear();
55 56
    }

57 58 59 60 61 62 63 64 65 66 67 68 69
    public void emptyEIDs(int eid) {
        for (SenderEid m : senderEids)
            if (m.eid <= eid) senderEids.remove(m);
    }

    public void addState(int sender, TransferableState state) {
        senderStates.add(new SenderState(sender, state));
    }

    public void emptyStates() {
        senderStates.clear();
    }

70 71
    public int getWaiting() {
        return waitingEid;
72 73
    }

74 75
    public void setWaiting(int wait) {
        this.waitingEid = wait;
76 77 78 79 80 81 82 83 84
    }
    public void setLastEID(int eid) {
        lastEid = eid;
    }

    public int getLastEID() {
        return lastEid;
    }

85
    public boolean moreThenF_EIDs(int eid) {
P
pjsousa@gmail.com 已提交
86

87 88
        int count = 0;
        HashSet<Integer> replicasCounted = new HashSet<Integer>();
P
pjsousa@gmail.com 已提交
89

90
        for (SenderEid m : senderEids) {
91 92 93 94 95
            if (m.eid == eid && !replicasCounted.contains(m.sender)) {
                replicasCounted.add(m.sender);
                count++;
            }
        }
96
        
97
        return count > f;
P
pjsousa@gmail.com 已提交
98
    }
99
    public boolean moreThenF_Replies() {
100 101 102 103 104

        int count = 0;
        HashSet<Integer> replicasCounted = new HashSet<Integer>();

        for (SenderState m : senderStates) {
105
            if (!replicasCounted.contains(m.sender)) {
106 107 108 109 110 111 112
                replicasCounted.add(m.sender);
                count++;
            }
        }

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

114 115 116 117 118 119 120 121 122 123
    public TransferableState getValidState() {
        
        SenderState[] st = new SenderState[senderStates.size()];
        senderStates.toArray(st);
        int count = 0;

        for (int i = 0; i < st.length; i++) {

            for (int j = i; j < st.length; j++) {

124
                if (st[i].state.equals(st[j].state) && st[j].state.hasState()) count++;
125 126 127 128 129 130 131 132 133 134 135
                if (count > f) return st[j].state;
            }
        }

        return null;
    }

    public int getReplies() {
        return senderStates.size();
    }

136 137
    public StateLog getLog() {
        return log;
P
pjsousa@gmail.com 已提交
138
    }
139

140
    private class SenderEid {
141 142 143 144

        private int sender;
        private int eid;

145
        SenderEid(int sender, int eid) {
146 147 148 149 150 151
            this.sender = sender;
            this.eid = eid;
        }

        @Override
        public boolean equals(Object obj) {
152 153
            if (obj instanceof SenderEid) {
                SenderEid m = (SenderEid) obj;
154 155 156 157 158 159 160 161 162 163 164 165
                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;
        }
166
    }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    private class SenderState {

        private int sender;
        private TransferableState state;

        SenderState(int sender, TransferableState state) {
            this.sender = sender;
            this.state = state;
        }

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

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