org/objectweb/proactive/core/component/type/annotations/multicast/ParamDispatchMode.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.type.annotations.multicast;
00032 
00033 import java.io.Serializable;
00034 import java.lang.reflect.ParameterizedType;
00035 import java.lang.reflect.Type;
00036 import java.util.ArrayList;
00037 import java.util.List;
00038 
00039 import org.objectweb.proactive.core.component.exceptions.ParameterDispatchException;
00040 
00049 public enum ParamDispatchMode implements ParamDispatch, Serializable {BROADCAST, ONE_TO_ONE, 
00050     ROUND_ROBIN, CUSTOM;
00051     /*
00052      *
00053      * @see org.objectweb.proactive.core.component.type.annotations.ParametersDispatch#dispatch(java.util.List, int, int)
00054      */
00055     private List<Object> dispatch(List<?> inputParameter, int nbOutputReceivers)
00056         throws ParameterDispatchException {
00057         List<Object> result = new ArrayList<Object>();
00058 
00059         switch (this) {
00060         case BROADCAST:
00061             for (int i = 0; i < nbOutputReceivers; i++) {
00062                 result.add(inputParameter);
00063             }
00064             break;
00065         case ONE_TO_ONE:
00066             if (inputParameter.size() != nbOutputReceivers) {
00067                 throw new ParameterDispatchException(
00068                     "in a one-to-one distribution, the list of parameters on the client side" +
00069                     "must have a size equal to the number of connected receivers");
00070             }
00071             for (int i = 0; i < nbOutputReceivers; i++) {
00072                 result.add(inputParameter.get(i));
00073             }
00074 
00075             break;
00076         case ROUND_ROBIN:
00077             for (int i = 0; i < inputParameter.size(); i++) {
00078                 result.add(inputParameter.get(i % nbOutputReceivers));
00079             }
00080             break;
00081         default:
00082             result = BROADCAST.dispatch(inputParameter, nbOutputReceivers);
00083             break;
00084         }
00085 
00086         return result;
00087     }
00088 
00089     public List<Object> dispatch(Object inputParameter, int nbOutputReceivers)
00090         throws ParameterDispatchException {
00091         if (inputParameter instanceof List) {
00092             return dispatch((List) inputParameter, nbOutputReceivers);
00093         }
00094 
00095         // no dispatch in case of non-list parameters
00096         List<Object> result = new ArrayList<Object>();
00097 
00098         for (int i = 0; i < nbOutputReceivers; i++) {
00099             result.add(inputParameter);
00100         }
00101 
00102         return result;
00103     }
00104 
00105     /*
00106      * @see org.objectweb.proactive.core.component.type.annotations.collective.ParamDispatch#getDispatchSize(java.util.List, int)
00107      */
00108     private int expectedDispatchSize(List<?> inputParameter, int nbOutputReceivers)
00109         throws ParameterDispatchException {
00110         int result = 0;
00111 
00112         switch (this) {
00113         case BROADCAST:
00114             result = nbOutputReceivers;
00115             break;
00116         case ONE_TO_ONE:
00117             if (inputParameter.size() != nbOutputReceivers) {
00118                 throw new ParameterDispatchException(
00119                     "in a one-to-one distribution, the list of parameters on the client side" +
00120                     "must have a size equal to the number of connected receivers");
00121             }
00122             result = nbOutputReceivers;
00123             break;
00124         case ROUND_ROBIN:
00125             result = inputParameter.size();
00126             break;
00127         default:
00128             result = BROADCAST.expectedDispatchSize(inputParameter, nbOutputReceivers);
00129         }
00130 
00131         return result;
00132     }
00133 
00134     /*
00135      * @see org.objectweb.proactive.core.component.type.annotations.collective.ParamDispatch#getDispatchSize(java.lang.Object, int)
00136      */
00137     public int expectedDispatchSize(Object inputParameter, int nbOutputReceivers)
00138         throws ParameterDispatchException {
00139         if (inputParameter instanceof List) {
00140             return expectedDispatchSize((List) inputParameter, nbOutputReceivers);
00141         }
00142 
00143         return nbOutputReceivers;
00144     }
00145 
00146     /*
00147      * @see org.objectweb.proactive.core.component.type.annotations.collective.ParamDispatch#matchesClientSideParameterType(java.lang.reflect.Type)
00148      */
00149     public boolean match(Type clientSideInputParameterType, Type serverSideInputParameterType)
00150         throws ParameterDispatchException {
00151         boolean result = false;
00152         boolean clientSideParamTypeIsParameterizedType = (clientSideInputParameterType instanceof ParameterizedType);
00153         boolean serverSideParamTypeIsParameterizedType = (serverSideInputParameterType instanceof ParameterizedType);
00154         Class clientSideClass = null;
00155         Class clientSideElementsType = null;
00156         Class clientSideRawType = null;
00157 
00158         Class serverSideClass = null;
00159         Class serverSideElementsType = null;
00160         Class serverSideRawType = null;
00161 
00162         if (clientSideParamTypeIsParameterizedType) {
00163             clientSideRawType = (Class) ((ParameterizedType) clientSideInputParameterType).getRawType();
00164             if (!(((ParameterizedType)clientSideInputParameterType).getActualTypeArguments().length == 1)) {
00165                 throw new ParameterDispatchException("client side input parameter type " +
00166                                                      clientSideInputParameterType + " can only be parameterized with one type");
00167             }
00168             Type cType = ((ParameterizedType) clientSideInputParameterType).getActualTypeArguments()[0];
00169             if (cType instanceof ParameterizedType) {
00170                 clientSideElementsType = (Class)((ParameterizedType)cType).getRawType();
00171             } else {
00172                 clientSideElementsType = (Class)cType;
00173             }
00174         } else {
00175             if (clientSideInputParameterType instanceof Class) {
00176                 clientSideClass = (Class) clientSideInputParameterType;
00177             } else {
00178                 throw new ParameterDispatchException("client side input parameter type " +
00179                     clientSideInputParameterType + " can only be either a parameterized type or a class");
00180             }
00181         }
00182 
00183         if (serverSideParamTypeIsParameterizedType) {
00184             serverSideRawType = ((Class) ((ParameterizedType) serverSideInputParameterType).getRawType());
00185             if (!(((ParameterizedType)serverSideInputParameterType).getActualTypeArguments().length == 1)) {
00186                 throw new ParameterDispatchException("server side input parameter type " +
00187                                                      serverSideInputParameterType + " can only be parameterized with one type");
00188             }
00189             Type sType = ((ParameterizedType) serverSideInputParameterType).getActualTypeArguments()[0];
00190             if (sType instanceof ParameterizedType) {
00191                 serverSideElementsType = (Class)((ParameterizedType)sType).getRawType();
00192             } else {
00193                 serverSideElementsType = (Class)sType;
00194             }
00195             
00196             serverSideElementsType = ((Class) ((ParameterizedType) serverSideInputParameterType).getOwnerType());
00197         } else {
00198             if (serverSideInputParameterType instanceof Class) {
00199                 serverSideClass = (Class) serverSideInputParameterType;
00200             } else {
00201                 throw new ParameterDispatchException("server side input parameter type " +
00202                     serverSideInputParameterType + " is incompatible with " +
00203                     "client side input parameter type " + clientSideInputParameterType);
00204             }
00205         }
00206 
00207         switch (this) {
00208         case BROADCAST:
00209             if (clientSideParamTypeIsParameterizedType) {
00210                 if (serverSideParamTypeIsParameterizedType) {
00211                     result = clientSideRawType.isAssignableFrom(serverSideRawType) && clientSideElementsType.isAssignableFrom(clientSideElementsType);
00212                 } else {
00213                     result = true; // maybe this constraint should be softened
00214                 }
00215             } else {
00216                 result = clientSideClass.isAssignableFrom(serverSideClass);
00217             }
00218             break;
00219         case ONE_TO_ONE:
00220             if (clientSideParamTypeIsParameterizedType) {
00221                 if (clientSideElementsType.isAssignableFrom(serverSideClass)) {
00222                     result = true;
00223                 } else if (serverSideParamTypeIsParameterizedType) {
00224                     result = clientSideElementsType.isAssignableFrom(serverSideRawType);
00225                 }
00226             } else {
00227                 result = false; // maybe this constraint should be softened
00228             }
00229             break;
00230         case ROUND_ROBIN:
00231             result = ONE_TO_ONE.match(clientSideInputParameterType, serverSideInputParameterType);
00232             break;
00233         default:
00234             result = BROADCAST.match(clientSideInputParameterType, serverSideInputParameterType);
00235         }
00236 
00237         return result;
00238     }
00239 }

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