コード例 #1
0
ファイル: test_parc_JSON.c プロジェクト: PARC/Libparc
LONGBOW_TEST_CASE(JSON, parcJSON_GetByPath)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    PARCJSON *json = data->json;

    char *s = parcJSON_ToString(json);
    printf("%s\n", s);
    parcMemory_Deallocate((void **) &s);

    const PARCJSONValue *value = parcJSON_GetByPath(json, "/string");
    assertTrue(parcJSONValue_IsString(value), "Expected /string to be a string type.");
    value = parcJSON_GetByPath(json, "/null");
    assertTrue(parcJSONValue_IsNull(value), "Expected /null to be a null type.");
    value = parcJSON_GetByPath(json, "/true");
    assertTrue(parcJSONValue_IsBoolean(value), "Expected /true to be a boolean type.");
    value = parcJSON_GetByPath(json, "/integer");
    assertTrue(parcJSONValue_IsNumber(value), "Expected /integer to be a number type.");
    value = parcJSON_GetByPath(json, "/float");
    assertTrue(parcJSONValue_IsNumber(value), "Expected /float to be a number type.");
    value = parcJSON_GetByPath(json, "/array");
    assertTrue(parcJSONValue_IsArray(value), "Expected /array to be an array type.");
    value = parcJSON_GetByPath(json, "/nonexistent");
    assertNull(value, "Expected /nonexistent to be NULL");

    value = parcJSON_GetByPath(json, "/array/1");
    assertTrue(parcJSONValue_IsBoolean(value), "Expected /array/0 to be a boolean type.");

    value = parcJSON_GetByPath(json, "/array/5");
    assertTrue(parcJSONValue_IsArray(value), "Expected /array/5 to be an array type.");

    assertNotNull(value, "Expected non-null pair");
}
コード例 #2
0
char *
ccnxControlFacade_ToString(const CCNxTlvDictionary *contentDictionary)
{
    char *string;
    char *jsonString = NULL;

    PARCJSON *json = ccnxControlFacade_GetJson(contentDictionary);
    if (json != NULL) {
        jsonString = parcJSON_ToString(json);
    }

    int failure = asprintf(&string, "CCNxControl { isCPI=%s, isNotification=%s, JSON=\"%s\"}",
                           ccnxControlFacade_IsCPI(contentDictionary) ? "true" : "false",
                           ccnxControlFacade_IsNotification(contentDictionary) ? "true" : "false",
                           jsonString != NULL ? jsonString : "NULL");


    if (jsonString) {
        parcMemory_Deallocate((void **) &jsonString);
    }

    assertTrue(failure > -1, "Error asprintf");

    char *result = parcMemory_StringDuplicate(string, strlen(string));
    free(string);

    return result;
}
コード例 #3
0
ファイル: test_parc_JSON.c プロジェクト: PARC/Libparc
LONGBOW_TEST_CASE(JSON, parcJSON_ToString)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    char *actual = parcJSON_ToString(data->json);
    assertTrue(strcmp(data->expected, actual) == 0, "Expected %s, actual %s", data->expected, actual);

    parcMemory_Deallocate((void **) &actual);
}
コード例 #4
0
char *
ccnxManifestHashGroup_ToString(const CCNxManifestHashGroup *section)
{
    PARCJSON *json = ccnxManifestHashGroup_ToJson(section);
    char *stringRep = parcJSON_ToString(json);
    parcJSON_Release(&json);
    return stringRep;
}
コード例 #5
0
static void
_cmd_Version(_MetisCommandLineInterface_Session *session, _MetisCommandLineInterface_Command *command, const char *params)
{
    PARCJSON *versionJson = metisConfiguration_GetVersion(metisForwarder_GetConfiguration(session->parentCli->metis));
    char *str = parcJSON_ToString(versionJson);
    parcEventQueue_Printf(session->streamBuffer, "%s", str);
    parcMemory_Deallocate((void **) &str);
    parcJSON_Release(&versionJson);
}
コード例 #6
0
ファイル: parc_ObjectTesting.c プロジェクト: isolis/Libparc
static void
_parcObjectTesting_AssertToJSON(const PARCObject *instance)
{
    PARCJSON *json = parcObject_ToJSON(instance);
    char *result = parcJSON_ToString(json);
    assertNotNull(result, "Something should be returned");
    parcMemory_Deallocate(&result);
    parcJSON_Release(&json);
}
コード例 #7
0
ファイル: test_parc_JSON.c プロジェクト: PARC/Libparc
LONGBOW_TEST_CASE(JSON, parcJSON_GetMembers)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    char *s = parcJSON_ToString(data->json);
    parcMemory_Deallocate((void **) &s);

    PARCList *members = parcJSON_GetMembers(data->json);
    assertTrue(parcList_Size(members) == 8, "Expected 8, actual %zd", parcList_Size(members));
}
コード例 #8
0
PARCBufferComposer *
ccnxPortalAnchor_Serialize(const CCNxPortalAnchor *namePrefix, PARCBufferComposer *composer)
{
    PARCJSON *json = ccnxPortalAnchor_ToJSON(namePrefix);

    char *string = parcJSON_ToString(json);
    parcBufferComposer_PutString(composer, string);
    parcMemory_Deallocate(&string);
    parcJSON_Release(&json);
    return composer;
}
コード例 #9
0
/**
 * Returns the inner operation JSON from the request.
 *
 *   INPUT: "{ CPI_REQUEST: { SEQUENCE:number key: { operation } }}"
 *   OUTPUT: "{ key : { operation } }"
 *
 *   Example return: "{ operation }"
 * @param <#param1#>
 * @return The inner json, do not destroy it
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
PARCJSONPair *
cpi_ParseRequest(PARCJSON *request)
{
    PARCJSONValue *value = parcJSON_GetValueByName(request, cpiRequest);
    assertNotNull(value, "Could not find JSON key %s in %s", cpiRequest, parcJSON_ToString(request));
    assertTrue(parcJSONValue_IsJSON(value), "cpiRequest is unexpected type");

    PARCJSON *requestJson = parcJSONValue_GetJSON(value);
    PARCJSONPair *result = parcJSON_GetPairByIndex(requestJson, 1);

    return result;
}
コード例 #10
0
ファイル: athena_LRUContentStore.c プロジェクト: PARC/Athena
/**
 * Create a PARCBuffer payload containing a JSON string with information about this ContentStore's
 * size.
 */
static PARCBuffer *
_createStatSizeResponsePayload(const AthenaLRUContentStore *impl, const CCNxName *name, uint64_t chunkNumber)
{
    PARCJSON *json = parcJSON_Create();

    parcJSON_AddString(json, "moduleName", AthenaContentStore_LRUImplementation.description);
    parcJSON_AddInteger(json, "time", parcClock_GetTime(impl->wallClock));
    parcJSON_AddInteger(json, "numEntries", impl->numEntries);
    parcJSON_AddInteger(json, "sizeInBytes", impl->currentSizeInBytes);

    char *jsonString = parcJSON_ToString(json);

    parcJSON_Release(&json);

    PARCBuffer *result = parcBuffer_CreateFromArray(jsonString, strlen(jsonString));

    parcMemory_Deallocate(&jsonString);

    return parcBuffer_Flip(result);
}
コード例 #11
0
ファイル: athena_InterestControl.c プロジェクト: PARC/Athena
static CCNxMetaMessage *
_create_stats_response(Athena *athena, CCNxName *ccnxName)
{
    PARCJSON *json = parcJSON_Create();

    struct timeval tv;
    gettimeofday(&tv, NULL);

    uint64_t nowInMillis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);

    parcJSON_AddString(json, "moduleName", athenaAbout_Name());
    parcJSON_AddInteger(json, "time", nowInMillis);
    parcJSON_AddInteger(json, "numProcessedInterests",
                        athena->stats.numProcessedInterests);
    parcJSON_AddInteger(json, "numProcessedContentObjects",
                        athena->stats.numProcessedContentObjects);
    parcJSON_AddInteger(json, "numProcessedControlMessages",
                        athena->stats.numProcessedControlMessages);
    parcJSON_AddInteger(json, "numProcessedInterestReturns",
                        athena->stats.numProcessedInterestReturns);

    char *jsonString = parcJSON_ToString(json);

    parcJSON_Release(&json);

    PARCBuffer *payload = parcBuffer_CreateFromArray(jsonString, strlen(jsonString));

    parcMemory_Deallocate(&jsonString);

    CCNxContentObject *contentObject =
        ccnxContentObject_CreateWithNameAndPayload(ccnxName, parcBuffer_Flip(payload));
    ccnxContentObject_SetExpiryTime(contentObject, nowInMillis + 100); // this response is good for 100 millis

    CCNxMetaMessage *result = ccnxMetaMessage_CreateFromContentObject(contentObject);

    ccnxContentObject_Release(&contentObject);
    parcBuffer_Release(&payload);

    return result;
}
コード例 #12
0
CpiMessageType
controlPlaneInterface_GetCPIMessageType(PARCJSON *json)
{
    assertNotNull(json, "Invalid state, got NULL json from control message");

    PARCJSONValue *value = parcJSON_GetValueByName(json, cpiResponse);
    if (value != NULL) {
        return CPI_RESPONSE;
    }

    value = parcJSON_GetValueByName(json, cpiRequest);
    if (value != NULL) {
        return CPI_REQUEST;
    }

    value = parcJSON_GetValueByName(json, cpiAck);
    if (value != NULL) {
        return CPI_ACK;
    }

    trapIllegalValue(json, "Expected CpiMessageType, actual %s", parcJSON_ToString(json));
}
コード例 #13
0
CPIConnectionList *
cpiConnectionList_FromJson(PARCJSON *json)
{
    assertNotNull(json, "Parameter must be non-null");

    PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_ConnectionList);
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpi_ConnectionList,
                  parcJSON_ToString(json));
    PARCJSONArray *tunnelListJson = parcJSONValue_GetArray(value);

    CPIConnectionList *list = cpiConnectionList_Create();

    size_t length = parcJSONArray_GetLength(tunnelListJson);
    for (size_t i = 0; i < length; i++) {
        value = parcJSONArray_GetValue(tunnelListJson, i);
        PARCJSON *tunnelJson = parcJSONValue_GetJSON(value);
        CPIConnection *tunnel = cpiConnection_CreateFromJson(tunnelJson);
        cpiConnectionList_Append(list, tunnel);
    }

    return list;
}
コード例 #14
0
CPIInterfaceSet *
cpiInterfaceSet_FromJson(PARCJSON *json)
{
    assertNotNull(json, "Parameter must be non-null");

    PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_InterfaceList);
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpi_InterfaceList,
                  parcJSON_ToString(json));
    PARCJSONArray *ifaceSetJson = parcJSONValue_GetArray(value);

    CPIInterfaceSet *set = cpiInterfaceSet_Create();

    size_t length = parcJSONArray_GetLength(ifaceSetJson);
    for (size_t i = 0; i < length; i++) {
        value = parcJSONArray_GetValue(ifaceSetJson, i);
        PARCJSON *ifaceJson = parcJSONValue_GetJSON(value);
        CPIInterface *iface = cpiInterface_FromJson(ifaceJson);
        cpiInterfaceSet_Add(set, iface);
    }

    return set;
}
コード例 #15
0
ファイル: athena_InterestControl.c プロジェクト: PARC/Athena
static CCNxMetaMessage *
_create_FIBList_response(Athena *athena, CCNxName *ccnxName, PARCList *fibEntryList)
{
    PARCJSON *jsonPayload = parcJSON_Create();
    PARCJSONArray *jsonEntryList = parcJSONArray_Create();
    parcJSON_AddArray(jsonPayload, JSON_KEY_RESULT, jsonEntryList);

    for (size_t i = 0; i < parcList_Size(fibEntryList); ++i) {
        AthenaFIBListEntry *entry = parcList_GetAtIndex(fibEntryList, i);
        if (entry != NULL) {
            CCNxName *prefixName = athenaFIBListEntry_GetName(entry);
            if (prefixName) {
                char *prefix = ccnxName_ToString(prefixName);
                int linkId = athenaFIBListEntry_GetLinkId(entry);
                const char *linkName = athenaTransportLinkAdapter_LinkIdToName(athena->athenaTransportLinkAdapter, linkId);
                parcLog_Debug(athena->log, "  Route: %s->%s", prefix, linkName);

                PARCJSON *jsonItem = parcJSON_Create();
                parcJSON_AddString(jsonItem, JSON_KEY_NAME, prefix);
                parcJSON_AddString(jsonItem, JSON_KEY_LINK, linkName);

                PARCJSONValue *jsonItemValue = parcJSONValue_CreateFromJSON(jsonItem);
                parcJSON_Release(&jsonItem);

                parcJSONArray_AddValue(jsonEntryList, jsonItemValue);
                parcJSONValue_Release(&jsonItemValue);

                parcMemory_Deallocate(&prefix);
            } else {
                int linkId = athenaFIBListEntry_GetLinkId(entry);
                const char *linkName = athenaTransportLinkAdapter_LinkIdToName(athena->athenaTransportLinkAdapter, linkId);
                parcLog_Debug(athena->log, "  Route: <empty>->%s", linkName);

                PARCJSON *jsonItem = parcJSON_Create();
                parcJSON_AddString(jsonItem, JSON_KEY_NAME, "");
                parcJSON_AddString(jsonItem, JSON_KEY_LINK, linkName);

                PARCJSONValue *jsonItemValue = parcJSONValue_CreateFromJSON(jsonItem);
                parcJSON_Release(&jsonItem);

                parcJSONArray_AddValue(jsonEntryList, jsonItemValue);
                parcJSONValue_Release(&jsonItemValue);
            }
        }
    }

    char *jsonString = parcJSON_ToString(jsonPayload);

    parcJSONArray_Release(&jsonEntryList);
    parcJSON_Release(&jsonPayload);

    PARCBuffer *payload = parcBuffer_CreateFromArray(jsonString, strlen(jsonString));

    CCNxContentObject *contentObject =
        ccnxContentObject_CreateWithNameAndPayload(ccnxName, parcBuffer_Flip(payload));

    struct timeval tv;
    gettimeofday(&tv, NULL);
    uint64_t nowInMillis = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
    ccnxContentObject_SetExpiryTime(contentObject, nowInMillis + 100); // this response is good for 100 millis

    CCNxMetaMessage *result = ccnxMetaMessage_CreateFromContentObject(contentObject);

    ccnxContentObject_Release(&contentObject);
    parcBuffer_Release(&payload);
    parcMemory_Deallocate(&jsonString);

    athena_EncodeMessage(result);
    return result;
}
コード例 #16
0
CpiOperation
cpi_getCPIOperation2(const PARCJSON *json)
{
    PARCJSONValue *cpi_value = parcJSON_GetValueByName(json, cpiRequest);

    if (cpi_value == NULL) {
        cpi_value = parcJSON_GetValueByName(json, cpiResponse);
    }
    assertNotNull(cpi_value, "Could not get Request or response");

    PARCJSON *cpi_json = parcJSONValue_GetJSON(cpi_value);

    /*
     * The JSON is defined as { REQUEST : { SEQUENCE: xxx, <OPERATION>: xxx } }
     * so we want to get the key of the 2nd item (index 1) of the array of objects
     * under the request
     */

    PARCJSONPair *item1Pair = parcJSON_GetPairByIndex(cpi_json, 1);
    PARCBuffer *name = parcJSONPair_GetName(item1Pair);
    const char *p = parcBuffer_Overlay(name, 0);

    if (strncasecmp(p, cpiForwarding_AddRouteJsonTag(), strlen(cpiForwarding_AddRouteJsonTag())) == 0) {
        return CPI_REGISTER_PREFIX;
    }

    if (strncasecmp(p, cpiForwarding_RemoveRouteJsonTag(), strlen(cpiForwarding_RemoveRouteJsonTag())) == 0) {
        return CPI_UNREGISTER_PREFIX;
    }

    if (strncasecmp(p, cpiPause, strlen(cpiPause)) == 0) {
        return CPI_PAUSE;
    }

    if (strncasecmp(p, cpiFlush, strlen(cpiFlush)) == 0) {
        return CPI_FLUSH;
    }

    if (strncasecmp(p, cpiCancelFlow_CancelFlowJsonTag(), strlen(cpiCancelFlow_CancelFlowJsonTag())) == 0) {
        return CPI_CANCEL_FLOW;
    }

    if (strncasecmp(p, cpiLinks_InterfaceListJsonTag(), strlen(cpiLinks_InterfaceListJsonTag())) == 0) {
        return CPI_INTERFACE_LIST;
    }

    if (strncasecmp(p, cpiForwarding_RouteListJsonTag(), strlen(cpiForwarding_RouteListJsonTag())) == 0) {
        return CPI_PREFIX_REGISTRATION_LIST;
    }

    if (strncasecmp(p, cpiLinks_CreateTunnelJsonTag(), strlen(cpiLinks_CreateTunnelJsonTag())) == 0) {
        return CPI_CREATE_TUNNEL;
    }

    if (strncasecmp(p, cpiLinks_RemoveTunnelJsonTag(), strlen(cpiLinks_RemoveTunnelJsonTag())) == 0) {
        return CPI_REMOVE_TUNNEL;
    }

    if (strncasecmp(p, cpiLinks_ConnectionListJsonTag(), strlen(cpiLinks_ConnectionListJsonTag())) == 0) {
        return CPI_CONNECTION_LIST;
    }

    if (strncasecmp(p, cpiLinks_AddEtherConnectionJasonTag(), strlen(cpiLinks_AddEtherConnectionJasonTag())) == 0) {
        return (CPI_ADD_ETHER_CONNECTION);
    }

    // Refactor, case 1026
    if (strncasecmp(p, "AddConnEther", strlen("AddConnEther")) == 0) {
        return CPI_ADD_CONNECTION_ETHERNET;
    }

    // Refactor, case 1026
    if (strncasecmp(p, "RemoveConnEther", strlen("RemoveConnEther")) == 0) {
        return CPI_REMOVE_CONNECTION_ETHERNET;
    }

    // Refactor, case 1026
    if (strncasecmp(p, "AddListener", strlen("AddListener")) == 0) {
        return CPI_ADD_LISTENER;
    }

    // Refactor, case 1026
    if (strncasecmp(p, "RemoveListener", strlen("RemoveListener")) == 0) {
        return CPI_REMOVE_LISTENER;
    }

    trapIllegalValue(json, "Could not parse: %s\n", parcJSON_ToString(json));
}