User Manual, Developers Guide and API Documentation

Pool.hpp

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 #ifndef WNS_CONTAINER_POOL_HPP
00029 #define WNS_CONTAINER_POOL_HPP
00030 
00031 #include <WNS/Assure.hpp>
00032 
00033 #include <WNS/simulator/ISimulator.hpp>
00034 #include <WNS/events/scheduler/Interface.hpp>
00035 
00036 #include <set>
00037 
00038 #include <iostream>
00039 
00040 namespace wns { namespace container {
00041 
00047     template<typename ID>
00048     class Pool
00049     {
00050         typedef std::set<ID> PoolContainer;
00051     public:
00057         explicit
00058         Pool(simTimeType _unbindDelay, ID _lowestId, ID _highestId)
00059             {
00060                 unbindDelay = _unbindDelay;
00061                 highestId = _highestId;
00062                 lowestId = _lowestId;
00063 
00064                 maximumCapacity = 0;
00065                 ID tmp = lowestId;
00066                 while(tmp < highestId)
00067                 {
00068                     ++tmp;
00069                     ++maximumCapacity;
00070                 }
00071             }
00072 
00076         virtual
00077         ~Pool() {}
00078 
00082         void
00083         bind(ID id)
00084             {
00085                 assure(id <= highestId, "Please choose a lower ID (<" << highestId);
00086                 assure(id >= lowestId, "Please choose a higher ID (>" << lowestId);
00087                 assure(isAvailable(id), "ID is already bound");
00088                 boundIds.insert(id);
00089 
00090             }
00091 
00095         void
00096         unbind(ID id)
00097             {
00098                 assure(id <= highestId, "Please choose a lower ID (<" << highestId);
00099                 assure(id >= lowestId, "Please choose a higher ID (>" << lowestId);
00100                 assure(!isAvailable(id), "ID is not bound");
00101 
00102                 wns::simulator::getEventScheduler()->scheduleDelay(Unbind(this, id), unbindDelay);
00103             }
00104 
00116         ID
00117         suggestPort() const
00118         {
00119             ID id = lowestId;
00120 
00121             // This searches for a free port beginning at lowerBorder.
00122             while(
00123                 // go on as long as no free port found
00124                 boundIds.find(id) != boundIds.end() &&
00125                 // go on as long as not highest port number reached
00126                 id <= highestId)
00127             {
00128                 ++id;
00129             }
00130 
00131             assure(id <= highestId, "No free ID available");
00132             return id;
00133         }
00134 
00140         bool
00141         isAvailable(ID id) const
00142         {
00143             assure(id <= highestId, "Please choose a lower ID (<" << highestId);
00144             assure(id >= lowestId, "Please choose a higher ID (>" << lowestId);
00145             return boundIds.find(id) == boundIds.end();
00146         }
00147 
00148         int
00149         getCapacity() const
00150         {
00151             int capacity = maximumCapacity - boundIds.size();
00152             assure(capacity >= 0, "Pool allocated more elements than were allowed! " << maximumCapacity << "/" << boundIds.size());
00153             return capacity;
00154         }
00160         simTimeType
00161         getUnbindDelay() const
00162             {
00163                 return unbindDelay;
00164             }
00165 
00166     private:
00170         class Unbind
00171         {
00172         public:
00178             Unbind(Pool<ID>* pp,  ID unbindId)
00179                 {
00180                     assure(pp, "No valid instance of Pool given!");
00181                     portPoolInstance = pp;
00182                     id = unbindId;
00183                 }
00187             void
00188             operator()()
00189                 {
00190                     portPoolInstance->boundIds.erase(id);
00191                 }
00192 
00193         private:
00197             Pool<ID>* portPoolInstance;
00198 
00202             ID id;
00203         };
00204 
00208         simTimeType unbindDelay;
00212         PoolContainer boundIds;
00213 
00217         ID highestId;
00218 
00222         ID lowestId;
00223 
00227         int maximumCapacity;
00228     };
00229 } // container
00230 } // wns
00231 
00232 #endif // NOT defined WNS_CONTAINER_POOL_HPP
00233 
00234 
00235 /*
00236   Local Variables:
00237   mode: c++
00238   fill-column: 80
00239   c-basic-offset: 8
00240   c-tab-always-indent: t
00241   indent-tabs-mode: t
00242   tab-width: 8
00243   End:
00244 */

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