// src/backend/resource_policy_fifo.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This 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
// (at your option) any later version.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include <sgpemv2/resource_policy_fifo.hh>


using namespace std;
using namespace sgpem;

ResourcePolicyFiFo::~ResourcePolicyFiFo()
{}


PolicyParameters&
ResourcePolicyFiFo::get_parameters()
{
  return _parameters;
}

void
ResourcePolicyFiFo::configure()
 throw(UserInterruptException)
{
}

void
ResourcePolicyFiFo::enforce(Environment& environment, Environment::SubRequestQueue& queue, SubRequest& sr)
 throw(UserInterruptException)
{
  typedef Environment::SubRequestQueue SubRequestQueue;

  // the last inserted request goes on the last free place.
  SubRequestQueue old_queue(queue);
  queue.clear();

  typedef SubRequestQueue::iterator It;
  for (It i = old_queue.begin(); i != old_queue.end(); i++)
  {
    // allocated ones remain allocated
    if((**i).get_state() == Request::state_allocated)
      queue.push_back(*i);
  }



  typedef SubRequestQueue::iterator It;
  for (It i = old_queue.begin(); i != old_queue.end(); i++)
    if((**i).get_state() != Request::state_allocated && *i != &sr)
      queue.push_back(*i);
  queue.push_back(&sr);
}

Glib::ustring
ResourcePolicyFiFo::get_description() const
{
  return "A resource policy which satisfies earlier requests before older ones.";
}

Glib::ustring
ResourcePolicyFiFo::get_name() const
{
  return "First In, First Out";
}


void
ResourcePolicyFiFo::activate()
{
}

void
ResourcePolicyFiFo::deactivate()
{
}
