1 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 , RequestQueue { 40 41 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 55 public RequestQueueImpl(UniqueID ownerID) { 56 this.requestQueue = new CircularArrayList(20); 57 this.ownerID = ownerID; 58 this.requestFilterOnMethodName = new RequestFilterOnMethodName(); 59 } 60 61 62 66 public java.util.Iterator 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 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 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 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 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 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 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 toString() { 197 StringBuffer sb = new StringBuffer (); 198 sb.append("--- RequestQueueImpl n=").append(requestQueue.size()).append(" requests --- ->\n"); 199 int count = 0; 200 java.util.Iterator 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 225 protected void notifyOneListener(ProActiveListener listener, ProActiveEvent event) { 226 ((RequestQueueEventListener) listener).requestQueueModified((RequestQueueEvent) event); 227 } 228 229 233 241 private Request findOldest(RequestFilter requestFilter, boolean shouldRemove) { 242 java.util.Iterator 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 266 private Request findYoungest(RequestFilter requestFilter, boolean shouldRemove) { 267 java.util.ListIterator 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 out) throws java.io.IOException { 285 org.objectweb.proactive.Body owner = LocalBodyStore.getInstance().getLocalBody(ownerID); 287 owner.getFuturePool().setMigrationTag(); 288 out.defaultWriteObject(); 289 } 290 291 292 296 private class RequestFilterOnMethodName implements RequestFilter, java.io.Serializable { 297 298 private String 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 methodName) { 308 this.methodName = methodName; 309 } 310 } 311 } 312 | Popular Tags |