/*
 * Protocol initialization function. Check parameters, allocate storage space, read parameters, etc.
 */
void DearInit(Node* node, DearData** dearPtr, const NodeInput* nodeInput, int interfaceIndex) {
 BOOL retVal;
 
 if (MAC_IsWiredNetwork(node, interfaceIndex))
  ERROR_ReportError("DEAR supports only wireless interfaces");
  
 if (node->numberInterfaces > 1)
  ERROR_ReportError("DEAR supports only one interface of node");
  
 // Allocate memory for variables of this node.
 DearData* dear = (DearData*)MEM_malloc(sizeof(DearData));
 (*dearPtr) = dear;
 
 // Reset transmission power.
 DearSetTxPower(node, DearGetTxPower(node));
 
 // Initalize parameters.
 dear->destination = 1;
 dear->defaultPower = DearGetTxPower(node);
 dear->defaultRange = DearGetPropagationDistance(node);
 dear->neighbours = (DearTableEntry*)MEM_malloc((1 + node->numNodes) * sizeof(DearTableEntry));

 // Read parameter(s).
 IO_ReadInt(node->nodeId, ANY_ADDRESS, nodeInput, "DEAR-DESTINATION", &retVal, (int*)(&dear->destination));
 if (!retVal)
  ERROR_ReportError("DEAR-DESTINATION not specified!");
 
 // Initialize statistics.
 dear->numIndirectTransmissions =
 dear->numDirectTransmissions = 0;
 
 // Obtain the neighbours.
 for (int i = 1; i <= node->numNodes; i++) {
  // Get the neighbour node.
  Node *neighbour = DearGetNodeById(node, i);
  Node *destination = DearGetNodeById(node, dear->destination);
  
  // Store the neighbour's maximum battery.
  dear->neighbours[i].battery_max = DearGetRemainingBattery(neighbour);
  
  // Get the positions.
  double x0, y0, z0, x1, y1, z1, x2, y2, z2;
  DearGetNodePosition(node, x0, y0, z0);
  DearGetNodePosition(neighbour, x1, y1, z1);
  DearGetNodePosition(destination, x2, y2, z2);
  
  // Calculate distance between destination and neighbour.
  // Then set the valid neighbour property.
  x0 -= x1; y0 -= y1; z0 -= z1;
  double distance0 = sqrt(x0 * x0 + y0 * y0 + z0 * z0);
  dear->neighbours[i].valid = (distance0 < dear->defaultRange);
  
  // Calculate distance between destination and neighbour.
  // Then estimate transmission power in dBm. Assume the default transmission power
  // is used for the given neighbour size (this is expected to preserve the error rate).
  x2 -= x1; y2 -= y1; z2 -= z1;
  double distance2 = sqrt(x2 * x2 + y2 * y2 + z2 * z2);
  dear->neighbours[i].power = DearGetPowerForDistance(neighbour, distance2, dear->defaultPower, dear->defaultRange) + DearPowerBoost;
  
  // NOTE the power does NOT fall below the default power (this violates "correctness").
  if (dear->neighbours[i].power < dear->defaultPower)
   dear->neighbours[i].power = dear->defaultPower;
 }
 
 // Tell IP to use our function to route packets.
 NetworkIpSetRouterFunction(node, &DearRouterFunction, interfaceIndex);
}
Exemplo n.º 2
0
/*
 * FUNCTION    MacTdmaInit
 * PURPOSE     Initialization function for TDMA protocol of MAC layer.
 *
 * Parameters:
 *     node:      node being initialized.
 *     nodeInput: structure containing contents of input file
 */
void MacTdmaInit(Node* node,
                 int interfaceIndex,
                 const NodeInput* nodeInput,
                 const int subnetListIndex,
                 const int numNodesInSubnet)
{
    BOOL wasFound;
    BOOL automaticScheduling = 0;

    char schedulingString[MAX_STRING_LENGTH];

    int i;
    int numSlots;
    int  numChannels = PROP_NumberChannels(node);

    MacDataTdma* tdma = (MacDataTdma *)MEM_malloc(sizeof(MacDataTdma));
    clocktype duration;

    if (DEBUG) {
        printf("partition %d, node %d in tdmainitt\n",
               node->partitionData->partitionId, node->nodeId);
        printf("partition %d, node %d sees %d nodes in subnet\n"
               "subnetListIndex %d\n",
               node->partitionData->partitionId,
               node->nodeId,
               numNodesInSubnet,
               subnetListIndex);
        fflush(stdout);
    }

    ERROR_Assert(tdma != NULL,
                 "TDMA: Unable to allocate memory for TDMA data structure.");

    memset(tdma, 0, sizeof(MacDataTdma));
    tdma->myMacData = node->macData[interfaceIndex];
    tdma->myMacData->macVar = (void *)tdma;

    //
    // TDMA-NUMBER-OF-SLOTS-PER-FRAME: number of slots per frame
    //
    IO_ReadInt(node,node->nodeId, interfaceIndex, nodeInput,
               "TDMA-NUM-SLOTS-PER-FRAME", &wasFound, &numSlots);

    if (wasFound) {
        tdma->numSlotsPerFrame = numSlots;
    }
    else {
        tdma->numSlotsPerFrame = numNodesInSubnet;
    }

    //
    // TDMA-SLOT-DURATION: slot duration
    //
    IO_ReadTime(node,node->nodeId, interfaceIndex, nodeInput,
                "TDMA-SLOT-DURATION", &wasFound, &duration);

    if (wasFound) {
        tdma->slotDuration = duration;
    }
    else {
        tdma->slotDuration = DefaultTdmaSlotDuration;
    }

    //
    // TDMA-GUARD-TIME: to be inserted between slots
    //
    IO_ReadTime(node,node->nodeId, interfaceIndex, nodeInput,
                "TDMA-GUARD-TIME", &wasFound, &duration);

    if (wasFound) {
        tdma->guardTime = duration;
    }
    else {
        tdma->guardTime = DefaultTdmaGuardTime;
    }

    //
    // TDMA-INTER-FRAME-TIME: to be inserted between frames
    //
    IO_ReadTime(node,node->nodeId, interfaceIndex, nodeInput,
                "TDMA-INTER-FRAME-TIME", &wasFound, &duration);

    if (wasFound) {
        tdma->interFrameTime = duration;
    }
    else {
        tdma->interFrameTime = DefaultTdmaInterFrameTime;
    }

    //
    // TDMA-SCHEDULING: type of scheduling (automatic or from file)
    //
    IO_ReadString(node,node->nodeId, interfaceIndex, nodeInput,
                  "TDMA-SCHEDULING", &wasFound, schedulingString);

    if (wasFound) {
        if (strcmp(schedulingString, "AUTOMATIC") == 0) {
            automaticScheduling = TRUE;
        }
        else if (strcmp(schedulingString, "FILE") == 0) {
            automaticScheduling = FALSE;
        }
        else {
            ERROR_ReportError(
                "TDMA-SCHEDULING must be either AUTOMATIC or FILE\n");
        }
    }
    else {
        automaticScheduling = TRUE;
    }

    //
    // Build frameDescriptor
    //
    // If TDMA-SCHEDULING = AUTOMATIC is specified, all nodes
    // other than transmitters listen to the channel.
    //
    // If TDMA-SCHEDULING = FILE is specified, all nodes stay
    // idle unless Rx or Tx is specified in TDMA-SCHEDULING-FILE.
    //

    tdma->frameDescriptor =
        (char*)MEM_malloc(tdma->numSlotsPerFrame * sizeof(char));

    for (i = 0; i < tdma->numSlotsPerFrame; i++) {
        tdma->frameDescriptor[i] = TDMA_STATUS_IDLE;
    }

    if (automaticScheduling == TRUE) {
        for (i = 0; i < tdma->numSlotsPerFrame; i++) {
            if (subnetListIndex == i % numNodesInSubnet) {
                tdma->frameDescriptor[i] = TDMA_STATUS_TX;
            }
            else {
                tdma->frameDescriptor[i] = TDMA_STATUS_RX;
            }
        }
    }
    else {
        NodeInput scheduleInput;

        IO_ReadCachedFile(node,node->nodeId, interfaceIndex, nodeInput,
                          "TDMA-SCHEDULING-FILE", &wasFound, &scheduleInput);

        if (wasFound == FALSE) {
            ERROR_ReportError("TDMA-SCHEDULING-FILE is missing\n");
        }

        for (i = 0; i < scheduleInput.numLines; i++) {
            char token[MAX_STRING_LENGTH];
            char *strPtr = NULL;
            char *returnValue = NULL;
            int slotId;

            ERROR_Assert(i < tdma->numSlotsPerFrame,
                         "TDMA: Too many slots in TDMA Scheduling File.");

            IO_GetToken(token, scheduleInput.inputStrings[i], &strPtr);

            slotId = (int)atoi(token);
            ERROR_Assert(i == slotId,
                         "TDMA: Line on TDMA Scheduling File does not correspond with slot ID");

            returnValue = IO_GetToken(token, strPtr, &strPtr);

            while (returnValue != NULL) {
                char nodeString[MAX_STRING_LENGTH];
                char *typeStr;
                NodeAddress nodeId;
                int numReturnVals = 0;

                numReturnVals = sscanf(token, "%s", nodeString);
                assert(numReturnVals == 1);

                IO_ConvertStringToLowerCase(nodeString);

                typeStr = strrchr(nodeString, '-');
                *typeStr = 0;
                typeStr++; // to the next character

                if (strcmp(nodeString, "all") == 0) {
                    if (strcmp(typeStr, "rx") != 0) {
                        ERROR_ReportError("'All' must be used with '-Rx'\n");
                    }
                    nodeId = node->nodeId;
                }
                else {
                    nodeId = (NodeAddress)atoi(nodeString);
                }

                returnValue = IO_GetToken(token, strPtr, &strPtr);

                if (node->nodeId != nodeId) {
                    continue;
                }

                if (strcmp(typeStr, "rx") == 0) {
                    //
                    // '-Tx' has priority over '-Rx' (likely from 'All-Rx')
                    //
                    if (tdma->frameDescriptor[slotId] != TDMA_STATUS_TX) {
                        tdma->frameDescriptor[slotId] = TDMA_STATUS_RX;
                    }
                }
                else if (strcmp(typeStr, "tx") == 0) {
                    tdma->frameDescriptor[slotId] = TDMA_STATUS_TX;
                }
                else {
                    ERROR_ReportError("Slot type must be either Rx or Tx\n");
                }
            }
        }
        assert(i == tdma->numSlotsPerFrame);
    }

    if (DEBUG) {
        printf("partition %d, node %d: ", node->partitionData->partitionId, node->nodeId);
        for (i = 0; i < tdma->numSlotsPerFrame; i++) {
            printf("%d ", (int)tdma->frameDescriptor[i]);
        }
        printf("\n");
    }

    // Use first listening channel
    for (i = 0; i < numChannels; i++)
    {
        if (PHY_IsListeningToChannel(node, tdma->myMacData->phyNumber, i)
                == TRUE)
        {
            tdma->channelIndex = i;
            break;
        }
    }

    tdma->stats.pktsSentUnicast = 0;
    tdma->stats.pktsSentBroadcast = 0;
    tdma->stats.pktsGotUnicast = 0;
    tdma->stats.pktsGotBroadcast = 0;
    tdma->stats.numTxSlotsMissed = 0;

#ifdef PARALLEL //Parallel
    tdma->lookaheadHandle = PARALLEL_AllocateLookaheadHandle (node);
    PARALLEL_AddLookaheadHandleToLookaheadCalculator(
        node, tdma->lookaheadHandle, 0);

    // Also sepcific a minimum lookahead, in case EOT can't be used in the sim.
    PARALLEL_SetMinimumLookaheadForInterface(node, tdma->slotDuration);
    int index;
    for (index = 0; index < node->numberPhys; index ++ )
    {
        if (node->phyData[index]->phyModel == PHY802_11a)
        {
            PARALLEL_SetMinimumLookaheadForInterface(node,
                    DOT11_802_11a_DELAY_UNTIL_SIGNAL_AIRBORN);
        }
        if (node->phyData[index]->phyModel == PHY802_11b)
        {
            PARALLEL_SetMinimumLookaheadForInterface(node,
                    DOT11_802_11b_DELAY_UNTIL_SIGNAL_AIRBORN);
        }
    }
#endif //endParallel

    MacTdmaInitializeTimer(node, tdma);

}
CDtnCacheSummaryStore::CDtnCacheSummaryStore(Node* node, const NodeInput* nodeInput, int interfaceIndex):m_node(node)
{

    //initialize seed
    RANDOM_SetSeed(m_seed,node->globalSeed,node->nodeId,ROUTING_PROTOCOL_ICAN, interfaceIndex);

    //initiate bloomfilter parameters
    BOOL retVal;
    //read expected number of item
    int nItem;
    IO_ReadInt(
        node->nodeId,
        ANY_ADDRESS,
        nodeInput,
        "BF-NUMITEM",
        &retVal,
        &nItem);
    if (retVal == FALSE || nItem < 0)
    {
        char errorString[MAX_STRING_LENGTH];
        sprintf(errorString,
                "Wrong BF-NUMITEM configuration format!\n"
               );
        ERROR_ReportError(errorString);
    }
    //read expected false postive rate
    double fpRate;
    IO_ReadDouble(
        node->nodeId,
        ANY_ADDRESS,
        nodeInput,
        "BF-FPRATE",
        &retVal,
        &fpRate);
    if (retVal == FALSE || nItem < 0)
    {
        char errorString[MAX_STRING_LENGTH];
        sprintf(errorString,
                "Wrong BF-FPRATE configuration format!\n"
               );
        ERROR_ReportError(errorString);
    }
    //read salt seed
    int seed;
    IO_ReadInt(
        node->nodeId,
        ANY_ADDRESS,
        nodeInput,
        "BF-SALT",
        &retVal,
        &seed);
    if (retVal == FALSE || nItem < 0)
    {
        char errorString[MAX_STRING_LENGTH];
        sprintf(errorString,
                "Wrong BF-SALT configuration format!\n"
               );
        ERROR_ReportError(errorString);
    }

    //calculate BF parameters
    bfparameters.projected_element_count    = nItem;
    bfparameters.false_positive_probability = fpRate;
    bfparameters.random_seed                = seed;
    bfparameters.compute_optimal_parameters();


    m_pCacheSummaries = new BloomFilterStore;

}