/*
 * Protocol finalization function.
 */
void DearFinalize(Node *node) {
 char buf[MAX_STRING_LENGTH];
 
 // Obtain a pointer to the local variable space.
 DearData* dear = (DearData*)NetworkIpGetRoutingProtocol(node, ROUTING_PROTOCOL_DEAR);
 assert(dear != NULL);
 
 // Report statistics in stat file.
 // Direct transmissions.
 sprintf(buf, "Number of direct transmissions = %u", dear->numDirectTransmissions);
 IO_PrintStat(node, "Network", "DEAR", ANY_DEST, -1, buf);
 
 // Indirect transmissions.
 sprintf(buf, "Number of indirect transmissions = %u", dear->numIndirectTransmissions);
 IO_PrintStat(node, "Network", "DEAR", ANY_DEST, -1, buf);
}
Esempio n. 2
0
void BrpFinalize(Node* node)
{
    BrpData *dataPtr = (BrpData*)
        NetworkIpGetRoutingProtocol(node, ROUTING_PROTOCOL_BRP);

    // If protocol data pointer is NULL give error Message.
    ERROR_Assert(dataPtr != NULL,
       "Pointer to the protocol Data Cannot be NULL");

    if (dataPtr->statsPrinted) {
        return;
    }

    if (dataPtr->printStats) {
        dataPtr->statsPrinted = TRUE;
        BrpPrintStats(node);
    }
    BrpPrintStats(node);
}
/*
 * Protocol routing function.
 */
void DearRouterFunction(Node* node, Message* msg, NodeAddress destAddr, NodeAddress previousHopAddress, BOOL* packetWasRouted) {
 double ratio = 1.0e+299;
 NodeAddress nextHop = 0;
 NodeAddress *nextHopInfoPtr = (NodeAddress *)MESSAGE_ReturnInfo(msg, INFO_TYPE_DearNextHop);
 IpHeaderType* ipHeader = (IpHeaderType*)msg->packet;
   
 // Obtain a pointer to the local variable space.
 DearData* dear = (DearData*)NetworkIpGetRoutingProtocol(node, ROUTING_PROTOCOL_DEAR);
 assert(dear != NULL);
  
 // Do not route any packets destined to self.
 if (MAPPING_GetNodeIdFromInterfaceAddress(node, destAddr) == node->nodeId) {
  // Remove the next hop info field if present.
  if (nextHopInfoPtr != NULL)
   MESSAGE_RemoveInfo(node, msg, INFO_TYPE_DearNextHop);
  return;
 }
 
 // Do not route the packet if we are not the next hop for this packet.
 if (nextHopInfoPtr != NULL && *nextHopInfoPtr != node->nodeId)
  return;
 
 // Find the best neighbour to route packet to.
 for (int i = 1; i <= node->numNodes; i++) {
  // Skip non-neighbours.
  if (!dear->neighbours[i].valid)
   continue;
  
  // Skip the degenerate case where destination sends to destination.
  if (i == dear->destination)
   continue;
   
  // Get the candidate node.
  Node *candidate = DearGetNodeById(node, i);
  
  // Read the battery level of the candidate.
  double battery = DearGetRemainingBatteryPercent(candidate, dear->neighbours[i].battery_max);
  
  // Make sure the node isn't dead.
  if (battery <= 0.0)
   continue;
   
  // Calculate the power in mW.
  double power_mW = pow(10.0, dear->neighbours[i].power / 10.0);
  
  // Calculate the metric.
  double metric = power_mW / battery;
  
  if (candidate->nodeId != node->nodeId) {
   // INDIRECT TRANSMISSION: Add the default transmission power to neighbour.
   battery = DearGetRemainingBatteryPercent(node, dear->neighbours[node->nodeId].battery_max);
   
   if (battery <= 0.0)
    continue; // This can only happen if the originating node is dead.
   
   // Calculate the power in mW.
   power_mW = pow(10.0, dear->defaultPower / 10.0);
   
   // Adjust the metric. 
   metric += power_mW / battery;
  }
  
  // Check if calculated metric is lower than the lowest metric so far.
  if (metric < ratio) {
   nextHop = i;
   ratio = metric;
  }
 }
 
 // Set the real destination if needed (when next hop is current node,
 // the real next hop is the final destination).
 if (nextHop == node->nodeId)
  nextHop = dear->destination;
 
 // Route the packet only when the destination is considered reachable.
 if (nextHop) {
  if (dear->neighbours[nextHop].valid) {
   dear->numIndirectTransmissions++;
   DearSetTxPower(node, dear->defaultPower);   
  } else {
   dear->numDirectTransmissions++;
   DearSetTxPower(node, dear->neighbours[node->nodeId].power);
  }
  
  // Allocate the next hop information header if needed.
  if (nextHopInfoPtr == NULL) {
   MESSAGE_AddInfo(node, msg, sizeof(nextHop), INFO_TYPE_DearNextHop);
   nextHopInfoPtr = (NodeAddress *)MESSAGE_ReturnInfo(msg, INFO_TYPE_DearNextHop);
   assert(nextHopInfoPtr);
  }
  
  // Write the next hop, and route the packet.
  *nextHopInfoPtr = nextHop;
  *packetWasRouted = TRUE;  
  NetworkIpSendPacketToMacLayer(node, msg, DEFAULT_INTERFACE, nextHop);
 }
}