User Manual, Developers Guide and API Documentation

MM1Step6.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/MM1Step6.hpp>
00029 #include <WNS/queuingsystem/JobContextProvider.hpp>
00030 
00031 using namespace wns::queuingsystem;
00032 
00033 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00034     SimpleMM1Step6,
00035     wns::simulator::ISimulationModel,
00036     "openwns.queuingsystem.SimpleMM1Step6",
00037     wns::PyConfigViewCreator);
00038 
00039 SimpleMM1Step6::SimpleMM1Step6(const wns::pyconfig::View& config) :
00040     priorityDistribution_(Job::lowPriority, Job::highPriority),
00041     config_(config),
00042     logger_(config.get("logger")),
00043     idle(true),
00044     // Below we will put one Context Provider in the collection
00045     cpc_(new wns::probe::bus::ContextProviderCollection()),
00046     // The name of the measurement source. Must match the one configured
00047     // in the global Probe Bus Registry
00048     sojournTime_(cpc_, "SojournTime")
00049 {
00050     wns::pyconfig::View disConfig = config.get("jobInterArrivalTimeDistribution");
00051     std::string disName = disConfig.get<std::string>("__plugin__");
00052     jobInterarrivalTime_ = 
00053         wns::distribution::DistributionFactory::creator(disName)->create(disConfig);
00054 
00055     disConfig = config.get("jobProcessingTimeDistribution");
00056     disName = disConfig.get<std::string>("__plugin__");
00057     jobProcessingTime_ = 
00058         wns::distribution::DistributionFactory::creator(disName)->create(disConfig);
00059 
00060     // PDU Context Provider will receive a Job PDU when probing and read
00061     // Context information from it
00062     cpc_->addProvider(JobContextProvider());
00063 }
00064 
00065 void
00066 SimpleMM1Step6::doStartup()
00067 {
00068     MESSAGE_SINGLE(NORMAL, logger_, "MM1Step6 started, generating first job\n" << *this);
00069 
00070     generateNewJob();
00071 }
00072 
00073 void
00074 SimpleMM1Step6::doShutdown()
00075 {
00076     assure(cpc_, "No Context Provider Collection");
00077     delete cpc_;
00078 }
00079 
00080 void
00081 SimpleMM1Step6::generateNewJob()
00082 {
00083     // Create a new job
00084     JobPtr job(new Job(drawJobPriority()));
00085 
00086     if (job->getPriority() == Job::lowPriority)
00087     {
00088         lowPriorityQueue_.push_back(job);
00089     }
00090     else
00091     {
00092         highPriorityQueue_.push_back(job);
00093     }
00094 
00095 
00096     wns::simulator::Time delayToNextJob = (*jobInterarrivalTime_)();
00097 
00098     MESSAGE_SINGLE(NORMAL, logger_, "Generated new job, next job in " << delayToNextJob << "s\n" << *this);
00099 
00100     if (idle)
00101     {
00102         processNextJob();
00103     }
00104 
00105     wns::simulator::getEventScheduler()->scheduleDelay(
00106         boost::bind(&SimpleMM1Step6::generateNewJob, this),
00107         delayToNextJob);
00108 }
00109 
00110 void
00111 SimpleMM1Step6::onJobProcessed()
00112 {
00113     assure(currentJob_, "onJobProcessed called with no current Job");
00114     // Calculate the Jobs sojourn time
00115     wns::simulator::Time  now;
00116     wns::simulator::Time  sojournTime;
00117     now = wns::simulator::getEventScheduler()->getTime();
00118     sojournTime = now - currentJob_->getCreationTime();
00119 
00120     // Give some debug output
00121     MESSAGE_SINGLE(NORMAL, logger_, "Finished a job\n" << *this);
00122     MESSAGE_BEGIN(NORMAL, logger_, m, "Sojourn Time of last job ");
00123     m << sojournTime;
00124     MESSAGE_END();
00125 
00126     // Probe the value. The Job Context Provider will automatically
00127     // attach information about the priority of the job.
00128     // It therefore requieres put() to be called with the Job
00129     // as an argument.
00130     sojournTime_.put(currentJob_, sojournTime);
00131 
00132     // if there are still jobs, serve them
00133     if ( getNumberOfJobs() > 0 )
00134     {
00135         processNextJob();
00136     }
00137     else
00138     {
00139         idle = true;
00140     }
00141 }
00142 
00143 void
00144 SimpleMM1Step6::processNextJob()
00145 {
00146     currentJob_ = getNextJob();
00147 
00148     wns::simulator::Time processingTime = (*jobProcessingTime_)();
00149 
00150     wns::simulator::getEventScheduler()->scheduleDelay(
00151         boost::bind(&SimpleMM1Step6::onJobProcessed, this),
00152         processingTime);
00153 
00154     idle = false;
00155 
00156     MESSAGE_SINGLE(NORMAL, logger_, "Processing next job, processing time: " << processingTime << "s\n" << *this);
00157 }
00158 
00159 int
00160 SimpleMM1Step6::getNumberOfJobs() const
00161 {
00162     return lowPriorityQueue_.size() + highPriorityQueue_.size();
00163 }
00164 
00165 JobPtr
00166 SimpleMM1Step6::getNextJob()
00167 {
00168     JobPtr nextJob;
00169 
00170     if (highPriorityQueue_.size() > 0)
00171     {
00172         nextJob = highPriorityQueue_.front();
00173 
00174         highPriorityQueue_.pop_front();
00175     }
00176     else if(lowPriorityQueue_.size() > 0)
00177     {
00178         nextJob = lowPriorityQueue_.front();
00179 
00180         lowPriorityQueue_.pop_front();
00181     }
00182     else
00183     {
00184         throw wns::Exception("getNextJob called but now Job queued. You must check for available jobs before calling getNextJob!");
00185     }
00186 
00187     return nextJob;
00188 }
00189 
00190 std::string
00191 SimpleMM1Step6::doToString() const
00192 {
00193     std::stringstream ss;
00194     ss << "Jobs in system: " << getNumberOfJobs();
00195     return ss.str();
00196 }
00197 
00198 Job::Priority
00199 SimpleMM1Step6::drawJobPriority()
00200 {
00201     if (priorityDistribution_() == static_cast<int>(Job::lowPriority))
00202     {
00203         return Job::lowPriority;
00204     }
00205     else
00206     {
00207         return Job::highPriority;
00208     }
00209 }
00210 

Generated on Fri May 25 03:31:52 2012 for openWNS by  doxygen 1.5.5