-
Notifications
You must be signed in to change notification settings - Fork 0
/
node2.c
140 lines (117 loc) · 4.91 KB
/
node2.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
#include <stdio.h>
#include <string.h>
#include "project3.h"
#define NODE_NUM 2
extern int TraceLevel;
struct distance_table {
int costs[MAX_NODES][MAX_NODES];
};
struct distance_table dt2;
struct NeighborCosts *neighbor2;
extern float clocktime;
void printdt2(int MyNodeNumber, struct NeighborCosts *neighbor, struct distance_table *dtptr);
/* students to write the following two routines, and maybe some others */
void rtinit2() {
printf("rinit2() called at time %f\n", clocktime);
neighbor2 = getNeighborCosts(NODE_NUM);
// Initialize distance table
int i, j;
for (i = 0; i < MAX_NODES; i++){
for (j = 0; j < MAX_NODES; j++){
dt2.costs[i][j] = INFINITY;
}
}
for (i = 0; i < MAX_NODES; i++){
dt2.costs[i][i] = neighbor2->NodeCosts[i];
}
printdt2(NODE_NUM, neighbor2, &dt2);
for (i = 0; i < MAX_NODES; i++){
if (i != NODE_NUM && neighbor2->NodeCosts[i] != INFINITY) { // make sure we aren't sending to self
// create struct
struct RoutePacket pkt = {NODE_NUM, i};
memcpy(pkt.mincost, neighbor2 -> NodeCosts, sizeof(int) * MAX_NODES);
// send each pkt
printf("At time t=%f, node %d sends packet to node %d with: %d %d %d %d\n", clocktime, pkt.sourceid, pkt.destid, pkt.mincost[0], pkt.mincost[1], pkt.mincost[2], pkt.mincost[3]);
toLayer2(pkt);
}
}
}
void rtupdate2( struct RoutePacket *rcvdpkt ) {
print_rcvdpkt(rcvdpkt);
// update distance table here
int i, j;
int has_changed = 0;
for (i = 0; i < MAX_NODES; i++){
if (rcvdpkt->sourceid != i){
if (rcvdpkt->mincost[i] + neighbor2->NodeCosts[rcvdpkt->sourceid] < dt2.costs[i][rcvdpkt->sourceid]){ // if given cost is less than current cost
dt2.costs[i][rcvdpkt->sourceid] = rcvdpkt->mincost[i] + neighbor2->NodeCosts[rcvdpkt->sourceid]; // set current cost to new cost
if (dt2.costs[i][rcvdpkt->sourceid] == min_array(dt2.costs[i])){
has_changed = 1;
}
}
}
}
if (has_changed == 1){
struct RoutePacket pkt;
pkt.sourceid = NODE_NUM;
for (i = 0; i < MAX_NODES; i++){
pkt.mincost[i] = min_array(dt2.costs[i]);
}
for (i = 0; i < MAX_NODES; i++){
if (i != NODE_NUM && neighbor2->NodeCosts[i] != INFINITY){
pkt.destid = i;
printf("Distance table updated. At time t=%f, node %d sends packet to node %d with: %d %d %d %d\n", clocktime, pkt.sourceid, pkt.destid, pkt.mincost[0], pkt.mincost[1], pkt.mincost[2], pkt.mincost[3]);
toLayer2(pkt);
}
}
}
printdt2(NODE_NUM, 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