コード例 #1
0
int StructuredLearnerRpc::FindOrCreateSession(const Json::Value& root, Json::Value& response) {
  int sess_ind = -1;
  char sess_id[1000];
  char errStr[1000];
  if(strlen(root.get("session_id", "").asString().c_str()) < 1000)
    strcpy(sess_id, root.get("session_id", "").asString().c_str());
  else
    strcpy(sess_id, "");
  
  if(!strlen(sess_id)) {
    if(!NewSession(root, response) || !InitializeSession(root, response)) {
      JSON_ERROR("Failed to create new session in classify_example\n", -1);
    }
    sess_ind = num_sessions-1;//FindSession(response.get("session_id", "").asString().c_str());
    assert(sess_ind >= 0);
    //fprintf(stderr, "New Session %d\n", sess_ind);
  } else {
    sess_ind=FindSession(sess_id, true);
    if(sess_ind < 0) {
      sprintf(errStr, "Invalid session_id %s in classify_example()\n", sess_id);  JSON_ERROR(errStr, sess_ind); 
    }
    //fprintf(stderr, "Found Session %d %s\n", sess_ind, sess_id);
  }
  response["session_id"] = sessions[sess_ind].id;
  return sess_ind;
}
コード例 #2
0
bool StructuredLearnerRpc::ClassifyExample(const Json::Value& root, Json::Value& response) {
  if(learner) {
    int sess_ind = FindOrCreateSession(root, response);
    if(sess_ind < 0) 
      return false;
    
    if(root.isMember("partial_label")) {
      if(!sessions[sess_ind].partial_label)
        sessions[sess_ind].partial_label = learner->NewStructuredLabel(sessions[sess_ind].example->x);
      if(!sessions[sess_ind].partial_label->load(root["partial_label"], learner)) {
	JSON_ERROR("Invalid 'partial_label' parameter", sess_ind); 
      }
    }

    SparseVector *w = learner->GetCurrentWeights();
    double score = learner->Inference(sessions[sess_ind].example->x, sessions[sess_ind].example->y, w, 
                               sessions[sess_ind].partial_label);
    
    if(!isnan(score)) response["score"] = score;
    response["y"] = sessions[sess_ind].example->y->save(learner);
    if(root.isMember("visualization")) {
      char fname[1000]; strcpy(fname, root["visualization"].asString().c_str());
      learner->VisualizeExample(fname, sessions[sess_ind].example, NULL);
    }
    delete w;

    UnlockSession(sess_ind);

    return true;
  } else
    return false;
}
コード例 #3
0
bool StructuredLearnerRpc::AddNewExample(const Json::Value& root, Json::Value& response) {
  if(learner) {
    char sess_id[1000];
    int sess_ind;
    if(strlen(root.get("session_id", "").asString().c_str()) < 1000)
      strcpy(sess_id, root.get("session_id", "").asString().c_str());
    else
      strcpy(sess_id, "");

    StructuredExample *ex = NULL;
    sess_ind = FindSession(sess_id, true);
    if(sess_ind >= 0) {
      ex = sessions[sess_ind].example;
      response["session_id"] = sessions[sess_ind].id;
    } else {
      ex = new StructuredExample;
      ex->x = learner->NewStructuredData();
      ex->y = learner->NewStructuredLabel(ex->x);
    }

    if(root.isMember("x")) {
      if(!ex->x->load(root["x"], learner)) { JSON_ERROR("Invalid 'x' parameter", sess_ind); }
    }
    if(root.isMember("y")) {
      if(!ex->y->load(root["y"], learner)) { JSON_ERROR("Invalid 'x' parameter", sess_ind); }
    } else if(sess_ind < 0) {
      delete ex;
      JSON_ERROR("No 'y' parameter specified", sess_ind); 
    }

    int ind = learner->AddExample(ex->x, ex->y);
    if(ind < 0) { JSON_ERROR("Error adding example\n", sess_ind) }
    response["index"] = ind;

    learner->SaveTrainingSet(ind);

    if(sess_ind >= 0)
      UnlockSession(sess_ind);
    else {
      delete ex;
    }

    return true;
  } else
    return false;
コード例 #4
0
/** 
 * @brief Checks if the message has subscription field with
 * subscribe=true
 * 
 * @param message
 * 
 * @retval true if has subscribe=true, false otherwise
 */
bool
LSMessageIsSubscription(LSMessage *message)
{
    bool ret = false;
    struct json_object *sub_object = NULL;
    const char *payload = LSMessageGetPayload(message);

    struct json_object *object = json_tokener_parse(payload);
    if (JSON_ERROR(object))
        goto exit;
   
    if (!json_object_object_get_ex(object, "subscribe", &sub_object) || JSON_ERROR(sub_object))
        goto exit;

    _LSErrorIfFail(json_object_get_type(sub_object) == json_type_boolean, NULL);
    
    ret = json_object_get_boolean(sub_object);

exit:
    if (!JSON_ERROR(object))
        json_object_put(object);
    return ret;
}
コード例 #5
0
bool
_LSPrivateGetMallinfo(LSHandle* sh, LSMessage *message, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);

    struct json_object *ret_obj = NULL;
    struct json_object *true_obj = NULL;
    struct json_object *mallinfo_obj = NULL;
    struct json_object *allocator_name_obj = NULL;
    struct json_object *slot_a_obj = NULL;
    struct json_object *slot_d_obj = NULL;
    struct json_object *slot_e_obj = NULL;
    struct json_object *slot_f_obj = NULL;
    struct json_object *slot_h_obj = NULL;
    struct json_object *slot_i_obj = NULL;
    struct json_object *slot_j_obj = NULL;

    const char *sender = LSMessageGetSenderServiceName(message);

    if (!sender || strcmp(sender, MONITOR_NAME) != 0)
    {
        g_critical("WARNING: mallinfo debug method not called by monitor;"
                   " ignoring (service name: %s, unique_name: %s)",
                   sender, LSMessageGetSender(message));
        return true;
    }

    ret_obj = json_object_new_object();
    if (JSON_ERROR(ret_obj)) goto error;
       
    true_obj = json_object_new_boolean(true);
    if (JSON_ERROR(true_obj)) goto error;
 
    mallinfo_obj = json_object_new_object();
    if (JSON_ERROR(mallinfo_obj)) goto error;

    /* returnValue: true,
     * mallinfo: {key: int,...}
     */

    typedef struct mallinfo (*mallinfo_t)();
    static mallinfo_t mallinfo_p = NULL;

    if (mallinfo_p == NULL) {
        mallinfo_p = (mallinfo_t)dlsym(RTLD_DEFAULT, "mallinfo");
        if (mallinfo_p == NULL)
            mallinfo_p = (mallinfo_t)-1;
    }
    struct mallinfo mi;
    if (mallinfo_p != (mallinfo_t)-1) {
        mi = mallinfo_p();
    } else {
        memset(&mi, '\0', sizeof(mi));
    }
    
    allocator_name_obj = json_object_new_string("ptmalloc");
    if (JSON_ERROR(allocator_name_obj)) goto error;

    slot_a_obj = json_object_new_int(mi.arena);
    if (JSON_ERROR(slot_a_obj)) goto error;

    slot_d_obj = json_object_new_int(mi.hblks);
    if (JSON_ERROR(slot_d_obj)) goto error;

    slot_e_obj = json_object_new_int(mi.hblkhd);
    if (JSON_ERROR(slot_e_obj)) goto error;

    slot_f_obj = json_object_new_int(mi.usmblks);
    if (JSON_ERROR(slot_f_obj)) goto error;

    slot_h_obj = json_object_new_int(mi.uordblks);
    if (JSON_ERROR(slot_h_obj)) goto error;

    slot_i_obj = json_object_new_int(mi.fordblks);
    if (JSON_ERROR(slot_i_obj)) goto error;
    
    slot_j_obj = json_object_new_int(mi.keepcost);
    if (JSON_ERROR(slot_j_obj)) goto error;

    json_object_object_add(mallinfo_obj, "allocator", allocator_name_obj);
    json_object_object_add(mallinfo_obj, "sbrk_bytes", slot_a_obj);
    json_object_object_add(mallinfo_obj, "mmap_count", slot_d_obj);
    json_object_object_add(mallinfo_obj, "mmap_bytes", slot_e_obj);
    json_object_object_add(mallinfo_obj, "max_malloc_bytes", slot_f_obj);
    json_object_object_add(mallinfo_obj, "malloc_bytes", slot_h_obj);
    json_object_object_add(mallinfo_obj, "slack_bytes", slot_i_obj);
    json_object_object_add(mallinfo_obj, "trimmable_slack_bytes", slot_j_obj);
        
    json_object_object_add(ret_obj, "returnValue", true_obj);
    json_object_object_add(ret_obj, "mallinfo", mallinfo_obj);

    bool reply_ret = LSMessageReply(sh, message, json_object_to_json_string(ret_obj), &lserror);
    if (!reply_ret)
    {
        g_critical("%s: sending malloc info failed", __FUNCTION__);
        LSErrorPrint(&lserror, stderr);
        LSErrorFree(&lserror);
    }

    json_object_put(ret_obj);
    
    return true;

error:
    
    if (!JSON_ERROR(ret_obj)) json_object_put(ret_obj);
    if (!JSON_ERROR(true_obj)) json_object_put(true_obj);
    if (!JSON_ERROR(mallinfo_obj)) json_object_put(mallinfo_obj);
    
    if (!JSON_ERROR(allocator_name_obj)) json_object_put(allocator_name_obj);
    if (!JSON_ERROR(slot_a_obj)) json_object_put(slot_a_obj);
    if (!JSON_ERROR(slot_d_obj)) json_object_put(slot_d_obj);
    if (!JSON_ERROR(slot_e_obj)) json_object_put(slot_e_obj);
    if (!JSON_ERROR(slot_f_obj)) json_object_put(slot_f_obj);
    if (!JSON_ERROR(slot_h_obj)) json_object_put(slot_h_obj);
    if (!JSON_ERROR(slot_i_obj)) json_object_put(slot_i_obj);
    if (!JSON_ERROR(slot_j_obj)) json_object_put(slot_j_obj);
    
    return true;
}
コード例 #6
0
bool
_LSPrivateInrospection(LSHandle* sh, LSMessage *message, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);

    GHashTableIter iter_category, iter_element;
    gpointer name_category, table_category, name_element, callback;
    struct LSCategoryTable *pTable = NULL;

    struct json_object *ret_obj = NULL;
    struct json_object *category_obj = NULL;
    struct json_object *element_obj = NULL;

    ret_obj = json_object_new_object();

    g_hash_table_iter_init(&iter_category, sh->tableHandlers);
    while (g_hash_table_iter_next(&iter_category, &name_category, &table_category))
    {
        // skip hidden method
        if (strcmp("/com/palm/luna/private", name_category) == 0)
            continue;

        pTable = (struct LSCategoryTable *)table_category;
        category_obj = json_object_new_object();

        // methods
        g_hash_table_iter_init(&iter_element, pTable->methods);
        while (g_hash_table_iter_next(&iter_element, &name_element, &callback))
        {
            element_obj = json_object_new_string("METHOD");
            json_object_object_add(category_obj, name_element, element_obj);
        }

        // signals
        g_hash_table_iter_init(&iter_element, pTable->signals);
        while (g_hash_table_iter_next(&iter_element, &name_element, &callback))
        {
            element_obj = json_object_new_string("SIGNAL");
            json_object_object_add(category_obj, name_element, element_obj);
        }

        json_object_object_add(ret_obj, name_category, category_obj);
    }

    bool reply_ret = LSMessageReply(sh, message, json_object_to_json_string(ret_obj), &lserror);
    if (!reply_ret)
    {
        g_critical("%s: sending introspection data failed", __FUNCTION__);
        LSErrorPrint(&lserror, stderr);
        LSErrorFree(&lserror);
        goto error;
    }

    json_object_put(ret_obj);

    return true;

error:

    if (!JSON_ERROR(ret_obj)) json_object_put(ret_obj);
    if (!JSON_ERROR(category_obj)) json_object_put(category_obj);
    if (!JSON_ERROR(element_obj)) json_object_put(element_obj);

    return false;
}
コード例 #7
0
bool
_LSPrivateDoMallocTrim(LSHandle* sh, LSMessage *message, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);

    struct json_object *ret_obj = NULL;
    struct json_object *true_obj = NULL;
    struct json_object *malloc_trim_obj = NULL;

    ret_obj = json_object_new_object();
    if (JSON_ERROR(ret_obj)) goto error;
       
    true_obj = json_object_new_boolean(true);
    if (JSON_ERROR(true_obj)) goto error;
 

    /* returnValue: true,
     * malloc_trim: int
     */

    typedef int (*malloc_trim_t)(size_t);
    static malloc_trim_t malloc_trim_p = NULL;

    if (malloc_trim_p == NULL) {
        malloc_trim_p = (malloc_trim_t)dlsym(RTLD_DEFAULT, "malloc_trim");
        if (malloc_trim_p == NULL)
            malloc_trim_p = (malloc_trim_t)-1;
    }
    
    int result;
    if (malloc_trim_p != (malloc_trim_t)-1) {
        result = malloc_trim_p(0);
    } else {
        result = -1;
    }

    malloc_trim_obj = json_object_new_int(result);
    if (JSON_ERROR(malloc_trim_obj)) goto error;
        
    json_object_object_add(ret_obj, "returnValue", true_obj);
    json_object_object_add(ret_obj, "malloc_trim", malloc_trim_obj);

    bool reply_ret = LSMessageReply(sh, message, json_object_to_json_string(ret_obj), &lserror);
    if (!reply_ret)
    {
        g_critical("%s: sending malloc trim result failed", __FUNCTION__);
        LSErrorPrint(&lserror, stderr);
        LSErrorFree(&lserror);
    }

    json_object_put(ret_obj);
    
    return true;

error:
    
    if (!JSON_ERROR(ret_obj)) json_object_put(ret_obj);
    if (!JSON_ERROR(true_obj)) json_object_put(true_obj);
    if (!JSON_ERROR(malloc_trim_obj)) json_object_put(malloc_trim_obj);
    
    return true;
}
コード例 #8
0
ファイル: json_parser.cpp プロジェクト: RayGralak/phd2
static json_value *json_parse(char *source, const char **error_pos,
                              const char **error_desc, int *error_line,
                              block_allocator *allocator)
{
    json_value *root = 0;
    json_value *top = 0;

    char *name = 0;
    char *it = source;

    int escaped_newlines = 0;

    while (*it)
    {
        switch (*it)
        {
        case '{':
        case '[':
        {
            // create new value
            json_value *object = json_alloc(allocator);

            // name
            object->name = name;
            name = 0;

            // type
            object->type = (*it == '{') ? JSON_OBJECT : JSON_ARRAY;

            // skip open character
            ++it;

            // set top and root
            if (top)
            {
                json_append(top, object);
            }
            else if (!root)
            {
                root = object;
            }
            else
            {
                JSON_ERROR(it, "Second root. Only one root allowed");
            }
            top = object;
        }
        break;

        case '}':
        case ']':
        {
            if (!top || top->type != ((*it == '}') ? JSON_OBJECT : JSON_ARRAY))
            {
                JSON_ERROR(it, "Mismatch closing brace/bracket");
            }

            // skip close character
            ++it;

            // set top
            top = top->parent;
        }
        break;

        case ':':
            if (!top || top->type != JSON_OBJECT || !name)
            {
                JSON_ERROR(it, "Unexpected character");
            }
            ++it;
            break;

        case ',':
            CHECK_TOP();
            ++it;
            break;

        case '"':
        {
            CHECK_TOP();

            // skip '"' character
            ++it;

            char *first = it;
            char *last = it;
            while (*it)
            {
                if ((unsigned char)*it < '\x20')
                {
                    JSON_ERROR(first, "Control characters not allowed in strings");
                }
                else if (*it == '\\')
                {
                    switch (it[1])
                    {
                    case '"':
                        *last = '"';
                        break;
                    case '\\':
                        *last = '\\';
                        break;
                    case '/':
                        *last = '/';
                        break;
                    case 'b':
                        *last = '\b';
                        break;
                    case 'f':
                        *last = '\f';
                        break;
                    case 'n':
                        *last = '\n';
                        ++escaped_newlines;
                        break;
                    case 'r':
                        *last = '\r';
                        break;
                    case 't':
                        *last = '\t';
                        break;
                    case 'u':
                    {
                        unsigned int codepoint;
                        if (hatoui(it + 2, it + 6, &codepoint) != it + 6)
                        {
                            JSON_ERROR(it, "Bad unicode codepoint");
                        }

                        if (codepoint <= 0x7F)
                        {
                            *last = (char)codepoint;
                        }
                        else if (codepoint <= 0x7FF)
                        {
                            *last++ = (char)(0xC0 | (codepoint >> 6));
                            *last = (char)(0x80 | (codepoint & 0x3F));
                        }
                        else if (codepoint <= 0xFFFF)
                        {
                            *last++ = (char)(0xE0 | (codepoint >> 12));
                            *last++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
                            *last = (char)(0x80 | (codepoint & 0x3F));
                        }
                    }
                    it += 4;
                    break;
                    default:
                        JSON_ERROR(first, "Unrecognized escape sequence");
                    }

                    ++last;
                    it += 2;
                }
                else if (*it == '"')
                {
                    *last = 0;
                    ++it;
                    break;
                }
                else
                {
                    *last++ = *it++;
                }
            }