void rtinit1() { int i, j; struct distance_table *thisnode1; thisnode1 = &dt1; // set the temporary packet mincosts to INFINITY RPACKET.mincost[0] = INF; RPACKET.mincost[1] = INF; RPACKET.mincost[2] = INF; RPACKET.mincost[3] = INF; printf("At time t = %f, rtinit1() called\n", clocktime); // set links from node i to node j as INFINITY for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { thisnode1->costs[i][j]= INF; } } neighbor1 = getNeighborCosts(1); // link weight from node 0 thisnode1->costs[0][0] = neighbor1->NodeCosts[0]; thisnode1->costs[1][1] = neighbor1->NodeCosts[1]; thisnode1->costs[2][2] = neighbor1->NodeCosts[2]; thisnode1->costs[3][3] = neighbor1->NodeCosts[3]; // check each distance from node i to node j in comparison to our somePacket for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (RPACKET.mincost[i] > thisnode1->costs[i][j]) { RPACKET.mincost[i] = thisnode1->costs[i][j]; } } } // set somePacket's sourceID to 1 (we're in node1) // use tolayer2 to send to another node, depending on the destID RPACKET.sourceid = 1; RPACKET.destid = 0; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 0 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); RPACKET.destid = 2; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 2 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); RPACKET.destid = 3; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 3 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); printdt1(1, neighbor1, thisnode1); printf("We are done with rtinit1 routine\n"); }
void rtinit0() { int i, j; struct distance_table *thisnode0; thisnode0 = &dt0; // set the temporary packet mincosts to INFINITY somePacket.mincost[0] = INFINITY; somePacket.mincost[1] = INFINITY; somePacket.mincost[2] = INFINITY; somePacket.mincost[3] = INFINITY; printf("At time t = %f, rtinit0() called\n", clocktime); // set links from node i to node j as INFINITY for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { thisnode0->costs[i][j]= INFINITY; } } neighbor0 = getNeighborCosts(0); // link weight from node 0 thisnode0->costs[0][0] = neighbor0->NodeCosts[0]; thisnode0->costs[1][1] = neighbor0->NodeCosts[1]; thisnode0->costs[2][2] = neighbor0->NodeCosts[2]; thisnode0->costs[3][3] = neighbor0->NodeCosts[3]; // check each distance from node i to node j in comparison to our somePacket for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (somePacket.mincost[i] > thisnode0->costs[i][j]) { somePacket.mincost[i] = thisnode0->costs[i][j]; } } } // set somePacket's sourceID to 0 (we're in node0) // use tolayer2 to send to another node, depending on the destID somePacket.sourceid = 0; somePacket.destid = 1; toLayer2(somePacket); printf("At time t = %f, node 0 sends packet to node 1 with: %d %d %d %d\n", clocktime, somePacket.mincost[0], somePacket.mincost[1], somePacket.mincost[2], somePacket.mincost[3]); somePacket.destid = 2; toLayer2(somePacket); printf("At time t = %f, node 0 sends packet to node 2 with: %d %d %d %d\n", clocktime, somePacket.mincost[0], somePacket.mincost[1], somePacket.mincost[2], somePacket.mincost[3]); somePacket.destid = 3; toLayer2(somePacket); printf("At time t = %f, node 0 sends packet to node 3 with: %d %d %d %d\n", clocktime, somePacket.mincost[0], somePacket.mincost[1], somePacket.mincost[2], somePacket.mincost[3]); printdt0(0, neighbor0, thisnode0); printf("We are done with rtinit0 routine\n"); }
void rtupdate1( struct RoutePacket *rcvdpkt ) { int i, j, rcvdSource, update = 0; struct distance_table *thisnode1; thisnode1 = &dt1; for (i = 0; i < 4; i++) { RPACKET.mincost[i] = INF; } printf("Now we are in rtupdate1()\n"); // check what sourceID of received packet is rcvdSource = rcvdpkt->sourceid; printf("Received packet's source ID is %d\n", rcvdSource); for (i = 0; i < 4; i++) { // go through and see if we need to update distance-table with new mincost if (thisnode1->costs[i][rcvdSource] > (thisnode1->costs[rcvdSource][rcvdSource] + rcvdpkt->mincost[i])) { thisnode1->costs[i][rcvdSource] = (thisnode1->costs[rcvdSource][rcvdSource] + rcvdpkt->mincost[i]); update = 1; } } // for this case, we update distance table if (update == 1) { for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (RPACKET.mincost[i] > thisnode1->costs[i][j]) { RPACKET.mincost[i] = thisnode1->costs[i][j]; } } } printf("We updated distance table from rtupdate1()\n"); // set somePacket's sourceID to 1 (we're in node1) // use tolayer2 to send to another node, depending on the destID RPACKET.sourceid = 1; RPACKET.destid = 0; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 0 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); RPACKET.destid = 2; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 2 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); RPACKET.destid = 3; toLayer2(RPACKET); printf("At time t = %f, node 1 sends packet to node 3 with: %d %d %d %d\n", clocktime, RPACKET.mincost[0], RPACKET.mincost[1], RPACKET.mincost[2], RPACKET.mincost[3]); } printdt1(1, neighbor1, thisnode1); printf("We are done with rtupdate1()\n"); }
void rtupdate1( struct RoutePacket *rcvdpkt ) { int i, j; int updated = 0; //Update distance table for(i=0; i<MAX_NODES; i++){ //If neighbor has a new minimum cost to another node, update it in its distance table if(dt1.costs[i][rcvdpkt->sourceid] > rcvdpkt->mincost[i]){ dt1.costs[i][rcvdpkt->sourceid] = rcvdpkt->mincost[i]; //If this would creates a new shorter path to another node from this node, update it in the distance table if(dt1.costs[i][MY_NUMBER] > dt1.costs[rcvdpkt->sourceid][MY_NUMBER] + rcvdpkt->mincost[i]){ dt1.costs[i][MY_NUMBER] = dt1.costs[rcvdpkt->sourceid][MY_NUMBER] + rcvdpkt->mincost[i]; updated = 1; //Flag that this node has a new path to another node } } } if(TraceLevel > 0){ printf("At time t=%f, node %d receives a packet from node %d. The updated distance table follows: \n", clocktime, MY_NUMBER, rcvdpkt->sourceid); printdt1(MY_NUMBER, neighbor1, &dt1); } //If this node has a shorter path to another node, broadcast this new information if(updated){ for(i=0; i<MAX_NODES; i++){ if(neighbor1->NodeCosts[i] != INFINITY && i != MY_NUMBER){ int costs[MAX_NODES]; for(j=0; j<MAX_NODES; j++){ costs[j] = dt1.costs[j][MY_NUMBER]; } struct RoutePacket toSend = {MY_NUMBER, i}; memcpy(toSend.mincost, costs, sizeof(int)*MAX_NODES); if(TraceLevel > 0){ printf("At time t=%f, node %d sends packet to node %d with:", clocktime, MY_NUMBER, i); for(j=0; j<MAX_NODES; j++){ printf(" %d", dt1.costs[j][MY_NUMBER]); } printf(" to reflect updated distance table. \n"); } toLayer2(toSend); } } } }
void rtinit1() { if(TraceLevel > 0){ printf("At time t=%f, rtinit1() called. \n", clocktime); } neighbor1 = getNeighborCosts(MY_NUMBER); int i, j; //Populate distance table with neighbor costs and infinity for everyone else for(i=0; i<MAX_NODES; i++){ for(j=0; j<MAX_NODES; j++){ if(j != MY_NUMBER){ dt1.costs[i][j] = INFINITY; } else{ dt1.costs[i][j] = neighbor1->NodeCosts[i]; } } } //Send packets to all neighbors with this node's neighbor costs for(i=0; i<MAX_NODES; i++){ if(neighbor1->NodeCosts[i] != INFINITY && i != MY_NUMBER){ int costs[MAX_NODES]; for(j=0; j<MAX_NODES; j++){ costs[j] = dt1.costs[j][MY_NUMBER]; } struct RoutePacket toSend = {MY_NUMBER, i}; memcpy(toSend.mincost, costs, sizeof(int)*MAX_NODES); if(TraceLevel > 0){ printf("At time t=%f, node %d sends packet to node %d with:", clocktime, MY_NUMBER, i); for(j=0; j<MAX_NODES; j++){ printf(" %d", dt1.costs[j][MY_NUMBER]); } printf("\n"); } toLayer2(toSend); } } if(TraceLevel > 0){ printdt1(MY_NUMBER, neighbor1, &dt1); } }