org/objectweb/proactive/ext/migration/MigrationStrategyImpl.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.ext.migration;
00032 
00033 import org.apache.log4j.Logger;
00034 import org.objectweb.proactive.core.util.log.Loggers;
00035 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00036 
00037 
00043 public class MigrationStrategyImpl implements java.io.Serializable,
00044     MigrationStrategy {
00045     static Logger logger = ProActiveLogger.getLogger(Loggers.MIGRATION);
00046     private java.util.Vector<Destination> table;
00047     private int index;
00048 
00052     public MigrationStrategyImpl() {
00053         super();
00054         table = new java.util.Vector<Destination>();
00055         index = -1; //Negative value for first execution of getNextDestination
00056     }
00057 
00063     public MigrationStrategyImpl(String filename) {
00064         super();
00065         table = new java.util.Vector<Destination>();
00066         index = -1; //Negative value for first execution of getNextDestination
00067         String s;
00068         java.io.FileReader f_in = null;
00069         try {
00070             f_in = new java.io.FileReader(filename);
00071         } catch (java.io.FileNotFoundException e) {
00072             logger.error("File not Found");
00073         }
00074 
00075         // on ouvre un "lecteur" sur ce fichier
00076         java.io.BufferedReader _in = new java.io.BufferedReader(f_in);
00077 
00078         // on lit a partir de ce fichier
00079         // NB : a priori on ne sait pas combien de lignes on va lire !!
00080         try {
00081             // tant qu'il y a quelque chose a lire
00082             while (_in.ready()) {
00083                 // on le lit
00084                 s = _in.readLine();
00085                 java.util.StringTokenizer tokens = new java.util.StringTokenizer(s,
00086                         " ");
00087                 this.add(new NodeDestination(new String(tokens.nextToken()),
00088                         tokens.nextToken()));
00089             }
00090         } // catch (IOException e) {}
00091         catch (Exception e) {
00092         }
00093 
00094         try {
00095             _in.close();
00096         } catch (java.io.IOException e) {
00097         }
00098     }
00099 
00103     public void add(Destination r) {
00104         table.addElement(r);
00105     }
00106 
00107     public void add(String nodeURL, String method) {
00108         table.addElement(new NodeDestination(nodeURL, method));
00109     }
00110 
00115     public void addNext(Destination r) {
00116         if ((index == -1) || (index == (table.size() - 1))) {
00117             table.addElement(r);
00118         } else {
00119             table.add(index + 1, r);
00120         }
00121     }
00122 
00123     public void addNext(String nodeURL, String method) {
00124         if ((index == -1) || (index == (table.size() - 1))) {
00125             table.addElement(new NodeDestination(nodeURL, method));
00126         } else {
00127             table.add(index + 1, new NodeDestination(nodeURL, method));
00128         }
00129     }
00130 
00131     public void remove(String nodeURL, String method) {
00132         removeFromItinerary(new NodeDestination(nodeURL, method));
00133     }
00134 
00135     public void remove(Destination d) {
00136         removeFromItinerary(d);
00137     }
00138 
00139     //Maybe we should return something
00140     private void removeFromItinerary(Destination r) {
00141         //System.out.println("MigrationStrategyImpl: removeFromItinerary() the result is " + table.removeElement(r));
00142         int i = 0;
00143         Destination r2;
00144         while (i < table.size()) {
00145             r2 = table.elementAt(i);
00146             if ((r2.getDestination().equals(r.getDestination())) &&
00147                     (r2.getMethodName().equals(r.getMethodName()))) {
00148                 table.removeElementAt(i);
00149                 //we have removed an element before the index , so we shift the index
00150                 if (i < index) {
00151                     index--;
00152                 }
00153                 return;
00154             }
00155             i++;
00156         }
00157     }
00158 
00163     public Destination next() {
00164         index++;
00165         if (index < table.size()) {
00166             Destination r = table.elementAt(index);
00167 
00168             //index++;
00169             return (r);
00170         } else {
00171             //System.out.println("MigrationStrategyImpl: next() no next destination found");
00172             return (null);
00173         }
00174     }
00175 
00180     public Destination getCurrentDestination() {
00181         if ((index < table.size()) && (index >= 0)) {
00182             Destination r = table.elementAt(index);
00183             return (r);
00184         } else //should never happens
00185          {
00186             return (null);
00187         }
00188     }
00189 
00193     public Destination getNextExcept(String s) {
00194         Destination temp;
00195         if (s == null) {
00196             return next();
00197         }
00198         while ((temp = next()) != null) {
00199             if (!temp.getDestination().equals(s)) {
00200                 return temp;
00201             }
00202         }
00203         return null;
00204     }
00205 
00206     public int size() {
00207         return table.size();
00208     }
00209 
00210     public void decrease() {
00211         if (index >= 0) {
00212             index--;
00213         }
00214     }
00215 
00216     public void reset() {
00217         index = -1; //Negative value for first execution of getNextDestination
00218     }
00219 
00223     public java.util.Vector<String> toVector() {
00224         java.util.Vector<String> temp = new java.util.Vector<String>();
00225         for (int i = 0; i < table.size(); i++) {
00226             temp.add(table.elementAt(i).getDestination());
00227         }
00228         return temp;
00229     }
00230 }

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