Example #1
0
static bool 
timingServiceResponse(LSHandle *sh, LSMessage *reply, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);
    LSMessageToken token;

    const char *payload;
    
    clock_gettime(CLOCK_MONOTONIC, &stopTime);

    token = LSMessageGetResponseToken(reply);
    payload = LSMessageGetPayload(reply);
    
    double duration = ((double)stopTime.tv_sec + (((double)stopTime.tv_nsec)/1000000000.0)) -
                     ((double)startTime.tv_sec + (((double)startTime.tv_nsec)/1000000000.0));
    
    roundtripTime += duration;
    roundtripCount++;
    rcvdBytes += strlen(payload);
    sentBytes += url ? strlen(url) : 0;
    sentBytes += message ? strlen(message) : 0;

    g_message("%s Got response: duration %.02f ms, token %ld, payload %s", __FUNCTION__, duration * 1000.0, token, payload);

    if (--count > 0)
    {
        // resend the message!
        LSMessageToken sessionToken;

        clock_gettime(CLOCK_MONOTONIC, &startTime);
        
        /* Basic sending */
        bool retVal = LSCallFromApplication(sh, url, message, appId,
                     timingServiceResponse, ctx, &sessionToken, &lserror);
        if (!retVal)
        {
            LSErrorPrint (&lserror, stderr);
            LSErrorFree (&lserror);
        }
    } else {
        bool retVal = LSCallCancel (sh, token, &lserror);
        if (!retVal)
        {
            LSErrorPrint (&lserror, stderr);
            LSErrorFree (&lserror);
        }
        g_timeout_add (300, goodbye, ctx);
        return true;
    }

    return true;
}
Example #2
0
static bool
message_filter(LSHandle *sh, LSMessage* reply, void* ctx)
{
    LOG(__PRETTY_FUNCTION__);

    const char* payload = LSMessageGetPayload(reply);

#if !defined (INSTANTIATION_WORK)
    payload = testvalue;
#endif
    const LSMessageToken responseToken = LSMessageGetResponseToken(reply);
    Type* listener = static_cast<Type* >(ctx);
    if (payload)
        listener->set_value(payload);
    listener->cv_.notify_one();
    return true;
}
Example #3
0
/**
* @brief Internal callback for service responses.
*
* @param  sh
* @param  reply
* @param  ctx
*
* @retval
*/
static bool
message_filter_async(LSHandle *sh, LSMessage* reply, void* ctx)
{
    LOG(__PRETTY_FUNCTION__);
    const char* payload = LSMessageGetPayload(reply);

#if !defined (INSTANTIATION_WORK)
    // Set the test value
    payload = returnTestValue;
#endif
    const LSMessageToken responseToken = LSMessageGetResponseToken(reply);
    invoke_listener_base* listener = static_cast<invoke_listener_base* >(ctx);

    if (listener && payload)
    {
        (*listener)(payload, responseToken);
        return true;
    }

    return false;
}
Example #4
0
static bool 
serviceResponse(LSHandle *sh, LSMessage *reply, void *ctx)
{
    LSError lserror;
    LSErrorInit(&lserror);
    LSMessageToken token;

    const char *payload;
    bool free_payload = false;

    token = LSMessageGetResponseToken(reply);
    payload = LSMessageGetPayload(reply);

    //g_message("%s Handling: %ld, %s", __FUNCTION__, token, payload);
    
    if (line_number) {
      printf("%2d: ", current_line_number++);
    }
    
    if (query_list != NULL) {
      // Use set of queries to transform original object into reduced form that
      // only contains queried selections -- then pass that through normal formatting.
      struct json_object *original = json_tokener_parse(payload);
      struct json_object *new_object = json_object_new_object();
      GList * query = query_list;
      if ( original && !is_error(original) ) {
        while (query) {
          char * query_text = (char*)query->data;
          struct json_object * result = apply_query(original, query_text);
          json_object_object_add(new_object, query_text, result);
          query = query->next;
        }
        payload = strdup(json_object_get_string(new_object));
        free_payload = true;
        json_object_put(new_object);
      }
    }
    
    if (format_response) {
      struct json_object *object = json_tokener_parse(payload);
      if ( !object || is_error(object) ) {
        // fall back to plain print
        printf("%s\n", payload);
      } else {
        pretty_print(object, 0, line_number ? 4 /* expected characters in line numbers */ : 0);
        printf("\n");
        json_object_put(object);
      }
    } else {
      printf("%s\n", payload);
    }
    
    if (free_payload)
      free((void*)payload);
    
    fflush(stdout);

    if (--count == 0)
    {
        bool retVal = LSCallCancel (sh, token, &lserror);
        if (!retVal)
        {
            LSErrorPrint (&lserror, stderr);
            LSErrorFree (&lserror);
        }
        g_timeout_add (300, goodbye, ctx);
        return true;
    }

    return true;
}