-
Notifications
You must be signed in to change notification settings - Fork 0
/
node3.c
118 lines (96 loc) · 2.84 KB
/
node3.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include "helper.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;
struct distance_table
{
int costs[4][4];
} dt3;
int connectcosts3[4] = { 7, 999, 2, 0 };
struct vector_table vt3;
/* students to write the following two routines, and maybe some others */
void rtinit3()
{
// initialize vector table
for (unsigned int i = 0; i < 4; i++) {
for (unsigned int j = 0; j < 4; j++) {
vt3.costs[i][j] = 999;
}
}
copy_vector(vt3.costs[3], connectcosts3, 4);
vt3.next_hops[0] = 0;
vt3.next_hops[1] = 999;
vt3.next_hops[2] = 2;
vt3.next_hops[3] = 999;
// send to direct neighbors
struct rtpkt pkt;
pkt.sourceid = 3;
for (unsigned int i = 0; i < 4; i++) {
pkt.mincost[i] = vt3.costs[3][i];
}
for (unsigned int i = 0; i < 3; i++) {
pkt.destid = i;
tolayer2(pkt);
}
printf("NODE3 VECTOR TABLE:\n");
printvt(&vt3);
printf("-------------------\n");
}
void rtupdate3(rcvdpkt)
struct rtpkt *rcvdpkt;
{
// copy vector to appropriate entry based on neighbor rcvd from
int neighbor = rcvdpkt->sourceid;
for (unsigned int i = 0; i < 4; i++) {
vt3.costs[neighbor][i] = rcvdpkt->mincost[i];
}
// copy node dv to a temporary vector
int node3_dv_copy[4];
for (unsigned int dest = 0; dest < 4; dest++) {
node3_dv_copy[dest] = vt3.costs[3][dest];
}
for (unsigned int dest = 0; dest < 4; dest++) {
int min_to_dest = node3_dv_copy[dest];
for (unsigned int neighbor = 0; neighbor < 4; neighbor++) {
int new_cost = node3_dv_copy[neighbor] + vt3.costs[neighbor][dest];
if (new_cost < min_to_dest) {
node3_dv_copy[dest] = new_cost;
vt3.next_hops[dest] = vt3.next_hops[neighbor];;
}
}
}
int equal = compare_vectors(node3_dv_copy, vt3.costs[3], 4);
if (equal == 0) {
// update vector table
copy_vector(vt3.costs[3], node3_dv_copy, 4);
// send updated dv to all neighbors
struct rtpkt pkt;
pkt.sourceid = 3;
copy_vector(pkt.mincost, vt3.costs[3], 4);
// send to each neighbor
for (unsigned int i = 0; i < 3; i++) {
pkt.destid = i;
tolayer2(pkt);
}
}
printf("NODE3 VECTOR TABLE (AFTER UPDATE):\n");
printvt(&vt3);
printf("---------------------\n");
}
printdt3(dtptr)
struct distance_table *dtptr;
{
printf(" via \n");
printf(" D3 | 0 2 \n");
printf(" ----|-----------\n");
printf(" 0| %3d %3d\n",dtptr->costs[0][0], dtptr->costs[0][2]);
printf("dest 1| %3d %3d\n",dtptr->costs[1][0], dtptr->costs[1][2]);
printf(" 2| %3d %3d\n",dtptr->costs[2][0], dtptr->costs[2][2]);
}