org/objectweb/proactive/core/component/controller/GathercastControllerImpl.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.component.controller;
00032 
00033 import java.lang.reflect.Method;
00034 import java.util.ArrayList;
00035 import java.util.Arrays;
00036 import java.util.HashMap;
00037 import java.util.Iterator;
00038 import java.util.List;
00039 import java.util.Map;
00040 
00041 import org.objectweb.fractal.api.Component;
00042 import org.objectweb.fractal.api.control.IllegalBindingException;
00043 import org.objectweb.fractal.api.factory.InstantiationException;
00044 import org.objectweb.fractal.api.type.TypeFactory;
00045 import org.objectweb.proactive.core.ProActiveRuntimeException;
00046 import org.objectweb.proactive.core.body.migration.MigrationException;
00047 import org.objectweb.proactive.core.body.request.ServeException;
00048 import org.objectweb.proactive.core.component.Constants;
00049 import org.objectweb.proactive.core.component.ProActiveInterface;
00050 import org.objectweb.proactive.core.component.collectiveitfs.GatherBindingChecker;
00051 import org.objectweb.proactive.core.component.collectiveitfs.GatherRequestsQueues;
00052 import org.objectweb.proactive.core.component.exceptions.ParameterDispatchException;
00053 import org.objectweb.proactive.core.component.identity.ProActiveComponent;
00054 import org.objectweb.proactive.core.component.representative.ItfID;
00055 import org.objectweb.proactive.core.component.request.ComponentRequest;
00056 import org.objectweb.proactive.core.component.type.ProActiveInterfaceType;
00057 import org.objectweb.proactive.core.component.type.ProActiveTypeFactoryImpl;
00058 import org.objectweb.proactive.core.node.Node;
00059 
00060 
00061 public class GathercastControllerImpl
00062     extends AbstractProActiveController
00063     implements GathercastController {
00064     private Map<String, List<ItfID>> bindingsOnServerItfs = new HashMap<String, List<ItfID>>();
00065     private Map<String, ProActiveInterface> gatherItfs = new HashMap<String, ProActiveInterface>();
00066     private GatherRequestsQueues gatherRequestsHandler;
00067 
00068     public GathercastControllerImpl(Component owner) {
00069         super(owner);
00070     }
00071 
00072     /*
00073      * @see org.objectweb.proactive.core.component.controller.AbstractCollectiveInterfaceController#init()
00074      */
00075     public void init() {
00076         if (gatherRequestsHandler == null) {
00077             gatherRequestsHandler = new GatherRequestsQueues((ProActiveComponent) owner);
00078             List<Object> interfaces = Arrays.asList(owner.getFcInterfaces());
00079             Iterator<Object> it = interfaces.iterator();
00080 
00081             while (it.hasNext()) {
00082                 addManagedInterface((ProActiveInterface) it.next());
00083             }
00084         }
00085     }
00086 
00087     private boolean addManagedInterface(ProActiveInterface itf) {
00088         if (gatherItfs.containsKey(itf.getFcItfName())) {
00089             //            controllerLogger.error("the interface named " + itf.getFcItfName() +
00090             //                " is already managed by the collective interfaces controller");
00091             return false;
00092         }
00093 
00094         ProActiveInterfaceType itfType = (ProActiveInterfaceType) itf.getFcItfType();
00095 
00096         if (itfType.isFcGathercastItf()) {
00097             gatherItfs.put(itf.getFcItfName(), itf);
00098         } else {
00099             //            controllerLogger.error("the interface named " + itf.getFcItfName() +
00100             //                " cannot be managed by this collective interfaces controller");
00101             return false;
00102         }
00103 
00104         return true;
00105     }
00106 
00107     protected Method searchMatchingMethod(Method clientSideMethod,
00108         Method[] serverSideMethods, boolean clientItfIsMulticast,
00109         boolean serverItfIsGathercast, ProActiveInterface serverSideItf) {
00110         return searchMatchingMethod(clientSideMethod, serverSideMethods,
00111             clientItfIsMulticast);
00112     }
00113 
00114     /*
00115      * @see org.objectweb.proactive.core.component.controller.AbstractCollectiveInterfaceController#searchMatchingMethod(java.lang.reflect.Method, java.lang.reflect.Method[])
00116      */
00117     protected Method searchMatchingMethod(Method clientSideMethod,
00118         Method[] serverSideMethods, boolean clientItfIsMulticast) {
00119         try {
00120             return GatherBindingChecker.searchMatchingMethod(clientSideMethod,
00121                 serverSideMethods, clientItfIsMulticast);
00122         } catch (ParameterDispatchException e) {
00123             e.printStackTrace();
00124         } catch (NoSuchMethodException e) {
00125             e.printStackTrace();
00126         }
00127         return null;
00128     }
00129 
00130     /*
00131      * @see org.objectweb.proactive.core.component.controller.AbstractProActiveController#setControllerItfType()
00132      */
00133     @Override
00134     protected void setControllerItfType() {
00135         try {
00136             setItfType(ProActiveTypeFactoryImpl.instance()
00137                                                .createFcItfType(Constants.GATHERCAST_CONTROLLER,
00138                     GathercastController.class.getName(), TypeFactory.SERVER,
00139                     TypeFactory.MANDATORY, TypeFactory.SINGLE));
00140         } catch (InstantiationException e) {
00141             throw new ProActiveRuntimeException(
00142                 "cannot create controller type for controller " +
00143                 this.getClass().getName());
00144         }
00145     }
00146 
00147     /*
00148      * @see org.objectweb.proactive.core.component.controller.GatherController#handleRequestOnGatherItf(org.objectweb.proactive.core.component.request.ComponentRequest)
00149      */
00150     public Object handleRequestOnGatherItf(ComponentRequest r)
00151         throws ServeException {
00152         if (gatherRequestsHandler == null) {
00153             init();
00154         }
00155         return gatherRequestsHandler.addRequest(r);
00156     }
00157 
00158     /*
00159      * TODO : throw exception when binding already exists? (this would make the method synchronous)
00160      * @see org.objectweb.proactive.core.component.controller.ProActiveBindingController#addedBindingOnServerItf(org.objectweb.proactive.core.component.ProActiveInterface, org.objectweb.proactive.core.component.ProActiveInterface)
00161      */
00162     public void addedBindingOnServerItf(String serverItfName,
00163         ProActiveComponent sender, String clientItfName) {
00164         ItfID itfID = new ItfID(clientItfName, sender.getID());
00165         if (bindingsOnServerItfs.containsKey(serverItfName)) {
00166             if (bindingsOnServerItfs.get(serverItfName).contains(itfID)) {
00167                 throw new ProActiveRuntimeException(
00168                     "trying to add twice the binding of client interface " +
00169                     clientItfName + " on server interface " + serverItfName);
00170             }
00171             bindingsOnServerItfs.get(serverItfName).add(itfID);
00172         } else {
00173             List<ItfID> connectedClientItfs = new ArrayList<ItfID>();
00174             connectedClientItfs.add(itfID);
00175             bindingsOnServerItfs.put(serverItfName, connectedClientItfs);
00176         }
00177     }
00178 
00179     /*
00180      *
00181      * @see org.objectweb.proactive.core.component.controller.ProActiveBindingController#removedBindingOnServerItf(java.lang.String, org.objectweb.proactive.core.component.identity.ProActiveComponent, java.lang.String)
00182      */
00183     public void removedBindingOnServerItf(String serverItfName,
00184         ProActiveComponent owner, String clientItfName) {
00185         ItfID itfID = new ItfID(clientItfName, owner.getID());
00186         if (bindingsOnServerItfs.containsKey(serverItfName)) {
00187             List<ItfID> connectedClientItfs = bindingsOnServerItfs.get(serverItfName);
00188             if (connectedClientItfs.contains(itfID)) {
00189                 connectedClientItfs.remove(itfID);
00190             } else {
00191                 controllerLogger.error(
00192                     "could not remove binding on server interface " +
00193                     serverItfName +
00194                     " because owner component is not listed as connected components");
00195             }
00196         } else {
00197             controllerLogger.error(
00198                 "could not remove binding on server interface " +
00199                 serverItfName +
00200                 " because there is no component listed as connected on this server interface");
00201         }
00202     }
00203 
00204     public List<ItfID> getConnectedClientItfs(String serverItfName) {
00205         return bindingsOnServerItfs.get(serverItfName);
00206     }
00207 
00208     @Override
00209     public void migrateDependentActiveObjectsTo(Node node)
00210         throws MigrationException {
00211         if (gatherRequestsHandler != null) {
00212             gatherRequestsHandler.migrateFuturesHandlersTo(node);
00213         }
00214     }
00215     
00216         public void ensureCompatibility(ProActiveInterfaceType clientItfType, ProActiveInterface itf) throws IllegalBindingException {
00217                 // nothing to do in this version
00218                 
00219         }
00220 
00221         private void writeObject(java.io.ObjectOutputStream out)
00222         throws java.io.IOException {
00223         out.defaultWriteObject();
00224     }
00225 
00226     private void readObject(java.io.ObjectInputStream in)
00227         throws java.io.IOException, ClassNotFoundException {
00228         in.defaultReadObject();
00229     }
00230 }

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