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 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");
}
Exemple #3
0
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);
            }
        }
    }
}
Exemple #4
0
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);
    }
}
Exemple #5
0
void rtinit1()
{
  int custos[4] = {1,0,1,INFINITO}; //vetor com os meus custos para os nós 0,1,2 e 3 respectivamente
  int i,j;
  for(i=0;i<4;i++)
  {
    envios[i].sourceid = 1; // todos sairão com o sourceid = 1
    envios[i].destid = i;
    for(j=0;j<4;j++)
    {
      envios[i].mincost[j] = custos[j]; // preenchendo os custos dos pacotes a enviar
      if(i==j)
        dt1.costs[i][j] = custos[i]; // se os indices da matriz forem iguais, então atualizo meus custos buscando no vetor de custos
      else
        dt1.costs[i][j] = INFINITO; // senão eu coloco o valor correspondente ao INFINITO
    }
  }
  tolayer2(envios[0]);
  tolayer2(envios[2]); 
  printdt1(&dt1);
}
Exemple #6
0
rtinit1() 
{
  printf("AT: node1.rtinit1() ... \n");

  //Iterate through Distance Table and initialize values to Infinity (999)!
  int i = 0;
  for (i = 0; i < 4; i++){
    int j = 0;
    for (j = 0; j < 4; j++){
      dt1.costs [i][j] = 999;
    }
  }

  //And Initialize the first neighbor values!
  dt1.costs [0][0] = 1;
  dt1.costs [1][1] = 0;
  dt1.costs [2][2] = 1;

  //Create the Packet that Node 1 will send to its
  //adjacent neighbors with updated link costs.
  struct rtpkt updatePkt;
  updatePkt.sourceid = 1;

  int index = 0;
  for (index = 0; index < 4; index++)
    updatePkt.mincost [index] = dt1.costs[index][index];

  //To Node 0
  updatePkt.destid = 0;
  tolayer2(updatePkt);

  //To Node 2
  updatePkt.destid = 2;
  tolayer2(updatePkt);

  //DEBUG
  printf("Node 1 just sent off the packet {1,0,1,999} to Node 0 and 2! \n");
  printdt1(&dt1);

}