KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > futurelist > FutureReceiver


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.examples.futurelist;
32
33 import org.apache.log4j.Logger;
34 import org.objectweb.proactive.ext.util.FutureList;
35
36
37 public class FutureReceiver implements java.io.Serializable JavaDoc {
38     
39     static Logger logger = Logger.getLogger(FutureReceiver.class.getName());
40
41   int etape = 0; // this is to count the jumps we have made so far
42
BlockedObject blocked;
43   java.util.Vector JavaDoc waitingFutures = new java.util.Vector JavaDoc();
44   FutureList futureList;
45
46
47   public FutureReceiver() {
48   }
49
50
51   public void getFuture() {
52     //System.out.println("FutureReceiver: Now processing getFuture()");
53
//we create a future objet through this call since the callee is blocked
54
waitingFutures.add(blocked.createFuture());
55   }
56
57
58   public void getFutureAndAddToFutureList() {
59     //System.out.println("FutureReceiver: Now processing getFutureAndAddToFutureList()");
60
EmptyFuture f = blocked.createFuture();
61     waitingFutures.add(f);
62     this.addToFutureList(f);
63   }
64
65
66   public void displayAllFutures() {
67     EmptyFuture temp;
68
69     for (java.util.Enumeration JavaDoc e = waitingFutures.elements(); e.hasMoreElements();) {
70       temp = (EmptyFuture)e.nextElement();
71       logger.info("Result: " + temp.getName());
72     }
73   }
74
75   //call a method on the blockedObject to unblock it
76
public void unblockOtherObject() {
77     blocked.go();
78   }
79
80
81   public void setBlockedObject(BlockedObject t) {
82     blocked = t;
83   }
84
85
86   /**
87    * Request its body futurePool and create an empty future list
88    */

89   public void createFutureList() {
90     org.objectweb.proactive.core.body.ActiveBody b = (org.objectweb.proactive.core.body.ActiveBody) org.objectweb.proactive.ProActive.getBodyOnThis();
91     this.futureList = new FutureList();
92   }
93
94
95   public void addToFutureList(Object JavaDoc o) {
96     this.futureList.add(o);
97   }
98
99
100   public void displayAwaited() {
101     if (futureList != null) {
102       logger.info("FutureReceiver: I am still waiting " + futureList.countAwaited() + " futures");
103     }
104   }
105
106
107   public void displayAllAwaited() {
108     if (futureList != null) {
109       logger.info("FutureReceiver: I am waiting for all my futures: " + futureList.allAwaited());
110     }
111   }
112
113
114   public void displayNoneAwaited() {
115     if (futureList != null) {
116      logger.info("FutureReceiver: I don't have any pending future: " + futureList.noneAwaited());
117     }
118   }
119
120
121   public void waitAllFuture() {
122     if (futureList != null) {
123       logger.info("FutureReceiver: waiting all futures ");
124       futureList.waitAll();
125     }
126   }
127
128
129   public void waitOneFuture() {
130     if (futureList != null) {
131       logger.info("FutureReceiver: waiting one future ");
132       futureList.waitOne();
133     }
134   }
135
136
137   public void waitAndDisplayOneFuture() {
138     Object JavaDoc tmp;
139     if (futureList != null) {
140       tmp = futureList.waitAndRemoveOne();
141       logger.info("I got this future: " + tmp);
142     }
143   }
144
145
146   public void waitAndDisplayAllFuture() {
147     if (futureList != null) {
148       this.waitAllFuture();
149       this.displayAllFutures();
150       // System.out.println("I got this future: "+tmp);
151
}
152   }
153
154   //call a method on the other agent to unblock it and wait for its replies
155
public void unblockOtherObjectAndWaitAll() {
156     blocked.go();
157     this.waitAllFuture();
158   }
159
160
161   public void unblockOtherObjectAndWaitOne() {
162     blocked.go();
163     this.waitOneFuture();
164   }
165 }
166
Popular Tags