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_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"); }
/** * 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; }