-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode2.c
More file actions
73 lines (62 loc) · 2.59 KB
/
node2.c
File metadata and controls
73 lines (62 loc) · 2.59 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
#include <stdio.h>
#include "project3.h"
#include "nodeCommon.h"
extern int TraceLevel;
struct distance_table dt2;
struct NeighborCosts *neighbor2;
/* students to write the following two routines, and maybe some others */
void rtinit2() {
neighbor2 = initNode(2, neighbor2, &dt2);
}
void rtupdate2( struct RoutePacket *rcvdpkt ) {
updateNode(2, neighbor2, rcvdpkt, &dt2);
printdt(2, neighbor2, &dt2);
}
/////////////////////////////////////////////////////////////////////
// printdt
// This routine is being supplied to you. It is the same code in
// each node and is tailored based on the input arguments.
// Required arguments:
// MyNodeNumber: This routine assumes that you know your node
// number and supply it when making this call.
// struct NeighborCosts *neighbor: A pointer to the structure
// that's supplied via a call to getNeighborCosts().
// It tells this print routine the configuration
// of nodes surrounding the node we're working on.
// struct distance_table *dtptr: This is the running record of the
// current costs as seen by this node. It is
// constantly updated as the node gets new
// messages from other nodes.
/////////////////////////////////////////////////////////////////////
void printdt2( int MyNodeNumber, struct NeighborCosts *neighbor,
struct distance_table *dtptr ) {
int i, j;
int TotalNodes = neighbor->NodesInNetwork; // Total nodes in network
int NumberOfNeighbors = 0; // How many neighbors
int Neighbors[MAX_NODES]; // Who are the neighbors
// Determine our neighbors
for ( i = 0; i < TotalNodes; i++ ) {
if (( neighbor->NodeCosts[i] != INFINITY ) && i != MyNodeNumber ) {
Neighbors[NumberOfNeighbors] = i;
NumberOfNeighbors++;
}
}
// Print the header
printf(" via \n");
printf(" D%d |", MyNodeNumber );
for ( i = 0; i < NumberOfNeighbors; i++ )
printf(" %d", Neighbors[i]);
printf("\n");
printf(" ----|-------------------------------\n");
// For each node, print the cost by travelling thru each of our neighbors
for ( i = 0; i < TotalNodes; i++ ) {
if ( i != MyNodeNumber ) {
printf("dest %d|", i );
for ( j = 0; j < NumberOfNeighbors; j++ ) {
printf( " %4d", dtptr->costs[i][Neighbors[j]] );
}
printf("\n");
}
}
printf("\n");
} // End of printdt2