KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.proactive.ext.mixedlocation;
2
3
4 import org.objectweb.proactive.ext.locationserver.TimedRequestWithLocationServer;
5
6 import org.objectweb.proactive.core.body.UniversalBody;
7 import org.objectweb.proactive.core.body.future.FutureProxy;
8 import org.objectweb.proactive.core.mop.MethodCall;
9 import org.objectweb.proactive.core.mop.StubObject;
10 import org.objectweb.proactive.ext.locationserver.LocationServer;
11
12
13 public class TimedRequestWithMixedLocation
14     extends TimedRequestWithLocationServer {
15
16     private static final int MAX_TRIES = 30;
17     private static int counter = 0;
18     private int tries;
19     transient protected LocationServer server;
20     protected long startTime;
21
22     public TimedRequestWithMixedLocation(MethodCall methodCall,
23                                          UniversalBody sender, boolean isOneWay,
24                                          long nextSequenceID,
25                                          LocationServer server) {
26         super(methodCall, sender, isOneWay, nextSequenceID, server);
27     }
28
29     protected void sendRequest(UniversalBody destinationBody)
30                         throws java.io.IOException JavaDoc {
31         System.out.println(
32                 "RequestWithMixedLocation: sending to universal " + counter);
33         try {
34             destinationBody.receiveRequest(this);
35         } catch (Exception JavaDoc e) {
36             // e.printStackTrace();
37
this.backupSolution(destinationBody);
38         }
39     }
40
41     /**
42    * Implements the backup solution
43    */

44     protected void backupSolution(UniversalBody destinationBody)
45                            throws java.io.IOException JavaDoc {
46
47         boolean ok = false;
48         tries = 0;
49         System.out.println(
50                 "RequestWithMixedLocationr: backupSolution() contacting server " +
51                 server);
52         System.out.println(
53                 "RequestWithMixedLocation.backupSolution() : looking for " +
54                 destinationBody);
55         //get the new location from the server
56
while (!ok && (tries < MAX_TRIES)) {
57
58             UniversalBody mobile = (UniversalBody)server.searchObject(destinationBody.getID());
59             System.out.println(
60                     "RequestWithMixedLocation: backupSolution() server has sent an answer");
61
62             //we want to bypass the stub/proxy
63
UniversalBody newDestinationBody = (UniversalBody)((FutureProxy)((StubObject)mobile).getProxy()).getResult();
64             // !!!!
65
// !!!! should put a counter here to stop calling if continuously failing
66
// !!!!
67
try {
68                 // sendRequest(newDestinationBody);
69
newDestinationBody.receiveRequest(this);
70                 //everything went fine, we have to update the current location of the object
71
//so that next requests don't go through the server
72
System.out.println(
73                         "RequestWithMixedLocation: backupSolution() updating location");
74                 if (sender != null) {
75                     sender.updateLocation(newDestinationBody.getID(),
76                                           newDestinationBody.getRemoteAdapter());
77                 }
78                 ok = true;
79             } catch (Exception JavaDoc e) {
80                 System.out.println(
81                         "RequestWithMixedLocation: backupSolution() failed");
82                 tries++;
83                 try {
84                     Thread.sleep(500);
85                 } catch (Exception JavaDoc e2) {
86                     e2.printStackTrace();
87                 }
88             }
89         }
90     }
91 }
Popular Tags