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

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