org/objectweb/proactive/ext/mixedlocation/RequestWithMixedLocation.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.ext.mixedlocation;
00032 
00033 import org.apache.log4j.Logger;
00034 import org.objectweb.proactive.ProActive;
00035 import org.objectweb.proactive.core.UniqueID;
00036 import org.objectweb.proactive.core.body.LocalBodyStore;
00037 import org.objectweb.proactive.core.body.UniversalBody;
00038 import org.objectweb.proactive.core.body.ft.protocols.FTManager;
00039 import org.objectweb.proactive.core.body.future.FutureProxy;
00040 import org.objectweb.proactive.core.body.request.RequestImpl;
00041 import org.objectweb.proactive.core.mop.MethodCall;
00042 import org.objectweb.proactive.core.mop.StubObject;
00043 import org.objectweb.proactive.core.util.log.Loggers;
00044 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00045 import org.objectweb.proactive.ext.locationserver.LocationServer;
00046 import org.objectweb.proactive.ext.locationserver.LocationServerFactory;
00047 
00048 
00049 public class RequestWithMixedLocation extends RequestImpl
00050     implements java.io.Serializable {
00051     static Logger logger = ProActiveLogger.getLogger(Loggers.MIGRATION);
00052     private static final int MAX_TRIES = 15;
00053     private static int counter = 0;
00054     private int tries;
00055     transient protected LocationServer server;
00056 
00057     public RequestWithMixedLocation(MethodCall methodCall,
00058         UniversalBody sender, boolean isOneWay, long nextSequenceID,
00059         LocationServer server) {
00060         super(methodCall, sender, isOneWay, nextSequenceID);
00061         if (logger.isDebugEnabled()) {
00062             logger.debug("RequestWithMixedLocation.RequestWithMixedLocation " +
00063                 ++counter);
00064         }
00065         this.server = server;
00066     }
00067 
00068     protected int sendRequest(UniversalBody destinationBody)
00069         throws java.io.IOException {
00070         int ftres = FTManager.NON_FT;
00071         if (logger.isDebugEnabled()) {
00072             logger.debug("RequestWithMixedLocation: sending to universal " +
00073                 counter);
00074         }
00075         try {
00076             ftres = destinationBody.receiveRequest(this);
00077         } catch (Exception e) {
00078             this.backupSolution(destinationBody);
00079         }
00080         return ftres;
00081     }
00082 
00086     protected void backupSolution(UniversalBody destinationBody)
00087         throws java.io.IOException {
00088         boolean ok = false;
00089         tries = 0;
00090         //get the new location from the server
00091         UniqueID bodyID = destinationBody.getID();
00092         while (!ok && (tries < MAX_TRIES)) {
00093             UniversalBody remoteBody = null;
00094             UniversalBody mobile = queryServer(bodyID);
00095 
00096             //we want to bypass the stub/proxy
00097             remoteBody = (UniversalBody) ((FutureProxy) ((StubObject) mobile).getProxy()).getResult();
00098 
00099             try {
00100                 remoteBody.receiveRequest(this);
00101 
00102                 //everything went fine, we have to update the current location of the object
00103                 //so that next requests don't go through the server
00104                 if (sender != null) {
00105                     sender.updateLocation(bodyID, remoteBody);
00106                 } else {
00107                     LocalBodyStore.getInstance().getLocalBody(getSourceBodyID())
00108                                   .updateLocation(bodyID, remoteBody);
00109                 }
00110                 ok = true;
00111             } catch (Exception e) {
00112                 logger.error("FAILED = " + " for method " + methodName);
00113                 tries++;
00114             }
00115         }
00116     }
00117 
00118     protected UniversalBody queryServer(UniqueID bodyID) {
00119         if (server == null) {
00120             server = LocationServerFactory.getLocationServer();
00121         }
00122         UniversalBody mobile = (UniversalBody) server.searchObject(bodyID);
00123         logger.debug("backupSolution() server has sent an answer");
00124         ProActive.waitFor(mobile);
00125         return mobile;
00126     }
00127 }

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