示例#1
0
/**
 * Receive a ContentObject message that comes back from the tutorial_Server in response to an Interest we sent.
 * This message will be a chunk of the requested content, and should be received in ordered sequence.
 * Depending on the CCNxName in the content object, we hand it off to either _receiveFileChunk() or
 * _receiveDirectoryListingChunk() to process.
 *
 * @param [in] contentObject A CCNxContentObject containing a response to an CCNxInterest we sent.
 * @param [in] domainPrefix A CCNxName containing the domain prefix of the content we requested.
 *
 * @return The number of chunks of the content left to transfer.
 */
static uint64_t
_receiveContentObject(CCNxContentObject *contentObject, const CCNxName *domainPrefix)
{
    CCNxName *contentName = ccnxContentObject_GetName(contentObject);

    uint64_t chunkNumber = tutorialCommon_GetChunkNumberFromName(contentName);

    // Get the number of the final chunk, as specified by the sender.
    uint64_t finalChunkNumberSpecifiedByServer = ccnxContentObject_GetFinalChunkNumber(contentObject);

    // Get the type of the incoming message. Was it a response to a fetch' or a 'list' command?
    char *command = tutorialCommon_CreateCommandStringFromName(contentName, domainPrefix);

    // Process the payload.
    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);

    if (strncasecmp(command, tutorialCommon_CommandList, strlen(command)) == 0) {
        // This is a chunk of the directory listing.
        _receiveDirectoryListingChunk(payload, chunkNumber, finalChunkNumberSpecifiedByServer);
    } else if (strncasecmp(command, tutorialCommon_CommandFetch, strlen(command)) == 0) {
        // This is a chunk of a file.
        char *fileName = tutorialCommon_CreateFileNameFromName(contentName);
        _receiveFileChunk(fileName, payload, chunkNumber, finalChunkNumberSpecifiedByServer);
        parcMemory_Deallocate((void **) &fileName);
    } else {
        printf("tutorial_Client: Unknown command: %s\n", command);
    }

    parcMemory_Deallocate((void **) &command);

    return (finalChunkNumberSpecifiedByServer - chunkNumber); // number of chunks left to transfer
}
LONGBOW_TEST_CASE(Global, ccnxContentObject_GetSetFinalChunkNumber)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/foo/bar");
    PARCBuffer *payload = parcBuffer_Allocate(100);

    CCNxContentObject *contentObject = ccnxContentObject_CreateWithDataPayload(name, payload);

    ccnxContentObject_SetFinalChunkNumber(contentObject, 100);
    ccnxContentObject_AssertValid(contentObject);
    assertTrue(ccnxContentObject_GetFinalChunkNumber(contentObject) == 100, "Expected final chunk number to be 100");

    ccnxContentObject_SetFinalChunkNumber(contentObject, 20010);
    ccnxContentObject_AssertValid(contentObject);
    assertTrue(ccnxContentObject_GetFinalChunkNumber(contentObject) == 20010, "Expected final chunk number to be 20010");

    ccnxName_Release(&name);
    parcBuffer_Release(&payload);
    ccnxContentObject_Release(&contentObject);
}