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

📄 node2.c

📁 一个linux下的各种组播路由算法编程
💻 C
📖 第 1 页 / 共 5 页
字号:
   if (tmp1 == NULL) {
      tmp1 = new NodeListEntry;
      tmp1->nodePtr(nd);
      tmp1->next(firstm);
      firstm = tmp1;
      num++;
   };
   return(num);
};

void TheNodeList::removeMCGroup(int addr) {

   MCGroup *tmp1, *tmp2;

   tmp1 = groupsHd;
   while ((tmp1 != NULL) && (tmp1->address() != addr)) {
       tmp2 = tmp1;
       tmp1 = tmp1->next();
   };
   if (tmp1 != NULL) {
      if (tmp1 == groupsHd) groupsHd = tmp1->next();
      else tmp2->next(tmp1->next());
      delete tmp1;
      grCount--;
   };
   
   NodeListEntry *tmp3 = nodeListHd;
   while (tmp3 != NULL) {
      SourceList *tmps = tmp3->nodePtr()->sourceList();
      SourceList *tmps2 = NULL;
      while (tmps != NULL) {
          if ((tmps->source()->address() == addr) && 
              (tmps->source()->type() != Background)) {
              removeTree(tmp3->nodePtr(), addr);
              if (tmps ==  tmp3->nodePtr()->sourceList()) {
                 tmp3->nodePtr()->sourceList(tmps->next());
                 delete tmps;
                 tmps = tmp3->nodePtr()->sourceList();
              }
              else {
                 tmps2->next(tmps->next());
                 delete tmps;
                 tmps = tmps2->next();
              };
              srceCount--;
          }
          else {
              tmps2 = tmps;
              tmps = tmps->next();
          };
      };
     tmp3 = tmp3->next();
   };
};

MCGroup::~MCGroup() {

   NodeListEntry *tmp1, *tmp2;

   tmp1 = firstm;
   while (tmp1 != NULL) {
      tmp2 = tmp1;
      tmp1 = tmp1->next();
      delete tmp2;
   };
};
 
void TheNodeList::addMCSource(Node *nd, double peak, double avg_rho, 
                              double burst, int addr, int model) {

   MCGroup *tmp1 = groupsHd;

   while ((tmp1 != NULL) && (tmp1->address() != addr)) tmp1 = tmp1->next();
   if (tmp1 != NULL) {
       nd->addSource(model, 1, 1, peak, avg_rho, burst, addr);
       srceCount++;
   };
};
   
void TheNodeList::removeMCSource(Node *nd, int addr) {

   removeTree(nd, addr);
   if (nd->removeSource(1, 1, addr) == True) srceCount--;
};

void TheNodeList::addMCDestination(Node *nd, int addr) {

   MCGroup *tmp2 = groupsHd;
   while ((tmp2 != NULL) &&  (tmp2->address() != addr)) tmp2 = tmp2->next();
   if (tmp2 != NULL) tmp2->addMember(nd);
   else {
      tmp2 = new MCGroup(addr);
      tmp2->next(groupsHd);
      groupsHd = tmp2;
      grCount++;
      tmp2->addMember(nd);
   };
};

void TheNodeList::removeMCDestination(Node *nd, int addr) {

   MCGroup *tmp2 = groupsHd;
   while ((tmp2 != NULL) &&  (tmp2->address() != addr)) tmp2 = tmp2->next();
   if (tmp2 != NULL) tmp2->removeMember(nd);
};

void MCGroup::removeMember(Node *nd) {

   NodeListEntry *tmp1, *tmp2;
   tmp1 = firstm;
   while ((tmp1 != NULL) && (tmp1->nodePtr() != nd)) {
      tmp2 = tmp1;
      tmp1 = tmp1->next();
   };
   if (tmp1 != NULL) {
      if (tmp1 == firstm) firstm = tmp1->next();
      else tmp2->next(tmp1->next());
      delete tmp1;
      num--;
   };   
};

void TheNodeList::writeToFile(char *fileName) {

   FILE *fp;
   NodeListEntry *tmp;
   fp = fopen(fileName, "w");
   fprintf(fp, "NETSIM");


   fprintf(fp, "\n\n%d\n\n", num);

   //First save the nodes and their positions
   tmp = nodeListHd;
   while (tmp != NULL) {
      Node *nd = tmp->nodePtr();
      fprintf(fp, "%d\t%lf\t%lf\n", nd->name(), nd->X(), nd->Y());
      tmp = tmp->next();
   };

   fprintf(fp, "\n");

   //Now save the adjacency lists of each node
   tmp = nodeListHd;
   while (tmp != NULL) {
      Node *nd = tmp->nodePtr();
      int i = 0;
      AdjacencyListEntry *adj = nd->adjacentNodes();
      while (adj != NULL) {
          i++;
          adj = adj->next();
      };
      fprintf(fp, "%d\t", i);
      adj = nd->adjacentNodes();
      while (adj != NULL) {
          fprintf(fp, "%d\t%lf\t%lf\t%lf\t", adj->nodePtr()->name(), 
		  adj->linkCapacity(), adj->peak(), adj->average());
          adj = adj->next();
      };

      fprintf(fp, "\n");
      tmp = tmp->next();
   };
   
   fprintf(fp, "\n");

   fprintf(fp, "%d\n", grCount);
   MCGroup *mcg = groupsHd;
   while (mcg != NULL) {
      fprintf(fp, "%d\t%d\t", mcg->address(), mcg->count());
      tmp = mcg->headm();
      while (tmp != NULL) {
          fprintf(fp, "%d\t", tmp->nodePtr()->name());
          tmp = tmp->next();
      };
      fprintf(fp, "\n");
      mcg = mcg->next();
   };

   fprintf(fp, "\n");

   fprintf(fp, "%d\n", srceCount);
   tmp = nodeListHd;
   while (tmp != NULL) {
      SourceList *tmps = tmp->nodePtr()->sourceList();
      while (tmps != NULL) {
           Source *ss = tmps->source();
           if (ss->model() == IBP) {
           IBPSource *s = (IBPSource *)ss;
           fprintf(fp, "%d\t%d\t%d\t%d\t%d\t%lf\t%lf\t%lf\n", s->model(),
                   s->nodePtr()->name(), s->type(), s->priority(), s->address(), 
                   s->peak(), s->average(), s->burst());
           }
           else if (ss->model() == VOICE)  {
           VoiceSource *s = (VoiceSource *)ss;
           fprintf(fp, "%d\t%d\t%d\t%d\t%d\t%lf\t%lf\t%lf\n", s->model(),
                   s->nodePtr()->name(), s->type(), s->priority(), s->address(), 
                   s->peak(), s->rho(), s->burst());
           }
           else if (ss->model() == VIDEO)  {
           VideoSource *s = (VideoSource *)ss;
           fprintf(fp, "%d\t%d\t%d\t%d\t%d\t%lf\t%lf\t%lf\n", s->model(),
                   s->nodePtr()->name(), s->type(), s->priority(), s->address(), 
                   s->peak(), s->rho(), s->burst());
           }
	   else if (ss->model() == BACKGROUND)  {
           BackgroundSource *s = (BackgroundSource *)ss;
           fprintf(fp, "%d\t%d\t%d\t%d\t%d\t%lf\t%lf\t%lf\n", s->model(),
                   s->nodePtr()->name(), s->type(), s->priority(), s->address(), 
                   s->peak(), s->rho(), s->burst());
           };
           tmps = tmps->next();
      };
      tmp = tmp->next();
   };

   fprintf(fp, "\n");

   tmp = nodeListHd;
   while (tmp != NULL) {
      RoutingTableEntry *tmpr = tmp->nodePtr()->routingTable();
      int i = 0;
      while (tmpr != NULL) {
            i++;
            tmpr = tmpr->next();
      };
      fprintf(fp, "%d\t", i);
      tmpr = tmp->nodePtr()->routingTable();
      while (tmpr != NULL) {
          NodeListEntry *tmp2 = tmpr->children();
          int j = 0;
          while (tmp2 != NULL) {
             j++;
             tmp2 = tmp2->next();
          };
          fprintf(fp, "%d\t%d\t%d\t", tmpr->address(), 
                      tmpr->source()->name(), j);
          tmp2 = tmpr->children();
          while (tmp2 != NULL) {
             fprintf(fp, "%d\t", tmp2->nodePtr()->name());
             tmp2 = tmp2->next();
          };
          tmpr = tmpr->next();
      };
      fprintf(fp, "\n");
      tmp = tmp->next();
   };
   
   fclose(fp);
};

Node *TheNodeList::nodeOf(int name) {

   int found = 0;
   NodeListEntry *tmp = nodeListHd;
   while ((tmp != NULL) && (found == 0)) {
      if (tmp->nodePtr()->name() == name) found = 1;
      else tmp = tmp->next();
   };
   if (tmp != NULL) return(tmp->nodePtr());
   else return(NULL);
};


int TheNodeList::readFromFile(char *fileName) {

   FILE *fp;
   int name;
   int i, j, k, l, addr, count, type, priority, model;
   double x, y;
   double cap, pk, avg, peak, avg_rho, burst, cost;
   NodeListEntry *tmp;
   Node *nd1, *nd2;

   deleteGraph();
   maxName = 0;

   fp = fopen(fileName, "a");
   fclose(fp);
   fp = fopen(fileName, "r");
   char s[10];
   fscanf(fp, "%s", s); 
   if (strcmp(s, "NETSIM") == 0) {
      fscanf(fp, "%d\n", &count);   
      i = 0;
      while (i < count) {
         fscanf(fp, "%d%lf%lf", &name, &x, &y);
         tmp = new NodeListEntry;
         nd1 = new Node(x, y);
         tmp->nodePtr(nd1);
         addNode(tmp);
         nd1->name(name);
         if (name > maxName) maxName = name;
         i++;
      };

      tmp = nodeListHd;

      while (tmp != NULL) {
         nd1 = tmp->nodePtr();
         fscanf(fp, "%d", &i);
         for (j = 0; j < i; j++) {         
             fscanf(fp, "%d%lf%lf%lf", &name, &cap, &pk, &avg);        
             nd2 = nodeOf(name);
             nd1->addNeighbor(nd2, cap, pk, avg);
         };
         tmp = tmp->next();
      };    

      fscanf(fp, "%d", &i);
      for (j = 0; j < i; j++) { 
         fscanf(fp, "%d%d", &addr, &count);
         for (k = 0; k < count; k++) {
             fscanf(fp, "%d", &name);
             nd1 = nodeOf(name);
             addMCDestination(nd1, addr);
         };
      };

      fscanf(fp, "%d", &i);
      for (j = 0; j < i; j++) { 
         fscanf(fp, "%d%d%d%d%d%lf%lf%lf", &model, &name, &type, &priority, 
                                         &addr, &peak, &avg_rho, &burst);
         nd1 = nodeOf(name);
         nd1->addSource(model, type, priority, peak, avg_rho, burst, addr);
      };
      srceCount = i;

      tmp = nodeListHd;

      while (tmp != NULL) {
         nd1 = tmp->nodePtr();
         fscanf(fp, "%d", &i);
         for (j = 0; j < i; j++) {         
             fscanf(fp, "%d%d%d", &addr, &name, &k);        
             nd2 = nodeOf(name);
             RoutingTableEntry *tmpRout = new RoutingTableEntry(addr, nd2);
             tmpRout->next(nd1->routingTable());
             nd1->routingTable(tmpRout);
             for (l = 0; l < k; l++) {
                  fscanf(fp, "%d", &name);
                  nd2 = nodeOf(name);
                  tmpRout->addDestination(nd2);
             };
         };
         tmp = tmp->next();
      };    

   fclose(fp);
   return(0);
   }
   else {                      
      fclose(fp);
      return(1);
   };
};

NodeStats *TheNodeList::simulation(double batchSize, int minBatches, 
                                     int maxBatches, char *outFile) {

   //Generate the first arrivals only for real-time sources or background
   //sources for links traversed by real-time traffic.

   NodeListEntry *tmp = nodeListHd;

   while (tmp != NULL) { 
      SourceList *tmps = tmp->nodePtr()->sourceList();
      while (tmps != NULL) {
         int found = False;
         if (tmps->source()->type() == Background) {
           RoutingTableEntry *tmpr = tmp->nodePtr()->routingTable();
            while ((tmpr != NULL) && (found == False)) {
               NodeListEntry *tmpn = tmpr->children();
               while ((tmpn != NULL) && (found == False)) {
                  if (tmpn->nodePtr()->name() == tmps->source()->address()) 
                              found = True;
                  else tmpn = tmpn->next();
               };
               tmpr = tmpr->next();
            };
         };
         if ((tmps->source()->type() != Background) || (found == True)) {
            tmps->source()->seqNum(0); //reset all seq. numbers
            Data *dd = new Data(tmps->source()->seqNum(), 
                                tmps->source()->type(), 
                                tmps->source()->priority(), 0.0, 
                                tmps->source()->address(), 
                                tmps->source()->nodePtr()); 
            E->insertEvent(bottom, tmps->source(), 
                                       (tmps->source()->type() + 5), 0.0, dd );
         };
         tmps = tmps->next();
      };
      tmp = tmp->next();
   };

   //Initialize the statistics
   MCGroup *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) {
            NodeListEntry *tmp2 = tmpg->headm();
            while (tmp2 != NULL) {

⌨️ 快捷键说明

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