org/objectweb/proactive/core/group/ProxyForGroup.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.group;
00032 
00033 import java.lang.reflect.InvocationTargetException;
00034 import java.lang.reflect.Type;
00035 import java.lang.reflect.TypeVariable;
00036 import java.util.Collection;
00037 import java.util.HashMap;
00038 import java.util.Iterator;
00039 import java.util.List;
00040 import java.util.ListIterator;
00041 import java.util.Map;
00042 import java.util.Set;
00043 import java.util.Vector;
00044 
00045 import org.apache.log4j.Logger;
00046 import org.objectweb.proactive.Body;
00047 import org.objectweb.proactive.ProActive;
00048 import org.objectweb.proactive.core.UniqueID;
00049 import org.objectweb.proactive.core.body.LocalBodyStore;
00050 import org.objectweb.proactive.core.body.proxy.AbstractProxy;
00051 import org.objectweb.proactive.core.component.ProActiveInterface;
00052 import org.objectweb.proactive.core.exceptions.manager.NFEManager;
00053 import org.objectweb.proactive.core.exceptions.proxy.FailedGroupRendezVousException;
00054 import org.objectweb.proactive.core.group.spmd.MethodCallSetSPMDGroup;
00055 import org.objectweb.proactive.core.group.threadpool.ThreadPool;
00056 import org.objectweb.proactive.core.mop.ClassNotReifiableException;
00057 import org.objectweb.proactive.core.mop.ConstructionOfReifiedObjectFailedException;
00058 import org.objectweb.proactive.core.mop.ConstructorCall;
00059 import org.objectweb.proactive.core.mop.MOP;
00060 import org.objectweb.proactive.core.mop.MethodCall;
00061 import org.objectweb.proactive.core.mop.Proxy;
00062 import org.objectweb.proactive.core.mop.StubObject;
00063 import org.objectweb.proactive.core.node.Node;
00064 import org.objectweb.proactive.core.util.log.Loggers;
00065 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00066 import org.objectweb.proactive.core.util.profiling.PAProfilerEngine;
00067 import org.objectweb.proactive.core.util.profiling.Profiling;
00068 import org.objectweb.proactive.core.util.timer.CompositeAverageMicroTimer;
00069 
00070 
00071 public class ProxyForGroup extends AbstractProxy implements Proxy, Group,
00072     java.io.Serializable {
00073 
00075     protected static Logger logger = ProActiveLogger.getLogger(Loggers.GROUPS);
00076 
00078     protected String className;
00079 
00081     protected Vector memberList;
00082 
00084     protected Map elementNames;
00085 
00088     // NOT FULLY IMPLEMENTED !!!
00089     transient protected UniqueID proxyForGroupID;
00090 
00092     protected int waited = 0;
00093 
00095     protected boolean dispatching = false;
00096 
00098     protected boolean uniqueSerialization = false;
00099 
00101     protected StubObject stub;
00102 
00104     transient protected ThreadPool threadpool;
00105 
00107     protected CompositeAverageMicroTimer timer;
00108 
00109     /* ----------------------- CONSTRUCTORS ----------------------- */
00110     public ProxyForGroup(String nameOfClass)
00111         throws ConstructionOfReifiedObjectFailedException {
00112         this();
00113         this.className = nameOfClass;
00114     }
00115 
00116     public ProxyForGroup() throws ConstructionOfReifiedObjectFailedException {
00117         this.memberList = new Vector();
00118         this.proxyForGroupID = new UniqueID();
00119         this.threadpool = new ThreadPool();
00120         this.elementNames = new HashMap();
00121         if (Profiling.GROUP) {
00122             timer = new CompositeAverageMicroTimer("Group");
00123             PAProfilerEngine.registerTimer(timer);
00124         }
00125     }
00126 
00127     public ProxyForGroup(ConstructorCall c, Object[] p)
00128         throws ConstructionOfReifiedObjectFailedException {
00129         this();
00130     }
00131 
00132     /* ----------------------------- GENERAL ---------------------------------- */
00133 
00137     protected void setDispatchingOn() {
00138         this.dispatching = true;
00139     }
00140 
00144     protected void setDispatchingOff() {
00145         this.dispatching = false;
00146     }
00147 
00151     protected void setUniqueSerializationOn() {
00152         this.uniqueSerialization = true;
00153     }
00154 
00158     protected void setUniqueSerializationOff() {
00159         this.uniqueSerialization = false;
00160     }
00161 
00166     protected boolean isDispatchingOn() {
00167         return this.dispatching;
00168     }
00169 
00170     private boolean isDispatchingCall(MethodCall mc) {
00171         for (int i = 0; i < mc.getNumberOfParameter(); i++)
00172             if (ProActiveGroup.isScatterGroupOn(mc.getParameter(i))) {
00173                 return true;
00174             }
00175         return false;
00176     }
00177 
00178     /* ------------------------ THE PROXY'S METHOD ------------------------ */
00179 
00188     public synchronized Object reify(MethodCall mc)
00189         throws InvocationTargetException  {
00190         //System.out.println("A method is called : \"" + mc.getName() + "\" on " + this.memberList.size() + " membres.");
00191         ExceptionListException exceptionList = null;
00192 
00193         /* if the method called is toString, apply it to the proxy, not to the members */
00194         if ("toString".equals(mc.getName())) {
00195             return this.toString();
00196         }
00197 
00198         /* if the method called is hashCode, apply it to the proxy, not to the members */
00199         if ("hashCode".equals(mc.getName())) {
00200             return new Integer(this.hashCode());
00201         }
00202 
00203         if ("equals".equals(mc.getName()) && (mc.getNumberOfParameter() == 1)) {
00204             return new Boolean(this.equals(mc.getParameter(0)));
00205         }
00206         
00207         // there may be some reorganization of the parameters
00208 //        redistributeParameters(mc);
00209 
00210         /* result will be a stub on a proxy for group representing the group of results */
00211         Object result = null;
00212 
00213         /* check if the threadpool is big enough to make the call (is there is not enough thread, create new ones) */
00214         this.threadpool.checkNumberOfThreads(this.memberList.size());
00215 
00216         /* if OneWay : do not construct result */
00217         if (mc.isOneWayCall()) {
00218             exceptionList = new ExceptionListException();
00219             this.oneWayCallOnGroup(mc, exceptionList);
00220         }
00221         /* Special case : the method returns void but is Synchronous because it throws Exception */
00222         else if (mc.getReifiedMethod().getReturnType() == Void.TYPE) {
00223             exceptionList = new ExceptionListException();
00224             this.oneWayCallOnGroup(mc, exceptionList);
00225         }
00226         /* if the call is asynchronous the group of result will be a group a future */
00227         else { // with group in general case : SYNC == ASYNC !!!!
00228             result = this.asynchronousCallOnGroup(mc);
00229         }
00230 
00231         /* A barrier of synchronisation to be sur that all calls are done before continuing the execution */
00232         this.threadpool.complete();
00233 
00234         /* Throws the exceptionList if one or more exceptions occur in the oneWayCall */
00235         if ((exceptionList != null) && (exceptionList.size() != 0)) {
00236             FailedGroupRendezVousException fgrve = new FailedGroupRendezVousException(
00237                     "RendezVous failed in group method: " + mc.getName(),
00238                     exceptionList, this);
00239             NFEManager.fireNFE(fgrve, this);
00240         }
00241 
00242         return result;
00243     }
00244 
00246     public void finalize() {
00247         this.threadpool.clean();
00248     }
00249 
00250     /* ------------ FOR ASYNCHRONOUS CALL ------------ */
00251 
00257     protected Object asynchronousCallOnGroup(MethodCall mc) throws InvocationTargetException {
00258         Object result;
00259         Body body = ProActive.getBodyOnThis();
00260         if (Profiling.GROUP) {
00261             timer.setTimer("asynchronousCallOnGroup." + mc.getName());
00262             timer.start();
00263         }
00264 
00265         // Creates a stub + ProxyForGroup for representing the result
00266         String returnTypeClassName = null;
00267         try {
00268             Object[] paramProxy = new Object[0];
00269             Type t = mc.getReifiedMethod().getGenericReturnType();
00270             if (t instanceof TypeVariable) {
00271                 returnTypeClassName = mc.getGenericTypesMapping().get(t).getName();
00272             } else {
00273                 returnTypeClassName = mc.getReifiedMethod().getReturnType().getName();
00274             }
00275 
00276             result = MOP.newInstance(returnTypeClassName,
00277                     null, null, ProxyForGroup.class.getName(), paramProxy);
00278             
00279             ((ProxyForGroup) ((StubObject) result).getProxy()).className = returnTypeClassName;
00280             
00281         } catch (ClassNotReifiableException e1) {
00282             throw new InvocationTargetException(e1, "Method " + mc.getReifiedMethod().toGenericString() + " : cannot return a group of results for the non reifiable type " + returnTypeClassName);
00283         } catch (Exception e2) {
00284                 throw new InvocationTargetException(e2, "Method " + mc.getReifiedMethod().toGenericString() + " : cannot create group of results");
00285         }
00286 
00287         int size = this.memberList.size();
00288 
00289         // Init the lists of result with null value to permit the "set(index)" operation
00290         Vector memberListOfResultGroup = ((ProxyForGroup) ((StubObject) result).getProxy()).memberList;
00291         for (int i = 0; i < size; i++) {
00292             memberListOfResultGroup.add(null);
00293         }
00294 
00295         // Creating Threads
00296         if (isDispatchingCall(mc) == false) {
00297             if (uniqueSerialization) {
00298                 mc.transformEffectiveArgumentsIntoByteArray();
00299             }
00300             for (int index = 0; index < this.memberList.size(); index++)
00301                 this.threadpool.addAJob(new ProcessForAsyncCall(this,
00302                         this.memberList, memberListOfResultGroup, index, mc,
00303                         body));
00304         } else { // isDispatchingCall == true
00305             //Object[] individualEffectiveArguments = distributeParameters(mc);
00306             for (int index = 0; index < memberList.size(); index++) {
00307                 
00308                 Object[] individualEffectiveArguments = new Object[mc.getNumberOfParameter()];
00309                 for (int i = 0; i < mc.getNumberOfParameter(); i++)
00310                     if (ProActiveGroup.isScatterGroupOn(mc.getParameter(i))) {
00311                         individualEffectiveArguments[i] = ProActiveGroup.get(mc.getParameter(
00312                                     i),
00313                                 index % ProActiveGroup.size(mc.getParameter(i)));
00314                     } else {
00315                         individualEffectiveArguments[i] = mc.getParameter(i);
00316                     }
00317                 this.threadpool.addAJob(new ProcessForAsyncCall(this,
00318                         this.memberList, memberListOfResultGroup, index,
00319                         new MethodCall(mc.getReifiedMethod(), mc.getGenericTypesMapping(),
00320                             individualEffectiveArguments, mc.getExceptionContext()),
00321                         body));
00322             }
00323         }
00324 
00325         LocalBodyStore.getInstance().setCurrentThreadBody(body);
00326         if (Profiling.GROUP) {
00327             timer.stop();
00328         }
00329         return result;
00330     }
00331 
00337 //    protected Object[] distributeParameters(MethodCall mc) {
00338 //        Object[] individualEffectiveArguments = null;
00339 //        individualEffectiveArguments = new Object[mc.getNumberOfParameter()];
00340 //        for (int index = 0; index < memberList.size(); index++) {
00341 //            for (int i = 0; i < mc.getNumberOfParameter(); i++)
00342 //                if (ProActiveGroup.isScatterGroupOn(mc.getParameter(i))) {
00343 //                    individualEffectiveArguments[i] = ProActiveGroup.get(mc.getParameter(
00344 //                                i),
00345 //                            index % ProActiveGroup.size(mc.getParameter(i)));
00346 //                } else {
00347 //                    individualEffectiveArguments[i] = mc.getParameter(i);
00348 //                }
00349 //        }
00350 //        return individualEffectiveArguments;
00351 //    }
00352 
00359     protected void addToListOfResult(Vector memberListOfResultGroup,
00360         Object result, int index) {
00361         memberListOfResultGroup.set(index, result);
00362     }
00363 
00364     /* -------------------- FOR ONEWAY CALL ---------------------- */
00365 
00370     protected void oneWayCallOnGroup(MethodCall mc,
00371         ExceptionListException exceptionList) throws InvocationTargetException {
00372         Body body = ProActive.getBodyOnThis();
00373         if (Profiling.GROUP) {
00374             timer.setTimer("oneWayCallOnGroup." + mc.getName());
00375             timer.start();
00376         }
00377 
00378         // Creating Threads
00379         if (isDispatchingCall(mc) == false) {
00380             if (uniqueSerialization) {
00381                 mc.transformEffectiveArgumentsIntoByteArray();
00382             }
00383             for (int index = 0; index < this.memberList.size(); index++) {
00384                 this.threadpool.addAJob(new ProcessForOneWayCall(this,
00385                         this.memberList, index, mc, body, exceptionList));
00386             }
00387         } else { // isDispatchingCall == true
00388 //            Object[] individualEffectiveArguments = distributeParameters(mc);
00389             for (int index = 0; index < memberList.size(); index++) {
00390                 Object[] individualEffectiveArguments = new Object[mc.getNumberOfParameter()];
00391                 for (int i = 0; i < mc.getNumberOfParameter(); i++)
00392                     if (ProActiveGroup.isScatterGroupOn(mc.getParameter(i))) {
00393                         individualEffectiveArguments[i] = ProActiveGroup.get(mc.getParameter(
00394                                     i),
00395                                 index % ProActiveGroup.size(mc.getParameter(i)));
00396                     } else {
00397                         individualEffectiveArguments[i] = mc.getParameter(i);
00398                     }
00399                 this.threadpool.addAJob(new ProcessForOneWayCall(this,
00400                         this.memberList, index,
00401                         new MethodCall(mc.getReifiedMethod(), mc.getGenericTypesMapping(),
00402                             individualEffectiveArguments, mc.getExceptionContext()),
00403                         body, exceptionList));
00404             }
00405         }
00406 
00407         LocalBodyStore.getInstance().setCurrentThreadBody(body);
00408 
00409         if (Profiling.GROUP) {
00410             timer.stop();
00411         }
00412     }
00413 
00414     /* ------------------- java.util.List methods------------------ */
00415 
00423     public boolean add(Object o) {
00424         try {
00425             if ((MOP.forName(this.className)).isAssignableFrom(o.getClass())) {
00426 
00427                 /* if o is an reified object and if it is "assignableFrom" the class of the group, ... add it into the group */
00428                 if (MOP.isReifiedObject(o)) {
00429                     return this.memberList.add(o);
00430                 }
00431                 // COMPONENTS
00432 
00433                 /* if o is a reference on a component interface*/
00434                 else if (o instanceof ProActiveInterface) {
00435                     return this.memberList.add(o);
00436                 } /* if o is a Group */ else if (o instanceof org.objectweb.proactive.core.group.ProxyForGroup) {
00437 
00438                     /* like an addMerge call */
00439                     return this.memberList.addAll(((org.objectweb.proactive.core.group.ProxyForGroup) o).memberList);
00440                 } /* o is a standard Java object */ else {
00441                     return this.memberList.add(o);
00442                 }
00443             } else {
00444                 if (logger.isInfoEnabled()) {
00445                     logger.info("uncompatible Object");
00446                 }
00447                 return false;
00448             }
00449         } catch (java.lang.ClassNotFoundException e) {
00450             if (logger.isInfoEnabled()) {
00451                 logger.info("Unknown class : " + this.className);
00452             }
00453         }
00454         return true;
00455     }
00456 
00462     public boolean addAll(Collection c) {
00463         boolean modified = false;
00464         Iterator iterator = c.iterator();
00465         while (iterator.hasNext()) {
00466             modified |= this.add(iterator.next());
00467         }
00468         return modified;
00469     }
00470 
00475     public void clear() {
00476         this.memberList.clear();
00477     }
00478 
00483     public boolean contains(Object o) {
00484         return this.memberList.contains(o);
00485     }
00486 
00492     public boolean containsAll(Collection c) {
00493         boolean contained;
00494         Iterator iterator = c.iterator();
00495         while (iterator.hasNext()) {
00496             contained = this.contains(iterator.next());
00497             if (!contained) {
00498                 return false;
00499             }
00500         }
00501         return true;
00502     }
00503 
00515     public boolean equals(Object o) {
00516         ProxyForGroup p = ProActiveGroup.findProxyForGroup(o);
00517         if (p != null) {
00518             // comparing with another group
00519             return this.memberList.equals(((org.objectweb.proactive.core.group.ProxyForGroup) p).memberList);
00520         } else {
00521             return false;
00522         }
00523     }
00524 
00529     public int hashCode() {
00530         return this.memberList.hashCode();
00531     }
00532 
00537     public boolean isEmpty() {
00538         return this.memberList.isEmpty();
00539     }
00540 
00545     public Iterator iterator() {
00546         return this.memberList.iterator();
00547     }
00548 
00555     public boolean remove(Object o) {
00556         return this.memberList.remove(o);
00557     }
00558 
00565     public boolean removeAll(Collection c) {
00566         boolean modified = false;
00567         Iterator iterator = c.iterator();
00568         while (iterator.hasNext()) {
00569             modified |= this.remove(iterator.next());
00570         }
00571         return modified;
00572     }
00573 
00580     public boolean retainAll(Collection c) {
00581         boolean modified = false;
00582         Iterator iterator = c.iterator();
00583         while (iterator.hasNext()) {
00584             Object tmp = iterator.next();
00585             if (this.contains(tmp)) {
00586                 modified |= this.remove(tmp);
00587             }
00588         }
00589         return modified;
00590     }
00591 
00596     public int size() {
00597         return this.memberList.size();
00598     }
00599 
00604     public Object[] toArray() {
00605         return this.memberList.toArray();
00606     }
00607 
00615     public Object[] toArray(Object[] a) {
00616         return this.memberList.toArray(a);
00617     }
00618 
00619     /* ---------------------- THE GROUP'S METHOD ------------------- */
00620 
00629     public void addMerge(Object oGroup) {
00630         try {
00631 
00632             /* check oGroup is an Reified Object and if it is "assignableFrom" the class of the group */
00633             if ((MOP.isReifiedObject(oGroup)) &&
00634                     ((MOP.forName(this.className)).isAssignableFrom(
00635                         oGroup.getClass()))) {
00636 
00637                 /* check oGroup is an object representing a group */
00638                 if (((StubObject) oGroup).getProxy() instanceof org.objectweb.proactive.core.group.ProxyForGroup) {
00639                     memberList.addAll(((ProxyForGroup) ((StubObject) oGroup).getProxy()).memberList);
00640                 } /* if oGroup is a Standard Active Object (but not a group), just add it */ else {
00641                     this.add(oGroup);
00642                 }
00643             } /* if oGroup is a Group */ else if (oGroup instanceof org.objectweb.proactive.core.group.ProxyForGroup) {
00644                 memberList.addAll(((org.objectweb.proactive.core.group.ProxyForGroup) oGroup).memberList);
00645             }
00646         } catch (java.lang.ClassNotFoundException e) {
00647             if (logger.isInfoEnabled()) {
00648                 logger.info("Unknown class : " + this.className);
00649             }
00650         }
00651     }
00652 
00659     public int indexOf(Object obj) {
00660         return this.memberList.indexOf(obj);
00661     }
00662 
00667     public ListIterator listIterator() {
00668         return this.memberList.listIterator();
00669     }
00670 
00676     public Object remove(int index) {
00677         // decrease indexes in the map element names <-> indexes
00678         Iterator it = elementNames.keySet().iterator();
00679         while (it.hasNext()) {
00680             String key = (String) it.next();
00681             Integer value = (Integer) elementNames.get(key);
00682             if (value.intValue() > index) {
00683                 elementNames.put(key, new Integer(value.intValue() - 1));
00684             }
00685         }
00686         return this.memberList.remove(index);
00687     }
00688 
00694     public Object get(int i) {
00695         return this.memberList.get(i);
00696     }
00697 
00703     public Class getType() throws java.lang.ClassNotFoundException {
00704         return MOP.forName(this.className);
00705     }
00706 
00711     public String getTypeName() {
00712         return this.className;
00713     }
00714 
00719     public Object getGroupByType() {
00720         Object result;
00721         try { // a new proxy is generated
00722             result = MOP.newInstance(this.className, 
00723                     null, null, ProxyForGroup.class.getName(), null);
00724         } catch (Exception e) {
00725             e.printStackTrace();
00726             return null;
00727         }
00728         ProxyForGroup proxy = (ProxyForGroup) ((StubObject) result).getProxy();
00729         proxy.memberList = this.memberList;
00730         proxy.className = this.className;
00731         proxy.proxyForGroupID = this.proxyForGroupID;
00732         proxy.waited = this.waited;
00733         return result;
00734     }
00735 
00736     //  This is the best thing to do, but createStubObject has a private acces !!!! :
00737     //    // Instanciates the stub object
00738     //    StubObject stub = MOP.createStubObject(this.className, MOP.forName(this.className));
00739     //    // Connects the proxy to the stub
00740     //    stub.setProxy(this);
00741     //    return stub;
00742     //  }
00743     //  An other way is to "store" the stub and return it when asked
00744 
00750     public Group union(Group g) {
00751         try {
00752             if ((MOP.forName(this.getTypeName())).isAssignableFrom(MOP.forName(
00753                             g.getTypeName()))) {
00754                 ProxyForGroup result = new ProxyForGroup(this.getTypeName());
00755 
00756                 // add the members of this
00757                 Iterator it = this.iterator();
00758                 while (it.hasNext()) {
00759                     result.add(it.next());
00760                 }
00761 
00762                 // add the members of g
00763                 it = g.iterator();
00764                 while (it.hasNext()) {
00765                     result.add(it.next());
00766                 }
00767                 return result;
00768             }
00769         } catch (ClassNotFoundException e) {
00770             e.printStackTrace();
00771         } catch (ConstructionOfReifiedObjectFailedException e) {
00772             e.printStackTrace();
00773         }
00774 
00775         // the group are incompatible (i.e. they have not members of the the same class)
00776         return null;
00777     }
00778 
00784     public Group intersection(Group g) {
00785         try {
00786             if ((MOP.forName(this.getTypeName())).isAssignableFrom(MOP.forName(
00787                             g.getTypeName()))) {
00788                 ProxyForGroup result = new ProxyForGroup(this.getTypeName());
00789                 Object member;
00790                 Iterator it = this.iterator();
00791 
00792                 // add the members of the group that belong to g
00793                 while (it.hasNext()) {
00794                     member = it.next();
00795                     if (g.indexOf(member) > -1) {
00796                         result.add(member);
00797                     }
00798                 }
00799                 return result;
00800             }
00801         } catch (ClassNotFoundException e) {
00802             e.printStackTrace();
00803         } catch (ConstructionOfReifiedObjectFailedException e) {
00804             e.printStackTrace();
00805         }
00806 
00807         // the group are incompatible (i.e. they have not members of the the same class)
00808         return null;
00809     }
00810 
00816     public Group exclude(Group g) {
00817         try {
00818             if ((MOP.forName(this.getTypeName())).isAssignableFrom(MOP.forName(
00819                             g.getTypeName()))) {
00820                 ProxyForGroup result = new ProxyForGroup(this.getTypeName());
00821                 Object member;
00822                 Iterator it = this.iterator();
00823                 while (it.hasNext()) {
00824                     member = it.next();
00825                     if (g.indexOf(member) < 0) {
00826                         result.add(member);
00827                     }
00828                 }
00829                 return result;
00830             }
00831         } catch (ClassNotFoundException e) {
00832             e.printStackTrace();
00833         } catch (ConstructionOfReifiedObjectFailedException e) {
00834             e.printStackTrace();
00835         }
00836 
00837         // the group are incompatible (i.e. they have not members of the the same class)
00838         return null;
00839     }
00840 
00846     public Group difference(Group g) {
00847         try {
00848             if ((MOP.forName(this.getTypeName())).isAssignableFrom(MOP.forName(
00849                             g.getTypeName()))) {
00850                 ProxyForGroup result = new ProxyForGroup(this.getTypeName());
00851                 Object member;
00852                 Iterator it = this.iterator();
00853 
00854                 // add the members of the group that do not belong to g
00855                 while (it.hasNext()) {
00856                     member = it.next();
00857                     if (g.indexOf(member) < 0) {
00858                         result.add(member);
00859                     }
00860                 }
00861                 it = g.iterator();
00862                 // add the members of g that do not belong to the group
00863                 while (it.hasNext()) {
00864                     member = it.next();
00865                     if (this.indexOf(member) < 0) {
00866                         result.add(member);
00867                     }
00868                 }
00869                 return result;
00870             }
00871         } catch (ClassNotFoundException e) {
00872             e.printStackTrace();
00873         } catch (ConstructionOfReifiedObjectFailedException e) {
00874             e.printStackTrace();
00875         }
00876 
00877         // the group are incompatible (i.e. they have not members of the the same class)
00878         return null;
00879     }
00880 
00887     public Group range(int begin, int end) {
00888         // bag arguments => return null
00889         if (begin > end) {
00890             return null;
00891         }
00892         if (begin < 0) {
00893             begin = 0;
00894         }
00895         if (end > this.size()) {
00896             end = this.size();
00897         }
00898         try {
00899             ProxyForGroup result = new ProxyForGroup(this.getTypeName());
00900             for (int i = begin; i <= end; i++) {
00901                 result.add(this.get(i));
00902             }
00903             return result;
00904         } catch (ConstructionOfReifiedObjectFailedException e) {
00905             e.printStackTrace();
00906             return null;
00907         }
00908     }
00909 
00914     public void setSPMDGroup(Object spmdGroup) {
00915         try {
00916             this.reify(new MethodCallSetSPMDGroup(spmdGroup));
00917         } catch (InvocationTargetException e) {
00918             logger.info("Unable to set the SPMD group");
00919             e.printStackTrace();
00920         }
00921     }
00922 
00926     public void display() {
00927         logger.info("Number of member : " + memberList.size());
00928         for (int i = 0; i < memberList.size(); i++)
00929             logger.info("  " + i + " : " +
00930                 memberList.get(i).getClass().getName());
00931     }
00932 
00933     /* ------------------- SYNCHRONIZATION -------------------- */
00934 
00938     public void waitAll() {
00939         ProActive.waitForAll(this.memberList);
00940     }
00941 
00945     public void waitOne() {
00946         ProActive.waitForAny(this.memberList);
00947     }
00948 
00953     public void waitTheNth(int n) {
00954         ProActive.waitFor(this.memberList.get(n));
00955     }
00956 
00961     public void waitN(int n) {
00962         for (int i = 0; i < n; i++) {
00963             this.waitTheNth(i);
00964         }
00965     }
00966 
00971     public Object waitAndGetOne() {
00972         return this.memberList.get(ProActive.waitForAny(this.memberList));
00973     }
00974 
00979     public Object waitAndGetOneThenRemoveIt() {
00980         return this.memberList.remove(ProActive.waitForAny(this.memberList));
00981     }
00982 
00988     public Object waitAndGetTheNth(int n) {
00989         ProActive.waitForTheNth(this.memberList, n);
00990         return this.memberList.get(n);
00991     }
00992 
00997     public int waitOneAndGetIndex() {
00998         int index = 0;
00999         this.memberList.get(ProActive.waitForAny(this.memberList));
01000         while (ProActive.isAwaited(this.memberList.get(index))) {
01001             index++;
01002         }
01003         return index;
01004     }
01005 
01010     public boolean allAwaited() {
01011         for (int i = 0; i < this.memberList.size(); i++)
01012             if (!(ProActive.isAwaited(this.memberList.get(i)))) {
01013                 return false;
01014             }
01015         return true;
01016     }
01017 
01022     public boolean allArrived() {
01023         for (int i = 0; i < this.memberList.size(); i++)
01024             if (ProActive.isAwaited(this.memberList.get(i))) {
01025                 return false;
01026             }
01027         return true;
01028     }
01029 
01035     public ExceptionListException getExceptionList() {
01036         ExceptionListException exceptionList = new ExceptionListException();
01037         for (int i = 0; i < this.memberList.size(); i++) {
01038             if (this.memberList.get(i) instanceof Throwable) {
01039                 exceptionList.add(new ExceptionInGroup(null, i,
01040                         (Throwable) this.memberList.get(i)));
01041             }
01042         }
01043         return exceptionList;
01044     }
01045 
01052     public void purgeExceptionAndNull() {
01053         Iterator it = this.memberList.iterator();
01054         while (it.hasNext()) {
01055             Object element = (Object) it.next();
01056             if ((element instanceof Throwable) || (element == null)) {
01057                 it.remove();
01058             }
01059         }
01060     }
01061 
01066     public void setRatioMemberToThread(int i) {
01067         this.threadpool.ratio(i);
01068     }
01069 
01074     public void setAdditionalThread(int i) {
01075         this.threadpool.thread(i);
01076     }
01077 
01078     /* ---------------------- METHOD FOR SYNCHRONOUS CREATION OF A TYPED GROUP ---------------------- */
01079 
01086     public void createMemberWithMultithread(String className, Class[] genericParameters,
01087         Object[][] params, Node[] nodeList) {
01088         // Initializes the Group to the correct size
01089         for (int i = 0; i < params.length; i++) {
01090             this.memberList.add(null);
01091         }
01092         for (int i = 0; i < params.length; i++) {
01093             this.threadpool.addAJob(new ProcessForGroupCreation(this,
01094                     className, genericParameters, params[i], nodeList[i % nodeList.length], i));
01095         }
01096         this.threadpool.complete();
01097     }
01098 
01105     protected void createMemberWithMultithread(String className, Class[] genericParameters,
01106         Object[] params, Node[] nodeList) {
01107         // Initializes the Group to the correct size
01108         for (int i = 0; i < nodeList.length; i++) {
01109             this.memberList.add(null);
01110         }
01111         for (int i = 0; i < nodeList.length; i++) {
01112             this.threadpool.addAJob(new ProcessForGroupCreation(this,
01113                     className, genericParameters, params, nodeList[i], i));
01114         }
01115         this.threadpool.complete();
01116     }
01117 
01118     /*
01119      * @see java.util.List#set(int, java.lang.Object)
01120      */
01121      public Object set(int index, Object o) {
01122         return this.memberList.set(index, o);
01123     }
01124      
01125      
01126 
01127     /*
01128      * @see java.util.List#add(int, java.lang.Object)
01129      */
01130     public void add(int index, Object element) {
01131         memberList.add(index, element);
01132     }
01133 
01134     /*
01135      * @see java.util.List#addAll(int, java.util.Collection)
01136      */
01137     public boolean addAll(int index, Collection c) {
01138         return memberList.addAll(index, c);
01139     }
01140 
01141     /*
01142      * @see java.util.List#lastIndexOf(java.lang.Object)
01143      */
01144     public int lastIndexOf(Object o) {
01145         return memberList.lastIndexOf(o);
01146     }
01147 
01148     /*
01149      * @see java.util.List#listIterator(int)
01150      */
01151     public ListIterator listIterator(int index) {
01152         return memberList.listIterator(index);
01153     }
01154 
01155     /*
01156      * @see java.util.List#subList(int, int)
01157      */
01158     public List subList(int fromIndex, int toIndex) {
01159         return memberList.subList(fromIndex, toIndex);
01160     }
01161 
01162     /* ------------------------ PRIVATE METHODS FOR SERIALIZATION --------------------- */
01163     private void writeObject(java.io.ObjectOutputStream out)
01164         throws java.io.IOException {
01165         // this.threadpool.finalize();
01166         out.defaultWriteObject();
01167     }
01168 
01169     private void readObject(java.io.ObjectInputStream in)
01170         throws java.io.IOException, ClassNotFoundException {
01171         in.defaultReadObject();
01172         this.proxyForGroupID = new UniqueID();
01173         this.threadpool = new ThreadPool();
01174     }
01175 
01176     //Map class style method
01177 
01188     public boolean containsKey(String key) {
01189         return this.elementNames.containsKey(key);
01190     }
01191 
01201     public boolean containsValue(Object value) {
01202         return this.memberList.contains(value);
01203     }
01204 
01220     public synchronized Object getNamedElement(String key) {
01221         return get(((Integer) this.elementNames.get(key)).intValue());
01222     }
01223 
01237     public synchronized void addNamedElement(String key, Object value) {
01238         if (elementNames.containsKey(key)) {
01239             removeNamedElement(key);
01240         }
01241         this.elementNames.put(key, new Integer(this.size()));
01242         this.add(value);
01243     }
01244 
01255     public Set keySet() {
01256         return this.elementNames.keySet();
01257     }
01258 
01264     public synchronized Object removeNamedElement(String key) {
01265         int index = ((Integer) elementNames.get(key)).intValue();
01266         Object removed = get(index);
01267         remove(index);
01268         elementNames.remove(key);
01269         return removed;
01270     }
01271     
01272     protected void redistributeParameters(MethodCall mc) {
01273         // nothing here
01274     }
01275 
01276     
01280     public String getClassName() {
01281     
01282         return className;
01283     }
01284 
01285     
01289     public Vector getMemberList() {
01290     
01291         return memberList;
01292     }
01293 
01294     
01298     public void setClassName(String className) {
01299     
01300         this.className = className;
01301     }
01302 
01303 }

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