org/objectweb/proactive/Service.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive;
00032 
00033 import org.objectweb.fractal.api.NoSuchInterfaceException;
00034 import org.objectweb.fractal.api.control.LifeCycleController;
00035 import org.objectweb.fractal.util.Fractal;
00036 import org.objectweb.proactive.core.ProActiveRuntimeException;
00037 import org.objectweb.proactive.core.body.request.BlockingRequestQueue;
00038 import org.objectweb.proactive.core.body.request.Request;
00039 import org.objectweb.proactive.core.body.request.RequestFilter;
00040 import org.objectweb.proactive.core.body.request.RequestProcessor;
00041 import org.objectweb.proactive.core.component.body.ComponentBody;
00042 
00043 
00082 public class Service {
00083     //
00084     // -- PROTECTED MEMBERS -----------------------------------------------
00085     //
00086     protected Body body;
00087     protected BlockingRequestQueue requestQueue;
00088 
00089     protected LifeCycleController lifeCycleController = null;
00090 
00091     //protected RequestFilterOnMethodName requestFilterOnMethodName = null;
00092     //
00093     // -- CONSTRUCTORS -----------------------------------------------
00094     //
00095 
00100     public Service(Body body) {
00101         this.body = body;
00102         this.requestQueue = body.getRequestQueue();
00103         if (((ComponentBody) body).isComponent()) {
00104             try {
00105                 lifeCycleController = Fractal.getLifeCycleController(((ComponentBody) body).getProActiveComponentImpl());
00106             } catch (NoSuchInterfaceException e) {
00107                 throw new ProActiveRuntimeException("could not find the life cycle controller for this component");
00108             }
00109         }
00110     }
00111 
00112     //
00113     // -- PUBLIC METHODS -----------------------------------------------
00114     //
00115     public String toString() {
00116         return "Service\n  Body=" + body.toString() + "\n  RequestQueue=" +
00117         requestQueue.toString();
00118     }
00119 
00124     public void serve(Request request) {
00125         body.serve(request);
00126     }
00127 
00133     public void fifoServing() {
00134         if (((ComponentBody) body).isComponent()) {
00135             while (LifeCycleController.STARTED.equals(
00136                         lifeCycleController.getFcState())) {
00137                 blockingServeOldest();
00138             }
00139         } else {
00140             while (body.isActive()) {
00141                 blockingServeOldest();
00142             }
00143         }
00144     }
00145 
00151     public void lifoServing() {
00152         while (body.isActive()) {
00153             blockingServeYoungest();
00154         }
00155     }
00156 
00157     // -- Serve Oldest ---------------------------------------------------
00158 
00164     public void blockingServeOldest() {
00165         blockingServeOldest(null, 0);
00166     }
00167 
00175     public void blockingServeOldest(long timeout) {
00176         blockingServeOldest(null, timeout);
00177     }
00178 
00187     public void blockingServeOldest(RequestFilter requestFilter, long timeout) {
00188         body.serve(requestQueue.blockingRemoveOldest(requestFilter, timeout));
00189     }
00190 
00197     public void blockingServeOldest(String methodName) {
00198         blockingServeOldest(new RequestFilterOnMethodName(methodName));
00199     }
00200 
00207     public void blockingServeOldest(RequestFilter requestFilter) {
00208         blockingServeOldest(requestFilter, 0);
00209     }
00210 
00215     public void serveOldest() {
00216         body.serve(requestQueue.removeOldest());
00217     }
00218 
00224     public void serveOldest(String methodName) {
00225         body.serve(requestQueue.removeOldest(methodName));
00226     }
00227 
00233     public void serveOldest(RequestFilter requestFilter) {
00234         body.serve(requestQueue.removeOldest(requestFilter));
00235     }
00236 
00237     // -- Serve Youngest ---------------------------------------------------
00238 
00244     public void blockingServeYoungest() {
00245         blockingServeYoungest(null, 0);
00246     }
00247 
00255     public void blockingServeYoungest(long timeout) {
00256         blockingServeYoungest(null, timeout);
00257     }
00258 
00265     public void blockingServeYoungest(String methodName) {
00266         blockingServeYoungest(new RequestFilterOnMethodName(methodName));
00267     }
00268 
00275     public void blockingServeYoungest(RequestFilter requestFilter) {
00276         blockingServeYoungest(requestFilter, 0);
00277     }
00278 
00287     public void blockingServeYoungest(RequestFilter requestFilter, long timeout) {
00288         body.serve(requestQueue.blockingRemoveYoungest(requestFilter, timeout));
00289     }
00290 
00295     public void serveYoungest() {
00296         body.serve(requestQueue.removeYoungest());
00297     }
00298 
00304     public void serveYoungest(String methodName) {
00305         body.serve(requestQueue.removeYoungest(methodName));
00306     }
00307 
00313     public void serveYoungest(RequestFilter requestFilter) {
00314         body.serve(requestQueue.removeYoungest(requestFilter));
00315     }
00316 
00317     // -- Serve All ---------------------------------------------------
00318 
00326     public void serveAll(String methodName) {
00327         serveAll(new RequestFilterOnMethodName(methodName));
00328     }
00329 
00335     public void serveAll(RequestFilter requestFilter) {
00336         requestQueue.processRequests(new ServingRequestProcessor(requestFilter),
00337             body);
00338     }
00339 
00340     // -- Serve And Flush Youngest ---------------------------------------------------
00341 
00349     public void flushingServeYoungest() {
00350         flushingServeYoungest(new AcceptAllRequestFilter());
00351     }
00352 
00362     public void flushingServeYoungest(String methodName) {
00363         flushingServeYoungest(new RequestFilterOnMethodName(methodName));
00364     }
00365 
00374     public void flushingServeYoungest(RequestFilter requestFilter) {
00375         requestQueue.processRequests(new FlushingServeYoungestRequestProcessor(
00376                 requestFilter), body);
00377     }
00378 
00379     // -- Serve And Flush Oldest ---------------------------------------------------
00380 
00388     public void flushingServeOldest() {
00389         flushingServeOldest(new AcceptAllRequestFilter());
00390     }
00391 
00401     public void flushingServeOldest(String methodName) {
00402         flushingServeOldest(new RequestFilterOnMethodName(methodName));
00403     }
00404 
00413     public void flushingServeOldest(RequestFilter requestFilter) {
00414         requestQueue.processRequests(new FlushingServeOldestRequestProcessor(
00415                 requestFilter), body);
00416     }
00417 
00418     // -- Other helpers methods ---------------------------------------------------
00419 
00423     public void waitForRequest() {
00424         requestQueue.waitForRequest();
00425     }
00426 
00431     public boolean hasRequestToServe() {
00432         return !requestQueue.isEmpty();
00433     }
00434 
00439     public boolean hasRequestToServe(String methodName) {
00440         return requestQueue.hasRequest(methodName);
00441     }
00442 
00447     public int getRequestCount() {
00448         return requestQueue.size();
00449     }
00450 
00454     public void flushAll() {
00455         requestQueue.clear();
00456     }
00457 
00458     //
00459     // -- getOldests ---------------------------------------------------
00460     //
00461 
00467     public Request getOldest() {
00468         return requestQueue.getOldest();
00469     }
00470 
00477     public Request getOldest(String methodName) {
00478         return requestQueue.getOldest(methodName);
00479     }
00480 
00487     public Request getOldest(RequestFilter requestFilter) {
00488         return requestQueue.getOldest(requestFilter);
00489     }
00490 
00497     public Request blockingGetOldest() {
00498         Request request = null;
00499         while ((request == null) && !requestQueue.isDestroyed()) {
00500             waitForRequest();
00501             request = requestQueue.getOldest();
00502         }
00503         return request;
00504     }
00505 
00506     //
00507     // -- getYoungests ---------------------------------------------------
00508     //
00509 
00515     public Request getYoungest() {
00516         return requestQueue.getYoungest();
00517     }
00518 
00525     public Request getYoungest(String methodName) {
00526         return requestQueue.getYoungest(methodName);
00527     }
00528 
00535     public Request getYoungest(RequestFilter requestFilter) {
00536         return requestQueue.getYoungest(requestFilter);
00537     }
00538 
00545     public Request blockingGetYoungest() {
00546         Request request = null;
00547         while ((request == null) && !requestQueue.isDestroyed()) {
00548             waitForRequest();
00549             request = requestQueue.getYoungest();
00550         }
00551         return request;
00552     }
00553 
00554     //
00555     // -- blockingRemoveOldests ---------------------------------------------------
00556     //
00557 
00566     public Request blockingRemoveOldest(RequestFilter requestFilter) {
00567         return blockingRemoveOldest(requestFilter, 0);
00568     }
00569 
00579     public Request blockingRemoveOldest(RequestFilter requestFilter,
00580         long timeout) {
00581         return requestQueue.blockingRemoveOldest(requestFilter, timeout);
00582     }
00583 
00591     public Request blockingRemoveOldest(String methodName) {
00592         return blockingRemoveOldest(new RequestFilterOnMethodName(methodName), 0);
00593     }
00594 
00601     public Request blockingRemoveOldest() {
00602         return blockingRemoveOldest(null, 0);
00603     }
00604 
00613     public Request blockingRemoveOldest(long timeout) {
00614         return blockingRemoveOldest(null, timeout);
00615     }
00616 
00617     //
00618     // -- blockingRemoveYoungests ---------------------------------------------------
00619     //
00620 
00629     public Request blockingRemoveYoungest(RequestFilter requestFilter) {
00630         return blockingRemoveYoungest(requestFilter, 0);
00631     }
00632 
00643     public Request blockingRemoveYoungest(RequestFilter requestFilter,
00644         long timeout) {
00645         return requestQueue.blockingRemoveYoungest(requestFilter, timeout);
00646     }
00647 
00655     public Request blockingRemoveYoungest(String methodName) {
00656         return blockingRemoveYoungest(new RequestFilterOnMethodName(methodName),
00657             0);
00658     }
00659 
00666     public Request blockingRemoveYoungest() {
00667         return blockingRemoveYoungest(null, 0);
00668     }
00669 
00678     public Request blockingRemoveYoungest(long timeout) {
00679         return blockingRemoveYoungest(null, timeout);
00680     }
00681 
00682     //
00683     // -- INNER CLASSES -----------------------------------------------
00684     //
00685 
00696     protected class ServingRequestProcessor implements RequestProcessor {
00697 
00699         private RequestFilter selectorRequestFilter;
00700 
00701         public ServingRequestProcessor(RequestFilter selectorRequestFilter) {
00702             this.selectorRequestFilter = selectorRequestFilter;
00703         }
00704 
00712         public int processRequest(Request request) {
00713             if (selectorRequestFilter.acceptRequest(request)) {
00714                 return REMOVE_AND_SERVE;
00715             } else {
00716                 return KEEP;
00717             }
00718         }
00719     } // end inner class ServingRequestProcessor
00720 
00732     protected class FlushingServeYoungestRequestProcessor
00733         implements RequestProcessor {
00734         private RequestFilter selectorRequestFilter;
00735         private Request requestToServe;
00736         private int counter;
00737         private int numberOfRequests;
00738 
00739         public FlushingServeYoungestRequestProcessor(
00740             RequestFilter selectorRequestFilter) {
00741             this.selectorRequestFilter = selectorRequestFilter;
00742         }
00743 
00751         public int processRequest(Request request) {
00752             if (counter == 0) {
00753                 // first call
00754                 numberOfRequests = requestQueue.size();
00755             }
00756             counter++;
00757             int shouldRemove;
00758             if (selectorRequestFilter.acceptRequest(request)) {
00759                 requestToServe = request;
00760                 shouldRemove = REMOVE;
00761             } else {
00762                 shouldRemove = KEEP;
00763             }
00764             if ((counter == numberOfRequests) && (requestToServe != null)) {
00765                 if (request == requestToServe) {
00766                     return REMOVE_AND_SERVE; // serve current request
00767                 } else {
00768                     body.serve(requestToServe); // serve an already removed request
00769                 }
00770             }
00771             return shouldRemove;
00772         }
00773     } // end inner class FlushingServeYoungestRequestProcessor
00774 
00786     protected class FlushingServeOldestRequestProcessor
00787         implements RequestProcessor {
00788         private RequestFilter selectorRequestFilter;
00789         private boolean hasServed;
00790 
00791         public FlushingServeOldestRequestProcessor(
00792             RequestFilter selectorRequestFilter) {
00793             this.selectorRequestFilter = selectorRequestFilter;
00794         }
00795 
00803         public int processRequest(Request request) {
00804             if (selectorRequestFilter.acceptRequest(request)) {
00805                 if (!hasServed) {
00806                     hasServed = true;
00807                     return REMOVE_AND_SERVE;
00808                 }
00809                 return REMOVE;
00810             } else {
00811                 return KEEP;
00812             }
00813         }
00814     } // end inner class FlushingServeYoungestRequestProcessor
00815 
00826     protected class RequestFilterOnMethodName implements RequestFilter,
00827         java.io.Serializable {
00828         private String methodName;
00829 
00830         public RequestFilterOnMethodName(String methodName) {
00831             this.methodName = methodName;
00832         }
00833 
00834         public boolean acceptRequest(Request request) {
00835             return methodName.equals(request.getMethodName());
00836         }
00837     }
00838 
00848     protected class AcceptAllRequestFilter implements RequestFilter {
00849 
00855         public boolean acceptRequest(Request request) {
00856             return true;
00857         }
00858     } // end inner class AcceptAllRequestFilter
00859 }

Generated on Mon Jan 22 15:16:12 2007 for ProActive by  doxygen 1.5.1