KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > future > FutureMap


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31
32 package org.objectweb.proactive.core.body.future;
33
34 import org.objectweb.proactive.core.ProActiveRuntimeException;
35 import org.objectweb.proactive.core.UniqueID;
36 import org.objectweb.proactive.core.body.UniversalBody;
37
38 /**
39  * Data structure which stores futures and corresponding automatic continuation to do.
40  * This map is like :
41  * [creatorID --> [sequenceID --> {list of futures to update, list of bodies for AC}]]
42  * @see FuturePool
43  * @see FutureProxy
44  */

45 public class FutureMap extends Object JavaDoc implements java.io.Serializable JavaDoc {
46
47     // main map
48
private java.util.HashMap JavaDoc indexedByBodyID;
49
50     //
51
// -- CONSTRUCTORS -----------------------------------------------
52
//
53

54     public FutureMap() {
55         indexedByBodyID = new java.util.HashMap JavaDoc();
56     }
57
58     /**
59      * Add an AC to do for bodyDest when the futurPool will receive the value of the future
60      * indexed by (id, creatorID)
61      * @param id sequence id of the future
62      * @param creatorID UniqueID of the creator body of the future
63      * @param bodyDest body which receives the future (id, bodyID)
64      */

65     public synchronized void addAutomaticContinuation(long id, UniqueID creatorID, UniversalBody bodyDest) {
66         java.util.HashMap JavaDoc indexedByID = (java.util.HashMap JavaDoc) (indexedByBodyID.get(creatorID));
67         if (indexedByID == null)
68             throw new ProActiveRuntimeException("There is no map for creatorID " + creatorID);
69         java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (indexedByID.get(new Long JavaDoc(id)));
70         // add bodyDest to the list of dest for future (id, bodyID)
71
if (listes != null)
72             listes[1].add(bodyDest);
73         else
74             throw new ProActiveRuntimeException("There is no list for future " + id);
75     }
76
77     /**
78      * Add a future (id, creatorID) in the map. The entry for this key could already
79      * exists, because a body can have copies of a future.
80      * @param id sequence id of the future
81      * @param creatorID UniqueID of the creator body of the future
82      * @param futureObject future to register
83      */

84     public synchronized void receiveFuture(Future futureObject) {
85         long id = futureObject.getID();
86         UniqueID creatorID = futureObject.getCreatorID();
87         java.util.HashMap JavaDoc indexedByID = (java.util.HashMap JavaDoc) (indexedByBodyID.get(creatorID));
88
89         // entry does not exist
90
if (indexedByID == null) {
91
92             //sub-map
93
java.util.HashMap JavaDoc newIndexedByID = new java.util.HashMap JavaDoc();
94             //list of futures
95
java.util.ArrayList JavaDoc futures = new java.util.ArrayList JavaDoc();
96             futures.add(futureObject);
97             //list of ACs (ie bodies destination)
98
java.util.ArrayList JavaDoc dests = new java.util.ArrayList JavaDoc();
99
100             java.util.ArrayList JavaDoc[] listes = new java.util.ArrayList JavaDoc[2];
101             listes[0] = futures;
102             listes[1] = dests;
103             newIndexedByID.put(new Long JavaDoc(id), listes);
104             indexedByBodyID.put(creatorID, newIndexedByID);
105         }
106         // entry for creatorID exists, but there is no sub-entry for id
107
else if (indexedByID.get(new Long JavaDoc(id)) == null) {
108
109             //list of futures
110
java.util.ArrayList JavaDoc futures = new java.util.ArrayList JavaDoc();
111             futures.add(futureObject);
112             //list of ACs
113
java.util.ArrayList JavaDoc dests = new java.util.ArrayList JavaDoc();
114
115             java.util.ArrayList JavaDoc[] listes = new java.util.ArrayList JavaDoc[2];
116             listes[0] = futures;
117             listes[1] = dests;
118             indexedByID.put(new Long JavaDoc(id), listes);
119         }
120         // one copy of an existing future
121
else {
122             (((java.util.ArrayList JavaDoc[]) (indexedByID.get(new Long JavaDoc(id))))[0]).add(futureObject);
123         }
124     }
125
126     /**
127      * Return the list of futures corresponding to (id,bodyID) if any, null otherwise.
128      * @param id sequence id of the future
129      * @param creatorID UniqueID of the creator body of the future
130      */

131     public synchronized java.util.ArrayList JavaDoc getFuturesToUpdate(long id, UniqueID creatorID) {
132         java.util.HashMap JavaDoc indexedByID = (java.util.HashMap JavaDoc) (indexedByBodyID.get(creatorID));
133         java.util.ArrayList JavaDoc resultat = null;
134
135         if (indexedByID != null) {
136             java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (indexedByID.get(new Long JavaDoc(id)));
137
138             if (listes != null) {
139                 java.util.ArrayList JavaDoc futures = listes[0];
140                 resultat = futures;
141             }
142         }
143         // one of these two lists could be null : it's not an error ! Futures could have been already
144
// updated. We must not throw exception in this case.
145
// Could be optimized...
146
return resultat;
147
148     }
149
150     /**
151      * Return the list of ACs to (ie bodies destination) corresponding to (id,bodyID) if any, null otherwise.
152      * @param id sequence id of the future
153      * @param creatorID UniqueID of the creator body of the future
154      */

155     public synchronized java.util.ArrayList JavaDoc getAutomaticContinuation(long id, UniqueID bodyID) {
156         java.util.ArrayList JavaDoc resultat = null;
157         java.util.HashMap JavaDoc indexedByID = (java.util.HashMap JavaDoc) (indexedByBodyID.get(bodyID));
158         if (indexedByID != null) {
159             java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (indexedByID.get(new Long JavaDoc(id)));
160             if (listes != null) {
161                 resultat = listes[1];
162             }
163         }
164         return resultat;
165
166     }
167
168     /**
169      * Remove entry corresponding to (id, creatorID) in the futureMap.
170      * @param id sequence id of the future
171      * @param creatorID UniqueID of the creator body of the future
172      */

173     public synchronized void removeFutures(long id, UniqueID creatorID) {
174         java.util.HashMap JavaDoc indexedByID = (java.util.HashMap JavaDoc) (indexedByBodyID.get(creatorID));
175         if (indexedByID != null) {
176             java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (indexedByID.remove(new Long JavaDoc(id)));
177         }
178     }
179
180
181
182     /*
183      * Unset the migration tag in all futures of the map.
184      * @see FutureProxy
185      */

186     public synchronized void unsetMigrationTag() {
187         java.util.Collection JavaDoc c1 = indexedByBodyID.values();
188         java.util.Iterator JavaDoc it1 = c1.iterator();
189
190         while (it1.hasNext()) {
191             java.util.Collection JavaDoc c2 = ((java.util.HashMap JavaDoc) (it1.next())).values();
192             java.util.Iterator JavaDoc it2 = c2.iterator();
193             while (it2.hasNext()) {
194                 java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (it2.next());
195                 java.util.ArrayList JavaDoc futures = listes[0];
196                 java.util.Iterator JavaDoc itFutures = futures.iterator();
197                 while (itFutures.hasNext()) {
198                     FutureProxy p = (FutureProxy) itFutures.next();
199                     p.unsetMigrationTag();
200                 }
201             }
202         }
203     }
204
205     /**
206      * Set the migration tag in all futures of the map.
207      * @see FutureProxy
208      */

209     public synchronized void setMigrationTag() {
210         java.util.Collection JavaDoc c1 = indexedByBodyID.values();
211         java.util.Iterator JavaDoc it1 = c1.iterator();
212
213         while (it1.hasNext()) {
214             java.util.Collection JavaDoc c2 = ((java.util.HashMap JavaDoc) (it1.next())).values();
215             java.util.Iterator JavaDoc it2 = c2.iterator();
216             while (it2.hasNext()) {
217                 java.util.ArrayList JavaDoc[] listes = (java.util.ArrayList JavaDoc[]) (it2.next());
218                 java.util.ArrayList JavaDoc futures = listes[0];
219                 java.util.Iterator JavaDoc itFutures = futures.iterator();
220                 while (itFutures.hasNext()) {
221                     FutureProxy p = (FutureProxy) itFutures.next();
222                     p.setMigrationTag();
223                 }
224             }
225         }
226     }
227     
228
229 }
230
Popular Tags