StateManager.java 5.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
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 Joao 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 37
    private int n;
    private int me;
38
    private int lastEid;
39
    private int waitingEid;
40 41
    private int replica;
    private byte[] state;
42

43
    public StateManager(int k, int f, int n, int me) {
44 45

        this.log = new StateLog(k);
46 47
        senderEids = new HashSet<SenderEid>();
        senderStates = new HashSet<SenderState>();
48
        this.f = f;
49 50 51 52
        this.n = n;
        this.me = me;
        this.replica = 0;
        this.state = null;
53
        this.lastEid = -1;
54
        this.waitingEid = -1;
55 56
    }

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    public int getReplica() {
        return replica;
    }

    public void changeReplica() {
        do {
            replica = (replica + 1) % n;
        } while (replica == me);
    }

    public void setReplicaState(byte[] state) {
        this.state = state;
    }

    public byte[] getReplicaState() {
        return state;
    }
    
75 76
    public void addEID(int sender, int eid) {
        senderEids.add(new SenderEid(sender, eid));
77 78
    }

79 80
    public void emptyEIDs() {
        senderEids.clear();
81 82
    }

83 84 85 86 87 88 89 90 91 92 93 94 95
    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();
    }

96 97
    public int getWaiting() {
        return waitingEid;
98 99
    }

100 101
    public void setWaiting(int wait) {
        this.waitingEid = wait;
102 103 104 105 106 107 108 109 110
    }
    public void setLastEID(int eid) {
        lastEid = eid;
    }

    public int getLastEID() {
        return lastEid;
    }

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

113 114
        int count = 0;
        HashSet<Integer> replicasCounted = new HashSet<Integer>();
P
pjsousa@gmail.com 已提交
115

116
        for (SenderEid m : senderEids) {
117 118 119 120 121
            if (m.eid == eid && !replicasCounted.contains(m.sender)) {
                replicasCounted.add(m.sender);
                count++;
            }
        }
122
        
123
        return count > f;
P
pjsousa@gmail.com 已提交
124
    }
125
    public boolean moreThenF_Replies() {
126 127 128 129 130

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

        for (SenderState m : senderStates) {
131
            if (!replicasCounted.contains(m.sender)) {
132 133 134 135 136 137 138
                replicasCounted.add(m.sender);
                count++;
            }
        }

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

140 141 142 143 144 145 146 147 148 149
    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++) {

150
                if (st[i].state.equals(st[j].state) && st[j].state.hasState()) count++;
151 152 153 154 155 156 157 158 159 160 161
                if (count > f) return st[j].state;
            }
        }

        return null;
    }

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

162 163
    public StateLog getLog() {
        return log;
P
pjsousa@gmail.com 已提交
164
    }
165

166
    private class SenderEid {
167 168 169 170

        private int sender;
        private int eid;

171
        SenderEid(int sender, int eid) {
172 173 174 175 176 177
            this.sender = sender;
            this.eid = eid;
        }

        @Override
        public boolean equals(Object obj) {
178 179
            if (obj instanceof SenderEid) {
                SenderEid m = (SenderEid) obj;
180 181 182 183 184 185 186 187 188 189 190 191
                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;
        }
192
    }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

    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 已提交
221
}