User Manual, Developers Guide and API Documentation

NearbyFirst.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-2009
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 
00028 #include <WNS/scheduler/strategy/dsastrategy/NearbyFirst.hpp>
00029 #include <WNS/scheduler/SchedulerTypes.hpp>
00030 #include <WNS/Positionable.hpp>
00031 
00032 using namespace wns::scheduler;
00033 using namespace wns::scheduler::strategy;
00034 using namespace wns::scheduler::strategy::dsastrategy;
00035 
00036 STATIC_FACTORY_REGISTER_WITH_CREATOR(NearbyFirst,
00037                                      DSAStrategyInterface,
00038                                      "NearbyFirst",
00039                                      wns::PyConfigViewCreator);
00040 
00041 NearbyFirst::NearbyFirst(const wns::pyconfig::View& config)
00042     : DSAStrategy(config)
00043 //      randomDist(NULL)
00044 {
00045 //    randomDist = new wns::distribution::StandardUniform();
00046 }
00047 
00048 NearbyFirst::~NearbyFirst()
00049 {
00050 //    delete randomDist;
00051 }
00052 
00053 // call this before each timeSlot/frame
00054 void
00055 NearbyFirst::initialize(SchedulerStatePtr schedulerState,
00056                          SchedulingMapPtr schedulingMap)
00057 {
00058     DSAStrategy::initialize(schedulerState,schedulingMap); // must always initialize base class too
00059 
00060     int maxSubChannel = schedulerState->currentState->strategyInput->getFChannels();
00061     int numberOfTimeSlots = schedulerState->currentState->strategyInput->getNumberOfTimeSlots();
00062     int maxSpatialLayers = schedulerState->currentState->strategyInput->getMaxSpatialLayers();
00063 
00064     int prio = schedulerState->currentState->getCurrentPriority();
00065     wns::scheduler::ConnectionSet conns = colleagues.registry->getConnectionsForPriority(prio);
00066 
00067     std::set<wns::scheduler::UserID> userIDs;
00068     wns::scheduler::ConnectionSet::iterator it;
00069 
00070     // store users in userIDs from type std::set
00071     for(it = conns.begin(); it != conns.end(); it++)
00072         userIDs.insert(colleagues.registry->getUserForCID(*it));
00073 
00074     unsigned int numberOfUsers = userIDs.size();
00075     unsigned int numberOfResources = maxSubChannel * numberOfTimeSlots * maxSpatialLayers;
00076 
00077     MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA: Distributing " 
00078         << numberOfUsers << " users on " << numberOfResources << " resources.");
00079 
00080     if(numberOfUsers == 0)
00081         return;
00082 
00083     // store resources in sortedResources_ from type std::set
00084     for(int i = 0; i < maxSubChannel; i++)
00085     {
00086         for(int j = 0; j < numberOfTimeSlots; j++)
00087         {
00088             for(int k = 0; k < maxSpatialLayers; k++)
00089             {
00090                 DSAResult res;
00091                 res.subChannel = i;
00092                 res.timeSlot = j;
00093                 res.spatialLayer = k;
00094                 sortedResources_.insert(res);
00095             }
00096         } 
00097     }
00098 
00099     int usersMore = numberOfResources % numberOfUsers;
00100     int usersLess = numberOfUsers - usersMore;
00101     int resourcesMore = int(numberOfResources / numberOfUsers) + 1;
00102     int resourcesLess = int(numberOfResources / numberOfUsers);
00103 
00104     MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA: " 
00105         << usersLess << " users get " << resourcesLess << " resources, "
00106         << usersMore << " users get " << resourcesMore << " resources");
00107 
00108     assure(resourcesLess > 0, "Not enough resources for all users");
00109     assure(usersMore * resourcesMore + usersLess * resourcesLess == numberOfResources,
00110         "Mismatched resource distribution");
00111 
00112     // BS Position
00113     wns::PositionableInterface* bsPos = schedulerState->myUserID.getNode()->getService<wns::PositionableInterface*>("mobility");
00114 
00115     //map<Distanz zu BS, UserID>
00116     std::map<double, wns::scheduler::UserID> userIdMap;
00117     std::set<wns::scheduler::UserID>::iterator userIdIt;
00118     for(userIdIt = userIDs.begin(); userIdIt != userIDs.end(); userIdIt++)
00119     {
00120         // SSs Positions
00121         wns::PositionableInterface* ssPos = (*userIdIt).getNode()->getService<wns::PositionableInterface*>("mobility");
00122         userIdMap[ssPos->getDistance(bsPos)] = *userIdIt;
00123     }
00124 
00125     std::map<double, wns::scheduler::UserID>::iterator userIdMapIt;
00126     userIdMapIt = userIdMap.begin();
00127 
00128     // Resources for first UserID
00129     resStart_[userIdMapIt->first] = sortedResources_.begin();
00130     resAmount_[userIdMapIt->first] = resourcesLess;
00131 
00132     MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA: User "
00133         << userIdMapIt->second.getNodeID() << " with distance " << userIdMapIt->first << " starts at "
00134         << sortedResources_.begin()->subChannel << "."
00135         << sortedResources_.begin()->timeSlot << "."
00136         << sortedResources_.begin()->spatialLayer
00137         << " and gets " << resourcesLess << " resources");
00138 
00139     userIdMapIt++;
00140 
00141     // Resources for other UserIDs
00142     int plus = 0;
00143     int user = 0;
00144     int counter = 0;
00145 
00146     std::set<DSAResult, FreqFirst>::iterator itr;
00147     for(itr = sortedResources_.begin(); itr != sortedResources_.end(); itr++)
00148     {
00149         if(counter == resourcesLess + plus)
00150         {
00151             resStart_[userIdMapIt->first] = itr;
00152             resAmount_[userIdMapIt->first] = resourcesLess + plus;
00153 
00154             MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA: User " 
00155                 << userIdMapIt->second.getNodeID() << " with distance " << userIdMapIt->first << " starts at "
00156                 << itr->subChannel << "."
00157                 << itr->timeSlot << "."
00158                 << itr->spatialLayer
00159                 << " and gets " << resourcesLess + plus << " resources");
00160 
00161             counter = 0;
00162             userIdMapIt++;
00163             user++;
00164         }
00165         if(plus == 0 && user == usersLess - 1)
00166         {
00167             plus++;
00168             counter++;
00169         }
00170     counter++;
00171     }
00173 //  std::cout <<  "DEBUGGG: ABD  " 
00174 //  << "BS:  " << schedulerState->myUserID.getNode()->getName() 
00175 //  << "  mit ID:  " << schedulerState->myUserID.getNode()->getNodeID() << "\n";
00176 
00177 /*  std::map<double, std::set<DSAResult>::iterator>::iterator resStartIt;
00178     for(resStartIt = resStart_.begin(); resStartIt != resStart_.end(); resStartIt++)
00179     {
00180             std::cout << "DEBUGGG: " 
00181             << "NearbyFirst::initialize:  "
00182             << "Distanz zu BS : " << resStartIt->first
00183             << "  Ressourcen-Start: " 
00184             << resStartIt->second->subChannel << "."
00185             << resStartIt->second->timeSlot << "."
00186             << resStartIt->second->spatialLayer << "\n";
00187     }
00188 
00189 
00190     std::map<double, int>::iterator resAmountIt;
00191     for(resAmountIt = resAmount_.begin(); resAmountIt != resAmount_.end(); resAmountIt++)
00192     {
00193         MESSAGE_SINGLE(NORMAL, logger, "NearbyFirst::initialize:  "
00194             << "Distanz zu BS : " << resAmountIt->first
00195             << "  Ressourcen-Groesse: " << resAmountIt->second);
00196     }
00197 */
00198 //  std::map<double, wns::scheduler::UserID>::iterator userIdMapIt;
00199 //  for(userIdMapIt = userIdMap.begin(); userIdMapIt != userIdMap.end(); userIdMapIt++)
00200 //  {
00201 //          std::cout << "DEBUGGG: " 
00202 //          << "NearbyFirst::initialize:  "
00203 //          << "Distanz zu BS : " << userIdMapIt->first
00204 //          << " UserID: " << userIdMapIt->second.getNodeID() << "\n";
00205 //  }
00207 
00208 } //initialize
00209 
00210 DSAResult
00211 NearbyFirst::getSubChannelWithDSA(RequestForResource& request,
00212                                    SchedulerStatePtr schedulerState,
00213                                    SchedulingMapPtr schedulingMap)
00214 {
00215 //  DSAResult dsaResult;
00216     MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA(" << request.toString()<<")");
00217 
00218     wns::PositionableInterface* bsPos = schedulerState->myUserID.getNode()->getService<wns::PositionableInterface*>("mobility");
00219     wns::PositionableInterface* ssPos = request.user.getNode()->getService<wns::PositionableInterface*>("mobility");
00220     double distance = ssPos->getDistance(bsPos);
00221 
00222     assure(resStart_.find(distance) != resStart_.end(), "No resources for user " + request.user.getNodeID());
00223     assure(resAmount_.find(distance) != resAmount_.end(), "Unknown resource amount for user " + request.user.getNodeID());
00224 
00225     int max = resAmount_[distance];
00226     std::set<DSAResult>::iterator dsaResultSetIt;
00227     int i = 0;
00228 
00229     bool found = false;
00230     for(dsaResultSetIt = resStart_[distance]; i < max && !found; dsaResultSetIt++)
00231     {
00232         i++;
00233         found = channelIsUsable(dsaResultSetIt->subChannel, 
00234                                 dsaResultSetIt->timeSlot,
00235                                 dsaResultSetIt->spatialLayer,
00236                                 request, 
00237                                 schedulerState, 
00238                                 schedulingMap);
00239     }
00240 
00241     if(!found)
00242     {
00243         MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA(): no free subchannel");
00244         DSAResult dsaResult;
00245         return dsaResult;
00246     }
00247     else
00248     {
00249         dsaResultSetIt--;
00250 
00252 //      std::cout << "DEBUGGG: " 
00253 //      << "User: " << request.user.getNodeID() << " mit Distanz: " << distance 
00254 //      << " bekommt die Resource: " << dsaResultSetIt->subChannel << "." << dsaResultSetIt->timeSlot << "." << dsaResultSetIt->spatialLayer << "\n";
00256 
00257         MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA(): Granting resource: "
00258             << dsaResultSetIt->subChannel << "." << dsaResultSetIt->timeSlot << "." 
00259             << dsaResultSetIt->spatialLayer << " to " << request.toString());
00260         return *dsaResultSetIt;
00261     }
00262 
00263 /*    DSAResult resource;
00264     std::vector<DSAResult> freeResources;
00265 
00266     for(dsaResultSetIt = resStart_[distance]; i < max ; dsaResultSetIt++)
00267     {
00268         i++;
00269         if (channelIsUsable(dsaResultSetIt->subChannel, dsaResultSetIt->timeSlot, dsaResultSetIt->spatialLayer, request, schedulerState, schedulingMap))
00270         {
00271             resource.subChannel = dsaResultSetIt->subChannel;
00272             resource.timeSlot = dsaResultSetIt->timeSlot;
00273             resource.spatialLayer = dsaResultSetIt->spatialLayer;
00274             freeResources.push_back(resource);
00275         }
00276     }
00277 
00278     if (freeResources.size() == 0) 
00279     {
00280         MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA(): no free subchannel");
00281         return dsaResult;
00282     }
00283     else
00284     {
00285         int rnd = int((*randomDist)() * freeResources.size()); 
00286         dsaResult = freeResources[rnd];
00287 
00288         MESSAGE_SINGLE(NORMAL, logger, "getSubChannelWithDSA(): Granting resource: "
00289             << dsaResult.subChannel << "." << dsaResult.timeSlot << "." 
00290             << dsaResult.spatialLayer << " to " << request.toString());
00291         return dsaResult;
00292     }*/
00293 } // getSubChannelWithDSA
00294 

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