org/objectweb/proactive/core/body/future/FutureMap.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.future;
00032 
00033 import java.util.ArrayList;
00034 import java.util.HashMap;
00035 
00036 import org.objectweb.proactive.core.ProActiveRuntimeException;
00037 import org.objectweb.proactive.core.UniqueID;
00038 import org.objectweb.proactive.core.body.UniversalBody;
00039 
00040 
00048 public class FutureMap extends Object implements java.io.Serializable {
00049     // main map
00050     private java.util.HashMap<UniqueID, HashMap<Long, ArrayList[]>> indexedByBodyID;
00051 
00052     //
00053     // -- CONSTRUCTORS -----------------------------------------------
00054     //
00055     public FutureMap() {
00056         indexedByBodyID = new java.util.HashMap<UniqueID, HashMap<Long, ArrayList[]>>();
00057     }
00058 
00066     public synchronized void addAutomaticContinuation(long id,
00067         UniqueID creatorID, UniversalBody bodyDest) {
00068         java.util.HashMap indexedByID = (indexedByBodyID.get(creatorID));
00069         if (indexedByID == null) {
00070             throw new ProActiveRuntimeException(
00071                 "There is no map for creatorID " + creatorID);
00072         }
00073         java.util.ArrayList[] listes = (java.util.ArrayList[]) (indexedByID.get(new Long(
00074                     id)));
00075 
00076         // add bodyDest to the list of dest for future (id, bodyID)
00077         if (listes != null) {
00078             listes[1].add(bodyDest);
00079         } else {
00080             throw new ProActiveRuntimeException("There is no list for future " +
00081                 id);
00082         }
00083     }
00084 
00090     public synchronized void receiveFuture(Future futureObject) {
00091         long id = futureObject.getID();
00092         UniqueID creatorID = futureObject.getCreatorID();
00093         java.util.HashMap<Long, ArrayList[]> indexedByID = (indexedByBodyID.get(creatorID));
00094 
00095         // entry does not exist
00096         if (indexedByID == null) {
00097             //sub-map
00098             java.util.HashMap<Long, ArrayList[]> newIndexedByID = new java.util.HashMap<Long, ArrayList[]>();
00099 
00100             //list of futures
00101             java.util.ArrayList<Future> futures = new java.util.ArrayList<Future>();
00102             futures.add(futureObject);
00103             //list of ACs (ie bodies destination)
00104             java.util.ArrayList<Future> dests = new java.util.ArrayList<Future>();
00105 
00106             java.util.ArrayList[] listes = new java.util.ArrayList[2];
00107             listes[0] = futures;
00108             listes[1] = dests;
00109             newIndexedByID.put(new Long(id), listes);
00110             indexedByBodyID.put(creatorID, newIndexedByID);
00111         }
00112         // entry for creatorID exists, but there is no sub-entry for id
00113         else if (indexedByID.get(new Long(id)) == null) {
00114             //list of futures
00115             java.util.ArrayList<Future> futures = new java.util.ArrayList<Future>();
00116             futures.add(futureObject);
00117             //list of ACs
00118             java.util.ArrayList<Future> dests = new java.util.ArrayList<Future>();
00119 
00120             java.util.ArrayList[] listes = new java.util.ArrayList[2];
00121             listes[0] = futures;
00122             listes[1] = dests;
00123             indexedByID.put(new Long(id), listes);
00124         }
00125         // one copy of an existing future
00126         else {
00127             ((indexedByID.get(new Long(id)))[0]).add(futureObject);
00128         }
00129     }
00130 
00136     public synchronized java.util.ArrayList getFuturesToUpdate(long id,
00137         UniqueID creatorID) {
00138         java.util.HashMap indexedByID = (indexedByBodyID.get(creatorID));
00139         java.util.ArrayList resultat = null;
00140 
00141         if (indexedByID != null) {
00142             java.util.ArrayList[] listes = (java.util.ArrayList[]) (indexedByID.get(new Long(
00143                         id)));
00144 
00145             if (listes != null) {
00146                 java.util.ArrayList futures = listes[0];
00147                 resultat = futures;
00148             }
00149         }
00150 
00151         // one of these two lists could be null : it's not an error ! Futures could have been already
00152         // updated. We must not throw exception in this case. 
00153         // Could be optimized...
00154         return resultat;
00155     }
00156 
00162     public synchronized java.util.ArrayList getAutomaticContinuation(long id,
00163         UniqueID bodyID) {
00164         java.util.ArrayList resultat = null;
00165         java.util.HashMap indexedByID = (indexedByBodyID.get(bodyID));
00166         if (indexedByID != null) {
00167             java.util.ArrayList[] listes = (java.util.ArrayList[]) (indexedByID.get(new Long(
00168                         id)));
00169             if (listes != null) {
00170                 resultat = listes[1];
00171             }
00172         }
00173         return resultat;
00174     }
00175 
00181     public synchronized void removeFutures(long id, UniqueID creatorID) {
00182         java.util.HashMap indexedByID = (indexedByBodyID.get(creatorID));
00183         if (indexedByID != null) {
00184             java.util.ArrayList[] listes = (java.util.ArrayList[]) (indexedByID.remove(new Long(
00185                         id)));
00186         }
00187     }
00188 
00189     /*
00190      * Unset the migration tag in all futures of the map.
00191      * @see FutureProxy
00192      */
00193     public synchronized void unsetMigrationTag() {
00194         java.util.Collection<HashMap<Long, ArrayList[]>> c1 = indexedByBodyID.values();
00195         java.util.Iterator<HashMap<Long, ArrayList[]>> it1 = c1.iterator();
00196 
00197         while (it1.hasNext()) {
00198             java.util.Collection c2 = (it1.next()).values();
00199             java.util.Iterator it2 = c2.iterator();
00200             while (it2.hasNext()) {
00201                 java.util.ArrayList[] listes = (java.util.ArrayList[]) (it2.next());
00202                 java.util.ArrayList futures = listes[0];
00203                 java.util.Iterator itFutures = futures.iterator();
00204                 while (itFutures.hasNext()) {
00205                     FutureProxy p = (FutureProxy) itFutures.next();
00206                     p.unsetMigrationTag();
00207                 }
00208             }
00209         }
00210     }
00211 
00216     public synchronized void setMigrationTag() {
00217         java.util.Collection<HashMap<Long, ArrayList[]>> c1 = indexedByBodyID.values();
00218         java.util.Iterator<HashMap<Long, ArrayList[]>> it1 = c1.iterator();
00219 
00220         while (it1.hasNext()) {
00221             java.util.Collection c2 = (it1.next()).values();
00222             java.util.Iterator it2 = c2.iterator();
00223             while (it2.hasNext()) {
00224                 java.util.ArrayList[] listes = (java.util.ArrayList[]) (it2.next());
00225                 java.util.ArrayList futures = listes[0];
00226                 java.util.Iterator itFutures = futures.iterator();
00227                 while (itFutures.hasNext()) {
00228                     FutureProxy p = (FutureProxy) itFutures.next();
00229                     p.setMigrationTag();
00230                 }
00231             }
00232         }
00233     }
00234 }

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