-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode2.c
More file actions
101 lines (77 loc) · 1.96 KB
/
Copy pathnode2.c
File metadata and controls
101 lines (77 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
extern struct rtpkt {
int sourceid; /* id of sending router sending this pkt */
int destid; /* id of router to which pkt being sent
(must be an immediate neighbor) */
int mincost[4]; /* min cost to node 0 ... 3 */
};
extern int TRACE;
extern int YES;
extern int NO;
int connectcosts2[4] = { 3, 1, 0, 2 };
struct distance_table
{
int costs[4][4];
} dt2;
/* students to write the following two routines, and maybe some others */
void sendUpdate2()
{
connectcosts2[0] = dt2.costs[2][0];
connectcosts2[1] = dt2.costs[2][1];
connectcosts2[2] = dt2.costs[2][2];
connectcosts2[3] = dt2.costs[2][3];
struct rtpkt to0, to1, to3;
to0.sourceid = 2;
to0.destid = 0;
to1.sourceid = 2;
to1.destid = 1;
to3.sourceid = 2;
to3.destid = 3;
int i;
for (i = 0; i < 4; ++i)
{
to0.mincost[i] = connectcosts2[i];
to1.mincost[i] = connectcosts2[i];
to3.mincost[i] = connectcosts2[i];
}
tolayer2(to0);
tolayer2(to1);
tolayer2(to3);
}
void rtinit2()
{
dt2.costs[2][0] = 3;
dt2.costs[2][1] = 1;
dt2.costs[2][2] = 0;
dt2.costs[2][3] = 2;
sendUpdate2();
}
void rtupdate2(rcvdpkt)
struct rtpkt *rcvdpkt;
{
int i;
unsigned char update = 0;
for (i = 0; i < 4; ++i)
{
if ((rcvdpkt->mincost[i] + dt2.costs[2][rcvdpkt->sourceid]) < dt2.costs[0][i])
{
dt2.costs[2][i] = rcvdpkt->mincost[i] + dt2.costs[2][rcvdpkt->sourceid];
update = 1;
}
}
if (1 == update)
sendUpdate2();
}
printdt2(dtptr)
struct distance_table *dtptr;
{
printf(" via \n");
printf(" D2 | 0 1 3 \n");
printf(" ----|-----------------\n");
printf(" 0| %3d %3d %3d\n",dtptr->costs[0][0],
dtptr->costs[0][1],dtptr->costs[0][3]);
printf("dest 1| %3d %3d %3d\n",dtptr->costs[1][0],
dtptr->costs[1][1],dtptr->costs[1][3]);
printf(" 3| %3d %3d %3d\n",dtptr->costs[3][0],
dtptr->costs[3][1],dtptr->costs[3][3]);
}