<commit_before>/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
 * Contact: <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 */
package org.objectweb.proactive.core.body.request;

import org.objectweb.proactive.Body;
import org.objectweb.proactive.core.UniqueID;
import org.objectweb.proactive.core.body.LocalBodyStore;
import org.objectweb.proactive.core.body.ft.protocols.FTManager;
import org.objectweb.proactive.core.event.AbstractEventProducer;
import org.objectweb.proactive.core.event.ProActiveEvent;
import org.objectweb.proactive.core.event.ProActiveListener;
import org.objectweb.proactive.core.event.RequestQueueEvent;
import org.objectweb.proactive.core.event.RequestQueueEventListener;
import org.objectweb.proactive.core.util.CircularArrayList;


public class RequestQueueImpl extends AbstractEventProducer implements java.io.Serializable, RequestQueue {
    //
    // -- PROTECTED MEMBERS -----------------------------------------------
    //
    protected CircularArrayList requestQueue;
    protected UniqueID ownerID;
    protected RequestFilterOnMethodName requestFilterOnMethodName;
    protected static final boolean SEND_ADD_REMOVE_EVENT = false;
    protected NonFunctionalRequestsProcessor nfRequestsProcessor;

    //
    // -- CONSTRUCTORS -----------------------------------------------
    //
    public RequestQueueImpl(UniqueID ownerID) {
        this.requestQueue = new CircularArrayList(20);
        this.ownerID = ownerID;
        this.requestFilterOnMethodName = new RequestFilterOnMethodName();
        this.nfRequestsProcessor = new NonFunctionalRequestsProcessor();
    }

    //
    // -- PUBLIC METHODS -----------------------------------------------
    //
    public java.util.Iterator iterator() {
        return requestQueue.iterator();
    }

    public CircularArrayList getInternalQueue() {
        return this.requestQueue;
    }

    public synchronized boolean isEmpty() {
        return requestQueue.isEmpty();
    }

    public synchronized int size() {
        return requestQueue.size();
    }

    public boolean hasRequest(String s) {
        return getOldest(s) != null;
    }

    public synchronized void clear() {
        requestQueue.clear();
    }

    public synchronized Request getOldest() {
        if (requestQueue.isEmpty()) {
            return null;
            //serves the non functional requests first.
        } else if (!nfRequestsProcessor.isEmpty()) {
            return nfRequestsProcessor.getOldestPriorityNFRequest(false);
        }
        return (Request) requestQueue.get(0);
    }

    public synchronized Request getOldest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findOldest(requestFilterOnMethodName, false);
    }

    public synchronized Request getOldest(RequestFilter requestFilter) {
        return findOldest(requestFilter, false);
    }

    public synchronized Request removeOldest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            Request r = nfRequestsProcessor.getOldestPriorityNFRequest(true);
            requestQueue.remove(r);
            return r;
        }

        Request r = (Request) requestQueue.remove(0);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
        }

        // END ProActiveEvent
        return r;
    }

    public synchronized Request removeOldest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findOldest(requestFilterOnMethodName, true);
    }

    public synchronized Request removeOldest(RequestFilter requestFilter) {
        return findOldest(requestFilter, true);
    }

    public synchronized Request getYoungest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            return nfRequestsProcessor.getYoungestPriorityNFRequest(false);
        }
        return (Request) requestQueue.get(requestQueue.size() - 1);
    }

    public synchronized Request getYoungest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findYoungest(requestFilterOnMethodName, false);
    }

    public synchronized Request getYoungest(RequestFilter requestFilter) {
        return findYoungest(requestFilter, false);
    }

    public synchronized Request removeYoungest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            Request r = nfRequestsProcessor.getYoungestPriorityNFRequest(true);
            requestQueue.remove(r);
            return r;
        }
        Request r = (Request) requestQueue.remove(requestQueue.size() - 1);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
        }

        // END ProActiveEvent
        return r;
    }

    public synchronized Request removeYoungest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findYoungest(requestFilterOnMethodName, true);
    }

    public synchronized Request removeYoungest(RequestFilter requestFilter) {
        return findYoungest(requestFilter, true);
    }

    public synchronized int add(Request request) {
        //System.out.println("  --> RequestQueue.add m="+request.getMethodName());
        // FAULT-TOLERANCE  
        int ftres = FTManager.NON_FT;
        FTManager ftm = request.getFTManager();
        if (ftm != null) {
            // null if FT is disable OR if request is an awaited request         
            ftres = ftm.onDeliverRequest(request);
            if (request.ignoreIt()) {
                return ftres;
            }
        }

        //if the request is non functional and priority, a reference on it is added in a nonFunctionalRequestsQueue.
        int priority = request.getNFRequestPriority();
        if ((priority == Request.NFREQUEST_IMMEDIATE_PRIORITY) || (priority == Request.NFREQUEST_PRIORITY)) {
            nfRequestsProcessor.addToNFRequestsQueue(request);
        }

        requestQueue.add(request);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
        }

        // END ProActiveEvent
        return ftres;
    }

    public synchronized int addToFront(Request request) {
        int ftres = 0;

        //if the request is non functional and priority, a reference on it is added in a nonFunctionalRequestsQueue.
        int priority = request.getNFRequestPriority();
        if ((priority == Request.NFREQUEST_IMMEDIATE_PRIORITY) || (priority == Request.NFREQUEST_PRIORITY)) {
            nfRequestsProcessor.addToNFRequestsQueue(request);
        }

        requestQueue.add(0, request);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
        }

        // END ProActiveEvent
        return ftres;
    }

    public void processRequests(RequestProcessor processor, Body body) {
        Request reqToServe = null;
        synchronized (this) {

            for (int i = 0; i < requestQueue.size(); i++) {
                Request r;

                // First, we deal with priotity non functional requests
                while (!nfRequestsProcessor.isEmpty()) {
                    r = nfRequestsProcessor.getOldestPriorityNFRequest(true);
                    LocalBodyStore.getInstance().getLocalBody(ownerID).serve(r);
                    requestQueue.remove(r);
                }
                if (requestQueue.isEmpty()) {
                    return;
                }

                r = (Request) requestQueue.get(i);
                int result = processor.processRequest(r);
                switch (result) {
                    case RequestProcessor.REMOVE_AND_SERVE:
                        requestQueue.remove(i);
                        i--;

                        // ProActiveEvent
                        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
                            notifyAllListeners(new RequestQueueEvent(ownerID,
                                RequestQueueEvent.REMOVE_REQUEST));
                        }
                        // END ProActiveEvent
                        // We store the request to be served
                        reqToServe = r;

                        break;
                    case RequestProcessor.REMOVE:
                        requestQueue.remove(i);
                        i--;
                        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
                            notifyAllListeners(new RequestQueueEvent(ownerID,
                                RequestQueueEvent.REMOVE_REQUEST));
                        }
                        break;
                    case RequestProcessor.KEEP:
                        break;
                }
            }
        }
        // If there is a request to serve, we serve it (but outside the synchronized block)
        if (reqToServe != null)
            body.serve(reqToServe);
    }

    @Override
    public synchronized String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("--- RequestQueueImpl n=").append(requestQueue.size()).append("   requests --- ->\n");
        int count = 0;
        java.util.Iterator iterator = requestQueue.iterator();
        while (iterator.hasNext()) {
            Request currentrequest = (Request) iterator.next();
            sb.append(count).append("--> ").append(currentrequest.getMethodName()).append("\n");
            count++;
        }
        sb.append("--- End RequestQueueImpl ---");
        sb.append("\n" + nfRequestsProcessor.toString());<commit_msg>there can be more than one request to be served !<commit_after>/*
 * ################################################################
 *
 * ProActive: The Java(TM) library for Parallel, Distributed,
 *            Concurrent computing with Security and Mobility
 *
 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
 * Contact: <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 */
package org.objectweb.proactive.core.body.request;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.proactive.Body;
import org.objectweb.proactive.core.UniqueID;
import org.objectweb.proactive.core.body.LocalBodyStore;
import org.objectweb.proactive.core.body.ft.protocols.FTManager;
import org.objectweb.proactive.core.event.AbstractEventProducer;
import org.objectweb.proactive.core.event.ProActiveEvent;
import org.objectweb.proactive.core.event.ProActiveListener;
import org.objectweb.proactive.core.event.RequestQueueEvent;
import org.objectweb.proactive.core.event.RequestQueueEventListener;
import org.objectweb.proactive.core.util.CircularArrayList;


public class RequestQueueImpl extends AbstractEventProducer implements java.io.Serializable, RequestQueue {
    //
    // -- PROTECTED MEMBERS -----------------------------------------------
    //
    protected CircularArrayList requestQueue;
    protected UniqueID ownerID;
    protected RequestFilterOnMethodName requestFilterOnMethodName;
    protected static final boolean SEND_ADD_REMOVE_EVENT = false;
    protected NonFunctionalRequestsProcessor nfRequestsProcessor;

    //
    // -- CONSTRUCTORS -----------------------------------------------
    //
    public RequestQueueImpl(UniqueID ownerID) {
        this.requestQueue = new CircularArrayList(20);
        this.ownerID = ownerID;
        this.requestFilterOnMethodName = new RequestFilterOnMethodName();
        this.nfRequestsProcessor = new NonFunctionalRequestsProcessor();
    }

    //
    // -- PUBLIC METHODS -----------------------------------------------
    //
    public java.util.Iterator iterator() {
        return requestQueue.iterator();
    }

    public CircularArrayList getInternalQueue() {
        return this.requestQueue;
    }

    public synchronized boolean isEmpty() {
        return requestQueue.isEmpty();
    }

    public synchronized int size() {
        return requestQueue.size();
    }

    public boolean hasRequest(String s) {
        return getOldest(s) != null;
    }

    public synchronized void clear() {
        requestQueue.clear();
    }

    public synchronized Request getOldest() {
        if (requestQueue.isEmpty()) {
            return null;
            //serves the non functional requests first.
        } else if (!nfRequestsProcessor.isEmpty()) {
            return nfRequestsProcessor.getOldestPriorityNFRequest(false);
        }
        return (Request) requestQueue.get(0);
    }

    public synchronized Request getOldest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findOldest(requestFilterOnMethodName, false);
    }

    public synchronized Request getOldest(RequestFilter requestFilter) {
        return findOldest(requestFilter, false);
    }

    public synchronized Request removeOldest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            Request r = nfRequestsProcessor.getOldestPriorityNFRequest(true);
            requestQueue.remove(r);
            return r;
        }

        Request r = (Request) requestQueue.remove(0);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
        }

        // END ProActiveEvent
        return r;
    }

    public synchronized Request removeOldest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findOldest(requestFilterOnMethodName, true);
    }

    public synchronized Request removeOldest(RequestFilter requestFilter) {
        return findOldest(requestFilter, true);
    }

    public synchronized Request getYoungest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            return nfRequestsProcessor.getYoungestPriorityNFRequest(false);
        }
        return (Request) requestQueue.get(requestQueue.size() - 1);
    }

    public synchronized Request getYoungest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findYoungest(requestFilterOnMethodName, false);
    }

    public synchronized Request getYoungest(RequestFilter requestFilter) {
        return findYoungest(requestFilter, false);
    }

    public synchronized Request removeYoungest() {
        if (requestQueue.isEmpty()) {
            return null;
        } else if (!nfRequestsProcessor.isEmpty()) {
            Request r = nfRequestsProcessor.getYoungestPriorityNFRequest(true);
            requestQueue.remove(r);
            return r;
        }
        Request r = (Request) requestQueue.remove(requestQueue.size() - 1);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.REMOVE_REQUEST));
        }

        // END ProActiveEvent
        return r;
    }

    public synchronized Request removeYoungest(String methodName) {
        requestFilterOnMethodName.setMethodName(methodName);
        return findYoungest(requestFilterOnMethodName, true);
    }

    public synchronized Request removeYoungest(RequestFilter requestFilter) {
        return findYoungest(requestFilter, true);
    }

    public synchronized int add(Request request) {
        //System.out.println("  --> RequestQueue.add m="+request.getMethodName());
        // FAULT-TOLERANCE  
        int ftres = FTManager.NON_FT;
        FTManager ftm = request.getFTManager();
        if (ftm != null) {
            // null if FT is disable OR if request is an awaited request         
            ftres = ftm.onDeliverRequest(request);
            if (request.ignoreIt()) {
                return ftres;
            }
        }

        //if the request is non functional and priority, a reference on it is added in a nonFunctionalRequestsQueue.
        int priority = request.getNFRequestPriority();
        if ((priority == Request.NFREQUEST_IMMEDIATE_PRIORITY) || (priority == Request.NFREQUEST_PRIORITY)) {
            nfRequestsProcessor.addToNFRequestsQueue(request);
        }

        requestQueue.add(request);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
        }

        // END ProActiveEvent
        return ftres;
    }

    public synchronized int addToFront(Request request) {
        int ftres = 0;

        //if the request is non functional and priority, a reference on it is added in a nonFunctionalRequestsQueue.
        int priority = request.getNFRequestPriority();
        if ((priority == Request.NFREQUEST_IMMEDIATE_PRIORITY) || (priority == Request.NFREQUEST_PRIORITY)) {
            nfRequestsProcessor.addToNFRequestsQueue(request);
        }

        requestQueue.add(0, request);

        // ProActiveEvent
        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
            notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.ADD_REQUEST));
        }

        // END ProActiveEvent
        return ftres;
    }

    public void processRequests(RequestProcessor processor, Body body) {
        List<Request> reqToServe = new ArrayList<Request>();
        synchronized (this) {

            for (int i = 0; i < requestQueue.size(); i++) {
                Request r;

                // First, we deal with priotity non functional requests
                while (!nfRequestsProcessor.isEmpty()) {
                    r = nfRequestsProcessor.getOldestPriorityNFRequest(true);
                    LocalBodyStore.getInstance().getLocalBody(ownerID).serve(r);
                    requestQueue.remove(r);
                }
                if (requestQueue.isEmpty()) {
                    return;
                }

                r = (Request) requestQueue.get(i);
                int result = processor.processRequest(r);
                switch (result) {
                    case RequestProcessor.REMOVE_AND_SERVE:
                        requestQueue.remove(i);
                        i--;

                        // ProActiveEvent
                        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
                            notifyAllListeners(new RequestQueueEvent(ownerID,
                                RequestQueueEvent.REMOVE_REQUEST));
                        }
                        // END ProActiveEvent
                        // We store the request to be served
                        reqToServe.add(r);

                        break;
                    case RequestProcessor.REMOVE:
                        requestQueue.remove(i);
                        i--;
                        if (SEND_ADD_REMOVE_EVENT && hasListeners()) {
                            notifyAllListeners(new RequestQueueEvent(ownerID,
                                RequestQueueEvent.REMOVE_REQUEST));
                        }
                        break;
                    case RequestProcessor.KEEP:
                        break;
                }
            }
        }
        // If there is a request to serve, we serve it (but outside the synchronized block)
        for (Request req : reqToServe) {
            body.serve(req);
        }
    }

    @Override
    public synchronized String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("--- RequestQueueImpl n=").append(requestQueue.size()).append("   requests --- ->\n");
        int count = 0;
        java.util.Iterator iterator = requestQueue.iterator();
        while (iterator.hasNext()) {
            Request currentrequest = (Request) iterator.next();
            sb.append(count).append("--> ").append(currentrequest.getMethodName()).append("\n");
            count++;
        }
        sb.append("--- End RequestQueueImpl ---");
        sb.append("\n" + nfRequestsProcessor.toString());
{'fraction_non_alphanumeric': 0.1085727009574705, 'fraction_numerical': 0.0029392117568470275, 'mean_word_length': 2.1984047856430706, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1567059', 'n_tokens_mistral': 5650, 'n_tokens_neox': 5483, 'n_words': 1352}