00001 // 00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH) 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 // 00018 00024 #include <assert.h> 00025 00026 #include "GiaTokenFactory.h" 00027 #include "Gia.h" 00028 00029 00030 Define_Module(GiaTokenFactory); 00031 00032 00033 void GiaTokenFactory::initialize( int stage ) 00034 { 00035 // wait until IPAddressResolver finished his initialization 00036 if(stage != MIN_STAGE_OVERLAY) 00037 return; 00038 00039 gia = check_and_cast<Gia*>(getParentModule()->getSubmodule("gia")); 00040 00041 stat_sentTokens = 0; 00042 00043 WATCH(stat_sentTokens); 00044 WATCH_VECTOR(tokenQueueVector); 00045 } 00046 00047 void GiaTokenFactory::handleMessages( cMessage* msg ) 00048 { 00049 error("this module doesn't handle messages, it runs only in initialize()"); 00050 } 00051 00052 void GiaTokenFactory::setNeighbors( GiaNeighbors* nghbors ) 00053 { 00054 neighbors = nghbors; 00055 } 00056 00057 void GiaTokenFactory::setMaxHopCount( uint32_t maxHopCount) 00058 { 00059 this->maxHopCount = maxHopCount; 00060 } 00061 00062 void GiaTokenFactory::grantToken() 00063 { 00064 if (neighbors->getSize() == 0) return; 00065 00066 // create priority queue 00067 createPriorityQueue(); 00068 00069 // update sentTokenCount at node on top of priority queue 00070 updateSentTokens(); 00071 00072 // send token to top of queue 00073 assert( tokenQueue.size() ); 00074 assert( !tokenQueue.top().node.isUnspecified() ); 00075 gia->sendToken(tokenQueue.top().node); 00076 00077 // increse statistic variable 00078 stat_sentTokens++; 00079 00080 updateQueueVector(); 00081 } 00082 00083 void GiaTokenFactory::createPriorityQueue() 00084 { 00085 clearTokenQueue(); 00086 for (uint32_t i = 0; i < neighbors->getSize(); i++ ) { 00087 FullGiaNodeInfo temp; 00088 temp.node= neighbors->get(i); 00089 temp.info = neighbors->get(temp.node); 00090 //temp.setCapacity(tempInfo->capacity); 00091 //temp.setSentTokens(tempInfo->sentTokens); 00092 00093 tokenQueue.push(temp); 00094 } 00095 } 00096 00097 void GiaTokenFactory::clearTokenQueue() 00098 { 00099 while( !tokenQueue.empty() ) 00100 tokenQueue.pop(); 00101 } 00102 00103 void GiaTokenFactory::updateQueueVector() 00104 { 00105 // fill tokenQueueVector 00106 tokenQueueVector.clear(); 00107 while (!tokenQueue.empty()) { 00108 tokenQueueVector.push_back(tokenQueue.top().node); 00109 tokenQueue.pop(); 00110 } 00111 } 00112 00113 void GiaTokenFactory::updateSentTokens() 00114 { 00115 if (tokenQueue.empty()) return; 00116 00117 tokenQueue.top().info->sentTokens++; 00118 } 00119 00120 bool GiaTokenFactory::tokenCompareGiaNode::operator()(const FullGiaNodeInfo& x, 00121 const FullGiaNodeInfo& y) 00122 { 00123 if (x.info->sentTokens > y.info->sentTokens) 00124 return true; 00125 if (x.info->sentTokens == y.info->sentTokens) 00126 if (x.node.getCapacity() < y.node.getCapacity()) //??? 00127 return true; 00128 return false; 00129 }