Rangstr json_get (const char *js, const char *p) { int x, n = 0; size_t rst; Rangstr rj2, rj = rangstr_new (js); Rangstr rs = rangstr_new (p); json_path_first (&rs); do { rst = rs.t; rs.f++; x = rangstr_find (&rs, '['); rs.f--; if (x != -1) rs.t = x; #if 0 printf ("x = %d f = %d t = %d\n", x, rs.f, rs.t); fprintf (stderr, "source (%s)\n", rangstr_dup (&rs)); fprintf (stderr, "onjson (%s)\n", rangstr_dup (&rj)); #endif if (rst == rs.t && n && rj.p) // last key break; if (!rj.p) break; do { rj2 = json_find (rangstr_str (&rj), &rs); //fprintf (stderr, "++ (%s)(%d vs %d)\n", rangstr_dup (&rs), x, rs.t); //if (rj.p[rj.f]=='[') { break; } //fprintf (stderr, "ee %c\n", rj.p[rj.f]); if (!rj2.p) { if (!rj.p[rj.t]) return rj2; break; } rj = rj2; #if 0 fprintf (stderr, "-- (%s)\n", rangstr_dup (&rj)); #endif } while (json_path_next (&rs)); //if (!rj.p) return rj; #if 0 printf ("x = %d\n", x); printf ("rsf = %d\n", rs.f); fprintf (stderr, "xxx (%s)\n", rangstr_dup (&rj)); return rj; #endif if ((rst == rs.t && n && rj.p)) // last key break; rs.t = rst; rs.f = x; n++; } while (x != -1); return rj; }
int json_wlparam_proc_all(JSONNODE* node, wlp_descr_t* wlp, void* params) { int ret; void* param; while(wlp->type != WLP_NULL) { JSONNODE_ITERATOR i_param = json_find(node, wlp->name), i_end = json_end(node); param = ((char*) params) + wlp->off; if(i_param == i_end) { /* If parameter is optional, try to assign it to default value */ if(wlp->flags & WLPF_OPTIONAL) { ret = wlparam_set_default(wlp, param); if(ret == WLPARAM_NO_DEFAULT) { tsload_error_msg(TSE_INTERNAL_ERROR, "Missing default value for %s", wlp->name); return WLPARAM_JSON_NOT_FOUND; } wlp++; continue; } tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s not specified", wlp->name); return WLPARAM_JSON_NOT_FOUND; } ret = json_wlparam_proc(*i_param, wlp, param); if(ret == WLPARAM_JSON_WRONG_TYPE) { tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s has wrong type", wlp->name); return ret; } if(ret == WLPARAM_JSON_OUTSIDE_RANGE) { tsload_error_msg(TSE_INVALID_DATA, "Workload parameter %s outside defined range", wlp->name); return ret; } wlp++; } return WLPARAM_JSON_OK; }
/** * \brief Callback of the HTTP client. * * \param[in] module_inst Module instance of HTTP client module. * \param[in] type Type of event. * \param[in] data Data structure of the event. \refer http_client_data */ static void http_client_callback(struct http_client_module *module_inst, int type, union http_client_data *data) { struct json_obj json, loc; switch (type) { case HTTP_CLIENT_CALLBACK_SOCK_CONNECTED: printf("Connected\r\n"); break; case HTTP_CLIENT_CALLBACK_REQUESTED: printf("Request complete\r\n"); break; case HTTP_CLIENT_CALLBACK_RECV_RESPONSE: printf("Received response %u data size %u\r\n", (unsigned int)data->recv_response.response_code, (unsigned int)data->recv_response.content_length); if (data->recv_response.content != NULL) { if (json_create(&json, data->recv_response.content, data->recv_response.content_length) == 0 && json_find(&json, "loc", &loc) == 0) { printf("Location : %s\r\n", loc.value.s); } } break; case HTTP_CLIENT_CALLBACK_DISCONNECTED: printf("Disconnected reason:%d\r\n", data->disconnected.reason); /* If disconnect reason is equals to -ECONNRESET(-104), * It means Server was disconnected your connection by the keep alive timeout. * This is normal operation. */ if (data->disconnected.reason == -EAGAIN) { /* Server has not responded. retry it immediately. */ http_client_send_request(&http_client_module_inst, MAIN_HTTP_CLIENT_TEST_URL, MAIN_HTTP_CLIENT_TEST_METHOD, NULL); } break; } }
void JSON_DELETE_ITEM_BY_NAME(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT json; C_TEXT name; C_LONGINT option; json.fromParamAtIndex(pParams, 1); name.fromParamAtIndex(pParams, 2); option.fromParamAtIndex(pParams, 3); JSONNODE *n = _fromHex(json); if(n){ std::wstring w; _copyString(name, w); JSONNODE_ITERATOR i; switch (option.getIntValue()) { case 1: i = json_find_nocase(n, w.c_str()); break; default: i = json_find(n, w.c_str()); break; } /* while (i != json_end(n)){ json_erase(n, i); ++i; } */ if (i != json_end(n)){ json_erase(n, i); } } }
static void linda_store_handler(void *p1, const uuid *u, const void *_s, int len) { linda *l = (linda*)p1; const char *s = (const char*)_s; if (len > 0) // add { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_store_handler: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_set(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_store_handler: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_set(l->sl, k, u); } json_close(j); } else if (len < 0) // remove (with hint) { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_out: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_erase(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_out: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_erase(l->sl, k, u); } json_close(j); } else // remove (brute search) { sb_uuid_efface(l->sl, u); } }
int linda_rm(hlinda *h, const char *s) { json *j = json_open(s); json *j1 = json_get_object(j); int is_int = 0, is_string = 0; const char *string_id = NULL; long long int_id = 0; uuid u; json *joid = json_find(j1, LINDA_OID); if (!joid) { json_close(j); return 0; } uuid_from_string(json_get_string(joid), &u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { int_id = json_get_integer(jid); is_int = 1; } else if (json_is_string(jid)) { string_id = json_get_string(jid); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } json_close(j); if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, int_id); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, int_id, &u); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, string_id, &u); } else { store_rem(h->l->st, &u); sb_uuid_efface(h->l->sl, &u); } return 1; }
int linda_out(hlinda *h, const char *s) { if (!h) return 0; json *j = json_open(s); json *j1 = json_get_object(j); json *joid = json_find(j1, LINDA_OID); uuid u; if (joid) { uuid_from_string(json_get_string(joid), &u); } else uuid_gen(&u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (h->l->sl && !h->l->is_int) { printf("linda_out: expected integer id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_int_uuid_create2(); h->l->is_int = 1; } sb_int_uuid_set(h->l->sl, k, &u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (h->l->sl && !h->l->is_string) { printf("linda_out: expected string id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_string_uuid_create2(); h->l->is_string = 1; } sb_string_uuid_set(h->l->sl, k, &u); } } store_hadd(h->hst, &u, s, strlen(s)); h->last_oid = u; json_close(j); return 0; }
static int linda_read(hlinda *h, const char *s, const char **buf, int rm, int nowait) { json *j = json_open(s); json *j1 = json_get_object(j); h->oid.u1 = h->oid.u2 = 0; int is_int = 0, is_string = 0; json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { h->int_id = json_get_integer(jid); h->jquery = json_open(s); sb_int_uuid_find(h->l->sl, h->int_id, &read_int_handler, h); json_close(h->jquery); is_int = 1; } else if (json_is_string(jid)) { h->string_id = json_get_string(jid); h->jquery = json_open(s); sb_string_uuid_find(h->l->sl, h->string_id, &read_string_handler, h); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } else { h->jquery = json_open(s); sb_iter(h->l->sl, &read_handler, h); json_close(h->jquery); } json_close(j); if (!h->oid.u1 && !h->oid.u2) return 0; if (rm) { if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, h->int_id); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, h->int_id, &h->oid); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(h->string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, h->string_id, &h->oid); } else { store_hrem(h->hst, &h->oid); sb_uuid_efface(h->l->sl, &h->oid); } } *buf = h->dst; return 1; }
static int read_handler(void *arg, void *k, void *v) { hlinda *h = (hlinda*)arg; uuid *u = (uuid*)v; if (!store_get(h->l->st, u, (void**)&h->dst, &h->len)) return 0; int match = 1; json *j1 = json_get_object(h->jquery); json *jdst = json_open(h->dst); json *j2 = json_get_object(jdst); size_t i, cnt = json_count(j1); for (i = 0; i < cnt; i++) { json *j1it = json_index(j1, i); const char *name = json_get_string(j1it); if (name[0] == '$') continue; json *j2it = json_find(j2, name); if (!j2it) { match = 0; continue; } if (json_is_integer(j1it)) { if (!json_is_integer(j2it)) { match = 0; break; } if (json_get_integer(j1it) != json_get_integer(j2it)) { match = 0; break; } } else if (json_is_real(j1it)) { if (!json_is_real(j2it)) { match = 0; break; } if (json_get_real(j1it) != json_get_real(j2it)) { match = 0; break; } } else if (json_is_string(j1it)) { if (!json_is_string(j2it)) { match = 0; break; } if (strcmp(json_get_string(j1it), json_get_string(j2it))) { match = 0; break; } } else if (json_is_true(j1it)) { if (!json_is_true(j2it)) { match = 0; break; } } else if (json_is_false(j1it)) { if (!json_is_false(j2it)) { match = 0; break; } } else if (json_is_null(j1it)) { if (!json_is_null(j2it)) { match = 0; break; } } } json_close(jdst); if (!match) return 1; h->oid.u1 = u->u1; h->oid.u2 = u->u2; return 0; }
static struct host parseHost (json_value *hostObj, int *ret) { json_value *jName = json_find(hostObj, "name", json_string); json_value *jMac = json_find(hostObj, "mac", json_string); if ((NULL == jName) || (NULL == jMac)) return (*ret = 1), (struct host) {0}; struct host result = { .name = utilStringDup(jName->u.string.ptr), .macAddress = utilStringDup(jMac->u.string.ptr) }; return result; } static struct host * parseHosts (json_value *root, size_t *numHosts) { json_value *jHosts = json_find(root, "hosts", json_array); if (NULL == jHosts) return NULL; const size_t numJHosts = jHosts->u.array.length; struct host *result = calloc(numJHosts, sizeof(*result)); for (size_t i=0; i<numJHosts; i++) { json_value *jHostObj = jHosts->u.array.values[i]; int ret = 0; result[i] = parseHost(jHostObj, &ret); if (ret) { free(result); return NULL; } } *numHosts = numJHosts; return result; } static struct resource parseResource (json_value *jResource, int *ret) { json_value *jName = json_find(jResource, "name", json_string); json_value *jHost = json_find(jResource, "host", json_string); json_value *jVars = json_find(jResource, "vars", json_array); if ((NULL == jName) || (NULL == jHost) || (NULL == jVars)) return (*ret = 1), (struct resource) {0}; const size_t numVars = jVars->u.array.length; char **keyMap = calloc(numVars, sizeof(char *)); char **valMap = calloc(numVars, sizeof(char *)); for (size_t i=0; i<numVars; i++) { json_value *jVar = jVars->u.array.values[i]; if (jVar->type != json_array) return (*ret = 1), (struct resource) {0}; if (jVar->u.array.length != 2) return (*ret = 1), (struct resource) {0}; json_value *jKey = jVar->u.array.values[0]; json_value *jVal = jVar->u.array.values[1]; if (jKey->type != json_string) return (*ret = 1), (struct resource) {0}; if (jVal->type != json_string) return (*ret = 1), (struct resource) {0}; keyMap[i] = utilStringDup(jKey->u.string.ptr); valMap[i] = utilStringDup(jVal->u.string.ptr); } struct resource result = { .name = utilStringDup(jName->u.string.ptr), .host = utilStringDup(jHost->u.string.ptr), .numVars = numVars, .keyMap = keyMap, .valMap = valMap, }; return result; } struct resource * parseResources (json_value *root, size_t *numResources) { json_value *jResources = json_find(root, "resources", json_array); if (NULL == jResources) return NULL; const size_t numJResources = jResources->u.array.length; struct resource *result = calloc(numJResources, sizeof(*result)); for (size_t i=0; i<numJResources; i++) { json_value *jResource = jResources->u.array.values[i]; int ret = 0; result[i] = parseResource(jResource, &ret); result[i].id = i; #if defined(CLUSTERD_BUILD) result[i].mutex = calloc(1, sizeof(pthread_mutex_t)); result[i].inUse = calloc(1, sizeof(bool)); pthread_mutex_init(result[i].mutex, NULL); #endif } *numResources = numJResources; return result; } configuration * parseConfiguration (void) { char *jsonConfig = readFile(_configPath); if (NULL == jsonConfig) { fprintf(stderr, "Error: Unable to read configuration file\n"); fprintf(stderr, "Does %s exist?\n", _configPath); return NULL; } json_value *root = json_parse(jsonConfig, strlen(jsonConfig)); free(jsonConfig); if (NULL == root) { fprintf(stderr, "Error: Configuration file %s is malformed\n", _configPath); fprintf(stderr, "Unable to parse json\n"); return NULL; } configuration *result = calloc(1, sizeof(configuration)); result->hosts = parseHosts(root, &result->numHosts); if (NULL == result->hosts) { fprintf(stderr, "Error: Configuration file %s is malformed\n", _configPath); fprintf(stderr, "Unable to parse hosts\n"); return NULL; } result->resources = parseResources(root, &result->numResources); if (NULL == result->hosts) { fprintf(stderr, "Error: Configuration file %s is malformed\n", _configPath); fprintf(stderr, "Unable to parse resources\n"); return NULL; } json_value *jMaster = json_find(root, "master", json_string); if (NULL == jMaster) { fprintf(stderr, "Error: No master hostname set\n"); return NULL; } if (jMaster->type != json_string) { fprintf(stderr, "Error: Master feild is not a string\n"); return NULL; } result->master = utilStringDup(jMaster->u.string.ptr); json_value_free(root); return result; }
static int parse_line(mux_ctx_t ctx, ff_msg_t msg) { #define SLEN(x) (sizeof(x) - 1) static const char pltf[] = "\"platform\""; static const char bid[] = "\"bid\""; static const char ask[] = "\"ask\""; const char *json = msg->json; const char *p; size_t psz; assert(msg->chan); assert(msg->json); if (UNLIKELY(msg->json[0] != '{')) { /* don't know what this is, could be #time */ goto bugger; } /* assign the time stamp */ { register unsigned int s = msg->rcv_stmp.sec; register unsigned int ms = msg->rcv_stmp.nsec / 1000000U; ssnp_set_stmp_sec(msg->snp, s); ssnp_set_stmp_msec(msg->snp, (uint16_t)ms); } /* construct the s**t name */ if (LIKELY((psz = json_find(&p, json, pltf, SLEN(pltf))) > 0)) { const char *q = p + psz; char sym[SLUT_SYMLEN]; uint16_t idx; if (*p++ != '"' || *--q != '"') { goto bugger; } /* otherwise it's all good */ memcpy(sym, msg->chan + 2, 6); sym[6] = '.'; if ((size_t)(q - p) > sizeof(sym) - 8) { goto bugger; } memcpy(sym + 7, p, (q - p)); sym[7 + (q - p)] = '\0'; /* little can go wrong from here, so just find the idx */ idx = (uint16_t)ute_sym2idx(ctx->wrr, sym); ssnp_set_tblidx(msg->snp, idx); ssnp_set_ttf(msg->snp, 0); } else { goto bugger; } /* care about the payload now */ msg->snp->bp = msg->snp->ap = 0; /* bid */ if (LIKELY((psz = json_find(&p, json, bid, SLEN(bid))) > 0)) { m30_t b = ffff_m30_get_s(&p); msg->snp->bp = b.u; } else { goto bugger; } /* ask */ if (LIKELY((psz = json_find(&p, json, ask, SLEN(ask))) > 0)) { m30_t a = ffff_m30_get_s(&p); msg->snp->ap = a.u; } else { goto bugger; } return 0; bugger: fputs("json b0rked> ", stderr); fputs(msg->json, stderr); fputc('\n', stderr); return -1; }