Exemplo n.º 1
0
json_t* ciel_close_output(struct ciel_output* out) {

  json_error_t error_bucket;

  fflush(out->fp);
  fclose(out->fp);

  json_t* close_output_message = json_pack_ex(&error_bucket, 0, "[s{sisI}]", "close_output", "index", out->index, "size", out->bytes_written);
  if(!close_output_message)
    ciel_json_error(0, &error_bucket);
  ciel_write_framed_json(close_output_message, ciel_out);
  json_decref(close_output_message);

  free(out);

  json_t* response = ciel_read_framed_json(ciel_in);

  char* response_verb;
  json_t* ref;

  if(json_unpack_ex(response, &error_bucket, 0, "[s{sO}]", &response_verb, "ref", &ref))
    ciel_json_error(0, &error_bucket);

  if(strcmp(response_verb, "close_output") != 0) {
    fprintf(stderr, "close_output: bad response: %s\n", response_verb);
    exit(1);
  }

  json_decref(response);

  return ref;

}
Exemplo n.º 2
0
int test_unpack()
{
    int ret = -1;
    int v1;
    int v2;
    json_error_t error;
    json_t *root = json_pack("{s:i, s:i, s:i, s:i}", "n1", 1, "n2", 2, "n3", 3, "n4", 4);

    if (!root)
        return -1;

    if (!json_unpack_ex(root, &error, JSON_STRICT, "{s:i, s:i}", "n1", &v1, "n2", &v2))
        fail("Unexpected success");

    if (json_error_code(&error) != json_error_end_of_input_expected) {
        if (json_error_code(&error) != json_error_out_of_memory)
            fail("Unexpected error code");

        goto out;
    }

    if (strcmp(error.text, "2 object item(s) left unpacked: n3, n4"))
        goto out;

    ret = 0;

out:
    json_decref(root);
    return ret;
}
/** Update database entry with new information (if needed)
  @param entry Entry to be updated
  @param new_config New config
  @return 0 if success, !0 in other case (reason printed)
  */
static int update_organization(organization_db_entry_t *entry,
				json_t *new_config) {
	int rc = 0;
	json_error_t jerr;
	json_int_t bytes_limit = 0;
	json_t *aux_enrichment = NULL;

	const int unpack_rc = json_unpack_ex(new_config, &jerr,
		JSON_STRICT,
		"{s?O,s?{s?I}}",
		"enrichment",&aux_enrichment,
		"limits","bytes",&bytes_limit);

	if (0 != unpack_rc) {
		const char *organization_uuid =
			organization_db_entry_get_uuid(entry);
		rdlog(LOG_ERR,"Couldn't unpack organization %s limits: %s",
						organization_uuid, jerr.text);
		rc = -1;
		goto unpack_err;
	}

	pthread_mutex_lock(&entry->mutex);
	swap_ptrs(entry->enrichment, aux_enrichment);
	entry->bytes_limit.max = (uint64_t)bytes_limit;
	pthread_mutex_unlock(&entry->mutex);

unpack_err:
	if (aux_enrichment) {
		json_decref(aux_enrichment);
	}

	return rc;
}
Exemplo n.º 4
0
static void checkMSE8_invalid_timestamp(struct mse_array *notifications_array) {
	/* No database -> output == input */

	assert_int_equal(notifications_array->size, 1);
	assert_int_equal(
	    notifications_array->data[0].string_size,
	    strlen(notifications_array->data[0].string)
	);

	json_int_t timestamp = 0;
	json_error_t jerr;

	json_t *ret = json_loads(notifications_array->data[0].string, 0, &jerr);
	assert_non_null(ret);
	const int unpack_rc = json_unpack_ex(ret, &jerr, 0,
	                                     "{s:{s:I}}",
	                                     "StreamingNotification",
	                                     "timestampMillis", &timestamp);

	assert_int_equal(unpack_rc, 0);
	assert_true(timestamp / 1000 != NOW);
	assert_true(notifications_array->data[0].timestamp_warnings == 1);

	json_decref(ret);
}
Exemplo n.º 5
0
static int
_parse(model_and_data_t m,
        column_reduction_t r_site,
        column_reduction_t r_edge,
        column_reduction_t r_trans,
        int **first_idx, int **second_idx,
        json_t *root)
{
    json_t *model_and_data = NULL;
    json_t *site_reduction = NULL;
    json_t *edge_reduction = NULL;
    json_t *trans_reduction = NULL;
    slong site_count, edge_count, state_count;
    int result = 0;

    /* unpack the top level of json input */
    {
        size_t flags;
        json_error_t err;
        flags = JSON_STRICT;
        result = json_unpack_ex(root, &err, flags,
                "{s:o, s?o, s?o, s?o}",
                "model_and_data", &model_and_data,
                "site_reduction", &site_reduction,
                "edge_reduction", &edge_reduction,
                "trans_reduction", &trans_reduction
                );
        if (result)
        {
            fprintf(stderr, "error: on line %d: %s\n", err.line, err.text);
            return result;
        }
    }

    /* validate the model and data section of the json input */
    result = validate_model_and_data(m, model_and_data);
    if (result) return result;

    /* initialize counts */
    site_count = model_and_data_site_count(m);
    edge_count = model_and_data_edge_count(m);
    state_count = model_and_data_state_count(m);

    /* validate the site reduction section of the json input */
    result = validate_column_reduction(
            r_site, site_count, "site", site_reduction);
    if (result) return result;

    /* validate the edge reduction section of the json input */
    result = validate_column_reduction(
            r_edge, edge_count, "edge", edge_reduction);
    if (result) return result;

    /* validate the state reduction section of the json input */
    result = validate_column_pair_reduction(r_trans, first_idx, second_idx,
            state_count, state_count, "trans", trans_reduction);
    if (result) return result;

    return result;
}
Exemplo n.º 6
0
static void checkMSE8_valid_result(struct mse_array *notifications_array) {
	/* No database -> output == input */
	assert_int_equal(notifications_array->size, 1);
	assert_int_equal(
	    notifications_array->data[0].string_size,
	    strlen(notifications_array->data[0].string)
	);

	const char *subscriptionName = NULL, *sensor_name = NULL;
	json_int_t sensor_id = 0;
	json_error_t jerr;

	json_t *ret = json_loads(notifications_array->data[0].string, 0, &jerr);
	assert_non_null(ret);
	const int unpack_rc = json_unpack_ex(ret, &jerr, 0,
	                                     "{s:{s:s,s:s,s:i}}",
	                                     "StreamingNotification",
	                                     "subscriptionName", &subscriptionName,
	                                     "sensor_name", &sensor_name,
	                                     "sensor_id", &sensor_id);

	assert_int_equal(unpack_rc, 0);
	assert_string_equal(subscriptionName, "MSE_SanCarlos");
	assert_string_equal(sensor_name, "MSE_testing");
	assert_int_equal(sensor_id, 255);
	json_decref(ret);
}
Exemplo n.º 7
0
int ciel_read_ref(struct ciel_input* ref, char* buffer, int length) {

  json_error_t error_bucket;

  while(1) {
    size_t bytes_read = fread(buffer, 1, length, ref->fp);
    if(bytes_read == 0) {
      if(ferror(ref->fp)) {
	fprintf(stderr, "Error reading ref id %s\n", ref->refid);
	return -1;
      }
      else {
	if(ref->eof || ref->is_blocking) {
	  return 0;
	}
	else {
	  json_int_t threshold = ref->bytes_read + ref->chunk_size;
	  json_t* wait_message = json_pack_ex(&error_bucket, 0, "[s{sssI}]", "wait_stream", "id", ref->refid, "bytes", threshold);
	  ciel_write_framed_json(wait_message, ciel_out);
	  json_decref(wait_message);
	  
	  json_t* response = ciel_read_framed_json(ciel_in);

	  char* response_verb;
	  int new_size;
	  int new_done;
	  int success;

	  if(json_unpack_ex(response, &error_bucket, 0, "[s{sisbsb}]", &response_verb, "size", &new_size, "done", &new_done, "success", &success))
	    ciel_json_error(0, &error_bucket);

	  ref->eof = new_done;

	  if(strcmp(response_verb, "wait_stream")) {
	    fprintf(stderr, "Weird response to wait_stream: %s\n", response_verb);
	    json_decref(response);
	    return -1;
	  }
	  if(!success) {
	    fprintf(stderr, "Error waiting for ref %s!\n", ref->refid);
	    json_decref(response);
	    return -1;
	  }

	  json_decref(response);
	}
      }
    }
    else {
      ref->bytes_read += bytes_read;
      return bytes_read;
    }
  }
}
Exemplo n.º 8
0
    int DrachtioController::sendRequestInsideDialog( boost::shared_ptr<JsonMsg> pMsg, const string& rid ) {
        boost::shared_ptr<SipDialog> dlg ;
        const char *dialogId=NULL ;
        json_error_t err ;
        
        if( 0 > json_unpack_ex(pMsg->value(), &err, 0, "{s:{s:s}}", "data", "dialogId", &dialogId) ) {
            DR_LOG(log_error) << "DrachtioController::sendRequestInsideDialog - failed parsing message: " << err.text << endl ;
            return -1 ;
        }
        if( !m_pDialogController->findDialogById( dialogId, dlg ) ) {
            //DO I need to look in dialog maker also ?  What about an UPDATE sent during an INVITE transaction?
            return -1;
        }
        m_pDialogController->sendRequestInsideDialog( pMsg, rid, dlg ) ;

        return 0 ;
    }
Exemplo n.º 9
0
json_t* ciel_get_task() {

  json_error_t error_bucket;
  json_t* task = ciel_read_framed_json(ciel_in);
  
  char* command;
  json_t* args;

  if(json_unpack_ex(task, &error_bucket, 0, "[sO]", &command, &args))
    ciel_json_error(0, &error_bucket);
  if(strcmp(command, "start_task") != 0) {
    fprintf(stderr, "Strange first task: %s\n", command);
    exit(1);
  }

  json_decref(task);
  return args;

}
Exemplo n.º 10
0
struct ciel_input* ciel_open_ref(json_t* ref) {

  json_error_t error_bucket;
  
  json_t* open_message = json_pack_ex(&error_bucket, 0, "[s{sO}]", "open_ref", "ref", ref);
  if(!open_message) {
    ciel_json_error(0, &error_bucket);
    return 0;
  }

  ciel_write_framed_json(open_message, ciel_out);
  json_decref(open_message);

  char* response_verb;
  char* filename;

  json_t* response = ciel_read_framed_json(ciel_in);
  if(json_unpack_ex(response, &error_bucket, 0, "[s{ss}]", &response_verb, "filename", &filename)) {
    ciel_json_error(0, &error_bucket);
    return 0;
  }

  struct ciel_input* ret = (struct ciel_input*)malloc(sizeof(struct ciel_input));

  ret->fp = fopen(filename, "r");
  ret->chunk_size = 0;
  ret->is_blocking = 1;
  ret->must_close = 0;
  ret->bytes_read = 0;
  ret->eof = 0;
  ret->refid = ciel_get_ref_id(ref);

  json_decref(response);

  return ret;

}
Exemplo n.º 11
0
json_t* ciel_package_lookup(char *key) {
  json_error_t error_bucket;

  json_t* package_lookup_request = json_pack_ex(&error_bucket, 0, "[s{ss}]", "package_lookup", "key", key);
  if (!package_lookup_request)
    ciel_json_error(0, &error_bucket);
  ciel_write_framed_json(package_lookup_request, ciel_out);
  json_decref(package_lookup_request);

  json_t* response = ciel_read_framed_json(ciel_in);

  char* response_verb;
  json_t* value;
  if (json_unpack_ex(response, &error_bucket, 0, "[s{sO}]", &response_verb, "value", &value))
    ciel_json_error(0, &error_bucket);
  
  if (strcmp(response_verb, "package_lookup") != 0) {
    fprintf(stderr, "package_lookup: bad response: %s\n", response_verb);
    exit(1);
  }

  json_decref(response);
  return value;
}
Exemplo n.º 12
0
int main()
{
    json_t *j, *j2;
    int i1, i2, i3;
    json_int_t I1;
    int rv;
    double f;
    char *s;

    json_error_t error;

    /*
     * Simple, valid json_pack cases
     */

    /* true */
    rv = json_unpack(json_true(), "b", &i1);
    if(rv || !i1)
        fail("json_unpack boolean failed");

    /* false */
    rv = json_unpack(json_false(), "b", &i1);
    if(rv || i1)
        fail("json_unpack boolean failed");

    /* null */
    if(json_unpack(json_null(), "n"))
        fail("json_unpack null failed");

    /* integer */
    j = json_integer(42);
    rv = json_unpack(j, "i", &i1);
    if(rv || i1 != 42)
        fail("json_unpack integer failed");
    json_decref(j);

    /* json_int_t */
    j = json_integer(5555555);
    rv = json_unpack(j, "I", &I1);
    if(rv || I1 != 5555555)
        fail("json_unpack json_int_t failed");
    json_decref(j);

    /* real */
    j = json_real(1.7);
    rv = json_unpack(j, "f", &f);
    if(rv || f != 1.7)
        fail("json_unpack real failed");
    json_decref(j);

    /* number */
    j = json_integer(12345);
    rv = json_unpack(j, "F", &f);
    if(rv || f != 12345.0)
        fail("json_unpack (real or) integer failed");
    json_decref(j);

    j = json_real(1.7);
    rv = json_unpack(j, "F", &f);
    if(rv || f != 1.7)
        fail("json_unpack real (or integer) failed");
    json_decref(j);

    /* string */
    j = json_string("foo");
    rv = json_unpack(j, "s", &s);
    if(rv || strcmp(s, "foo"))
        fail("json_unpack string failed");
    json_decref(j);

    /* empty object */
    j = json_object();
    if(json_unpack(j, "{}"))
        fail("json_unpack empty object failed");
    json_decref(j);

    /* empty list */
    j = json_array();
    if(json_unpack(j, "[]"))
        fail("json_unpack empty list failed");
    json_decref(j);

    /* non-incref'd object */
    j = json_object();
    rv = json_unpack(j, "o", &j2);
    if(j2 != j || j->refcount != 1)
        fail("json_unpack object failed");
    json_decref(j);

    /* incref'd object */
    j = json_object();
    rv = json_unpack(j, "O", &j2);
    if(j2 != j || j->refcount != 2)
        fail("json_unpack object failed");
    json_decref(j);
    json_decref(j);

    /* simple object */
    j = json_pack("{s:i}", "foo", 42);
    rv = json_unpack(j, "{s:i}", "foo", &i1);
    if(rv || i1 != 42)
        fail("json_unpack simple object failed");
    json_decref(j);

    /* simple array */
    j = json_pack("[iii]", 1, 2, 3);
    rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
    if(rv || i1 != 1 || i2 != 2 || i3 != 3)
        fail("json_unpack simple array failed");
    json_decref(j);

    /* object with many items & strict checking */
    j = json_pack("{s:i, s:i, s:i}", "a", 1, "b", 2, "c", 3);
    rv = json_unpack(j, "{s:i, s:i, s:i}", "a", &i1, "b", &i2, "c", &i3);
    if(rv || i1 != 1 || i2 != 2 || i3 != 3)
        fail("json_unpack object with many items failed");
    json_decref(j);

    /*
     * Invalid cases
     */

    j = json_integer(42);
    if(!json_unpack_ex(j, &error, 0, "z"))
        fail("json_unpack succeeded with invalid format character");
    check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);

    if(!json_unpack_ex(NULL, &error, 0, "[i]"))
        fail("json_unpack succeeded with NULL root");
    check_error("NULL root value", "<root>", -1, -1, 0);
    json_decref(j);

    /* mismatched open/close array/object */
    j = json_pack("[]");
    if(!json_unpack_ex(j, &error, 0, "[}"))
        fail("json_unpack failed to catch mismatched ']'");
    check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
    json_decref(j);

    j = json_pack("{}");
    if(!json_unpack_ex(j, &error, 0, "{]"))
        fail("json_unpack failed to catch mismatched '}'");
    check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
    json_decref(j);

    /* missing close array */
    j = json_pack("[]");
    if(!json_unpack_ex(j, &error, 0, "["))
        fail("json_unpack failed to catch missing ']'");
    check_error("Unexpected end of format string", "<format>", 1, 2, 2);
    json_decref(j);

    /* missing close object */
    j = json_pack("{}");
    if(!json_unpack_ex(j, &error, 0, "{"))
        fail("json_unpack failed to catch missing '}'");
    check_error("Unexpected end of format string", "<format>", 1, 2, 2);
    json_decref(j);

    /* garbage after format string */
    j = json_pack("[i]", 42);
    if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
        fail("json_unpack failed to catch garbage after format string");
    check_error("Garbage after format string", "<format>", 1, 4, 4);
    json_decref(j);

    j = json_integer(12345);
    if(!json_unpack_ex(j, &error, 0, "ia", &i1))
        fail("json_unpack failed to catch garbage after format string");
    check_error("Garbage after format string", "<format>", 1, 2, 2);
    json_decref(j);

    /* NULL format string */
    j = json_pack("[]");
    if(!json_unpack_ex(j, &error, 0, NULL))
        fail("json_unpack failed to catch null format string");
    check_error("NULL or empty format string", "<format>", -1, -1, 0);
    json_decref(j);

    /* NULL string pointer */
    j = json_string("foobie");
    if(!json_unpack_ex(j, &error, 0, "s", NULL))
        fail("json_unpack failed to catch null string pointer");
    check_error("NULL string argument", "<args>", 1, 1, 1);
    json_decref(j);

    /* invalid types */
    j = json_integer(42);
    j2 = json_string("foo");
    if(!json_unpack_ex(j, &error, 0, "s"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected string, got integer", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j, &error, 0, "n"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected null, got integer", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j, &error, 0, "b"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j2, &error, 0, "i"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected integer, got string", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j2, &error, 0, "I"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected integer, got string", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j, &error, 0, "f"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected real, got integer", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j2, &error, 0, "F"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j, &error, 0, "[i]"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected array, got integer", "<validation>", 1, 1, 1);

    if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
        fail("json_unpack failed to catch invalid type");
    check_error("Expected object, got integer", "<validation>", 1, 1, 1);

    json_decref(j);
    json_decref(j2);

    /* Array index out of range */
    j = json_pack("[i]", 1);
    if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
        fail("json_unpack failed to catch index out of array bounds");
    check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
    json_decref(j);

    /* NULL object key */
    j = json_pack("{si}", "foo", 42);
    if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
        fail("json_unpack failed to catch null string pointer");
    check_error("NULL object key", "<args>", 1, 2, 2);
    json_decref(j);

    /* Object key not found */
    j = json_pack("{si}", "foo", 42);
    if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
        fail("json_unpack failed to catch null string pointer");
    check_error("Object item not found: baz", "<validation>", 1, 3, 3);
    json_decref(j);

    /*
     * Strict validation
     */

    j = json_pack("[iii]", 1, 2, 3);
    rv = json_unpack(j, "[iii!]", &i1, &i2, &i3);
    if(rv || i1 != 1 || i2 != 2 || i3 != 3)
        fail("json_unpack array with strict validation failed");
    json_decref(j);

    j = json_pack("[iii]", 1, 2, 3);
    if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
        fail("json_unpack array with strict validation failed");
    check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
    json_decref(j);

    /* Like above, but with JSON_STRICT instead of '!' format */
    j = json_pack("[iii]", 1, 2, 3);
    if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
        fail("json_unpack array with strict validation failed");
    check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
    json_decref(j);

    j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
    rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1);
    if(rv || strcmp(s, "bar") != 0 || i1 != 42)
        fail("json_unpack object with strict validation failed");
    json_decref(j);

    /* Unpack the same item twice */
    j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
    if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
        fail("json_unpack object with strict validation failed");
    check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
    json_decref(j);

    j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
    if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
                      "[i{sisn}[ii]]", "foo", "bar"))
        fail("json_unpack complex value with strict validation failed");
    json_decref(j);

    /* ! and * must be last */
    j = json_pack("[ii]", 1, 2);
    if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
        fail("json_unpack failed to catch ! in the middle of an array");
    check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);

    if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
        fail("json_unpack failed to catch * in the middle of an array");
    check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
    json_decref(j);

    j = json_pack("{sssi}", "foo", "bar", "baz", 42);
    if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
        fail("json_unpack failed to catch ! in the middle of an object");
    check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);

    if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
        fail("json_unpack failed to catch ! in the middle of an object");
    check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
    json_decref(j);

    /* Error in nested object */
    j = json_pack("{s{snsn}}", "foo", "bar", "baz");
    if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
        fail("json_unpack nested object with strict validation failed");
    check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
    json_decref(j);

    /* Error in nested array */
    j = json_pack("[[ii]]", 1, 2);
    if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
        fail("json_unpack nested array with strict validation failed");
    check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
    json_decref(j);

    return 0;
}
Exemplo n.º 13
0
static int gen_jansson_value(yajl_gen gen, json_t *value) {
	json_error_t jerr;
	const char *str;
	size_t len;
	int rc;

	int type = json_typeof(value);
	switch(type) {
	case JSON_OBJECT:
		yajl_gen_map_open(gen);
		gen_jansson_object(gen,value);
		yajl_gen_map_close(gen);
		break;

	case JSON_ARRAY:
		yajl_gen_array_open(gen);
		gen_jansson_array(gen,value);
		yajl_gen_array_close(gen);
		break;

	case JSON_STRING:
		rc = json_unpack_ex(value, &jerr, 0, "s%", &str,&len);
		if(rc != 0) {
			rdlog(LOG_ERR,"Couldn't extract string: %s",jerr.text);
			return 0;
		}
		yajl_gen_string(gen, (const unsigned char *)str, len);
		break;

	case JSON_INTEGER:
		{
			json_int_t i = json_integer_value(value);
			yajl_gen_integer(gen,i);
		}
		break;

	case JSON_REAL:
		{
			double d = json_number_value(value);
			yajl_gen_double(gen,d);
		}
		break;

	case JSON_TRUE:
		yajl_gen_bool(gen,1);
		break;

	case JSON_FALSE:
		yajl_gen_bool(gen,0);
		break;

	case JSON_NULL:
		yajl_gen_null(gen);
		break;

	default:
		rdlog(LOG_ERR,"Unkown jansson type %d",type);
		break;
	};

	return 1;
}
Exemplo n.º 14
0
// ref: borrowed reference
struct ciel_input* ciel_open_ref_async(json_t* ref, int chunk_size, int sole_consumer, int must_block) {

  json_error_t error_bucket;

#ifdef CIEL_SOCKET
  json_t* open_message = json_pack_ex(&error_bucket, 0, "[s{sOsisbsbsbss}]", "open_ref_async",
				      "ref", ref, "chunk_size", chunk_size, "sole_consumer", sole_consumer,
				      "make_sweetheart", 0, "must_block", must_block, "fd_socket_name", ciel_socket_name);
#else
  json_t* open_message = json_pack_ex(&error_bucket, 0, "[s{sOsisbsbsb}]", "open_ref_async",
				      "ref", ref, "chunk_size", chunk_size, "sole_consumer", sole_consumer,
				      "make_sweetheart", 0, "must_block", must_block);
#endif /* CIEL_SOCKET */

  if(!open_message)
    ciel_json_error(0, &error_bucket);

  ciel_write_framed_json(open_message, ciel_out);
  json_decref(open_message);

  json_t* response = ciel_read_framed_json(ciel_in);

  char* response_verb;
  json_t* response_args;

  if(json_unpack_ex(response, &error_bucket, 0, "[so]", &response_verb, &response_args)) {
    fprintf(stderr, "Failed decoding open_ref_async array\n");
    ciel_json_error(0, &error_bucket);
    json_decref(response);
    return 0;
  }

  int fd_coming;
  int is_blocking;
  int is_done;
  /*int size;*/

  if(json_unpack_ex(response_args, &error_bucket, 0, "{sbsbsb}", 
		    "sending_fd", &fd_coming, "blocking", &is_blocking, 
		    "done", &is_done)) {
    fprintf(stderr, "Failed decoding open_ref_async object\n");
    ciel_json_error(0, &error_bucket);
    json_decref(response);
    return 0;
  }

  struct ciel_input* new_input = (struct ciel_input*)malloc(sizeof(struct ciel_input));

  new_input->chunk_size = chunk_size;
  new_input->is_blocking = is_blocking;
  new_input->must_close = !is_done;
  new_input->eof = is_done;
  new_input->bytes_read = 0;

#ifdef CIEL_SOCKET
  if(!fd_coming) {
#endif /* CIEL_SOCKET */
    json_t* filename_obj = json_object_get(response_args, "filename");
    if(!filename_obj) {
      fprintf(stderr, "No member 'filename' in open_ref_async response\n");
      json_decref(response);
      return 0;
    }
    const char* filename = json_string_value(filename_obj);
    if(!filename) {
      fprintf(stderr, "Member 'filename' in open_ref_async not a string\n");
      json_decref(response);
      return 0;
    }
    new_input->fp = fopen(filename, "r");
#ifdef CIEL_SOCKET
  }
  else {
    
    int recvd_fd = ciel_receive_fd();
    if(recvd_fd == -1) {
      fprintf(stderr, "Receive-FD failed in open_ref_async\n");
      json_decref(response);
      return 0;
    }
    new_input->fp = fdopen(recvd_fd, "r");
    
  }
#endif /* CIEL_SOCKET */

  json_decref(response);

  new_input->refid = ciel_get_ref_id(ref);

  return new_input;

}
Exemplo n.º 15
0
struct ciel_output* ciel_open_output(int index, int may_stream, int may_pipe, int make_sweetheart) {

  json_error_t error_bucket;

#ifdef CIEL_SOCKET
  json_t* open_output_message = json_pack_ex(&error_bucket, 0, "[s{sisbsbsbss}]", 
					     "open_output", "index", index,
					     "may_stream", may_stream, "may_pipe", may_pipe, 
					     "make_local_sweetheart", make_sweetheart,
					     "fd_socket_name", ciel_socket_name);
#else
  json_t* open_output_message = json_pack_ex(&error_bucket, 0, "[s{sisbsbsb}]", 
					     "open_output", "index", index,
					     "may_stream", may_stream, "may_pipe", may_pipe, 
					     "make_local_sweetheart", make_sweetheart);
#endif /* CIEL_SOCKET */

  if(!open_output_message)
    ciel_json_error(0, &error_bucket);
  
  ciel_write_framed_json(open_output_message, ciel_out);
  json_decref(open_output_message);

  json_t* open_response = ciel_read_framed_json(ciel_in);

  char* response_verb;
  json_t* response_args;

  if(json_unpack_ex(open_response, &error_bucket, 0, "[so]", &response_verb, &response_args))
    ciel_json_error(0, &error_bucket);

  if(strcmp(response_verb, "open_output") != 0) {
    fprintf(stderr, "open_output: bad response: %s\n", response_verb);
    exit(1);
  }

  json_t* sending_fd_json = json_object_get(response_args, "sending_fd");
  if(!sending_fd_json) {
    fprintf(stderr, "Open output response doesn't have member 'sending_fd'\n");
    json_decref(open_response);
    return 0;
  }

  struct ciel_output* ret = (struct ciel_output*)malloc(sizeof(struct ciel_output));
  ret->index = index;
  ret->bytes_written = 0;

#ifdef CIEL_SOCKET
  if(json_typeof(sending_fd_json) == JSON_TRUE) {

    int fd = ciel_receive_fd();
    ret->fp = fdopen(fd, "w");
    
  }
  else {

    json_t* filename_json = json_object_get(response_args, "filename");
    ret->fp = fopen(json_string_value(filename_json), "w");

  }
#else
  json_t* filename_json = json_object_get(response_args, "filename");
  ret->fp = fopen(json_string_value(filename_json), "w");
#endif /* CIEL_SOCKET */

  json_decref(open_response);
  return ret;

}
Exemplo n.º 16
0
int 
main(int argc, char **argv)
{

  if(argc < 5) {
    fprintf(stderr, "stream_producer needs at least 4 arguments\n");
    exit(1);
  }
  
  if(strcmp(argv[1], "--write-fifo") != 0) {
    fprintf(stderr, "stream_producer first arg must be --write-fifo\n");
    exit(1);
  }
  if(strcmp(argv[3], "--read-fifo") != 0) {
    fprintf(stderr, "stream producer third arg must be --read-fifo\n");
    exit(1);
  }

  printf("C binopt: start\n");

  ciel_init(argv[2], argv[4]);

  json_t* task_private = ciel_get_task();

  json_error_t error_bucket;
  json_t* kwargs;

  char* s_str;
  char* k_str;
  char* t_str;
  char* v_str;
  char* rf_str;
  char* cp_str;
  char* n_str;
  char* chunk_str;
  char* start_str;

  fprintf(stderr, "doing decode\n");

  if(json_unpack_ex(task_private, &error_bucket, 0, "{s[sssssssss]so}", "proc_pargs", 
		    &s_str, &k_str, &t_str, &v_str, &rf_str, &cp_str, &n_str, 
		    &chunk_str, &start_str, "proc_kwargs", &kwargs)) {
    ciel_json_error("Failed to decode proc_pargs", &error_bucket);
    exit(1);
  }

  fprintf(stderr, "doing conversion\n");

  s = strtod(s_str, NULL);
  k = strtod(k_str, NULL);
  t = strtod(t_str, NULL);
  v = strtod(v_str, NULL);
  rf = strtod(rf_str, NULL);
  cp = strtod(cp_str, NULL);
  n = (int)strtod(n_str, NULL);
  chunk = (int)strtod(chunk_str, NULL);
  start = strcmp(start_str, "false");

  fprintf(stderr, "Got arguments: %g %g %g %g %g %g %d %d %s\n", s, k, t, v, rf, cp, n, chunk, start_str);

  if(!start) {
    
    json_t* in_ref = json_object_get(kwargs, "input_ref");
    binopt_ciel_in = ciel_open_ref_async(in_ref, 1, 1, 0);
    ciel_set_input_unbuffered(binopt_ciel_in);

  }
  else {
    binopt_ciel_in = 0;
  }

  json_decref(task_private);

  binopt_ciel_out = ciel_open_output(0, 1, 0, 0);
  ciel_set_output_unbuffered(binopt_ciel_out);

  init_vals();
  if (start == 1) {
    OUTPUT(n);
    gen_initial_optvals();
  } else {
    int rowstart=0;
    INPUT(rowstart);
    if (rowstart==0) {
      double v;
      INPUT(v);
      char buf[20];
      int chars_written = min(snprintf(buf, 20, "%.9f\n", v), 20);
      ciel_write_output(binopt_ciel_out, buf, chars_written);
    } else {
      int rowto = (rowstart - chunk) > 0 ? (rowstart - chunk) : 0;
      process_rows(rowstart, rowto);
    }
  }


  if(!start) {
    ciel_close_ref(binopt_ciel_in);
  }

  ciel_close_output(binopt_ciel_out);

  ciel_exit();

  return 0;
}
Exemplo n.º 17
0
int main(int argc, char** argv) {

  if(argc < 5) {
    fprintf(stderr, "stream_producer needs at least 4 arguments\n");
    exit(1);
  }
  
  if(strcmp(argv[1], "--write-fifo") != 0) {
    fprintf(stderr, "stream_producer first arg must be --write-fifo\n");
    exit(1);
  }
  if(strcmp(argv[3], "--read-fifo") != 0) {
    fprintf(stderr, "stream producer third arg must be --read-fifo\n");
    exit(1);
  }

  printf("C stream consumer: start\n");

  ciel_init(argv[2], argv[4]);

  printf("FIFOs open\n");

  json_t* ref;
  int may_stream;
  int sole_consumer;
  int must_block;

  json_t* task_private = ciel_get_task();
  json_error_t error_bucket;

  if(json_unpack_ex(task_private, &error_bucket, 0, "{s[Obbb]}", "proc_pargs", &ref, &may_stream, &sole_consumer, &must_block)) {
    ciel_json_error(0, &error_bucket);
    exit(1);
  }

  json_decref(task_private);

  ciel_block_on_refs(1, ref);

  struct ciel_input* input;
  if(may_stream)
    input = ciel_open_ref_async(ref, 1024*1024*64, sole_consumer, must_block);
  else
    input = ciel_open_ref(ref);

  char read_buffer[4096];
  
  json_int_t bytes_read = 0;

  while(1) {
    int this_read = ciel_read_ref(input, read_buffer, 4096);
    if(this_read == -1) {
      fprintf(stderr, "Error reading input!");
      exit(1);
    }
    else if(this_read == 0) {
      break;
    }
    bytes_read += this_read;
  }

  ciel_close_ref(input);
  
  char* response_string;
  asprintf(&response_string, "Consumer read %lld bytes\n", bytes_read);
  ciel_define_output_with_plain_string(0, response_string);
  free(response_string);

  ciel_exit();

  return 0;

}