KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > mixedlocation > RequestWithMixedLocation


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.mixedlocation;
32
33 import org.apache.log4j.Logger;
34
35 import org.objectweb.proactive.ProActive;
36 import org.objectweb.proactive.core.UniqueID;
37 import org.objectweb.proactive.core.body.LocalBodyStore;
38 import org.objectweb.proactive.core.body.UniversalBody;
39 import org.objectweb.proactive.core.body.future.FutureProxy;
40 import org.objectweb.proactive.core.body.request.RequestImpl;
41 import org.objectweb.proactive.core.mop.MethodCall;
42 import org.objectweb.proactive.core.mop.StubObject;
43 import org.objectweb.proactive.ext.locationserver.LocationServer;
44 import org.objectweb.proactive.ext.locationserver.LocationServerFactory;
45
46
47 public class RequestWithMixedLocation extends RequestImpl
48     implements java.io.Serializable JavaDoc {
49     static Logger logger = Logger.getLogger(RequestWithMixedLocation.class.getName());
50     private static final int MAX_TRIES = 15;
51     private static int counter = 0;
52     private int tries;
53     transient protected LocationServer server;
54
55     public RequestWithMixedLocation(MethodCall methodCall,
56         UniversalBody sender, boolean isOneWay, long nextSequenceID,
57         LocationServer server) {
58         super(methodCall, sender, isOneWay, nextSequenceID);
59         if (logger.isDebugEnabled()) {
60             logger.debug("RequestWithMixedLocation.RequestWithMixedLocation " +
61                 ++counter);
62         }
63         this.server = server;
64     }
65
66     protected void sendRequest(UniversalBody destinationBody)
67         throws java.io.IOException JavaDoc {
68         if (logger.isDebugEnabled()) {
69             logger.debug("RequestWithMixedLocation: sending to universal " +
70                 counter);
71         }
72         try {
73             destinationBody.receiveRequest(this);
74         } catch (Exception JavaDoc e) {
75             this.backupSolution(destinationBody);
76         }
77     }
78
79     /**
80  * Implements the backup solution
81  */

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