⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node2.c

📁 一个linux下的各种组播路由算法编程
💻 C
📖 第 1 页 / 共 5 页
字号:
               addNodeStats(tmp2->nodePtr(), tmp->nodePtr(), tmpg->address(), 
                                                                 maxBatches);
               tmp2 = tmp2->next();
            };
         };
         tmp = tmp->next();
      };
      tmpg = tmpg->next();
   };
         
   //Run the simulator

   // set the global clock to zero;

   double t_global = 0.0;

// batch run begins here.....

   batchNum = 0;
   int confidence = False;

   FILE *fp;

   while ((confidence == False) && (batchNum < maxBatches)) {

      double t_start = t_global;
      double t_stop  = t_global + batchSize;

      //fp = fopen(outFile, "a");
      //fprintf(fp, "batch_num = %d\n",batchNum);
      //printf("t_global=%f\n",t_global);
      //printf("t_stop=%f\n",t_stop);
      //fclose(fp);

        
      Data *currentEventData;
      Event *currentEvent = new Event;

      while (t_global < t_stop) {

/* get the event at the head of the event list */

         int stats = False;
         currentEvent = E->head()->event;

/* advance the master clock to the next event */

         t_global = currentEvent->time; 

/* find out what event this was and act appropriately */

         currentEventData = currentEvent->data;
         int n = currentEvent->type;          // remove the event from
                                                // the event list

/* a background packet or a real-time packet was transferred from an input 
   link into the queue of a node */

         if (n <= 2) {
            if (currentEventData->type() != Background) {
               MCGroup *gr = groupsHd;
               while ((gr != NULL) && 
                      (currentEventData->address() != gr->address())) 
                       gr = gr->next();
               if (gr != NULL) {
                  NodeListEntry *member = gr->headm();
                  while ((member != NULL) && 
                    (member->nodePtr() != (Node*)(currentEvent->pointer)))
                     member = member->next();
                  if (member != NULL) stats = True;
               };
            };
((Node*)(currentEvent->pointer))->trafficIn(currentEventData,
                                              t_global, E, this, stats); 
         }

/* a background packet or real-time packet was transferred from the queue of 
   a node to the output links */

         else if ((n >= 3) && (n <= 4)) {

((AdjacencyListEntry *)(currentEvent->pointer))->trafficOut(currentEventData,
                                                                 t_global, E);
         }

/* a background packet or a real-time packet was transferred from a source
   into the queue of a node */

         else if (n >= 5) {
            if (currentEventData->type() == Realtime) stats = True;
((Source *)(currentEvent->pointer))->sourceToNode(currentEventData,
                                                    t_global, E, this, stats);
         };
         E->deleteEvent();
     }; // t_global >= t_stop


     //Statistics
     if (batchNum >= SKIPPEDBATCHES) {
         NodeStats *tmp1 = nodeStatsHd;
         while (tmp1 != NULL) {
            if (tmp1->batchPackets(batchNum) != 0)
                tmp1->totalDelay(batchNum, (tmp1->totalDelay(batchNum) / 
                                            tmp1->batchPackets(batchNum)));
            tmp1 = tmp1->next();
         };

         if (batchNum >= (minBatches - 1)) {
            int exit = True;
            tmp1 = nodeStatsHd;
            while (tmp1 != NULL) {
                double satisfy = tmp1->meanAndConf(batchNum + 1);
                if ((satisfy > Acceptable) || (satisfy < (-1 * Acceptable))) 
                exit = False;
                tmp1 = tmp1->next();
            };
            if (exit == True) confidence = True;
            else batchNum++;
         }
         else batchNum++;
     }
     else batchNum++;   // next batch
         
   }; //end of batch

   //Statistics
   //open file to write the stats into

   NodeStats *avgStats = NULL;
   tmpg = groupsHd;
   while (tmpg != NULL) {
     NodeListEntry *tmp = nodeListHd;
     while (tmp != NULL) {
       SourceList *tmps = tmp->nodePtr()->sourceList();
       int found = False;
       while ((tmps != NULL) && (found == False)) {
       if ((tmps->source()->address() == tmpg->address()) && 
                (tmps->source()->type() != Background)) found = True;
       else tmps = tmps->next();
       };
       if (found == True) {
            double minAvgD = DBL_MAX;
            double maxAvgD = 0;
            double avgAvgD = 0;
            double maxJitter = 0;
            unsigned long cellsR = 0;
            unsigned long cellsL = 0;
            double avgAvgL = 0;
            unsigned long cellsM = 0;
            NodeStats *tmpNS = new NodeStats;
            NodeStats *tmp1 = nodeStatsHd;

            while (tmp1 != NULL) {
               if ((tmp1->address() == tmpg->address()) && 
                   (tmp1->source() ==  tmp->nodePtr())) {
		 NodeListEntry *tmpm = tmpg->headm();
		 int foundm = False;
		 while ((tmpm != NULL) && (tmpm->nodePtr() != tmp1->current()))
		   tmpm = tmpm->next();
                  if (tmpm != NULL) {
		    cellsR += tmp1->packetsReceived();
		    cellsL += tmp1->packetsLost();
		    cellsM += tmp1->packetsMissed();
		    avgAvgL += tmp1->aveLoss();
		    if (tmp1->aveDelay() < minAvgD) minAvgD = tmp1->aveDelay();
		    if (tmp1->aveDelay() > maxAvgD) maxAvgD = tmp1->aveDelay();
		    avgAvgD += tmp1->aveDelay();
		    if (tmp1->jitter() > maxJitter) maxJitter = tmp1->jitter();
                  };
               };
               tmp1 = tmp1->next();
            };
            tmpNS->minDelay(minAvgD);
            tmpNS->maxDelay(maxAvgD);
            tmpNS->aveDelay(avgAvgD / tmpg->count());
            tmpNS->jitter(maxJitter);
            tmpNS->packetsReceived(cellsR);
            tmpNS->packetsLost(cellsL);
            tmpNS->aveLoss(avgAvgL / tmpg->count());
            tmpNS->packetsMissed(cellsM);
            tmpNS->source(tmp->nodePtr());
            tmpNS->address(tmpg->address());
            tmpNS->current(NULL);
            tmpNS->next(avgStats);
            avgStats = tmpNS;
        }; 
        tmp = tmp->next();
      };
      tmpg = tmpg->next();
   };

   fp = fopen(outFile, "a");
   if (confidence == True) 
   fprintf(fp, "conf. satisfied, # of batches = %d\n", (batchNum + 1));
   else  fprintf(fp, "conf. not satisfied, # of batches = %d\n", maxBatches);
   fprintf(fp, "Group\tSource\tNode\tCR\tCL\tAve L\tCM\tMin");
   fprintf(fp, " D\tMax D\tJitter\tAve D\tConf D\tConf L\n"); 

   NodeStats *tmp1 = nodeStatsHd;
   NodeStats *tmp2;
   while (tmp1 != NULL) {
         MCGroup *gr = groupsHd;
         while ((gr != NULL) && 
                (tmp1->address() != gr->address())) gr = gr->next();
         if (gr != NULL) {
            NodeListEntry *member = gr->headm();
            while ((member != NULL) && 
              (member->nodePtr() != tmp1->current())) member = member->next();
            if (member != NULL) {
               fprintf(fp, "%d\t%d\t%d\t", tmp1->address(), tmp1->source()->name(),
                                     tmp1->current()->name());
               fprintf(fp, "%ld\t%ld\t%.3lf\t%ld\t", tmp1->packetsReceived(),
                                   tmp1->packetsLost(), tmp1->aveLoss(),
                                   tmp1->packetsMissed());
               fprintf(fp, "%.4e\t%.4e\t%.4e\t%.4e\t%.4e\t%.4e\n", 
                      (tmp1->minDelay() * 1000), (tmp1->maxDelay() * 1000),      
                      (tmp1->jitter() * 1000), (tmp1->aveDelay() * 1000),
                      (tmp1->confDelay() * 1000), tmp1->confLoss());
            };
         };
         tmp2 = tmp1->next(); 
         delete tmp1;
         tmp1 = tmp2;      
   };

   nodeStatsHd = nodeStatsTl = NULL;
   fclose(fp);

   //Now clear the event list for the next runs.
   E->clear();
   batchNum = 0;

   //Empty all queues from their contents
   tmp = nodeListHd;
   while (tmp != NULL) {
      AdjacencyListEntry *tmpa = tmp->nodePtr()->adjacentNodes();
      while (tmpa != NULL) {
         tmpa->queue()->clearQ();
         tmpa->busy(False);
         tmpa = tmpa->next();
      };
      tmp = tmp->next();
   };
   return(avgStats);
};

void TheNodeList::removeTree(Node *source, int addr) {

   removeSubtree(source, addr, source);
};

void TheNodeList::addNodeStats(Node *c, Node *s, int addr, int batches) {

   NodeStats *st = new NodeStats(c, s, addr, batches);
   if (nodeStatsHd == NULL) nodeStatsHd = nodeStatsTl = st;
   else {
      nodeStatsTl->next(st);
      nodeStatsTl = st;
   };
};

double TheNodeList::graphDegree() {

   //calculates the average node degree of the graph
   NodeListEntry *tmp = nodeListHd;

   int i = 0;

   while (tmp != NULL) {
      AdjacencyListEntry *adj = tmp->nodePtr()->adjacentNodes();
      while (adj != NULL) {
        i++;
        adj = adj->next();
      };
      tmp = tmp->next();
   };
   return((double)(i) / num); 
};
     
void TheNodeList::statistics(Node *nd, Data *d, double t_global) {

     //perform statistics for the simulator
     if (batchNum >= SKIPPEDBATCHES) {
         NodeStats *tmp1 = nodeStatsHd;
         int found = False;
         while ((tmp1 != NULL) && (found == False)) {
            if ((tmp1->current() == nd) && 
                (tmp1->source() == (Node*)(d->source())) &&
                (tmp1->address() == d->address())) found = True;
            else tmp1 = tmp1->next();
         };

         if (found == True) {
                  double delay = t_global - d->time(); 
                  if (delay < tmp1->minDelay())
                                      tmp1->minDelay(delay);
                  if (delay > tmp1->maxDelay())
                                      tmp1->maxDelay(delay);
                  if (delay > DELAYBOUND) tmp1->addPacketsMissed(1);
                  tmp1->addPacketsReceived(1);
                  tmp1->addBatchPacketsReceived(batchNum, 1);
                  tmp1->addTotalDelay(batchNum, delay);
                  if (tmp1->packetsReceived() > 1) {
     
                    if ((d->stopJitter() == False) && 
			(fabs(delay - tmp1->prevDelay()) > tmp1->jitter()))
                    tmp1->jitter(fabs(delay - tmp1->prevDelay()));
                    if ((d->seqNum() - tmp1->prevSeqNum()) > 1) {
                       tmp1->addPacketsLost(d->seqNum() - 
                                         tmp1->prevSeqNum() - 1);
                       tmp1->addBatchPacketsLost(batchNum, d->seqNum() - 
                                         tmp1->prevSeqNum() - 1);
                    };                  
                  };
                  tmp1->prevDelay(delay);
                  tmp1->prevSeqNum(d->seqNum());
         };
     };
};

void TheNodeList::renumber() {

   //Renumber the nodes starting with 0 up to (num - 1)
   int oldName[num];
   NodeListEntry *tmp = nodeListHd;
   int i = 0;
   while (tmp != NULL) {
      oldName[i] = tmp->nodePtr()->name();
      tmp->nodePtr()->name(i);
      i++;
      tmp = tmp->next();
   };
   maxName = i;

   tmp = nodeListHd;
   while (tmp != NULL) {
      SourceList *s = tmp->nodePtr()->sourceList();
      while (s != NULL) {
         if (s->source()->type() == Background) {
            i = 0;
            while ((oldName[i] != s->source()->address()) &&
                                               (i < num)) i++;
            if (i < num) s->source()->address(i);
         };
         s =s->next();
      };
      tmp = tmp->next();
   };

   i = 0;
   while ((oldName[i] != dt) && (i < num)) i++;
   if (i < num) dt = i;

};

void TheNodeList::loadDistribution(double &mean, double &var, int *hstgrm) {
  
  int i;
  NodeListEntry *tmp = nodeListHd;
  for (i = 0; i < DIMENSION; i++) *(hstgrm + i) = 0;
  mean = i = 0;
  while (tmp != NULL) {
    AdjacencyListEntry *adj = tmp->nodePtr()->adjacentNodes();
    while (adj != NULL) {
      i++;
      if (fn == PEAK) {
        mean += adj->peak();
        *(hstgrm + (int)(adj->peak()/1e6)) += 1;
      }
      else {
        mean += adj->average();
        *(hstgrm + (int)(adj->average()/1e6)) += 1;
      };
      adj = adj->next();
    };
    tmp = tmp->next();
  };
  mean /= i;
  
  tmp = nodeListHd;
  var = 0;
  while (tmp != NULL) {
    AdjacencyListEntry *adj = tmp->nodePtr()->adjacentNodes();
    while (adj != NULL) {
      i++;
      if (fn == PEAK) var += (adj->peak() - mean) * (adj->peak() - mean);
      else var += (adj->average() - mean) * (adj->average() - mean);
      adj = adj->next();
    };
    tmp = tmp->next();
  };
  var /= (i * (i - 1));
  var = sqrt(var);
};

MCGroup *TheNodeList::groupOf(int addr) {

   int found = 0;
   MCGroup *tmp = groupsHd;
   while ((tmp != NULL) && (found == 0)) {
      if (tmp->address() == addr) found = 1;
      else tmp = tmp->next();
   };
   if (tmp != NULL) return(tmp);
   else return(NULL);
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -