User Manual, Developers Guide and API Documentation

Bounded.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. 5, 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 #include <WNS/ldk/buffer/Bounded.hpp>
00028 #include <WNS/ldk/IConnectorReceptacle.hpp>
00029 
00030 using namespace wns::ldk;
00031 using namespace wns::ldk::buffer;
00032 
00033 
00034 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00035     Bounded,
00036     Buffer,
00037     "wns.buffer.Bounded",
00038     FUNConfigCreator);
00039 
00040 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00041     Bounded,
00042     FunctionalUnit,
00043     "wns.buffer.Bounded",
00044     FUNConfigCreator);
00045 
00046 
00047 Bounded::Bounded(fun::FUN* fuNet, const wns::pyconfig::View& config) :
00048     Buffer(fuNet, config),
00049     fu::Plain<Bounded>(fuNet),
00050 
00051     buffer(ContainerType()),
00052     maxSize(config.get<int>("size")),
00053     currentSize(),
00054     sizeCalculator(),
00055     inWakeup(false)
00056 {
00057     {
00058         std::string pluginName = config.get<std::string>("sizeUnit");
00059         sizeCalculator = SizeCalculator::Factory::creator(pluginName)->create();
00060     }
00061 } // Bounded
00062 
00063 
00064 Bounded::~Bounded()
00065 {
00066     buffer.clear();
00067 
00068     if(sizeCalculator != NULL)
00069     {
00070         delete sizeCalculator;
00071     }
00072 } // ~Bounded
00073 
00074 
00075 //
00076 // CompoundHandler interface
00077 //
00078 bool
00079 Bounded::doIsAccepting(const CompoundPtr& compound) const
00080 {
00081     return currentSize + (*sizeCalculator)(compound) <= maxSize;
00082 } // isAccepting
00083 
00084 
00085 void
00086 Bounded::doSendData(const CompoundPtr& compound)
00087 {
00088     assure(isAccepting(compound), "sendData called although not accepting.");
00089     assure(compound->getRefCount() > 0, "Reference counting defect.");
00090 
00091     buffer.push_back(compound);
00092     currentSize += (*sizeCalculator)(compound);
00093 
00094     increaseTotalPDUs();
00095     probe();
00096 
00097     tryToSend();
00098 } // doSendData
00099 
00100 
00101 void
00102 Bounded::doOnData(const CompoundPtr& compound)
00103 {
00104     getDeliverer()->getAcceptor(compound)->onData(compound);
00105 } // processIncoming
00106 
00107 
00108 void
00109 Bounded::doWakeup()
00110 {
00111     tryToSend();
00112 } // wakeup
00113 
00114 
00115 void
00116 Bounded::tryToSend()
00117 {
00118     while(tryToSendOnce());
00119 
00120     if(inWakeup == false && currentSize < maxSize)
00121     {
00122         inWakeup = true;
00123         getReceptor()->wakeup();
00124         inWakeup = false;
00125     }
00126 } // tryToSend
00127 
00128 
00129 bool
00130 Bounded::tryToSendOnce()
00131 {
00132     if(buffer.empty() == true)
00133     {
00134         return false;
00135     }
00136 
00137     CompoundPtr compound = buffer.front();
00138 
00139     if(getConnector()->hasAcceptor(compound) == false)
00140     {
00141         return false;
00142     }
00143 
00144     buffer.pop_front();
00145     currentSize -= (*sizeCalculator)(compound);
00146 
00147     IConnectorReceptacle* target = getConnector()->getAcceptor(compound);
00148     target->sendData(compound);
00149 
00150     return true;
00151 } // tryToSendOnce
00152 
00153 //
00154 // Buffer interface
00155 //
00156 
00157 unsigned long int
00158 Bounded::getSize()
00159 {
00160     return currentSize;
00161 } // size
00162 
00163 
00164 unsigned long int
00165 Bounded::getMaxSize()
00166 {
00167     return maxSize;
00168 } // getMaxSize
00169 
00170 
00171 

Generated on Mon May 21 03:31:42 2012 for openWNS by  doxygen 1.5.5