路由模拟——设计方案的实现(3)

来源:互联网 发布:淘宝客贷变成了网商贷 编辑:程序博客网 时间:2024/06/05 14:26
 

3,创建网络

方法名称:              Net::ReCreate

方法参数:            

返回值:              

方法的目的:                重新创建网络(包含首次创建)。如果网络信息发生改变,则释放内存资源,重新获得网络信息,构建网络数据结构pNodeArray。同时此方法在获得网络信息的同时,填写一个重要的中间表:路由器标号与地址对映表。本方案实现的方式仍为简略方式,复杂网络则pNodeArray需构建为完成的邻接表。实现代码省略。

可能的出错:           文件操作失败:系统退出。

// 创建网络

void Net::ReCreate(void)

{

     if(IsChanged())

     {       

          AbsRout *pRout=NULL;

          AbsLine *pLine=NULL;

          std::fstream file;

          std::fstream indexFile;

         char *fileName="routaddress.txt";

         char *indexFileName="IndexAddr.txt";

          unsigned int addr[IP_ADDRESS_LENGTH];

 

         //清空资源

          Clear();

         //获得网络结构信息

          GetNetInfor();

         pNodeArray = new NetNode[routNum];

 

          //打开存储“路由器地址表”文件

          file.open(fileName,std::ios_base::in|std::ios_base::out);

          if(file.fail())
          {
              std::cout<<"/n/n文件("<<fileName<<") 打开失败./n/n";
              char ch;
              std::cin>>ch;
              exit(-1);
           }

          //打开存储“路由号与地址对应表”文件

          indexFile.open(indexFileName,std::ios_base::in|std::ios_base::out);

          if(indexFile.fail())
          {
              std::cout<<"/n/n文件("<<indexFileName<<") 打开失败./n/n";
              char ch;
              std::cin>>ch;
              exit(-1);
           }

          for(int i=0;i<routNum;i++)

         {//网络的初始化

              indexFile<<i<<'/t';

              for(int j=0;j<IP_ADDRESS_LENGTH;j++)

              {

                   file>>addr[j];

                   indexFile<<addr[j]<<' ';

              }

              indexFile<<'/n';

              pRout = new CentralRout();

              pRout->SetSelfAddress(addr);

              (pNodeArray+i)->pRout = pRout;

              pLine = new Fiber();

              (pNodeArray+i)->pLine = pLine;

         }//end of for

 

          file.close();

          indexFile.close();

     }

     /*注释内容为网络邻接表的实现方式,邻接表可以表示任意复杂的网络。

       但这里从简省略,网络的信息仍依靠矩阵与向量保存。待系统需创建

       任意复杂的网络的时候,可对代码进行修改和扩充。

       (这一部分代码略 */

}

 

三,路由计算与路由表的建立

这一部分为比较核心的内容。路由计算的建立,与路由计算的实现是分离的。路由计算的实现我们再另外的文档中再单独展示。当把路由计算与其实现完成整合以后,就可以由路由器完成路由表的填写。

 

1设置路由计算方法

方法名称:              AbsRout::SetComputeMethods

方法参数:             void _routCompute(int **,int **,int **&,int,int)

返回值:              

方法的目的:           把路由计算的实现(参数_ routCompute)与路由器内路由计算的指针完成连接。

可能的出错:          

//设置路由计算方法

void AbsRout::SetComputeMethods(void _routCompute(int **,int **,int **&,int,int))

{

     RoutCompute = _routCompute;

}

2 路由计算的建立

方法名称:              Net::RoutComputeBuilder

方法参数:            

返回值:              

方法的目的:           路由计算的完整建立过程。包括实现路由计算的设置、洪泛方式送网络信息到路由器、调用路由表填写方法。

可能的出错:          

// 路由计算的建立

void Net::RoutComputeBuilder(void)

{

     if(pNodeArray!=NULL)

     {

          for(int i=0;i<routNum;i++)

         {

              (pNodeArray+i)->pRout->SetComputeMethods(ComputeMethods);

              (pNodeArray+i)->pRout->SetNetInfor(netArray,valArray,routNum);

         }//

          for(int i=0;i<routNum;i++)

              (pNodeArray+i)->pRout->SetRoutTable();

     }//

}

 

注释:函数ComputeMethods由dll文件实现,为路由算法的主题部分。

 

                                                           <未完>

 

原创粉丝点击