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); }
/** * 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; }
/** * Listen for arriving Interests and respond to them if possible. We expect that the Portal we are passed is * listening for messages matching the specified domainPrefix. * * @param [in] portal The CCNxPortal that we will read from. * @param [in] domainPrefix A CCNxName containing the domain prefix that the specified `portal` is listening for. * @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 _receiveAndAnswerInterests(CCNxPortal *portal, const CCNxName *domainPrefix, const char *directoryPath) { bool result = false; CCNxMetaMessage *inboundMessage = NULL; while ((inboundMessage = ccnxPortal_Receive(portal)) != NULL) { if (ccnxMetaMessage_IsInterest(inboundMessage)) { CCNxInterest *interest = ccnxMetaMessage_GetInterest(inboundMessage); CCNxContentObject *response = _createInterestResponse(interest, domainPrefix, directoryPath); // At this point, response has either the requested chunk of the request file/command, // or remains NULL. if (response != NULL) { // We had a response, so send it back through the Portal. CCNxMetaMessage *responseMessage = ccnxMetaMessage_CreateFromContentObject(response); if (ccnxPortal_Send(portal, responseMessage) == false) { fprintf(stderr, "ccnxPortal_Send failed (error %d). Is the Forwarder running?\n", ccnxPortal_GetError(portal)); } ccnxMetaMessage_Release(&responseMessage); ccnxContentObject_Release(&response); result = true; // We have received, and responded to, at least one Interest. } } ccnxMetaMessage_Release(&inboundMessage); } return result; }
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); }
int reader_writer(CCNxPortalFactory *factory, const char *uri) { CCNxPortal *portal = ccnxPortalFactory_GetInstance(factory, ccnxPortalTypeDatagram, ccnxPortalProtocol_TLV, &ccnxPortalAttributes_Blocking); CCNxName *prefix = ccnxName_CreateFromURI(uri); CCNxName *bye = ccnxName_CreateFromURI("lci:/Hello/Goodbye%21"); CCNxName *contentname = ccnxName_CreateFromURI("lci:/Hello/World"); if (ccnxPortal_Listen(portal, prefix, 365 * 86400, CCNxStackTimeout_Never)) { CCNxMetaMessage *msg; while ((msg = ccnxPortal_Receive(portal, CCNxStackTimeout_Never)) != NULL) { if (ccnxMetaMessage_IsInterest(msg)) { CCNxInterest *interest = ccnxMetaMessage_GetInterest(msg); CCNxName *interestName = ccnxInterest_GetName(interest); if (ccnxName_Equals(interestName, contentname)) { const PARCKeyId *publisherKeyId = ccnxPortal_GetKeyId(portal); char buffer[128]; time_t theTime = time(0); sprintf(buffer, "Hello World. The time is %s", ctime(&theTime)); PARCBuffer *payload = parcBuffer_CreateFromArray(buffer, 128); parcBuffer_Flip(payload); PARCBuffer *b = parcBuffer_Acquire(payload); CCNxContentObject *uob = ccnxContentObject_CreateWithDataPayload(contentname, b); // missing NULL check, case 1024 CCNxMetaMessage *message = ccnxMetaMessage_CreateFromContentObject(uob); if (ccnxPortal_Send(portal, message, CCNxTransportStackTimeCCNxStackTimeout_Neverout_Never) == false) { fprintf(stderr, "ccnx_write failed\n"); } // ccnxMessage_Release(message); } else if (ccnxName_Equals(interestName, bye)) { break; } } else { ccnxMetaMessage_Display(msg, 0); } ccnxMetaMessage_Release(&msg); } } ccnxName_Release(&prefix); ccnxName_Release(&bye); ccnxName_Release(&contentname); ccnxPortal_Release(&portal); return 0; }
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; }
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, 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_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"); }
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; }
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"); }
/** * 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; }