org/objectweb/proactive/ext/locationserver/TimedRequestWithLocationServer.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.objectweb.proactive.Body;
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.reply.Reply;
00041 import org.objectweb.proactive.core.body.request.RequestImpl;
00042 import org.objectweb.proactive.core.body.request.ServeException;
00043 import org.objectweb.proactive.core.mop.MethodCall;
00044 import org.objectweb.proactive.core.mop.StubObject;
00045 import org.objectweb.proactive.core.util.timer.MicroTimer;
00046 import org.objectweb.proactive.ext.locationserver.LocationServer;
00047 import org.objectweb.proactive.ext.locationserver.LocationServerFactory;
00048 
00049 
00050 public class TimedRequestWithLocationServer extends RequestImpl
00051     implements java.io.Serializable {
00052     private static final int MAX_TRIES = 30;
00053 
00058     // private long startTimeServer;
00059     //private long startTimeAgent;
00060     protected long startTime;
00061     private int tries;
00062     private transient LocationServer server;
00063 
00064     public TimedRequestWithLocationServer(MethodCall methodCall,
00065         UniversalBody sender, boolean isOneWay, long nextSequenceID,
00066         LocationServer server) {
00067         super(methodCall, sender, isOneWay, nextSequenceID);
00068         this.server = server;
00069     }
00070 
00071     public Reply serve(Body targetBody) throws ServeException {
00072         MicroTimer timer = new MicroTimer();
00073         timer.start();
00074 
00075         Reply r = super.serve(targetBody);
00076         timer.stop();
00077         System.out.println("TimedRequestWithLocationServer: " +
00078             timer.getCumulatedTime() + " for method " + methodName);
00079         return r;
00080     }
00081 
00082     protected int sendRequest(UniversalBody destinationBody)
00083         throws java.io.IOException {
00084         System.out.println("TimedRequestWithLocationServer: sending to remote " +
00085             methodName);
00086         int ftres = FTManager.NON_FT;
00087         try {
00088             startTime = System.currentTimeMillis();
00089             ftres = destinationBody.receiveRequest(this);
00090 
00091             long endTime = System.currentTimeMillis();
00092             System.out.println(
00093                 "TimedRequestWithLocationServer:  .............. 1/gamma = " +
00094                 (endTime - startTime) + " for method " + methodName);
00095             System.out.println(
00096                 "TimedRequestWithLocationServer:  .............. done  = " +
00097                 (endTime - startTime) + " for method " + methodName);
00098         } catch (Exception e) {
00099             // endTime = System.currentTimeMillis();
00100             //There can only be a problem when trying to contact the Agent
00101             System.out.println(
00102                 "TimedRequestWithLocationServer:  .............. FAILED = " +
00103                 (System.currentTimeMillis() - startTime) + " for method " +
00104                 methodName);
00105             //e.printStackTrace();
00106             System.out.println(">>>>>>>>>>>> Exception " + e);
00107             this.backupSolution(destinationBody);
00108         }
00109         return ftres;
00110     }
00111 
00115     protected void backupSolution(UniversalBody destinationBody)
00116         throws java.io.IOException {
00117         //   long startTimeGamma1=0;
00118         //   long endTimeGamma1=0;
00119         boolean ok = false;
00120         tries = 0;
00121 
00122         //   System.out.println("TimedRequestWithLocationServer: backupSolution() contacting server  at time " + System.currentTimeMillis());
00123         //get the new location from the server
00124         UniqueID bodyID = destinationBody.getID();
00125         while (!ok && (tries < MAX_TRIES)) {
00126             UniversalBody remoteBody = null;
00127             System.out.println(" ==== Query server ==== time " +
00128                 System.currentTimeMillis());
00129 
00130             UniversalBody mobile = queryServer(bodyID);
00131             System.out.println("=========================== time " +
00132                 System.currentTimeMillis());
00133             //we want to bypass the stub/proxy
00134             remoteBody = (UniversalBody) ((FutureProxy) ((StubObject) mobile).getProxy()).getResult();
00135 
00136             long startTimeGamma = System.currentTimeMillis();
00137             try {
00138                 remoteBody.receiveRequest(this);
00139 
00140                 long endTime = System.currentTimeMillis();
00141                 System.out.println(
00142                     "TimedRequestWithLocationServer:  .............. 1/gamma = " +
00143                     (endTime - startTimeGamma) + " for method " + methodName);
00144                 System.out.println(
00145                     "TimedRequestWithLocationServer:  .............. done = " +
00146                     (endTime - startTime) + " for method " + methodName);
00147                 //everything went fine, we have to update the current location of the object
00148                 //so that next requests don't go through the server
00149                 if (sender != null) {
00150                     sender.updateLocation(bodyID, remoteBody);
00151                 } else {
00152                     LocalBodyStore.getInstance().getLocalBody(getSourceBodyID())
00153                                   .updateLocation(bodyID, remoteBody);
00154                 }
00155                 ok = true;
00156             } catch (Exception e) {
00157                 System.out.println(
00158                     "TimedRequestWithLocationServer:  .............. FAILED = " +
00159                     (System.currentTimeMillis() - startTimeGamma) +
00160                     " for method " + methodName);
00161                 tries++;
00162             }
00163         }
00164     }
00165 
00166     protected UniversalBody queryServer(UniqueID bodyID) {
00167         long startTimeBackupSolution = System.currentTimeMillis();
00168         if (server == null) {
00169             server = LocationServerFactory.getLocationServer();
00170         }
00171 
00172         UniversalBody mobile = (UniversalBody) server.searchObject(bodyID);
00173         long endTimeBackupSolution = System.currentTimeMillis();
00174         System.out.println(
00175             "TimedRequestWithLocationServer: backupSolution() server has sent an answer after " +
00176             (endTimeBackupSolution - startTimeBackupSolution));
00177         ProActive.waitFor(mobile);
00178         return mobile;
00179     }
00180 }

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