-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
355 lines (313 loc) · 9.04 KB
/
node.c
File metadata and controls
355 lines (313 loc) · 9.04 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* This file implements the main process for a router node.
*
* @author Jeffrey Bromen
* @date 4/21/17
* @info Systems and Networks II
* @info Project 3
*/
#include <pthread.h>
#ifdef __APPLE__
#include <mach/semaphore.h>
#include <mach/task.h>
#define sem_init(a,b,c) semaphore_create(mach_task_self(), (semaphore_t *)a, SYNC_POLICY_FIFO, c)
#define sem_destroy(a) semaphore_destroy(mach_task_self(), *((semaphore_t *)a))
#define sem_post(a) semaphore_signal(*((semaphore_t *)a))
#define sem_wait(a) semaphore_wait(*((semaphore_t *)a))
#define sem_t semaphore_t
#else
#include <semaphore.h>
#endif
#include "lsNetwork.h"
#include "lsPacket.h"
#include "lsGraph.h"
#include "lsDijkstra.h"
/**
* Thread function for managing incoming and outgoing link-state packets.
*
* @param param - integer pointer to socket file descriptor
*/
void *networkThread(void *param);
/**
* Thread function for periodically changing a neighboring edge cost.
*
* @param param - character pointer to local router label
*/
void *dynamicThread(void *param);
/**
* Parses the command line arguments and stores the results in the parameters
*
* @param argc - number of arguments
* @param argv - argument vector
* @param label - label of local router
* @param port - local port number
* @param numRouter - number of routers in the network
* @param filename - file name of neighbor discovery file
* @param dynamic - flag indicating whether dynamic option was enabled
*
* @return - 0 if success, -1 if error
*/
int parseCommandLine(int argc, char **argv, char *label, int *port, int *numRouters, char **filename, int *dynamic);
/**
* Initializes the socket and data structures that are used in the program.
*
* @param fd - socket file descriptor
* @param port - local port number
* @param numRouters - number of routers in the network
* @param filename - file name of neighbor discovery file
* @param label - label of local router
*
* @return - 0 if success, -1 if error
*/
int initialization(int *fd, int port, int numRouters, char *filename, char label);
/**
* Creates and starts the network thread.
*
* @param fd - socket file descriptor
*
* @return - 0 if success, -1 if error
*/
int startNetworkThread(int *fd);
/**
* Creates and starts the dynamic thread.
*
* @param label - label of local router
*
* @return - 0 if success, -1 if error
*/
int startDynamicThread(char *label);
// Graph of all nodes and edges in the network
struct Graph *graph;
// List containing the neighbor info read from file
struct NeighborList *neighbors;
// Queue containing packets waiting to be sent
struct FifoQueue *sendQueue;
// Queue containing packets waiting to be processed
struct FifoQueue *recvQueue;
// Semaphores for synchronizing threads
sem_t sendLock;
sem_t recvLock;
sem_t dynamLock;
int main(int argc, char **argv)
{
int fd, port, numRouters, dynamic, dLock;
char label, *filename;
char recvBuffer[LS_PACKET_SIZE];
// Parse command line arguments to get parameters and set dynamic flag
if (parseCommandLine(argc, argv, &label, &port, &numRouters, &filename, &dynamic) < 0)
exit(EXIT_FAILURE);
// Initialize socket and data structures
if (initialization(&fd, port, numRouters, filename, label) < 0)
exit(EXIT_FAILURE);
// Start network thread
if (startNetworkThread(&fd) < 0)
exit(EXIT_FAILURE);
// Initialization finished, give other processes time to set up before continuing
printf("Press enter to continue after all nodes are running.\n");
getchar();
// Start dynamic change thread if dynamic flag is set
if (dynamic)
{
if (startDynamicThread(&label) < 0)
exit(EXIT_FAILURE);
dLock = 1;
}
else
dLock = 0;
// The main loop where all the processing occurs
while (1)
{
// Process recveived packets in received queue until empty
while (!isEmptyQueue(recvQueue))
{
// Pop packet from queue
sem_wait(&recvLock);
pop(recvQueue, recvBuffer);
sem_post(&recvLock);
// Update the graph
addEdgeFromPacket(graph, recvBuffer);
// If hop count greater than 0 after decrementing:
if (decrementHopCount(recvBuffer) > 0)
{
// Push packet to send queue to be sent on network thread
sem_wait(&sendLock);
push(sendQueue, recvBuffer);
sem_post(&sendLock);
}
}
// If all received packets are processed and the graph
// has been changed since the last shortest path calculation:
if (isEmptyQueue(recvQueue) && graph->updated)
{
// Calculate the shortest path and print the forwarding table
dijkstra(graph, label);
// If dynamic thread is initially blocked, allow it to continue
if (dLock)
{
sem_post(&dynamLock);
dLock = 0;
}
}
}
}
void *networkThread(void *param)
{
int fd = *((int *) param);
int recvLen;
char sendBuffer[LS_PACKET_SIZE];
char recvBuffer[LS_PACKET_SIZE];
// Main loop where the network thread behavior is determined
while (1)
{
// Forward packets in send queue until queue is empty
while (!isEmptyQueue(sendQueue))
{
sem_wait(&sendLock);
// Pop packet from send queue
pop(sendQueue, sendBuffer);
sem_post(&sendLock);
// Send packet to all adjacent neighbors
floodPacket(fd, sendBuffer, neighbors);
}
// Receive packet (time out set on socket receive operation)
recvLen = recv(fd, recvBuffer, LS_PACKET_SIZE, 0);
// If packet was received:
if (recvLen > 0)
{
sem_wait(&recvLock);
// Push packet to received queue to be processed in main thread
push(recvQueue, recvBuffer);
sem_post(&recvLock);
}
}
}
void *dynamicThread(void *param)
{
char label;
char packet[LS_PACKET_SIZE];
int i, num, numEdges, cost;
struct AdjList vertex;
struct AdjListNode *edge;
label = *((char *) param);
numEdges = neighbors->size;
// Block until first shortest path calculation
sem_wait(&dynamLock);
// Seed the random number generator
srand(time(NULL));
// Get the vertex of the local router
i = getIndex(graph->key, graph->size, label);
vertex = graph->array[i];
// Loop handling the creation of dynamic network changes
while (1)
{
// Change the cost of a random edge of the router every 5 seconds
sleep(5);
// Pick a random edge
num = rand() % (numEdges);
// Loop to the picked edge
i = 0;
edge = vertex.head;
while (edge && i < num)
{
edge = edge->next;
i++;
}
if (edge)
{
// Add a random number between -4 and +4 to get the new cost
cost = edge->cost + (rand() % 9) - 4;
// If new cost is less than 1, set to 1
cost = cost < 1 ? 1 : cost;
// Build link-state packet to enact change to graph
buildLSPacket(packet, 6, (edge->seqN + 1) % 256, label, graph->key[edge->dest], cost);
// Display changes
printf("Changing cost to reach %c from %d to %d\n", graph->key[edge->dest], edge->cost, cost);
// Push packet onto queue to be processed
sem_wait(&recvLock);
push(recvQueue, packet);
sem_post(&recvLock);
}
}
}
int parseCommandLine(int argc, char **argv, char *label, int *port, int *numRouters, char **filename, int *dynamic)
{
if (argc < 5) {
fprintf(stderr, "Not enough arguments. Use format:\n"
"routerLabel portNum totalNumRouters discoverFile [-dynamic]\n");
return -1;
}
if (strlen(argv[1]) != 1)
{
fprintf(stderr, "Please use a 1 character router label.\n");
return -1;
}
// Read command line arguments
*label = argv[1][0];
*port = atoi(argv[2]);
*numRouters = atoi(argv[3]);
*filename = argv[4];
// Set dynamic flag
if (argc >= 6 && !strcmp(argv[5], "-dynamic\0"))
*dynamic = 1;
else
*dynamic = 0;
return 0;
}
int initialization(int *fd, int port, int numRouters, char *filename, char label)
{
// Create and bind socket
if ((*fd = initializeSocket(port)) < 0)
return -1;
// Initialize data structures
graph = newGraph(numRouters, 0);
neighbors = newNeighborList();
sendQueue = newFifoQueue();
recvQueue = newFifoQueue();
if (!graph || !neighbors || !sendQueue || !recvQueue) {
printf("Malloc failed.\n");
return -1;
}
// Initialize semaphores
if(sem_init(&sendLock, 0, 1) != 0) {
printf("Sem init failed.\n");
return -1;
}
if(sem_init(&recvLock, 0, 1) != 0) {
printf("Sem init failed.\n");
return -1;
}
// Read discovery text file to find adjacent neighbor nodes
if (processTextFile(filename, neighbors) < 0)
return -1;
// Push neighbor edges to received queue to be processed in main loop
queueNeighbors(neighbors, recvQueue, label);
return 0;
}
int startNetworkThread(int *fd)
{
int err;
pthread_t network_thread;
// Start network thread
if ((err = pthread_create(&network_thread, NULL, &networkThread, fd))) {
fprintf(stderr, "Can't create Network Thread: [%s]\n", strerror(err));
return -1;
}
return 0;
}
int startDynamicThread(char *label)
{
int err;
pthread_t dynamic_thread;
if(sem_init(&dynamLock, 0, 1) != 0) {
printf("Sem init failed.\n");
return -1;
}
// Block dynamic thread until after first shortest path calculation
sem_wait(&dynamLock);
// Start dynamic thread
if ((err = pthread_create(&dynamic_thread, NULL, &dynamicThread, label))) {
fprintf(stderr, "Can't create Dynamic Thread: [%s]\n", strerror(err));
return -1;
}
return 0;
}