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

📄 rout.c

📁 一个linux下的各种组播路由算法编程
💻 C
📖 第 1 页 / 共 3 页
字号:
         nodes++;
         RoutingTableEntry *rout = tmp1->nodePtr()->routingTable();
         int found = False;
         while ((rout != NULL) && (found == False)) {
           if ((rout->address() == addr) && (rout->source() == source)) 
                                                          found = True;
           else rout = rout->next();
         };
         NodeListEntry *tmp = rout->children();
         while (tmp != NULL) {
             AdjacencyListEntry *adj = tmp1->nodePtr()->adjacentNodes();
             int found2 = False;
             while ((adj != NULL) && (found2 == False)) {
                if (adj->nodePtr() == tmp->nodePtr()) found2 = True;
                else adj = adj->next();
             };
             if (adj != NULL) {
                if (fn == PEAK) totalCost += (adj->peak() - pk);
                else totalCost += (adj->average() - avg);
             };
             tmp = tmp->next();
         };
         tmp2 = tmp1->next();
         delete tmp1;
         tmp1 = tmp2;
      };
      if ((DBV == True) && (dbViolation == True)) {
         removeTree(source, addr);
         return(DBVIOL);
      }
      else return(totalCost);
   }
   else return(NOGROUP);
 };
};
         
int TheNodeList::results(Node *source, int addr, Node *current, 
                          Node *dest, double &delay, 
                          int &hops, int &gotit) {

   int state = True;
   RoutingTableEntry *rout = current->routingTable();
   int found = False;
   while ((rout != NULL) && (found == False)) {
      if ((rout->address() == addr) && (rout->source() == source)) 
                                                     found = True;
      else rout = rout->next();
   };
   NodeListEntry *tmp = rout->children();
   while ((tmp != NULL) && (gotit == False)) {
      if (tmp->nodePtr() != dest) {
        int state2 = 
	  results(source, addr, tmp->nodePtr(), dest, delay, hops, gotit);
        if (state2 == False) state = False;
      }   
      else gotit = True;
      if (gotit == True) {
         hops++;
         AdjacencyListEntry *adj = current->adjacentNodes();
         while (adj->nodePtr() != tmp->nodePtr()) adj = adj->next(); 
         delay += adj->delay();
         if ((fn == PEAK) && (adj->peak() > 
			      (adj->linkCapacity() * ADMITRATIO)))
           state = False;
         if ((fn == AVERAGE) && (adj->average() > 
				 (adj->linkCapacity() * ADMITRATIO)))
           state = False;
      };
      tmp = tmp->next();
   };
   return(state);
};

int TheNodeList::prune(MCGroup *group, NodeListEntry **tree, Node *current, 
                    Node *source, int addr, Node *parent, double peak,
                    double avg) {

//This function is to be used by the Bellman Ford routing algorithm

   int status = False;

   //First locate the routing table entry corresponding to the given group and
   //source
   RoutingTableEntry *rout = current->routingTable();
   RoutingTableEntry *prout = NULL;
   int found = False;
   while ((rout != NULL) && (found == False)) {
      if ((rout->address() == addr) && (rout->source() == source)) found = True;
      else {
          prout = rout;
          rout = rout->next();
      };
   };
      

   //recursively prune the nodes down stream from current
   NodeListEntry *tmp = rout->children();
   NodeListEntry *prev = NULL;
   while (tmp != NULL) {
       int state = prune(group, tree, tmp->nodePtr(), source, addr, 
                         current, peak, avg);
       if (state == True) {
           rout->removeDestination(tmp->nodePtr());
           if (prev != NULL) tmp = prev->next();
           else tmp = rout->children();
       }
       else {
         prev = tmp;
         tmp = tmp->next();
       };
   };
   
   //If the node is a leaf node
   if (rout->children() == NULL) {

      //check if it is a group member
      tmp = group->headm();
      found = False;
      while ((tmp != NULL) && (found == False)) {
         if (tmp->nodePtr() == current) found = True;
         else tmp = tmp->next();
      };

      //if not delete it from the tree
      if ((found == False) && (current != source)) {

	 status = True;

         //adjust link weights
         if (parent != NULL) {
            AdjacencyListEntry *adj = parent->adjacentNodes();
            while (adj->nodePtr() != current) adj = adj->next();
            double wght = adj->peak() - peak; 
            adj->peak(wght);
            double average = adj->average() - avg;
            adj->average(average);
         };

         //delete the routing entry for current
         if (prout == NULL) current->routingTable(rout->next());
         else prout->next(rout->next()); 
         delete rout;

         //delete current from the tree
         tmp = *tree;
         NodeListEntry *tmp2 = NULL;
         found = False;
         while ((tmp != NULL) && (found == False)) {
            if (tmp->nodePtr() == current) found = True;
            else {
               tmp2 = tmp;
               tmp = tmp->next();
            };
         };
         if (found == True) {
            if (tmp == *tree) *tree = tmp->next();
            else tmp2->next(tmp->next());
            delete tmp;
         };
      };
   };
   return(status);
};

double TheNodeList::routerPIM(Node *source, int addr, double &d, 
                              double &maxd, double &mind, double &h, 
			      double &nodes) {

   double totalCost = 0;
   nodes = 0;

   //First delete any previous routing for the this group and source set.
   removeTree(source, addr);

   //Locate the source to get the peak rate
   SourceList *ss = source->sourceList();
   int found = False;
   while ((ss != NULL) && (found == False)) {
       if ((ss->source()->type() != Background) &&
           (ss->source()->address() == addr)) found = True;
       else ss = ss->next();
   };
   double pk = ss->source()->peak();
   double avg = ss->source()->average();

   //Locate the MC group that contains the destination set
   MCGroup *group = groupsHd;
   while ((group !=  NULL) && (group->address() != addr))   
      group = group->next();
   
   if (group != NULL) {

       double *cost_matrix;
       cost_matrix = new double[num];
       int *hop_matrix;
       hop_matrix = new int[num];
       int i;
       for (i = 0; i < num; i++) *(cost_matrix + i) = DBL_MAX;

      //The source is the first member in the tree
      NodeListEntry *tree = new NodeListEntry;
      tree->nodePtr(source);
      source->addRoutingEntry(addr, source);

      //For each member of the multicast group, if it is not already in the
      //tree, then find the shortest path from that member to any node already
      //in the tree
      NodeListEntry *tmp1 = group->headm();
      while (tmp1 != NULL) {
      
         //check if already in the tree
         NodeListEntry *tmp2 = tree;
         int inTree = False;
         while ((tmp2 != NULL) && (inTree == False)) {
            if (tmp2->nodePtr() == tmp1->nodePtr()) inTree = True;
            else tmp2 = tmp2->next();
         };

         if (inTree == False) {  //if not already in tree       

            //tmp1 is the first member in tree2, tree2 is a temporary tree
            tmp2 = new NodeListEntry;
            tmp2->nodePtr(tmp1->nodePtr());
            NodeListEntry *tree2 = tmp2;
            tmp1->nodePtr()->addRoutingEntry(addr, source);
            *(cost_matrix + tmp1->nodePtr()->name()) = 0;
            *(hop_matrix + tmp1->nodePtr()->name()) = 0;

            Node *lastNode = tmp1->nodePtr();
            int done = False;
            while (done == False) {
               tmp2 = tree2;   //for each node already in the tree2
               double bestCost = DBL_MAX;
	       int bestHop;
               Node *bestDest, *bestConn;
               Node *bestSource;
               AdjacencyListEntry *bestAdj;

               while (tmp2 != NULL) {

                 //find the routing table entry for the given group & source
                  RoutingTableEntry  *rout = tmp2->nodePtr()->routingTable();
                  int found = False;
                  while ((rout != NULL) && (found == False)) {
                     if ((rout->address() == addr) && (rout->source() == source))
                       found = True;
                     else rout = rout->next();
                  };

                  //scan the adjacency list for the least cost (weight) link
                  //same as Dijkstra
                  AdjacencyListEntry *adj = tmp2->nodePtr()->adjacentNodes();
                  while (adj != NULL) {
                     double weight;
		     if (fn == PEAK) weight = adj->peak();
		     else if (fn == AVERAGE) weight = adj->average();
		     NodeListEntry *tmp3 = tree2;
		     found = False;
		     while ((tmp3 != NULL) && (found == False)) {
		       if (tmp3->nodePtr() == adj->nodePtr()) found = True;
		       else tmp3 = tmp3->next();
		     };

		     int sat = False;
		     AdjacencyListEntry *adj2 = 
		       adj->nodePtr()->adjacentNodes();
		     while (adj2->nodePtr() != tmp2->nodePtr())
		       adj2 = adj2->next();
		     if ((fn == PEAK) && ((adj2->peak() + pk) >
			 (adj2->linkCapacity() * ADMITRATIO))) sat = True;
		     else if ((fn == AVERAGE) && ((adj2->average() + avg) >
			      (adj2->linkCapacity() * ADMITRATIO))) sat = True;
		     if ((found == False) && (sat == False)) {
		       switch (obj) {
		       case PLAIN:
			 if ((*(cost_matrix + tmp2->nodePtr()->name()) + 
			      weight) < bestCost) {
                           bestCost =  *(cost_matrix + tmp2->nodePtr()->name())
                                       + weight;
                           bestDest = adj->nodePtr();
                           bestSource = tmp2->nodePtr();
			   bestHop = *(hop_matrix + tmp2->nodePtr()->name())
                                     + 1; 
			   bestAdj = adj2;
                         };
			 break;
		       case MULT:
			 if ((*(cost_matrix + tmp2->nodePtr()->name()) + 
			      weight * (*(hop_matrix + tmp2->nodePtr()->name())
					  + 1)) < bestCost) {
                           bestCost =  *(cost_matrix + tmp2->nodePtr()->name())
                                       + weight * 
				       (*(hop_matrix + tmp2->nodePtr()->name())
				       + 1);
                           bestDest = adj->nodePtr();
                           bestSource = tmp2->nodePtr();
			   bestHop = *(hop_matrix + tmp2->nodePtr()->name())

⌨️ 快捷键说明

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