KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.proactive.ext.locationserver;
2
3 import org.objectweb.proactive.Body;
4 import org.objectweb.proactive.ProActive;
5 import org.objectweb.proactive.core.UniqueID;
6 import org.objectweb.proactive.core.body.LocalBodyStore;
7 import org.objectweb.proactive.core.body.UniversalBody;
8 import org.objectweb.proactive.core.body.future.FutureProxy;
9 import org.objectweb.proactive.core.body.reply.Reply;
10 import org.objectweb.proactive.core.body.request.RequestImpl;
11 import org.objectweb.proactive.core.body.request.ServeException;
12 import org.objectweb.proactive.core.mop.MethodCall;
13 import org.objectweb.proactive.core.mop.StubObject;
14 import org.objectweb.proactive.ext.locationserver.LocationServer;
15 import org.objectweb.proactive.ext.locationserver.LocationServerFactory;
16
17 import org.objectweb.proactive.ext.locationserver.util.MicroTimer;
18
19
20 public class TimedRequestWithLocationServer extends RequestImpl
21     implements java.io.Serializable JavaDoc {
22
23     private static final int MAX_TRIES = 30;
24     /**
25      * the number of time we try before reporting a failure
26      */

27
28     // private long startTimeServer;
29
//private long startTimeAgent;
30
protected long startTime;
31     private int tries;
32     private transient LocationServer server;
33
34     public TimedRequestWithLocationServer(MethodCall methodCall,
35                                           UniversalBody sender,
36                                           boolean isOneWay, long nextSequenceID,
37                                           LocationServer server) {
38         super(methodCall, sender, isOneWay, nextSequenceID);
39         this.server = server;
40     }
41
42     public Reply serve(Body targetBody)
43                 throws ServeException {
44
45         MicroTimer timer = new MicroTimer();
46         timer.start();
47
48         Reply r = super.serve(targetBody);
49         timer.stop();
50         System.out.println(
51                 "TimedRequestWithLocationServer: " +
52                 timer.getCumulatedTime() + " for method " + methodName);
53         return r;
54     }
55
56     protected void sendRequest(UniversalBody destinationBody)
57                         throws java.io.IOException JavaDoc {
58         System.out.println(
59                 "TimedRequestWithLocationServer: sending to remote " +
60                 methodName);
61         try {
62             startTime = System.currentTimeMillis();
63             destinationBody.receiveRequest(this);
64
65             long endTime = System.currentTimeMillis();
66             System.out.println(
67                     "TimedRequestWithLocationServer: .............. 1/gamma = " +
68                     (endTime - startTime) + " for method " + methodName);
69             System.out.println(
70                     "TimedRequestWithLocationServer: .............. done = " +
71                     (endTime - startTime) + " for method " + methodName);
72         } catch (Exception JavaDoc e) {
73             // endTime = System.currentTimeMillis();
74
//There can only be a problem when trying to contact the Agent
75
System.out.println(
76                     "TimedRequestWithLocationServer: .............. FAILED = " +
77                     (System.currentTimeMillis() - startTime) +
78                     " for method " + methodName);
79             //e.printStackTrace();
80
System.out.println(">>>>>>>>>>>> Exception " + e);
81             this.backupSolution(destinationBody);
82         }
83     }
84
85     /**
86      * Implements the backup solution
87      */

88     protected void backupSolution(UniversalBody destinationBody)
89                            throws java.io.IOException JavaDoc {
90
91         // long startTimeGamma1=0;
92
// long endTimeGamma1=0;
93
boolean ok = false;
94         tries = 0;
95
96         // System.out.println("TimedRequestWithLocationServer: backupSolution() contacting server at time " + System.currentTimeMillis());
97
//get the new location from the server
98
UniqueID bodyID = destinationBody.getID();
99         while (!ok && (tries < MAX_TRIES)) {
100
101             UniversalBody remoteBody = null;
102             System.out.println(
103                     " ==== Query server ==== time " +
104                     System.currentTimeMillis());
105
106             UniversalBody mobile = queryServer(bodyID);
107             System.out.println(
108                     "=========================== time " +
109                     System.currentTimeMillis());
110             //we want to bypass the stub/proxy
111
remoteBody = (UniversalBody)((FutureProxy)((StubObject)mobile).getProxy()).getResult();
112
113             long startTimeGamma = System.currentTimeMillis();
114             try {
115                 remoteBody.receiveRequest(this);
116
117                 long endTime = System.currentTimeMillis();
118                 System.out.println(
119                         "TimedRequestWithLocationServer: .............. 1/gamma = " +
120                         (endTime - startTimeGamma) + " for method " +
121                         methodName);
122                 System.out.println(
123                         "TimedRequestWithLocationServer: .............. done = " +
124                         (endTime - startTime) + " for method " + methodName);
125                 //everything went fine, we have to update the current location of the object
126
//so that next requests don't go through the server
127
if (sender != null)
128                     sender.updateLocation(bodyID, remoteBody);
129                 else {
130                     LocalBodyStore.getInstance().getLocalBody(getSourceBodyID())
131                                   .updateLocation(bodyID, remoteBody);
132                 }
133                 ok = true;
134             } catch (Exception JavaDoc e) {
135                 System.out.println(
136                         "TimedRequestWithLocationServer: .............. FAILED = " +
137                         (System.currentTimeMillis() - startTimeGamma) +
138                         " for method " + methodName);
139                 tries++;
140             }
141         }
142     }
143
144     protected UniversalBody queryServer(UniqueID bodyID) {
145
146         long startTimeBackupSolution = System.currentTimeMillis();
147         if (server == null) {
148             server = LocationServerFactory.getLocationServer();
149         }
150
151         UniversalBody mobile = (UniversalBody)server.searchObject(bodyID);
152         long endTimeBackupSolution = System.currentTimeMillis();
153         System.out.println(
154                 "TimedRequestWithLocationServer: backupSolution() server has sent an answer after " +
155                 (endTimeBackupSolution - startTimeBackupSolution));
156         ProActive.waitFor(mobile);
157         return mobile;
158     }
159 }
Popular Tags