LONGBOW_TEST_CASE(Global, ccnxPortal_Receive_ImmediateTimeout)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portalOut = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
    CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
   
    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);
   
    CCNxMetaMessage *interestMessage = ccnxMetaMessage_CreateFromInterest(interest);
   
    if (ccnxPortal_Send(portalOut, interestMessage, CCNxStackTimeout_Never)) {
        sleep(2);
        ccnxMetaMessage_Release(&interestMessage);
        CCNxMetaMessage *message = ccnxPortal_Receive(portalIn, CCNxStackTimeout_Immediate);
       
        assertTrue(ccnxInterest_Equals(interest, ccnxMetaMessage_GetInterest(message)), "Expected Interest to be received.");
        ccnxMetaMessage_Release(&message);
    }
   
    ccnxInterest_Release(&interest);
    ccnxPortal_Release(&portalIn);
    ccnxPortal_Release(&portalOut);
}
/**
 * Given a command (e.g "fetch") and an optional target name (e.g. "file.txt"), create an appropriate CCNxInterest
 * and write it to the Portal.
 *
 * @param command The command to be handled.
 * @param targetName The name of the target content, if any, that the command applies to.
 *
 * @return true If a CCNxInterest for the specified command and optional target was successfully issued and answered.
 */
static bool
_executeUserCommand(const char *command, const char *targetName)
{
    bool result = false;
    CCNxPortalFactory *factory = _setupConsumerPortalFactory();

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Chunked);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    // Given the user's command and optional target, create an Interest.
    CCNxInterest *interest = _createInterest(command, targetName);

    // Send the Interest through the Portal, and wait for a response.
    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);
    if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never)) {
        CCNxName *domainPrefix = ccnxName_CreateFromURI(tutorialCommon_DomainPrefix);  // e.g. 'lci:/ccnx/tutorial'

        result = _receiveResponseToIssuedInterest(portal, domainPrefix);

        ccnxName_Release(&domainPrefix);
    }

    ccnxMetaMessage_Release(&message);
    ccnxInterest_Release(&interest);
    ccnxPortal_Release(&portal);
    ccnxPortalFactory_Release(&factory);

    return result;
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Send_ImmediateTimeout_WouldBlock)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portalOut = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
   
    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);
   
    CCNxMetaMessage *interestMessage = ccnxMetaMessage_CreateFromInterest(interest);

    for (int count = 0; count < 10000; count++) {
        if (ccnxPortal_Send(portalOut, interestMessage, CCNxStackTimeout_Immediate) == false) {
            break;
        }
        count++;
    }
   
    assertFalse(ccnxPortal_Send(portalOut, interestMessage, CCNxStackTimeout_Immediate),
                "Expected send to fail due to blocking");
   
    ccnxMetaMessage_Release(&interestMessage);
    ccnxInterest_Release(&interest);
    ccnxPortal_Release(&portalOut);
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Open)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
   
    parcObjectTesting_AssertAcquire(portal);
   
    ccnxPortal_Release(&portal);
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Receive_NeverTimeout_Hang)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    CCNxMetaMessage *message = ccnxPortal_Receive(portalIn, CCNxStackTimeout_Never);
    ccnxMetaMessage_Release(&message);
    ccnxPortal_Release(&portalIn);
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Receive_5SecondTimeout)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
   
    ccnxPortal_Receive(portalIn, CCNxStackTimeout_MicroSeconds(5000000));
    assertTrue(errno == ENOMSG, "Expected errno to be set to ENOMSG, actual %s", strerror(errno));
   
    ccnxPortal_Release(&portalIn);
}
int
ccnServe(const PARCIdentity *identity, const CCNxName *listenName, const char *commandString)
{
    parcSecurity_Init();

    CCNxPortalFactory *factory = ccnxPortalFactory_Create(identity);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);
    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    if (ccnxPortal_Listen(portal, listenName, 365 * 86400, CCNxStackTimeout_Never)) {
        while (true) {
            CCNxMetaMessage *request = ccnxPortal_Receive(portal, CCNxStackTimeout_Never);

            if (request == NULL) {
                break;
            }

            CCNxInterest *interest = ccnxMetaMessage_GetInterest(request);

            if (interest != NULL) {
                CCNxName *interestName = ccnxInterest_GetName(interest);

                PARCBuffer *payload = makePayload(interestName, commandString);

                CCNxContentObject *contentObject = ccnxContentObject_CreateWithDataPayload(interestName, payload);

                CCNxMetaMessage *message = ccnxMetaMessage_CreateFromContentObject(contentObject);
                if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never) == false) {
                    fprintf(stderr, "ccnxPortal_Write failed: %d\n", ccnxPortal_GetError(portal));
                }
                {
                    char *name = ccnxName_ToString(interestName);
                    time_t theTime = time(0);
                    char *time = ctime(&theTime);
                    printf("%24.24s  %s\n", time, name);
                    parcMemory_Deallocate((void **) &name);
                }

                parcBuffer_Release(&payload);
            }
            ccnxMetaMessage_Release(&request);
        }
    }

    ccnxPortal_Release(&portal);

    ccnxPortalFactory_Release(&factory);

    parcSecurity_Fini();

    return 0;
}
LONGBOW_TEST_CASE(Global, ccnxPortal_GetKeyId)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    const PARCKeyId *actual = ccnxPortal_GetKeyId(portal);
    const PARCKeyId *expected = ccnxPortalFactory_GetKeyId(data->factory);

    ccnxPortal_Release(&portal);
   
    assertTrue(parcKeyId_Equals(actual, expected), "Expected the PARCKeyId instances to be equal.");
}
LONGBOW_TEST_CASE(Global, Hello)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
    CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);

    CCNxMetaMessage *interestMessage = ccnxMetaMessage_CreateFromInterest(interest);

    if (ccnxPortal_Send(portal, interestMessage, CCNxStackTimeout_Never)) {
        for (int responses = 0; responses == 0; ) {
            CCNxMetaMessage *message = ccnxPortal_Receive(portalIn, CCNxStackTimeout_Never);
            if (message != NULL) {
                if (ccnxMetaMessage_IsContentObject(message)) {
                    CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(message);

                    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);
                    if (parcBuffer_HasRemaining(payload) == false) {
                        fprintf(stderr, "**************** Content object has arrived WITH EMPTY CONTENT\n");
                    } else {
                        char *string = parcBuffer_ToString(payload);
                        fprintf(stderr, "**************** Content object has arrived: %s\n", string);
                        parcMemory_Deallocate((void **)&string);
                    }
                    responses++;
                }
                ccnxMetaMessage_Release(&message);
            }
        }
    }

    ccnxMetaMessage_Release(&interestMessage);
    ccnxPortal_Release(&portal);
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Send_NeverTimeout)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portalOut = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
    CCNxPortal *portalIn = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);
   
    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);
   
    CCNxMetaMessage *interestMessage = ccnxMetaMessage_CreateFromInterest(interest);
    ccnxInterest_Release(&interest);
   
    if (ccnxPortal_Send(portalOut, interestMessage, CCNxStackTimeout_Never)) {
        ccnxMetaMessage_Release(&interestMessage);
        CCNxMetaMessage *message = ccnxPortal_Receive(portalIn, CCNxStackTimeout_Never);
        ccnxMetaMessage_Release(&message);
    }
   
    ccnxPortal_Release(&portalIn);
    ccnxPortal_Release(&portalOut);
}
LONGBOW_TEST_CASE(Global, ccnxPortal_Ignore)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    bool actual = ccnxPortal_Ignore(portal, name, CCNxStackTimeout_Never);
    ccnxName_Release(&name);

    ccnxPortal_Release(&portal);
   
    assertTrue(actual, "Expected ccnxPortal_Ignore to return true");
}
int
consumer(void)
{
    parcSecurity_Init();
    
    CCNxPortalFactory *factory = setupConsumerFactory();
   
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");

    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);
    
    if (ccnxPortal_Send(portal, message,CCNxStackTimeout_Never)) {
        while (ccnxPortal_IsError(portal) == false) {
            CCNxMetaMessage *response = ccnxPortal_Receive(portal,CCNxStackTimeout_Never);
            if (response != NULL) {
                if (ccnxMetaMessage_IsContentObject(response)) {
                    CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);

                    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);

                    char *string = parcBuffer_ToString(payload);
                    printf("%s\n", string);
                    parcMemory_Deallocate((void **)&string);

                    break;
                }
            }
            ccnxMetaMessage_Release(&response);
        }
    }

    ccnxPortal_Release(&portal);

    ccnxPortalFactory_Release(&factory);
    
    parcSecurity_Fini();
    return 0;
}
LONGBOW_TEST_CASE(Global, ccnxPortal_GetFileId)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);
    ccnxInterest_Release(&interest);

    ccnxPortal_Send(portal, message, CCNxStackTimeout_Never);
    ccnxMetaMessage_Release(&message);
    ccnxPortal_Flush(portal, CCNxStackTimeout_Never);
    int fileId = ccnxPortal_GetFileId(portal);
    assertTrue(fileId != -1, "Expected ccnxPortal_GetFileId to not return -1");
    ccnxPortal_Release(&portal);
}
int
ccnGet(PARCIdentity *identity, CCNxName *name)
{
    CCNxPortalFactory *factory = ccnxPortalFactory_Create(identity);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);

    if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never)) {
        while (ccnxPortal_IsError(portal) == false) {
            CCNxMetaMessage *response = ccnxPortal_Receive(portal, CCNxStackTimeout_Never);
            if (response != NULL) {
                if (ccnxMetaMessage_IsContentObject(response)) {
                    CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);

                    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);

                    size_t length = parcBuffer_Remaining(payload);
                    ssize_t nwritten = write(1, parcBuffer_Overlay(payload, length), length);
                    assertTrue(nwritten == length, "Did not write whole buffer, got %zd expected %zu", nwritten, length);

                    break;
                }
                ccnxMetaMessage_Release(&response);
            }
        }
    }

    ccnxPortal_Release(&portal);

    ccnxPortalFactory_Release(&factory);

    return 0;
}
LONGBOW_TEST_CASE(Global, ccnxPortal_IsEOF)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);

    ccnxPortal_Send(portal, message, CCNxStackTimeout_Never);
    ccnxPortal_Flush(portal, CCNxStackTimeout_Never);

    bool actual = ccnxPortal_IsEOF(portal);
   
    ccnxInterest_Release(&interest);
    ccnxMetaMessage_Release(&message);
   
    ccnxPortal_Release(&portal);

    assertFalse(actual, "Expected to not be at EOF");
}
LONGBOW_TEST_CASE(Global, ccnxPortal_GetError)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
   
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(data->factory, TEST_STACK);

    CCNxName *name = ccnxName_CreateFromCString("lci:/Hello/World");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);
   
    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);
   
    ccnxPortal_Send(portal, message, CCNxStackTimeout_Never);
    ccnxPortal_Flush(portal, CCNxStackTimeout_Never);
   
    const int error = ccnxPortal_GetError(portal);
   
    ccnxMetaMessage_Release(&message);
    ccnxInterest_Release(&interest);
   
    ccnxPortal_Release(&portal);
   
    assertTrue(error == 0, "Expected 0 result from ccnxPortal_GetError");
}
/**
 * Using the CCNxPortal API, listen for and respond to Interests matching our domain prefix (as defined in tutorial_Common.c).
 * The specified directoryPath is the location of the directory from which file and listing responses will originate.
 *
 * @param [in] directoryPath A string containing the path to the directory being served.
 *
 * @return true if at least one Interest is received and responded to, false otherwise.
 */
static bool
_serveDirectory(const char *directoryPath)
{
    bool result = false;

    CCNxPortalFactory *factory = _setupServerPortalFactory();

    CCNxPortal *portal =
        ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message, &ccnxPortalAttributes_Blocking);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer. Is the Forwarder running?");

    CCNxName *domainPrefix = ccnxName_CreateFromURI(tutorialCommon_DomainPrefix);

    if (ccnxPortal_Listen(portal, domainPrefix)) {
        printf("tutorial_Server: now serving files from %s\n", directoryPath);
        result = _receiveAndAnswerInterests(portal, domainPrefix, directoryPath);
    }

    ccnxPortal_Release(&portal);
    ccnxPortalFactory_Release(&factory);

    return result;
}
Exemple #18
0
static const char *
_athenactl_SendInterestControl(PARCIdentity *identity, CCNxMetaMessage *message)
{
    const char *result = NULL;
    CCNxPortalFactory *factory = ccnxPortalFactory_Create(identity);

    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);

    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    athenactl_EncodeMessage(message);

    if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never)) {
        while (ccnxPortal_IsError(portal) == false) {
            CCNxMetaMessage *response = ccnxPortal_Receive(portal, CCNxStackTimeout_Never);
            if (response != NULL) {
                if (ccnxMetaMessage_IsContentObject(response)) {
                    CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);

                    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);

                    if (payload) {
                        result = parcBuffer_ToString(payload);
                    }
                }
                ccnxMetaMessage_Release(&response);
                break;
            }
        }
    }

    ccnxPortal_Release(&portal);

    ccnxPortalFactory_Release(&factory);
    return result;
}
/**
 * Run the consumer to fetch the specified file. Save it to disk once transferred.
 *
 * @param [in] target Name of the content to request.
 * @param [in] outFile Name of the file to which the buffer will be written.
 */
static int
_ccnxFileRepoClient_Run(char *target, char *outFile)
{
    parcSecurity_Init();

    PARCLog *log = _ccnxFileRepoClient_CreateLogger();

    CCNxPortalFactory *factory = _setupConsumerPortalFactory();
    CCNxPortal *portal = ccnxPortalFactory_CreatePortal(factory, ccnxPortalRTA_Message);
    assertNotNull(portal, "Expected a non-null CCNxPortal pointer.");

    CCNxName *name = ccnxName_CreateFromCString(target);
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest);

    if (ccnxPortal_Send(portal, message, CCNxStackTimeout_Never)) {
        while (ccnxPortal_IsError(portal) == false) {
            CCNxMetaMessage *response = ccnxPortal_Receive(portal, CCNxStackTimeout_Never);
            if (response != NULL) {
                if (ccnxMetaMessage_IsManifest(response)) {
                    parcLog_Info(log, "Received root manifest. Beginning to retrieve the content.");

                    // Extract the manifest and instantiate a new fetcher for it
                    CCNxManifest *root = ccnxMetaMessage_GetManifest(response);
                    CCNxFileRepoManifestFetcher *fetcher = ccnxFileRepoManifestFetcher_Create(portal, root);

                    // Initialize the file offset and I/O buffer
                    size_t fileOffset = 0;
                    PARCBuffer *chunkBuffer = parcBuffer_Allocate(ccnxFileRepoCommon_ClientBufferSize);

                    // Start reading from the manifest until done
                    bool done = false;
                    while (!done) {
                        // Reset the buffer information
                        parcBuffer_SetPosition(chunkBuffer, 0);
                        parcBuffer_SetLimit(chunkBuffer, ccnxFileRepoCommon_ClientBufferSize);

                        // Fill the buffer with data from the manifest
                        done = ccnxFileRepoManifestFetcher_FillBuffer(fetcher, chunkBuffer);
                        parcBuffer_Flip(chunkBuffer);

                        // Write the buffer to the file
                        size_t totalSize = parcBuffer_Remaining(chunkBuffer);
                        _ccnxFileRepoClient_AppendBufferToFile(outFile, chunkBuffer, fileOffset);
                        fileOffset += totalSize;

                        // Flip the buffer back around for writing
                        parcBuffer_Flip(chunkBuffer);
                    }
                    parcBuffer_Release(&chunkBuffer);

                    break;
                } else if (ccnxMetaMessage_IsContentObject(response)) {
                    parcLog_Info(log, "Received a content object. Dump the payload and exit.");
                    CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);
                    PARCBuffer *payload = ccnxContentObject_GetPayload(contentObject);
                    _ccnxFileRepoClient_AppendBufferToFile(outFile, payload, 0);
                    break;
                }
            }
            ccnxMetaMessage_Release(&response);
        }
    }

    ccnxPortal_Release(&portal);
    ccnxPortalFactory_Release(&factory);

    parcSecurity_Fini();
    return 0;
}