User Manual, Developers Guide and API Documentation

GGn.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/queuingsystem/GGn.hpp>
00029 #include <boost/bind.hpp>
00030 
00031 using namespace wns::queuingsystem;
00032 
00033 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00034     GGn,
00035     wns::simulator::ISimulationModel,
00036     "openwns.queuingsystem.ggn",
00037     wns::PyConfigViewCreator);
00038 
00039 GGn::GGn(const wns::pyconfig::View& config) :
00040     interArrivalDist_(NULL),
00041     processingTimeDist_(NULL),
00042     serverCount_(config.get<int>("serverCount")),
00043     servers_(serverCount_),
00044     cpc_(),
00045     queueSize_(cpc_, "QueueSize"),
00046     processingTime_(cpc_, "ProcessingTime"),
00047     sojournTime_(cpc_, "SojournTime"),
00048     waitingTime_(cpc_, "WaitingTime"),
00049     logger_(config.get("logger"))
00050 {
00051     wns::pyconfig::View disConfig(config, "iatDist");
00052     std::string disName = disConfig.get<std::string>("__plugin__");
00053     interArrivalDist_ = 
00054         wns::distribution::DistributionFactory::creator(disName)->create(disConfig);
00055 
00056     disConfig = config.get("procDist");
00057     disName = disConfig.get<std::string>("__plugin__");
00058     processingTimeDist_ = 
00059         wns::distribution::DistributionFactory::creator(disName)->create(disConfig);
00060 
00061     for(int i = 0; i < servers_.size(); i++)
00062     {
00063         servers_[i] = new Server(processingTimeDist_, this);
00064         emptyServers_.push_back(servers_[i]);    
00065     }
00066 }
00067 
00068 GGn::~GGn()
00069 {
00070     for(int i = 0; i < servers_.size(); i++)
00071         delete servers_[i];
00072 }
00073 
00074 void
00075 GGn::doStartup()
00076 {
00077     double iatMean;
00078     double ptMean;
00079 
00080     iatMean = (dynamic_cast<wns::distribution::IHasMean*>(interArrivalDist_))->getMean();
00081     ptMean = (dynamic_cast<wns::distribution::IHasMean*>(processingTimeDist_))->getMean();
00082 
00083     double load = ptMean / (serverCount_ * iatMean);
00084 
00085     MESSAGE_BEGIN(NORMAL, logger_, m, "G/G/n Started ");
00086     m << "\nMean IAT: " << iatMean;
00087     m << "\nMean PT: " << ptMean;
00088     m << "\nServers (n): " << serverCount_;
00089     m << "\nLoad: " << load;
00090     MESSAGE_END();
00091 
00092     assure(load < 1.0, "Load must be < 1.0!");
00093 
00094     onCreateJob();
00095 }
00096 
00097 void
00098 GGn::onCreateJob()
00099 {
00100     Job job = Job();
00101 
00102     // PASTA 
00103     queueSize_.put(queue_.size());
00104 
00105     queue_.push_back(job);
00106 
00107     double iat = (*interArrivalDist_)();
00108 
00109     wns::simulator::getEventScheduler()->scheduleDelay(
00110         boost::bind(&GGn::onCreateJob, this),
00111         iat);
00112 
00113     wns::simulator::Time now = wns::simulator::getEventScheduler()->getTime();
00114 
00115     MESSAGE_SINGLE(NORMAL, logger_, "Created a job. Total: " << queue_.size()
00116         << " Next Job will be created at: " << now + iat);
00117 
00118     // There are empty servers that can process the job directly
00119     if(!emptyServers_.empty())
00120     {
00121         assure(queue_.size() == 1, "Queue not empty but there are empty servers");
00122         Server* srv = emptyServers_.front();
00123         emptyServers_.pop_front();
00124         srv->processJob(getJob());
00125     }
00126 }
00127 
00128 bool
00129 GGn::hasJob(Server* server)
00130 {
00131     if(queue_.empty())
00132     {
00133         emptyServers_.push_back(server);
00134         return false;
00135     }
00136     else
00137     {
00138         return true;
00139     }
00140 }
00141 
00142 Job
00143 GGn::getJob()
00144 {
00145     assure(!queue_.empty(), "getJob called while queue empty");
00146     Job job = queue_.front();
00147     queue_.pop_front();
00148 
00149     wns::simulator::Time now = wns::simulator::getEventScheduler()->getTime();
00150     waitingTime_.put(now - job.getCreationTime());
00151 
00152     return job;
00153 }
00154 
00155 void
00156 GGn::onJobProcessed(Job job)
00157 {
00158     wns::simulator::Time now = wns::simulator::getEventScheduler()->getTime();
00159     sojournTime_.put(now - job.getCreationTime());
00160 
00161     MESSAGE_SINGLE(NORMAL, logger_, "Finished a job. Left: " << queue_.size());
00162 }
00163 
00164 void
00165 GGn::doShutdown()
00166 {
00167     MESSAGE_SINGLE(NORMAL, logger_, "Jobs left: " << queue_.size());
00168 }
00169 

Generated on Thu May 24 03:31:48 2012 for openWNS by  doxygen 1.5.5