TransferableState.java 5.4 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.io.Serializable;
22
import java.util.Arrays;
23

P
pjsousa@gmail.com 已提交
24 25 26 27 28
/**
 * This classe represents a state tranfered from a replica to another. The state associated with the last
 * checkpoint together with all the batches of messages received do far, comprises the sender's
 * current state
 * 
29
 * @author Jo�o Sousa
P
pjsousa@gmail.com 已提交
30
 */
31
public class TransferableState implements Serializable {
P
pjsousa@gmail.com 已提交
32 33

    private byte[][] messageBatches; // batches received since the last checkpoint.
34
    private int lastCheckpointEid; // Execution ID for the last checkpoint
P
pjsousa@gmail.com 已提交
35
    private byte[] state; // State associated with the last checkpoint
36
    private int lastEid = -1; // Execution ID for the last messages batch delivered to the application
P
pjsousa@gmail.com 已提交
37 38 39 40 41 42 43

    /**
     * Constructs a TansferableState
     * @param messageBatches Batches received since the last checkpoint.
     * @param nextEid Execution ID for the last checkpoint
     * @param state State associated with the last checkpoint
     */
44
    public TransferableState(byte[][] messageBatches, int lastCheckpointEid, int lastEid, byte[] state) {
P
pjsousa@gmail.com 已提交
45 46

        this.messageBatches = messageBatches; // batches received since the last checkpoint.
47 48
        this.lastCheckpointEid = lastCheckpointEid; // Execution ID for the last checkpoint
        this.lastEid = lastEid; // Execution ID for the last messages batch delivered to the application
P
pjsousa@gmail.com 已提交
49 50 51 52
        this.state = state; // State associated with the last checkpoint
        
    }

53
    public TransferableState() {
54
        this.messageBatches = null; // batches received since the last checkpoint.
55
        this.lastCheckpointEid = 0; // Execution ID for the last checkpoint
56
        this.state = null; // State associated with the last checkpoint
57
    }
P
pjsousa@gmail.com 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    /**
     * Retrieves all batches of messages
     * @return Batch of messages
     */
    public byte[][] getMessageBatches() {
        return messageBatches;
    }

    /**
     * Retrieves the specified batch of messages
     * @param eid Execution ID associated with the batch to be fetched
     * @return The batch of messages associated with the batch correspondent execution ID
     */
    public byte[] getMessageBatch(int eid) {
72 73
        if (eid >= lastCheckpointEid && eid < (lastCheckpointEid + messageBatches.length)) {
            return messageBatches[eid - lastCheckpointEid];
P
pjsousa@gmail.com 已提交
74 75 76 77 78 79 80 81 82 83
        }
        else return null;
    }

    /**
     * Retrieves the execution ID for the last checkpoint
     * @return Execution ID for the last checkpoint, or -1 if no checkpoint was yet executed
     */
    public int getCurrentCheckpointEid() {

84 85 86 87 88 89 90 91 92 93
        return lastCheckpointEid;
    }

    /**
     * Retrieves the execution ID for the last messages batch delivered to the application
     * @return Execution ID for the last messages batch delivered to the application
     */
    public int getLastEid() {

        return lastEid;
P
pjsousa@gmail.com 已提交
94 95 96 97 98 99 100 101 102
    }
    
    /**
     * Retrieves the state associated with the last checkpoint
     * @return State associated with the last checkpoint
     */
    public byte[] getState() {
        return state;
    }
103 104 105 106 107

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TransferableState) {
            TransferableState tState = (TransferableState) obj;
108 109 110 111 112 113 114 115 116

            if (this.messageBatches == null && tState.messageBatches == null) return true;

            if ((this.messageBatches != null && tState.messageBatches == null) ||
                    (this.messageBatches == null && tState.messageBatches != null)) return false;

            if (this.messageBatches != null && tState.messageBatches != null &&
                    this.messageBatches.length != tState.messageBatches.length) return false;
                 
117 118
            for (int i = 0; i < this.messageBatches.length; i++)
                if (!Arrays.equals(this.messageBatches[i], tState.messageBatches[i])) return false;
119
            
120 121
            return (Arrays.equals(this.state, tState.state) &&
                    tState.lastCheckpointEid == this.lastCheckpointEid && tState.lastEid == this.lastEid);
122 123 124 125 126 127 128
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 1;
129 130
        hash = hash * 31 + this.lastCheckpointEid;
        hash = hash * 31 + this.lastEid;
131 132 133 134 135 136 137 138
        if (this.state != null)
            for (int i = 0; i < this.state.length; i++) hash = hash * 31 + (int) this.state[i];
        else hash = hash * 31 + 0;
        if (this.messageBatches != null)
            for (int i = 0; i < this.messageBatches.length; i++)
                for (int j = 0; j < this.messageBatches[i].length; j++)
                    hash = hash * 31 + (int) this.messageBatches[i][j];
        else hash = hash * 31 + 0;
139 140
        return hash;
    }
P
pjsousa@gmail.com 已提交
141
}