org/objectweb/proactive/core/body/ft/message/ReceptionHistory.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.body.ft.message;
00032 
00033 import java.io.Serializable;
00034 import java.util.Iterator;
00035 import java.util.List;
00036 import java.util.Vector;
00037 
00038 import org.objectweb.proactive.core.body.ft.exception.ProtocolErrorException;
00039 
00040 
00046 public class ReceptionHistory implements Serializable {
00047     // the elements of the history
00048     private List elements;
00049 
00050     // the historized index of the last element 
00051     private long lastCommited;
00052 
00053     // the historized index of the first element
00054     private long base;
00055 
00056     // the last usable elements : the list elements can be longer that needed
00057     // if this histo has been updated but not commited
00058     private long lastRecoverable;
00059 
00063     public ReceptionHistory() {
00064         this.elements = new Vector();
00065         this.lastCommited = -1;
00066         this.lastRecoverable = -1;
00067         this.base = 0;
00068     }
00069 
00074     public void updateHistory(HistoryUpdater hu) {
00075         long toAddBase = hu.base;
00076         long toAddLast = hu.last;
00077         List toAdd = hu.elements;
00078 
00079         // if there is a gap between lastCommited and toAddBase, we can
00080         // suppose that this gap is commited. The current history is then 
00081         // replaced by toAdd
00082         if (toAddBase > (this.lastCommited + 1)) {
00083             // history is not contigue
00084             this.elements = toAdd;
00085             this.base = toAddBase;
00086             this.lastCommited = toAddLast;
00087         } else if (this.lastCommited < toAddLast) {
00088             // history is contigue 
00089             Iterator it = toAdd.iterator();
00090 
00091             // shift in elts up to this.lastCommited+1
00092             for (long i = toAddBase; i <= this.lastCommited; i++) {
00093                 it.next();
00094             }
00095 
00096             // add the rest to this.elements
00097             while (it.hasNext()) {
00098                 this.elements.add(it.next());
00099             }
00100             this.lastCommited = toAddLast;
00101         }
00102     }
00103 
00110     public void goToNextBase(long nextBase) {
00111         if (nextBase < this.base) {
00112             throw new ProtocolErrorException(
00113                 "nextBase is lower than current base !");
00114         }
00115         int shift = (int) (nextBase - this.base);
00116 
00117         // particular case : shift==histo_size
00118         // minimal histo is empty for this checkpoint
00119         if (shift == (this.elements.size() + 1)) {
00120             this.elements.clear();
00121             this.base = nextBase;
00122         } else {
00123             this.elements.subList(0, shift).clear();
00124             this.base = nextBase;
00125         }
00126     }
00127 
00132     public void confirmLastUpdate() {
00133         this.lastRecoverable = this.lastCommited;
00134     }
00135 
00136     public long getLastCommited() {
00137         return this.lastCommited;
00138     }
00139 
00140     public long getLastRecoverable() {
00141         return this.lastRecoverable;
00142     }
00143 
00149     public List getRecoverableHistory() {
00150         if (this.lastCommited == this.lastRecoverable) {
00151             return this.elements;
00152         } else {
00153             Vector toRet = new Vector();
00154             for (int i = 0; i <= (this.lastRecoverable - this.base); i++) {
00155                 toRet.add(this.elements.get(i));
00156             }
00157             this.elements = toRet;
00158             return toRet;
00159         }
00160     }
00161 
00165     public void compactHistory() {
00166         if (this.lastCommited > this.lastRecoverable) {
00167             this.elements.subList((int) ((this.lastRecoverable + 1) -
00168                 this.base), (int) ((this.lastCommited + 1) - this.base));
00169             this.lastCommited = this.lastRecoverable;
00170         }
00171     }
00172 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1