DV算法1

作者:冰蓝沸点
        

    DVSim.h:
       #include     <stdio.h>
#include     <stdlib.h>
#include    <process.h>
#include    <io.h>
#include    <iostream.h>

#define        INF        999
#define        NODES        5
#define     LINKCHANGES     0

/*--------------------------------------------------------------
*    distance_table definition:
*
*    costs[i][j]= path cost from this node to node i
*            when first hop is node j
*--------------------------------------------------------------*/
struct     distance_table
    {
        int costs[NODES][NODES];
    };


#ifdef DVSIM_H
/*------------------------------
*    Trace level
*------------------------------*/
int    TRACE = 1;


/*--------------------------------------
*    distance tables
*    minimum path costs
*    link costs
*--------------------------------------*/
struct     distance_table    dt[NODES];
int     min_cost[NODES][NODES];
int     linkcost[NODES][NODES] = {
        {0, 1, 2, INF, INF},
        {1, 0, INF, 15, 5},
        {2, INF, 0, 1, 2},
        {INF, 15, 1, 0, 10},
        {INF, 5, 2, 10, 0}
};
#else
extern int     TRACE;
extern struct    distance_table dt[NODES];
extern int     min_cost[NODES][NODES];
extern int     linkcost[NODES][NODES];

extern int rpc0 = 0;
extern int rpc1 = 0;
extern int rpc2 = 0;
extern int rpc3 = 0;
extern int rpc4 = 0;

extern int spc0 = 0;
extern int spc1 = 0;
extern int spc2 = 0;
extern int spc3 = 0;
extern int spc4 = 0;

#endif


/*--------------------------------------------------------------
*       rtpkt       Routing packet
*
*    sourceid    source router
*    destid       dest router (must be immediate neighbor)
*    mincost[]    minimum cost to other nodes
*--------------------------------------------------------------*/
struct rtpkt {
        int sourceid;
        int destid;
        int mincost[NODES];
};

/*------------------------------
*    function prototypes
*------------------------------*/
void     rtinit0();
void     rtinit1();
void     rtinit2();
void     rtinit3();
void     rtinit4();


void     rtupdate0 (struct rtpkt *);
void     rtupdate1 (struct rtpkt *);
void     rtupdate2 (struct rtpkt *);
void     rtupdate3 (struct rtpkt *);
void     rtupdate4 (struct rtpkt *);


void     printdt_0 (struct distance_table *);
void     printdt_1 (struct distance_table *);
void     printdt_2 (struct distance_table *);
void     printdt_3 (struct distance_table *);
void     printdt_4 (struct distance_table *);


void    linkhandler0 (int, int);
void     linkhandler1 (int, int);

double    get_clock();
void    tolayer2 (struct rtpkt);



/*    END DVsim.h ------------------------------------------*/



        Node.cpp
/*************************************************************************
*   node0.c    
*
*   Distance Vector Routing Simulation Package
*   Node event handlers
*
*   INITIALIZATION FUNCTION:    rtinit0
*   
*    1. initializes distance table for node 0
*    2. computes initial minimum path costs for node 0
*    3. send update packets to neighbors
*
*
*   UPDATE FUNCTION:         rtupdate0
*
*    Executes when the node receives an updates message:
*    1. receives update from neighbor
*    2. computes new values for distance table
*    3. if any minimum path cost changes, then sends update messages
*       to all neighbors.
*
*
*   TABLE PRINT FUNCTIONS:     printdt_0
*
*    Prints distance table for node 0.
*
*
*   LINK HANDLER FUNCTIONS:    linkhandler_0, linkhandler_1
*
*     These functions are called when the link cost from node 0 to
*    node "linkid" changes from the current value to newcost.
*    To use these, change constant LINKCHANGE in DVsim.c from 0 to 1.
*     Leave these functions empty for the standard project.
*
*-----------------------------------------------------------------------*/
#include     "DVsim.h"

/*------------------------
*    rtinit0
*------------------------*/
void rtinit0()
{
    for(int i = 0; i < NODES; i++)
        for(int j = 0; j < NODES; j++)
            dt[0].costs[i][j] = INF;       //The first step of initializing node0's costs table.
   
    for(i = 0; i < NODES; i++)             //The first step of initializing node0's min_cost table
        min_cost[0][i] = INF;              //The reason of seperating the initiazing process is
                                           //just for easy-coding.

    dt[0].costs[0][0] = min_cost[0][0] = 0;//The second step of initializing node0's min_cost table and
                                           //costs table. Their relationship can be drawn from the analyzing graph
    dt[0].costs[1][1] = min_cost[0][1] = 1;
    dt[0].costs[2][2] = min_cost[0][2] = 2;

    rtpkt pki;
    pki.sourceid = 0;
    for(i = 0; i < NODES; i++)
        pki.mincost[i] = dt[0].costs[i][i];//initializing the data segment in the packet.
    pki.destid = 1;
    tolayer2(pki);
    pki.destid = 2;
    tolayer2(pki);   
    spc0 += 2;
}

/*------------------------
*    rtinit1
*------------------------*/
void rtinit1()
{
    int i;
    for(i = 0; i < NODES; i++)
        for(int j = 0; j < NODES; j++)
            dt[1].costs[i][j] = INF;

    for(i = 0; i < NODES; i++)
        min_cost[1][i] = INF;

    dt[1].costs[0][0] = min_cost[1][0] = 1;
    dt[1].costs[1][1] = min_cost[1][1] = 0;
    dt[1].costs[3][3] = min_cost[1][3] = 15;
    dt[1].costs[4][4] = min_cost[1][4] = 5;

    rtpkt pki;
    pki.sourceid = 1;
    for(i = 0; i < NODES; i++)
        pki.mincost[i] = dt[1].costs[i][i];
    pki.destid = 0;
    tolayer2(pki);
    pki.destid = 3;
    tolayer2(pki);
    pki.destid = 4;
    tolayer2(pki);
    spc1 += 3;
}

/*------------------------
*    rtinit2
*------------------------*/
void rtinit2()
{
    int i;
    for(i = 0; i < NODES; i++)
        for(int j = 0; j < NODES; j++)
            dt[2].costs[i][j] = INF;

    for(i = 0; i < NODES; i++)
        min_cost[2][i] = INF;

    dt[2].costs[0][0] = min_cost[2][0] = 2;
    dt[2].costs[2][2] = min_cost[2][2] = 0;
    dt[2].costs[3][3] = min_cost[2][3] = 1;
    dt[2].costs[4][4] = min_cost[2][4] = 2;

    rtpkt pki;
    pki.sourceid = 2;
    for(i = 0; i < NODES; i++)
        pki.mincost[i] = dt[2].costs[i][i];
    pki.destid = 0;
    tolayer2(pki);
    pki.destid = 3;
    tolayer2(pki);
    pki.destid = 4;
    tolayer2(pki);
    spc2 += 3;
}

/*------------------------
*    rtinit3
*------------------------*/
void rtinit3()
{
    int i;
    for(i = 0; i < NODES; i++)
        for(int j = 0; j < NODES; j++)
            dt[3].costs[i][j] = INF;

    for(i = 0; i < NODES; i++)
        min_cost[3][i] = INF;

    dt[3].costs[1][1] = min_cost[3][1] = 15;
    dt[3].costs[2][2] = min_cost[3][2] = 1;
    dt[3].costs[3][3] = min_cost[3][3] = 0;
    dt[3].costs[4][4] = min_cost[3][4] = 10;

    rtpkt pki;
    pki.sourceid = 3;
    for(i = 0; i < NODES; i++)
        pki.mincost[i] = dt[3].costs[i][i];
    pki.destid = 1;
    tolayer2(pki);
    pki.destid = 2;
    tolayer2(pki);
    pki.destid = 4;
    tolayer2(pki);
    spc3 += 3;
}

/*------------------------
*    rtinit4
*------------------------*/
void rtinit4()
{
    int i;
    for(i = 0; i < NODES; i++)
        for(int j = 0; j < NODES; j++)
            dt[4].costs[i][j] = INF;

    for(i = 0; i < NODES; i++)
        min_cost[4][i] = INF;

    dt[4].costs[1][1] = min_cost[4][1] = 5;
    dt[4].costs[2][2] = min_cost[4][2] = 2;
    dt[4].costs[3][3] = min_cost[4][3] = 10;
    dt[4].costs[4][4] = min_cost[4][4] = 0;

    rtpkt pki;
    pki.sourceid = 4;
    for(i = 0; i < NODES; i++)
        pki.mincost[i] = dt[4].costs[i][i];
    pki.destid = 1;
    tolayer2(pki);
    pki.destid = 2;
    tolayer2(pki);
    pki.destid = 3;
    tolayer2(pki);
    spc4 += 3;
}

/*------------------------
*    rtupdate0
*------------------------*/
void rtupdate0 (struct rtpkt *rcvdpkt)
{
    int i, newWeight;
    bool changed = false;       //check if the mincost has changed.
    rtpkt pku;
    pku.sourceid = 0;
    for(i = 0; i < NODES; i++)
        pku.mincost[i] = min_cost[0][i];    //initializing

    rpc0++;

    for(i = 0; i < NODES; i++)//kernel part of the DVAlgorithm
    {
        newWeight = dt[0].costs[rcvdpkt->sourceid][rcvdpkt->sourceid] + rcvdpkt->mincost[i];//receive a packet, update according to the incoming info.
        if(newWeight < dt[0].costs[i][rcvdpkt->sourceid] && i != 0)
        {
            dt[0].costs[i][rcvdpkt->sourceid] = newWeight;//check if the private costs table should be updated.
            if(newWeight < min_cost[0][i])//check if the public min_cost table should be updated
            {
                pku.mincost[i] = min_cost[0][i] = newWeight;
                changed = true;
            }
        }
    }

    if(changed){
        pku.destid = 1;
        tolayer2(pku);
        pku.destid = 2;
        tolayer2(pku);
        spc0 += 2;
    }
}

/*------------------------
*    rtupdate1
*------------------------*/
void rtupdate1(struct rtpkt *rcvdpkt)
{
    int i, newWeight;
    bool changed = false;
    rtpkt pku;
    pku.sourceid = 1;
    for(i = 0; i < NODES; i++)
        pku.mincost[i] = min_cost[1][i];

    rpc1++;

    for(i = 0; i < NODES; i++)
    {
        newWeight = dt[1].costs[rcvdpkt->sourceid][rcvdpkt->sourceid] + rcvdpkt->mincost[i];
        if(newWeight<dt[1].costs[i][rcvdpkt->sourceid] && i!=1)
        {
            dt[1].costs[i][rcvdpkt->sourceid] = newWeight;
            if(newWeight < min_cost[1][i])
            {
                pku.mincost[i] = min_cost[1][i] = newWeight;
                changed = true;
            }
        }
    }

    if(changed){
        pku.destid = 0;
        tolayer2(pku);
        pku.destid = 3;
        tolayer2(pku);
        pku.destid = 4;
        tolayer2(pku);
        spc1 += 3;
    }
}

/*------------------------
*    rtupdate2
*------------------------*/
void rtupdate2(struct rtpkt *rcvdpkt)
{
    int i, newWeight;
    bool changed = false;
    rtpkt pku;
    pku.sourceid = 2;
    for(i = 0; i < NODES; i++)
        pku.mincost[i] = min_cost[2][i];

    rpc2++;

    for(i = 0; i < NODES; i++)
    {
        newWeight = dt[2].costs[rcvdpkt->sourceid][rcvdpkt->sourceid] + rcvdpkt->mincost[i];
        if(newWeight<dt[2].costs[i][rcvdpkt->sourceid] && i!=2)
        {
            dt[2].costs[i][rcvdpkt->sourceid] = newWeight;
            if(newWeight < min_cost[2][i])
            {
                pku.mincost[i] = min_cost[2][i] = newWeight;
                changed = true;
            }
        }
    }

    if(changed){
        pku.destid = 0;
        tolayer2(pku);
        pku.destid = 3;
        tolayer2(pku);
        pku.destid = 4;
        tolayer2(pku);
        spc2 += 3;
    }
}

/*------------------------
*    rtupdate3
*------------------------*/
void rtupdate3(struct rtpkt *rcvdpkt)
{
    int i, newWeight;
    bool changed = false;
    rtpkt pku;
    pku.sourceid = 3;
    for(i = 0; i < NODES; i++)
        pku.mincost[i] = min_cost[3][i];

    rpc3++;

    for(i = 0; i < NODES; i++)
    {
        newWeight = dt[3].costs[rcvdpkt->sourceid][rcvdpkt->sourceid] + rcvdpkt->mincost[i];
        if(newWeight<dt[3].costs[i][rcvdpkt->sourceid] && i!=3)
        {
            dt[3].costs[i][rcvdpkt->sourceid] = newWeight;
            if(newWeight < min_cost[3][i])
            {
                pku.mincost[i] = min_cost[3][i] = newWeight;
                changed = true;
            }
        }
    }

    if(changed){
        pku.destid = 1;
        tolayer2(pku);
        pku.destid = 2;
        tolayer2(pku);
        pku.destid = 4;
        tolayer2(pku);
        spc3 += 3;
    }
}

/*------------------------
*    rtupdate1
*------------------------*/
void rtupdate4(struct rtpkt *rcvdpkt)
{
    int i, newWeight;
    bool changed = false;
    rtpkt pku;
    pku.sourceid = 4;
    for(i = 0; i < NODES; i++)
        pku.mincost[i] = min_cost[4][i];

    rpc4++;

    for(i = 0; i < NODES; i++)
    {
        newWeight = dt[4].costs[rcvdpkt->sourceid][rcvdpkt->sourceid] + rcvdpkt->mincost[i];
        if(newWeight<dt[4].costs[i][rcvdpkt->sourceid] && i!=4)
        {
            dt[4].costs[i][rcvdpkt->sourceid] = newWeight;
            if(newWeight < min_cost[4][i])
            {
                pku.mincost[i] = min_cost[4][i] = newWeight;
                changed = true;
            }
        }
    }

    if(changed){
        pku.destid = 1;
        tolayer2(pku);
        pku.destid = 2;
        tolayer2(pku);
        pku.destid = 3;
        tolayer2(pku);
        spc4 += 3;
    }
}

/*----------------------------------------------------
*    printdt_0
*
*    Prints distance table at node 0.
*-----------------------------------------------------*/
void printdt_0 (struct distance_table *t)
{
   printf("/t/t ----------------------/n");
   printf("/t/t      |      via     /n");
   printf("/t/t   D0 |    1     2/n");
   printf("/t/t ----|-----------/n");
   printf("/t/t     1| %3d   %3d/n", t->costs[1][1], t->costs[1][2]);
   printf("/t/t     2| %3d   %3d/n", t->costs[2][1], t->costs[2][2]);
   printf("/t/tdest 3| %3d   %3d/n", t->costs[3][1], t->costs[3][2]);
   printf("/t/t     4| %3d   %3d/n", t->costs[4][1], t->costs[4][2]);
   printf("/t/t ----------------------/n");
}

/*----------------------------------------------------
*    printdt_1
*
*    Prints distance table at node 1.
*-----------------------------------------------------*/
void printdt_1 (struct distance_table *t)
{
   printf("/t/t --------------------------/n");
   printf("/t/t      |         via     /n");
   printf("/t/t   D1 |    0     3     4/n");
   printf("/t/t ----|-----------------/n");
   printf("/t/t     0| %3d   %3d   %3d/n", t->costs[0][0], t->costs[0][3], t->costs[0][4]);
   printf("/t/t     2| %3d   %3d   %3d/n", t->costs[2][0], t->costs[2][3], t->costs[2][4]);
   printf("/t/tdest 3| %3d   %3d   %3d/n", t->costs[3][0], t->costs[3][3], t->costs[3][4]);
   printf("/t/t     4| %3d   %3d   %3d/n", t->costs[4][0], t->costs[4][3], t->costs[4][4]);
   printf("/t/t --------------------------/n");
}

/*----------------------------------------------------
*    printdt_2
*
*    Prints distance table at node 2.
*-----------------------------------------------------*/
void printdt_2 (struct distance_table *t)
{
   printf("/t/t --------------------------/n");
   printf("/t/t      |         via     /n");
   printf("/t/t   D2 |    0     3     4/n");
   printf("/t/t ----|-----------------/n");
   printf("/t/t     0| %3d   %3d   %3d/n", t->costs[0][0], t->costs[0][3], t->costs[0][4]);
   printf("/t/t     1| %3d   %3d   %3d/n", t->costs[1][0], t->costs[1][3], t->costs[1][4]);
   printf("/t/tdest 3| %3d   %3d   %3d/n", t->costs[3][0], t->costs[3][3], t->costs[3][4]);
   printf("/t/t     4| %3d   %3d   %3d/n", t->costs[4][0], t->costs[4][3], t->costs[4][4]);
   printf("/t/t --------------------------/n");
}

/*----------------------------------------------------
*    printdt_3
*
*    Prints distance table at node 3.
*-----------------------------------------------------*/
void printdt_3 (struct distance_table *t)
{
   printf("/t/t --------------------------/n");
   printf("/t/t      |         via     /n");
   printf("/t/t   D3 |    1     2     4/n");
   printf("/t/t ----|-----------------/n");
   printf("/t/t     0| %3d   %3d   %3d/n", t->costs[0][1], t->costs[0][2], t->costs[0][4]);
   printf("/t/t     1| %3d   %3d   %3d/n", t->costs[1][1], t->costs[1][2], t->costs[1][4]);
   printf("/t/tdest 2| %3d   %3d   %3d/n", t->costs[2][1], t->costs[2][2], t->costs[2][4]);
   printf("/t/t     4| %3d   %3d   %3d/n", t->costs[4][1], t->costs[4][2], t->costs[4][4]);
   printf("/t/t --------------------------/n");
}

/*----------------------------------------------------
*    printdt_4
*
*    Prints distance table at node 4.
*-----------------------------------------------------*/
void printdt_4 (struct distance_table *t)
{
   printf("/t/t --------------------------/n");
   printf("/t/t      |         via     /n");
   printf("/t/t   D4 |    1     2     3/n");
   printf("/t/t ----|-----------------/n");
   printf("/t/t     0| %3d   %3d   %3d/n", t->costs[0][1], t->costs[0][2], t->costs[0][3]);
   printf("/t/t     1| %3d   %3d   %3d/n", t->costs[1][1], t->costs[1][2], t->costs[1][3]);
   printf("/t/tdest 2| %3d   %3d   %3d/n", t->costs[2][1], t->costs[2][2], t->costs[2][3]);
   printf("/t/t     3| %3d   %3d   %3d/n", t->costs[3][1], t->costs[3][2], t->costs[3][3]);
   printf("/t/t --------------------------/n");
}


/*--------------------------------------------------------------
*    linkhandler0 & linkhandler1
*---------------------------------------------------------------*/
void linkhandler0 (int linkid, int newcost) {}
void linkhandler1 (int linkid, int newcost) {}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值