iflist.cc

Go to the documentation of this file.
00001 
00002 /*
00003  * iflist.cc
00004  * Copyright (C) 2000 by the University of Southern California
00005  * $Id: iflist.cc,v 1.4 2005/08/25 18:58:04 johnh Exp $
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License,
00009  * version 2, as published by the Free Software Foundation.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00019  *
00020  *
00021  * The copyright of this module includes the following
00022  * linking-with-specific-other-licenses addition:
00023  *
00024  * In addition, as a special exception, the copyright holders of
00025  * this module give you permission to combine (via static or
00026  * dynamic linking) this module with free software programs or
00027  * libraries that are released under the GNU LGPL and with code
00028  * included in the standard release of ns-2 under the Apache 2.0
00029  * license or under otherwise-compatible licenses with advertising
00030  * requirements (or modified versions of such code, with unchanged
00031  * license).  You may copy and distribute such a system following the
00032  * terms of the GNU GPL for this module and the licenses of the
00033  * other code concerned, provided that you include the source code of
00034  * that other code when and as the GNU GPL requires distribution of
00035  * source code.
00036  *
00037  * Note that people who make modified versions of this module
00038  * are not obligated to grant this special exception for their
00039  * modified versions; it is their choice whether to do so.  The GNU
00040  * General Public License gives permission to release a modified
00041  * version without this exception; this exception also makes it
00042  * possible to release a modified version which carries forward this
00043  * exception.
00044  *
00045  */
00046 
00047 /***********************************************************/
00048 /* iflist.cc : Chalermek Intanagonwiwat (USC/ISI) 06/25/99 */
00049 /***********************************************************/
00050 
00051 
00052 #include <stdio.h>
00053 #include "config.h"
00054 #include "random.h"
00055 #include "iflist.h"
00056 
00057 
00058 void Agent_List::InsertFront(Agent_List **start, Agent_List *cur)
00059 {
00060   if (cur == NULL)
00061      return;
00062 
00063   cur->next = *start;
00064   *start = cur;
00065 }
00066 
00067 
00068 void Agent_List::Remove(Agent_List **prev, Agent_List *cur)
00069 {
00070   if (cur == NULL)
00071     return;
00072 
00073   *prev = cur->next;  
00074 }
00075 
00076 
00077 void Agent_List::FreeAll(Agent_List **prev)
00078 {
00079   Agent_List *temp;
00080   Agent_List *next;
00081 
00082   for (temp = *prev; temp != NULL; temp=next) {
00083     next = temp->next;
00084     delete temp;
00085   }
00086 
00087   *prev = NULL;
00088 }
00089 
00090 
00091 PrvCurPtr Agent_List::Find(Agent_List **start, ns_addr_t addr)
00092 {
00093   PrvCurPtr RetVal;
00094   Agent_List **prv, *cur;
00095 
00096   for (prv= start, cur=*start; cur != NULL; prv= &(cur->next), cur=cur->next) {
00097     if ( NODE_ADDR(cur) == addr.addr_ && PORT(cur)==addr.port_) 
00098       break;
00099   }
00100  
00101   RetVal.prv = prv;
00102   RetVal.cur = cur;
00103 
00104   return RetVal;
00105 }
00106 
00107 
00108 // Copy list1's elements not already in *result, and insert in *result 
00109 
00110 void Agent_List::Union(Agent_List **result, Agent_List *list1)
00111 {
00112   PrvCurPtr RetVal;
00113   Agent_List *dupAgent;
00114   
00115   for (Agent_List *cur=list1; cur != NULL; cur=AGENT_NEXT(cur)) {
00116     RetVal = Find(result, AGT_ADDR(cur));
00117     if (RetVal.cur == NULL) {
00118       dupAgent = new Agent_List;
00119       *dupAgent = *cur;
00120       InsertFront(result, dupAgent);
00121     }
00122   }
00123 }
00124 
00125 void Agent_List::print()
00126 {
00127   Agent_List *cur;
00128 
00129   for (cur=this; cur!=NULL; cur=cur->next) {
00130     printf("(%d,%d)\n", cur->agent_addr.addr_, cur->agent_addr.port_);
00131   }
00132 }
00133 
00134 
00135 In_List *In_List::FindMaxIn()
00136 {
00137   In_List *cur_in, *max_in;
00138   int     max_diff;
00139 
00140   max_in = NULL;
00141   max_diff = 0;
00142 
00143   for (cur_in = this; cur_in != NULL;
00144        cur_in = IN_NEXT(cur_in) ) {
00145     if ( TOTAL_RECV(cur_in) - PREV_RECV(cur_in) > max_diff) {
00146       max_diff = TOTAL_RECV(cur_in) - PREV_RECV(cur_in);
00147       max_in = cur_in;
00148     }
00149   }
00150 
00151   return max_in;
00152 }
00153 
00154 
00155 Out_List *Out_List::WhereToGo()
00156 {
00157   Out_List *cur_out;
00158   double slot = Random::uniform();
00159 
00160   for (cur_out = this; cur_out!=NULL; cur_out = OUT_NEXT(cur_out) ) {
00161     if (slot >= FROM_SLOT(cur_out) && slot < TO_SLOT(cur_out) )
00162       return cur_out;
00163   }
00164 
00165   if (cur_out == NULL && this != NULL)
00166     printf("Something must be wrong! \n");
00167 
00168   return cur_out;
00169 }
00170 
00171 
00172 void Out_List::CalRange()
00173 {
00174   Out_List *cur_out=this;
00175   double cur_slot=0.0;
00176 
00177   for (cur_out = this; cur_out != NULL; cur_out = OUT_NEXT(cur_out) ) {
00178 
00179     if ( GRADIENT(cur_out) <= 0.0 ) {
00180       FROM_SLOT(cur_out) = -1.0;
00181       TO_SLOT(cur_out) = -1.0;
00182       continue;
00183     }
00184 
00185     FROM_SLOT(cur_out) = cur_slot;
00186     cur_slot = cur_slot + GRADIENT(cur_out);
00187     TO_SLOT(cur_out) = cur_slot;
00188   }   
00189 
00190   return;
00191 }
00192 
00193 
00194 void Out_List::NormalizeGradient()
00195 {
00196   Out_List *cur;
00197   float sum=0.0;
00198   int   num=0; 
00199 
00200   for (cur=this; cur!=NULL; cur=OUT_NEXT(cur)) {
00201     sum = sum + GRADIENT(cur);
00202     num++;
00203   }
00204 
00205   if (num == 0) return;
00206 
00207   if (sum == 0.0) {
00208     float share= 1.0/num;
00209 
00210     for (cur=this; cur != NULL; cur = OUT_NEXT(cur)) {
00211       GRADIENT(cur) = share;
00212     }
00213     
00214     return;
00215   }
00216 
00217   for (cur=this; cur != NULL; cur = OUT_NEXT(cur)) {
00218     GRADIENT(cur) = GRADIENT(cur)/sum;
00219   }
00220 }
00221 
00222 
00223 
00224 
00225 
00226 
00227 

Generated on Tue Mar 6 16:47:46 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6