00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00090
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
00100
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
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
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
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
00160
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
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
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 }