-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.c
138 lines (119 loc) · 3.53 KB
/
router.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "router.h"
// Sets all weights to default MAX_WEIGHT. Then randomly generates weights from router to neighbor
void init_routers(router_t * routers)
{
for (int r = 0; r < N_NEIGHBORS; r++) {
routers[r].id = r;
// Preset costs all to 9999
for (int i = 0; i < N_NEIGHBORS; i++)
{
for (int j = 0; j < N_NEIGHBORS; j++)
{
routers[r].cost[i][j] = MAX_WEIGHT;
// r->distances[i][j] = MAX_WEIGHT;
}
}
}
// Initialize the distance vector for a particular router (want it to be symmetric across routers)
for (int r =0; r < N_NEIGHBORS; r++) {
for (int neighbor = 0; neighbor < N_NEIGHBORS; neighbor++)
{
if (neighbor == r)
{
routers[r].cost[r][r] = 0; // 0 along diagonal
// r->distances[r->id][r->id] = 0;
}
else
{
routers[r].cost[r][neighbor] = rand() / 100000000 + 1;
routers[neighbor].cost[neighbor][r] = routers[r].cost[r][neighbor]; // make it symmetric
// r->distances[r->id][neighbor] = r->cost[r->id][neighbor];
}
}
}
// Set random weights for router;
// Make the cost matrix symmetrix
}
void table_divider()
{
printf("+-----------------+");
for (int i = 0; i < N_NEIGHBORS; i++)
{
printf("--------+");
}
printf("\n");
}
// Print out the distance vector table
void display_router(router_t *r)
{
// LEAST cost table
// print header
table_divider();
printf("| Min Cost (id %d) |", r->id);
for (int i = 0; i < N_NEIGHBORS; i++)
{
printf(" %d |", i);
}
printf("\n");
table_divider();
// Print rows
for (int row = 0; row < N_NEIGHBORS; row++)
{
printf("| %-4d |", row);
// Print columns
for (int col = 0; col < N_NEIGHBORS; col++)
{
printf(" %-4d |", r->cost[row][col]);
}
printf("\n");
}
table_divider();
// DISTANCE table
// print header
// table_divider();
// printf("| Distance (id %d) |", r->id);
// for (int i = 0; i < N_NEIGHBORS; i++)
// {
// printf(" %d |", i);
// }
// printf("\n");
// table_divider();
// // Print rows
// for (int row = 0; row < N_NEIGHBORS; row++)
// {
// printf("| %-4d |", row);
// // Print columns
// for (int col = 0; col < N_NEIGHBORS; col++)
// {
// printf(" %-4d |", r->distances[row][col]);
// }
// printf("\n");
// }
// table_divider();
}
void print_weights(router_t * r) {
for (int neighbor = 0; neighbor < N_NEIGHBORS; neighbor++)
{
printf("Costs: %d->%d: %-4d\n", r->id, neighbor, r->cost[r->id][neighbor]);
// printf("Distances: %d->%d: %-4d\n", r->id, neighbor, r->distances[r->id][neighbor]);
}
}
// This function closes all the file descriptors associated with this router
void close_router(router_t *r)
{
// Close the read end from other routers
close(r->r_readfd);
// Close all the write descriptors to other routers
for (int i = 0; i < N_NEIGHBORS; i++)
{
if (i != r->id) { // router doesn't have file descriptor to itself
close(r->r_writefds[i]);
}
}
// Close file descriptors involving talking to main
close(r->shell_readfd);
close(r->shell_writefd);
}