-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
149 lines (134 loc) · 4.39 KB
/
commands.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
139
140
141
142
143
144
145
146
147
148
149
#include <stdlib.h>
#include "utils.c"
#include "shell.h"
// This function takes a reference to an argument and attempts to return a router pointer based on the inputted id
// Returns 1 if conversion is successful, otherwise 0
int get_router(shell_state *shell, router_t **router, char *arg)
{
int router_id;
if (str2int(&router_id, arg, 10) == STR2INT_SUCCESS)
{
if (router_id < N_NEIGHBORS)
{
*router = &shell->routers[router_id];
return 1;
}
else
{
printf("Router out of bounds (max id %d)\n", N_NEIGHBORS - 1);
}
}
else
{
printf("Could not parse router ID. Did you input an integer?\n");
}
return -1;
}
void help()
{
// printf()
printf("# of routers: %d\n\n", N_NEIGHBORS);
printf("COMMANDS:\n");
printf("1. List commands\n usage: `help`\n\n");
printf("2. Start simulation (uses randomly initialized weights)\n usage: `start`\n\n");
printf("3. List router IDs\n usage: `lr`\n\n");
printf("4. List weights/costs from router to its neighbors\n usage: `lw <router id>`\n\n");
printf("5. Set weights/costs for a router\n usage: `update <router id> <w1> <w2> ... <wn>`\n\n");
printf("6. Display router's current distance vector\n usage: `display <router id>`\n\n");
printf("7. Display messages exchanged since last update\n usage: `n_messages`\n\n");
printf("8. Exit\n usage: `exit`\n\n");
}
void display_router_cmd(shell_state *shell, char arguments[MAX_ARGS][MAX_ARG_LEN], int n_args)
{
if (n_args != 1)
{
printf("Expected one argument.\n");
return;
}
router_t *r = NULL;
if (get_router(shell, &r, arguments[0]) > 0)
{
if (shell->sim_active == 0) {
display_router(r);
} else {
main2r_msg msg = new_main2r(DISPLAY);
write(shell->routers_writefd[r->id], &msg, sizeof(msg));
wait_print();
}
}
}
void list_weights_cmd(shell_state *shell, char arguments[MAX_ARGS][MAX_ARG_LEN], int n_args)
{
if (n_args != 1)
{
printf("Expected one argument.\n");
return;
}
router_t *r = NULL;
if (get_router(shell, &r, arguments[0]) > 0)
{
if (shell->sim_active == 0) {
print_weights(r);
} else {
main2r_msg msg = new_main2r(LIST_WEIGHTS);
msg.command = LIST_WEIGHTS;
write(shell->routers_writefd[r->id], &msg, sizeof(msg));
wait_print();
}
}
}
void update_weights(shell_state *shell, char arguments[MAX_ARGS][MAX_ARG_LEN], int n_args)
{
if (n_args != N_NEIGHBORS + 1)
{
printf("Expected %d arguments.\n", N_NEIGHBORS);
printf("update <router id> <w0> <w1> <w2> ... <wn>\n");
return;
}
router_t *r = NULL;
if (get_router(shell, &r, arguments[0]) > 0)
{
// Parse the weights from the arg string
int new_weights[MAX_ARGS];
for (int i = 0; i < MAX_ARGS; i++) {
new_weights[i] = 0;
}
for (int arg = 1; arg < n_args; arg++)
{
// Attempt to parse each weight
int input_weight;
if (str2int(&input_weight, arguments[arg], 10) == STR2INT_SUCCESS)
{
new_weights[arg - 1] = input_weight;
// printf("got weight int %d\n", input_weight);
}
else
{
printf("Could not parse edge weight. Did you input an integer?\n");
break;
}
}
printf("\n");
// Copy over the parsed weights
if (shell->sim_active == 1) {
main2r_msg msg = new_main2r(UPDATE);
memcpy(&msg.args, new_weights, sizeof(new_weights));
// We send the entire MAX_ARGS length array since the other side only takes what it needs
write(shell->routers_writefd[r->id], &msg, sizeof(msg));
} else {
// We take a subset of the arguments
memcpy(&r->cost[r->id], new_weights, sizeof(int) * N_NEIGHBORS);
}
}
}
void exit_processes(shell_state *shell, char arguments[MAX_ARGS][MAX_ARG_LEN], int n_args) {
if (n_args != 0)
{
printf("Expected no arguments.\n");
return;
}
main2r_msg msg = new_main2r(EXIT);
for (int r = 0; r < N_NEIGHBORS; r++) {
write(shell->routers_writefd[r], &msg, sizeof(msg));
}
}