KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > request > RequestQueueImpl


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.core.body.request;
32
33 import org.objectweb.proactive.Body;
34 import org.objectweb.proactive.core.UniqueID;
35 import org.objectweb.proactive.core.body.LocalBodyStore;
36 import org.objectweb.proactive.core.event.*;
37 import org.objectweb.proactive.core.util.CircularArrayList;
38
39 public class RequestQueueImpl extends AbstractEventProducer implements java.io.Serializable JavaDoc, RequestQueue {
40
41     //
42
// -- PROTECTED MEMBERS -----------------------------------------------
43
//
44

45     protected CircularArrayList requestQueue;
46     protected UniqueID ownerID;
47     private RequestFilterOnMethodName requestFilterOnMethodName;
48
49     protected static final boolean SEND_ADD_REMOVE_EVENT = false;
50
51     //
52
// -- CONSTRUCTORS -----------------------------------------------
53
//
54

55     public RequestQueueImpl(UniqueID ownerID) {
56         this.requestQueue = new CircularArrayList(20);
57         this.ownerID = ownerID;
58         this.requestFilterOnMethodName = new RequestFilterOnMethodName();
59     }
60
61
62     //
63
// -- PUBLIC METHODS -----------------------------------------------
64
//
65

66     public java.util.Iterator JavaDoc iterator() {
67         return requestQueue.iterator();
68     }
69
70
71     public synchronized boolean isEmpty() {
72         return requestQueue.isEmpty();
73     }
74
75
76     public synchronized int size() {
77         return requestQueue.size();
78     }
79
80
81     public boolean hasRequest(String JavaDoc s) {
82         return getOldest(s) != null;
83     }
84
85
86     public synchronized void clear() {
87         requestQueue.clear();
88     }
89
90     public synchronized Request getOldest() {
91         if (requestQueue.isEmpty()) return null;
92         return (Request) requestQueue.get(0);
93     }
94
95
96     public synchronized Request getOldest(String JavaDoc methodName) {
97         requestFilterOnMethodName.setMethodName(methodName);
98         return findOldest(requestFilterOnMethodName, false);
99     }
100
101     public synchronized Request getOldest(RequestFilter requestFilter) {
102         return findOldest(requestFilter, false);
103     }
104
105     public synchronized Request removeOldest() {
106         if (requestQueue.isEmpty()) return null;
107         Request r = (Request) requestQueue.remove(0);
108         if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
109             notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
110         }
111         return r;
112     }
113
114     public synchronized Request removeOldest(String JavaDoc methodName) {
115         requestFilterOnMethodName.setMethodName(methodName);
116         return findOldest(requestFilterOnMethodName, true);
117     }
118
119     public synchronized Request removeOldest(RequestFilter requestFilter) {
120         return findOldest(requestFilter, true);
121     }
122
123     public synchronized Request getYoungest() {
124         if (requestQueue.isEmpty()) return null;
125         return (Request) requestQueue.get(requestQueue.size() - 1);
126     }
127
128     public synchronized Request getYoungest(String JavaDoc methodName) {
129         requestFilterOnMethodName.setMethodName(methodName);
130         return findYoungest(requestFilterOnMethodName, false);
131     }
132
133     public synchronized Request getYoungest(RequestFilter requestFilter) {
134         return findYoungest(requestFilter, false);
135     }
136
137     public synchronized Request removeYoungest() {
138         if (requestQueue.isEmpty()) return null;
139         Request r = (Request) requestQueue.remove(requestQueue.size() - 1);
140         if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
141             notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
142         }
143         return r;
144     }
145
146     public synchronized Request removeYoungest(String JavaDoc methodName) {
147         requestFilterOnMethodName.setMethodName(methodName);
148         return findYoungest(requestFilterOnMethodName, true);
149     }
150
151     public synchronized Request removeYoungest(RequestFilter requestFilter) {
152         return findYoungest(requestFilter, true);
153     }
154
155     public synchronized void add(Request request) {
156       //System.out.println(" --> RequestQueue.add m="+request.getMethodName());
157
requestQueue.add(request);
158       if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
159           notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
160       }
161     }
162
163     public synchronized void addToFront(Request request) {
164         requestQueue.add(0, request);
165         if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
166             notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
167         }
168     }
169
170     public synchronized void processRequests(RequestProcessor processor, Body body) {
171         for (int i = 0; i < requestQueue.size(); i++) {
172             Request r = (Request) requestQueue.get(i);
173             int result = processor.processRequest(r);
174             switch (result) {
175                 case RequestProcessor.REMOVE_AND_SERVE :
176                     requestQueue.remove(i);
177                     i --;
178                     if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
179                         notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
180                     }
181                     body.serve(r);
182                   break;
183                 case RequestProcessor.REMOVE :
184                     requestQueue.remove(i);
185                     i --;
186                     if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
187                         notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
188                     }
189                   break;
190                 case RequestProcessor.KEEP :
191                   break;
192             }
193         }
194     }
195
196     public synchronized String JavaDoc toString() {
197         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
198         sb.append("--- RequestQueueImpl n=").append(requestQueue.size()).append(" requests --- ->\n");
199         int count = 0;
200         java.util.Iterator JavaDoc iterator = requestQueue.iterator();
201         while (iterator.hasNext()) {
202             Request currentrequest = (Request) iterator.next();
203             sb.append(count).append("--> ").append(currentrequest.getMethodName()).append("\n");
204             count++;
205         }
206         sb.append("--- End RequestQueueImpl ---");
207         return sb.toString();
208     }
209
210
211     public void addRequestQueueEventListener(RequestQueueEventListener listener) {
212         addListener(listener);
213     }
214
215
216     public void removeRequestQueueEventListener(RequestQueueEventListener listener) {
217         removeListener(listener);
218     }
219
220
221     //
222
// -- PROTECTED METHODS -----------------------------------------------
223
//
224

225     protected void notifyOneListener(ProActiveListener listener, ProActiveEvent event) {
226         ((RequestQueueEventListener) listener).requestQueueModified((RequestQueueEvent) event);
227     }
228
229     //
230
// -- PRIVATE METHODS -----------------------------------------------
231
//
232

233     /**
234      * Return the oldest fullfilling the criteria defined by the
235      * given filter or null if no match
236      * The request is removed only if shouldRemove is set to true
237      * @param methodName the name of the method to look for
238      * @param shouldRemove whether to remove the request found or not
239      * @return the oldest matching request or null
240      */

241     private Request findOldest(RequestFilter requestFilter, boolean shouldRemove) {
242         java.util.Iterator JavaDoc iterator = requestQueue.iterator();
243         while (iterator.hasNext()) {
244             Request r = (Request) iterator.next();
245             if (requestFilter.acceptRequest(r)) {
246                 if (shouldRemove) {
247                     iterator.remove();
248                     if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
249                         notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
250                     }
251                 }
252                 return r;
253             }
254         }
255         return null;
256     }
257
258     /**
259      * Return the youngest request fullfilling the criteria defined by the
260      * given filter or null if no match
261      * The request is removed only if shouldRemove is set to true
262      * @param methodName the name of the method to look for
263      * @param shouldRemove whether to remove the request found or not
264      * @return the youngest matching request or null
265      */

266     private Request findYoungest(RequestFilter requestFilter, boolean shouldRemove) {
267         java.util.ListIterator JavaDoc iterator = requestQueue.listIterator(requestQueue.size());
268         while (iterator.hasPrevious()) {
269             Request r = (Request) iterator.previous();
270             if (requestFilter.acceptRequest(r)) {
271                 if (shouldRemove) {
272                     iterator.remove();
273                     if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
274                         notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
275                     }
276                 }
277                 return r;
278             }
279         }
280         return null;
281     }
282
283
284     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws java.io.IOException JavaDoc {
285         // we must set migration tag because requests could contain awaited future (parameters)
286
org.objectweb.proactive.Body owner = LocalBodyStore.getInstance().getLocalBody(ownerID);
287         owner.getFuturePool().setMigrationTag();
288         out.defaultWriteObject();
289     }
290
291
292     //
293
// -- INNER CLASSES -----------------------------------------------
294
//
295

296     private class RequestFilterOnMethodName implements RequestFilter, java.io.Serializable JavaDoc {
297
298         private String JavaDoc methodName;
299
300         public RequestFilterOnMethodName() {
301         }
302
303         public boolean acceptRequest(Request request) {
304             return methodName.equals(request.getMethodName());
305         }
306
307         public void setMethodName(String JavaDoc methodName) {
308             this.methodName = methodName;
309         }
310     }
311 }
312
Popular Tags