KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > locationserver > RequestWithLocationServer


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.locationserver;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.Body;
36 import org.objectweb.proactive.ProActive;
37 import org.objectweb.proactive.core.UniqueID;
38 import org.objectweb.proactive.core.body.LocalBodyStore;
39 import org.objectweb.proactive.core.body.UniversalBody;
40 import org.objectweb.proactive.core.body.future.FutureProxy;
41 import org.objectweb.proactive.core.body.reply.Reply;
42 import org.objectweb.proactive.core.body.request.RequestImpl;
43 import org.objectweb.proactive.core.body.request.ServeException;
44 import org.objectweb.proactive.core.mop.MethodCall;
45 import org.objectweb.proactive.core.mop.StubObject;
46
47
48 public class RequestWithLocationServer extends RequestImpl
49     implements java.io.Serializable JavaDoc {
50     private static final int MAX_TRIES = 30;
51     static Logger logger = Logger.getLogger(RequestWithLocationServer.class.getName());
52
53     /**
54  * the number of time we try before reporting a failure
55  */

56     private int tries;
57     private transient LocationServer server;
58
59     public RequestWithLocationServer(MethodCall methodCall,
60         UniversalBody sender, boolean isOneWay, long nextSequenceID,
61         LocationServer server) {
62         super(methodCall, sender, isOneWay, nextSequenceID);
63         this.server = server;
64     }
65
66     public Reply serve(Body targetBody) throws ServeException {
67         Reply r = super.serve(targetBody);
68         return r;
69     }
70
71     protected void sendRequest(UniversalBody destinationBody)
72         throws java.io.IOException JavaDoc {
73         try {
74             // startTime = System.currentTimeMillis();
75
destinationBody.receiveRequest(this);
76
77             // long endTime = System.currentTimeMillis();
78
} catch (Exception JavaDoc e) {
79             this.backupSolution(destinationBody);
80         }
81     }
82
83     /**
84  * Implements the backup solution
85  */

86     protected void backupSolution(UniversalBody destinationBody)
87         throws java.io.IOException JavaDoc {
88         boolean ok = false;
89         tries = 0;
90         //get the new location from the server
91
UniqueID bodyID = destinationBody.getID();
92         while (!ok && (tries < MAX_TRIES)) {
93             UniversalBody remoteBody = null;
94             UniversalBody mobile = queryServer(bodyID);
95
96             //we want to bypass the stub/proxy
97
remoteBody = (UniversalBody) ((FutureProxy) ((StubObject) mobile).getProxy()).getResult();
98             try {
99                 remoteBody.receiveRequest(this);
100
101                 //everything went fine, we have to update the current location of the object
102
//so that next requests don't go through the server
103
if (sender != null) {
104                     sender.updateLocation(bodyID, remoteBody);
105                 } else {
106                     LocalBodyStore.getInstance().getLocalBody(getSourceBodyID())
107                                   .updateLocation(bodyID, remoteBody);
108                 }
109                 ok = true;
110             } catch (Exception JavaDoc e) {
111                 logger.debug(
112                     "RequestWithLocationServer: .............. FAILED = " +
113                     " for method " + methodName);
114                 tries++;
115             }
116         }
117     }
118
119     protected UniversalBody queryServer(UniqueID bodyID) {
120         if (server == null) {
121             server = LocationServerFactory.getLocationServer();
122         }
123         UniversalBody mobile = (UniversalBody) server.searchObject(bodyID);
124
125         logger.debug(
126             "RequestWithLocationServer: backupSolution() server has sent an answer");
127
128         ProActive.waitFor(mobile);
129         return mobile;
130     }
131 }
132
Popular Tags