void test()
{
	uint64_t test_u64 = 0xFFF0000000000001LL;
	size_t size = 10000000;
	msgpack_sbuffer buf;
	msgpack_sbuffer_init(&buf);

	msgpack_packer * pk = msgpack_packer_new(&buf, msgpack_sbuffer_write);

	msgpack_pack_array(pk, size);
	{
		int idx = 0;
		for (; idx < size; ++idx)
			msgpack_pack_uint64(pk, test_u64);
	}
	msgpack_packer_free(pk);


	size_t upk_pos = 0;
	msgpack_unpacked msg;
	msgpack_unpacked_init(&msg);

	while (msgpack_unpack_next(&msg, buf.data, buf.size, &upk_pos)) {
	}

	msgpack_sbuffer_destroy(&buf);
}
Example #2
0
int main(void) {

    /* creates buffer and serializer instance. */
    msgpack_sbuffer* buffer = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

    /* serializes ["Hello", "MessagePack"]. */
    msgpack_pack_array(pk, 2);
    msgpack_pack_raw(pk, 5);
    msgpack_pack_raw_body(pk, "Hello", 5);
    msgpack_pack_raw(pk, 11);
    msgpack_pack_raw_body(pk, "MessagePack", 11);

    /* deserializes it. */
    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);
    bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);

    /* prints the deserialized object. */
    msgpack_object obj = msg.data;
    msgpack_object_print(stdout, obj);  /*=> ["Hello", "MessagePack"] */
    puts("");

    // msgpack_pack_object/pk

    /* cleaning */
    msgpack_sbuffer_free(buffer);
    msgpack_packer_free(pk);
}
Example #3
0
std::string encodeRequest(const Request &r) {
    std::shared_ptr<msgpack_sbuffer> buffer{msgpack_sbuffer_new(), &msgpack_sbuffer_free};
    std::shared_ptr<msgpack_packer> pk{msgpack_packer_new(buffer.get(), msgpack_sbuffer_write), &msgpack_packer_free};

    msgpack_pack_map(pk.get(), 5);
    msgpack_pack_raw(pk.get(), 4);
    msgpack_pack_raw_body(pk.get(), "func", 4);
    msgpack_pack_raw(pk.get(), r.func.size());
    msgpack_pack_raw_body(pk.get(), r.func.data(), r.func.size());
    msgpack_pack_raw(pk.get(), 6);
    msgpack_pack_raw_body(pk.get(), "metric", 6);
    msgpack_pack_raw(pk.get(), r.metric.size());
    msgpack_pack_raw_body(pk.get(), r.metric.data(), r.metric.size());
    msgpack_pack_raw(pk.get(), 9);
    msgpack_pack_raw_body(pk.get(), "timestamp", 9);
    msgpack_pack_uint64(pk.get(), r.timestamp);

    msgpack_pack_raw(pk.get(), 5);
    msgpack_pack_raw_body(pk.get(), "value", 5);
    switch(r.value.type()) {
        case Value::Type::Nil:
            msgpack_pack_nil(pk.get());
            break;
        case Value::Type::Boolean:
            if(r.value.as<bool>())
                msgpack_pack_true(pk.get());
            else
                msgpack_pack_false(pk.get());
            break;
        case Value::Type::Numeric:
            msgpack_pack_double(pk.get(), r.value.as<double>());
            break;
        case Value::Type::String:
            {
                std::string b = r.value.as<std::string>();
                msgpack_pack_raw(pk.get(), b.size());
                msgpack_pack_raw_body(pk.get(), b.data(), b.size());
            }
            break;
        case Value::Type::Table:
            {
                // Hack
                std::string b = r.value.as<std::string>();
                if(b.empty())
                    msgpack_pack_nil(pk.get());
                else
                    msgpack_sbuffer_write(reinterpret_cast<void *>(buffer.get()), b.data(), b.size());
            }
            break;
    }

    msgpack_pack_raw(pk.get(), 5);
    msgpack_pack_raw_body(pk.get(), "state", 5);
    if(r.state.empty())
        msgpack_pack_nil(pk.get());
    else
        msgpack_sbuffer_write(reinterpret_cast<void *>(buffer.get()), r.state.data(), r.state.size());

    return std::string(buffer->data, buffer->size);
}
Example #4
0
/* Given a series of tasks, serialize into a series of array objects
 * using messagepack and return pointer to the buffer containing the
 * serialized data
 */
msgpack_sbuffer *
oolong_serialize(msgpack_sbuffer *sbuf, oolong_task **tasks, int size) {
    int i;
    oolong_task *task;
    msgpack_packer *pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    for(i = 0; i < size; i++) {
        task = tasks[i];

        /* Begin array packing */
        msgpack_pack_array(pk, 3);

        /* Pack description */
        msgpack_pack_raw(pk, sizeof(task->description) + 1);
        msgpack_pack_raw_body(pk, task->description, sizeof(task->description) + 1);

        /* Pack completed */
        msgpack_pack_int(pk, task->completed);

        /* Pack due */
        msgpack_pack_int(pk, task->due);
    }

    msgpack_packer_free(pk);

    return sbuf;
}
Example #5
0
TEST(fixint, size)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    size_t sum = 0;

    EXPECT_EQ(0, msgpack_pack_fix_int8(pk, 0));
    EXPECT_EQ(sum+=2, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_int16(pk, 0));
    EXPECT_EQ(sum+=3, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_int32(pk, 0));
    EXPECT_EQ(sum+=5, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_int64(pk, 0));
    EXPECT_EQ(sum+=9, sbuf->size);

    EXPECT_EQ(0, msgpack_pack_fix_uint8(pk, 0));
    EXPECT_EQ(sum+=2, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_uint16(pk, 0));
    EXPECT_EQ(sum+=3, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_uint32(pk, 0));
    EXPECT_EQ(sum+=5, sbuf->size);
    EXPECT_EQ(0, msgpack_pack_fix_uint64(pk, 0));
    EXPECT_EQ(sum+=9, sbuf->size);

    msgpack_sbuffer_free(sbuf);
    msgpack_packer_free(pk);
}
Example #6
0
PERIODIC_THREAD_END


void mon_init(void)
{
   ASSERT_ONCE();

   /* open monitoring socket: */
   mon_socket = scl_get_socket("ap_mon");
   ASSERT_NOT_NULL(mon_socket);
   int64_t hwm = 1;
   zmq_setsockopt(mon_socket, ZMQ_SNDHWM, &hwm, sizeof(hwm));

   /* create monitoring connection: */
   const struct timespec period = {0, 20 * NSEC_PER_MSEC};
   pthread_mutexattr_init(&mutexattr);
   pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_INHERIT);
   pthread_mutex_init(&mutex, &mutexattr);

   /* init msgpack buffer: */
   ASSERT_NULL(msgpack_buf);
   msgpack_buf = msgpack_sbuffer_new();
   ASSERT_NOT_NULL(msgpack_buf);
   ASSERT_NULL(pk);
   pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write);

   periodic_thread_start(&emitter_thread, mon_emitter, "mon_thread", THREAD_PRIORITY, period, NULL);
}
Example #7
0
void
yakyak_init(struct yakyak *yy, size_t n)
{
  yy->buf = msgpack_sbuffer_new();
  yy->pk = msgpack_packer_new(yy->buf, msgpack_sbuffer_write);

  msgpack_pack_array(yy->pk, n);
}
Example #8
0
TEST(pack, num)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    EXPECT_EQ(0, msgpack_pack_int(pk, 1));

    msgpack_sbuffer_free(sbuf);
    msgpack_packer_free(pk);
}
Example #9
0
static inline msgpack_packer *monitor_method_new(char *method, msgpack_sbuffer *sbuffer)
{
  msgpack_packer *pk;

  msgpack_sbuffer_clear(sbuffer);
  pk = msgpack_packer_new(sbuffer, msgpack_sbuffer_write);
  monitor_method_add(method, pk);

  return pk;
}
Example #10
0
void
json_string_to_msgpack(const char *json_str, msgpack_sbuffer *buf)
{
	PackState		state;
	JsonLexContext	*lex = makeJsonLexContext(cstring_to_text(json_str), true);
	JsonSemAction	*sem;
	msgpack_packer	*pk;
	JsonContainer	top_level;
	JsonValue		top_level_value;

	/* initialize packer */
	pk = msgpack_packer_new(buf, msgpack_sbuffer_write);

	/* initialize top level object */
	top_level_value = palloc(sizeof(JsonValueData));

	top_level = palloc(sizeof(JsonContainerData));
	top_level->type = JSON_TOP_LEVEL;
	top_level->parent = NULL;
	top_level->via.top_level.value = top_level_value;

	/* initialize state object */
	state = palloc(sizeof(PackStateData));
	state->lex = lex;
	state->current_container = top_level;
	state->pk = pk;
	state->buf = buf;

	/* initialize sem action */
	sem = palloc(sizeof(JsonSemAction));
	sem->semstate 				= (void *) state;
	sem->object_start 			= sem_object_start;
	sem->object_end 			= sem_object_end;
	sem->array_start 			= sem_array_start;
	sem->array_end 				= sem_array_end;
	sem->object_field_start 	= sem_object_field_start;
	sem->object_field_end 		= NULL;
	sem->array_element_start 	= sem_array_element_start;
	sem->array_element_end 		= NULL;
	sem->scalar 				= sem_scalar;

	/* run parser */
	pg_parse_json(lex, sem);

	/* pack top level value */
	pack_value(pk, top_level_value);

	/* destroy packer */
	msgpack_packer_free(pk);

	/* destroy top level object */
	destroy_container(top_level);
}
Example #11
0
static void queue_cpu_sample_for_sending(rbkit_cpu_sample *sample) {
  msgpack_sbuffer* buffer = msgpack_sbuffer_new();
  msgpack_packer* packer = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  rbkit_cpu_sample_event *event = new_rbkit_cpu_sample_event(sample);

  pack_event((rbkit_event_header *)event, packer);
  free(event);

  send_message(buffer);
  msgpack_sbuffer_free(buffer);
  msgpack_packer_free(packer);
}
Example #12
0
static zmsg_t *create_call(const char *actionid, const char *module, const char *function, const char *data)
{
	zmsg_t *msg = zmsg_new();

    msgpack_sbuffer* pIntBuffer = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new( pIntBuffer, msgpack_sbuffer_write );

    if (data && actionid && module)
    {
        msgpack_pack_map( pk, 3 );
        {
            m_pack_raw(pk, "command");
            m_pack_raw(pk, "call");

            m_pack_raw(pk, "body");
            msgpack_pack_map( pk, 5 );
            {
                m_pack_raw(pk, "module");
                m_pack_raw(pk, module);
                
                m_pack_raw(pk, "function");
                m_pack_raw(pk, function);
                
                m_pack_raw(pk, "version");
                msgpack_pack_uint64(pk, 1);
                
                m_pack_raw(pk, "parameters");
                msgpack_pack_map( pk, 1 );
                {
					m_pack_raw(pk, function);
					m_pack_raw(pk, data);
				}
                m_pack_raw(pk, "seqno");
                msgpack_pack_uint64(pk, 0);
            }
            m_pack_raw(pk, "actionid");
            m_pack_raw(pk, actionid);
        }
    }  else {
        return NULL;
    }
	
    zmsg_pushmem(msg, pIntBuffer->data, pIntBuffer->size);

    msgpack_sbuffer_free( pIntBuffer );
    msgpack_packer_free( pk );

    return msg;
}
Example #13
0
static VALUE send_handshake_response(VALUE self) {
  msgpack_sbuffer *buffer = msgpack_sbuffer_new();
  msgpack_packer *packer = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  rbkit_hash_event *event = new_rbkit_hash_event(handshake, rbkit_status_as_hash(self));
  pack_event((rbkit_event_header *)event, packer);
  free(event);

  if(buffer && buffer->size > 0)
    respond_with_message(buffer->data, buffer->size);

  msgpack_sbuffer_free(buffer);
  msgpack_packer_free(packer);
  return Qnil;
}
Example #14
0
static rbkit_logger * get_trace_logger() {
  if (logger == 0) {
    logger = ALLOC_N(rbkit_logger, 1);
    logger->enabled = Qfalse;
    logger->sampling_profiler_enabled = Qfalse;
    logger->newobj_trace = 0;
    logger->freeobj_trace = 0;
    logger->object_table = st_init_numtable();
    logger->str_table = st_init_strtable();

    logger->sbuf = msgpack_sbuffer_new();
    logger->msgpacker = msgpack_packer_new(logger->sbuf, msgpack_sbuffer_write);
    logger->data = 0;
  }
  return logger;
}
Example #15
0
void publish_output(process_t *process, struct proc_data *procdata,
                    const char *ioname, const char *data, ssize_t length) {
  /* TODO(sissel): move this to a separate file for 'event' streaming */
  zmq_msg_t event;
  int rc;
  size_t msgsize;
  program_t *program = pn_proc_program(process);
  procnanny_t *pn = program->data;

  fprintf(stdout, "%s[%d]: (%d bytes) %.*s\n", pn_proc_program(process)->name,
          pn_proc_instance(process), length, length, data);

  /* Fields:
   *  - data (the string read)
   *  - program name
   *  - process instance
   *  - stdout or stderr
   */

  msgpack_sbuffer *buffer = msgpack_sbuffer_new();
  msgpack_packer *output_msg = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  msgpack_pack_map(output_msg, 5);

  /* "event" => "data" */
  msgpack_pack_string(output_msg, "event", -1);
  msgpack_pack_string(output_msg, "data", -1);

  msgpack_pack_string(output_msg, "program", -1);
  msgpack_pack_string(output_msg, program->name, program->name_len);

  msgpack_pack_string(output_msg, "instance", -1);
  msgpack_pack_int(output_msg, process->instance);

  msgpack_pack_string(output_msg, "source", -1);
  msgpack_pack_string(output_msg, ioname, -1);

  msgpack_pack_string(output_msg, "data", -1);
  msgpack_pack_string(output_msg, data, length);

  zmq_msg_init_data(&event, buffer->data, buffer->size, free_msgpack_buffer, buffer); 
  zmq_send(pn->eventsocket, &event, 0);
  zmq_msg_close(&event);

  msgpack_packer_free(output_msg);
} /* publish_output */
Example #16
0
yar_packager * yar_pack_start(yar_data_type type, uint size) /* {{{ */ {
	yar_packager *packager = malloc(sizeof(yar_packager));
	
	packager->bf = msgpack_sbuffer_new();
	packager->pk = msgpack_packer_new(packager->bf, msgpack_sbuffer_write);

	switch (type) {
		case YAR_DATA_ARRAY:
			msgpack_pack_array(packager->pk, size);
			break;
		case YAR_DATA_MAP:
			msgpack_pack_map(packager->pk, size);
			break;
		default:
			break;
	}

	return packager;
}
Example #17
0
void send_trigger(mdcli_t * client, char * target_worker, char * rule_id,  int ival, char * user_id) {
  zclock_log("activating trigger\ntarget=%s\nvalue=%d\nuser=%s",
             target_worker, ival, user_id);
  struct timeval tval;
  gettimeofday(&tval, NULL);
  // make a messagepack hash
  msgpack_sbuffer * buffer =  msgpack_sbuffer_new();
  msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
  // value chunk
  msgpack_pack_map(pk, 3);
  // key
  msgpack_pack_raw(pk, 5);
  msgpack_pack_raw_body(pk, "value", 5);
  // value
  msgpack_pack_int(pk, ival);

  //time chunk

  // key
  msgpack_pack_raw(pk, 5);
  msgpack_pack_raw_body(pk, "epoch", 5);
  // time
  msgpack_pack_int(pk, tval.tv_sec);

  msgpack_pack_raw(pk, 6);
  msgpack_pack_raw_body(pk, "micros", 6);
  // time
  msgpack_pack_int(pk, tval.tv_usec);


  zmsg_t * msg = zmsg_new();
  // really, the user_id should be being added by a
  // gatekeeper, not the block itself, or it's a security
  // hole. will do for now FIX
  
  zmsg_pushstr(msg, user_id);
  // zmsg_pushmem(msg, &trigger.line_id, sizeof(int));

  zmsg_pushmem(msg, buffer->data, buffer->size);
  zmsg_pushstr(msg, rule_id);
  zmsg_pushstr(msg, "DoAction");
  mdcli_send(client, target_worker, &msg);
}
Example #18
0
static VALUE send_hash_as_event(int argc, VALUE *argv, VALUE self) {
  VALUE hash_object;
  VALUE event_type;
  msgpack_sbuffer *buffer;
  msgpack_packer *packer;
  rbkit_hash_event *event;

  rb_scan_args(argc, argv, "20", &hash_object, &event_type);

  buffer = msgpack_sbuffer_new();
  packer = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  event = new_rbkit_hash_event(FIX2INT(event_type), hash_object);
  pack_event((rbkit_event_header *)event, packer);
  free(event);

  send_message(buffer);
  msgpack_sbuffer_free(buffer);
  msgpack_packer_free(packer);
  return Qnil;
}
Example #19
0
static char *
pack_data(int *size)
{
	msgpack_sbuffer *buffer = NULL;
	msgpack_packer *packer  = NULL;
	char *data;

	buffer = msgpack_sbuffer_new();
	msgpack_sbuffer_init(buffer);
	packer = msgpack_packer_new((void *)buffer, msgpack_sbuffer_write);

	pack_node(packer, &node);

	data = (char *)malloc(buffer->size + 1);
	memcpy(data, &buffer->data[0], buffer->size);
	data[buffer->size] = '\0';
	*size = buffer->size;

	msgpack_packer_free(packer);
	msgpack_sbuffer_free(buffer);
	return data;
}
Example #20
0
static VALUE send_objectspace_dump(VALUE self) {
  msgpack_sbuffer* buffer = msgpack_sbuffer_new();
  msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  rbkit_object_dump * dump = get_object_dump(logger->object_table);
  rbkit_object_space_dump_event *event = new_rbkit_object_space_dump_event(dump);

  // Object space dump can span across messages.
  // So we keep creating and queueing the messages
  // until we've packed all objects.
  while(event->packed_objects < event->object_count) {
    pack_event((rbkit_event_header *)event, pk);
    send_message(buffer);
  }

  free(event->current_page);
  free(event);
  free(dump);
  msgpack_sbuffer_free(buffer);
  msgpack_packer_free(pk);
  return Qnil;
}
Example #21
0
TEST(pack, insufficient)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    EXPECT_EQ(0, msgpack_pack_int(pk, 255)); // uint8 (2bytes)

    msgpack_unpack_return success;
    size_t offset = 0;

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    success = msgpack_unpack_next(&msg, sbuf->data, 1, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);
    EXPECT_EQ(0u, offset);

    msgpack_unpacked_destroy(&msg);

    msgpack_sbuffer_free(sbuf);
    msgpack_packer_free(pk);
}
Example #22
0
void
save_data(char *filename, save_data_function pack_function)
{
	msgpack_sbuffer *buffer = NULL;
	msgpack_packer *packer  = NULL;
	char *save_file_name;
	FILE *save_file;

	if ((save_file_name = get_save_file_path(filename)) == NULL) {
		red_log(REDD_WARNING, "get_save_file_path returned NULL");
		return;
	}

	buffer = msgpack_sbuffer_new();
	msgpack_sbuffer_init(buffer);

	packer = msgpack_packer_new((void *)buffer, msgpack_sbuffer_write);

	msgpack_pack_map(packer, 1);
	pack_function(packer);

	if ((save_file = fopen(save_file_name, "w")) != NULL) {
		size_t written;
		written = fwrite((void *)&buffer->data[0], 1, buffer->size, save_file);
		fclose(save_file);
		if (written != buffer->size) {
			red_log(REDD_WARNING, "Failed to write to %s", save_file_name);
		}
	} else {
		red_log(REDD_WARNING, "Failed to open %s", save_file_name);
	}

	free(save_file_name);
	save_file_name = NULL;
	msgpack_packer_free(packer);
	packer = NULL;
	msgpack_sbuffer_free(buffer);
	buffer = NULL;
}
Example #23
0
void blackbox_init(void)
{
    ASSERT_ONCE();
    ASSERT_NULL(blackbox_socket);
    /* get scl socket */
    blackbox_socket = scl_get_socket("blackbox");
    ASSERT_NOT_NULL(blackbox_socket);

    /* send blackbox header: */
    ASSERT_NULL(msgpack_buf);
    msgpack_buf = msgpack_sbuffer_new();
    ASSERT_NOT_NULL(msgpack_buf);
    ASSERT_NULL(pk);
    pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write);
    ASSERT_NOT_NULL(pk);
    msgpack_pack_array(pk, BLACKBOX_ITEMS);
    FOR_EACH(i, blackbox_spec)
    {
        size_t len = strlen(blackbox_spec[i]);
        msgpack_pack_raw(pk, len);
        msgpack_pack_raw_body(pk, blackbox_spec[i], len);
    }
Example #24
0
rpc_call_t *rpc_call_new(void *zmq, struct ev_loop *ev, const char *address,
                         const char *method) {
  rpc_call_t *rpc = calloc(1, sizeof(rpc_call_t));
  rpc->zmq = zmq;
  rpc->ev = ev;
  rpc->address = address;

  rpc->pack_buffer = msgpack_sbuffer_new();
  rpc->request = msgpack_packer_new(rpc->pack_buffer, msgpack_sbuffer_write);

  msgpack_pack_map(rpc->request, 2); /* method and args */
  msgpack_pack_string(rpc->request, "method", 6);
  msgpack_pack_string(rpc->request, method, strlen(method));
  msgpack_pack_string(rpc->request, "args", 4);

  /* The rest of the packing is up to the invoker of the rpc call.
   * Add whatever arguments are necessary later to rpc->request */

  printf("Created new rpc call object targeting %s method %s\n",
         address, method);
  return rpc;
} /* rpc_call_new */
Example #25
0
TEST(unpack, sequence)
{
    msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);

    EXPECT_EQ(0, msgpack_pack_int(pk, 1));
    EXPECT_EQ(0, msgpack_pack_int(pk, 2));
    EXPECT_EQ(0, msgpack_pack_int(pk, 3));

    msgpack_packer_free(pk);

    msgpack_unpack_return success;
    size_t offset = 0;

    msgpack_unpacked msg;
    msgpack_unpacked_init(&msg);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(1u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(2u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_SUCCESS, success);
    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
    EXPECT_EQ(3u, msg.data.via.u64);

    success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
    EXPECT_EQ(MSGPACK_UNPACK_CONTINUE, success);

    msgpack_sbuffer_free(sbuf);
    msgpack_unpacked_destroy(&msg);
}
Example #26
0
int main(void)
{
	msgpack_sbuffer* buffer = msgpack_sbuffer_new();
	msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

	msgpack_pack_array(pk, 2);
	msgpack_pack_raw(pk, 5);
	msgpack_pack_raw_body(pk, "hello", 5);
	msgpack_pack_raw(pk, 11);
	msgpack_pack_raw_body(pk, "messagepack", 11);

	msgpack_unpacked msg;
	msgpack_unpacked_init(&msg);
	bool success = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);

	msgpack_object obj = msg.data;
	msgpack_object_print(stdout, obj);
	printf("\n");

	msgpack_sbuffer_free(buffer);
	msgpack_packer_free(pk);

}
Example #27
0
static void
msgpack_wrap_redis_reply(const struct cmd *cmd, struct msg_out *out, const redisReply *r) {

	unsigned int i;
	msgpack_packer* pk = msgpack_packer_new(out, on_msgpack_write);

	/* copy verb, as jansson only takes a char* but not its length. */
	char *verb = "";
	size_t verb_sz = 0;
	if(cmd->count) {
		verb_sz = cmd->argv_len[0];
		verb = cmd->argv[0];
	}

	/* Create map object */
	msgpack_pack_map(pk, 1);

	/* The single element is the verb */
	msgpack_pack_raw(pk, verb_sz);
	msgpack_pack_raw_body(pk, verb, verb_sz);

	switch(r->type) {
		case REDIS_REPLY_STATUS:
		case REDIS_REPLY_ERROR:
			msgpack_pack_array(pk, 2);

			/* first element: book */
			if(r->type == REDIS_REPLY_ERROR)
				msgpack_pack_false(pk);
			else
				msgpack_pack_true(pk);

			/* second element: message */
			msgpack_pack_raw(pk, r->len);
			msgpack_pack_raw_body(pk, r->str, r->len);
			break;

		case REDIS_REPLY_STRING:
			if(verb_sz ==4 && strncasecmp(verb, "INFO", 4) == 0) {
				msg_info_reply(pk, r->str, r->len);
			} else {
				msgpack_pack_raw(pk, r->len);
				msgpack_pack_raw_body(pk, r->str, r->len);
			}
			break;

		case REDIS_REPLY_INTEGER:
			msgpack_pack_int(pk, r->integer);
			break;

		case REDIS_REPLY_ARRAY:
			if(verb_sz == 7 && strncasecmp(verb, "HGETALL", 7) == 0) {
				msg_hgetall_reply(pk, r);
				break;
			}

			msgpack_pack_array(pk, r->elements);

			for(i = 0; i < r->elements; ++i) {
				redisReply *e = r->element[i];
				switch(e->type) {
					case REDIS_REPLY_STRING:
						msgpack_pack_raw(pk, e->len);
						msgpack_pack_raw_body(pk, e->str, e->len);
						break;
					case REDIS_REPLY_INTEGER:
						msgpack_pack_int(pk, e->integer);
						break;
					default:
						msgpack_pack_nil(pk);
						break;
				}
			}

			break;

		default:
			msgpack_pack_nil(pk);
			break;
	}

	msgpack_packer_free(pk);
}
Example #28
0
int _main(void)
{
   THROW_BEGIN()

   /* init scl and get sockets:: */
   THROW_ON_ERR(scl_init("arduino"));
   void *rc_socket = scl_get_socket("rc_raw");
   THROW_IF(rc_socket == NULL, -ENODEV);
   void *power_socket = scl_get_socket("power");
   THROW_IF(power_socket == NULL, -ENODEV);

   /* allocate msgpack buffers: */
   msgpack_sbuffer *msgpack_buf = msgpack_sbuffer_new();
   THROW_IF(msgpack_buf == NULL, -ENOMEM);
   msgpack_packer *pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write);
   THROW_IF(pk == NULL, -ENOMEM);
 
   /* fetch parameters: */
   char *dev_path;
   tsint_t dev_speed;
   opcd_params_init("exynos_quad.arduino_serial.", 0);
   opcd_param_t params[] =
   {
      {"path", &dev_path},
      {"speed", &dev_speed},
      OPCD_PARAMS_END
   };
   opcd_params_apply("", params);
   
   /* opern serial port: */
   serialport_t port;
   THROW_ON_ERR(serial_open(&port, dev_path, tsint_get(&dev_speed), O_RDONLY));

   uint16_t channels_raw[PPM_CHAN_MAX];
   uint32_t voltage_raw, current_raw;
   float channels[PPM_CHAN_MAX];

   while (running)
   {
      int c = serial_read_char(&port);
      if (c < 0)
         msleep(1);
      
      /* parse channels: */
      int status = ppm_parse_frame(channels_raw, (uint8_t)(c));
      if (status)
      {
         int sig_valid = 0;
         int invalid_count = 0;
         FOR_N(i, PPM_CHAN_MAX)
         {
            uint16_t chan_in = channels_raw[i];
            if (chan_in >= PPM_VALUE_INVALID)
               invalid_count++;
            if (chan_in > PPM_VALUE_MAX)
               chan_in = PPM_VALUE_MAX;
            if (chan_in < PPM_VALUE_MIN)
               chan_in = PPM_VALUE_MIN;
            channels[i] = (float)(chan_in - PPM_VALUE_MIN) / (PPM_VALUE_MAX - PPM_VALUE_MIN) * 2.f - 1.f;
         }
         sig_valid = (invalid_count < (PPM_CHAN_MAX / 3));
         
         /* send channels: */
         msgpack_sbuffer_clear(msgpack_buf);
         msgpack_pack_array(pk, 1 + PPM_CHAN_MAX);
         PACKI(sig_valid);               /* index 0: valid */
         PACKFV(channels, PPM_CHAN_MAX); /* index 1, .. : channels */
         scl_copy_send_dynamic(rc_socket, msgpack_buf->data, msgpack_buf->size);
      }

      /* parse adc voltage/current: */
      status = power_parse_frame(&voltage_raw, &current_raw, (uint8_t)(c));
      if (status)
      {
         float voltage = (float)(voltage_raw) / 1000.0;
         float current = (float)(current_raw) / 1000.0;
         
         /* send voltage / current: */
         msgpack_sbuffer_clear(msgpack_buf);
         msgpack_pack_array(pk, 2);
         PACKF(voltage); /* index 0 */
         PACKF(current); /* index 1 */
	      scl_copy_send_dynamic(power_socket, msgpack_buf->data, msgpack_buf->size);
      }
   }
Example #29
0
int main_i2c(void)
{
   THROW_BEGIN();

   void *gps_socket = scl_get_socket("gps");
   THROW_IF(gps_socket == NULL, -EIO);
   int64_t hwm = 1;
   zmq_setsockopt(gps_socket, ZMQ_SNDHWM, &hwm, sizeof(hwm));

   void *sats_socket = scl_get_socket("sats");
   THROW_IF(sats_socket == NULL, -EIO);
 
   i2c_bus_t bus;
   i2c_dev_t device;

   uint8_t data_w[128];
   uint8_t data_r[128];   

   if (i2c_bus_open(&bus, "/dev/i2c-1"))
   {
      syslog(LOG_CRIT, "could not open i2c device");   
      exit(EXIT_FAILURE);
   }
   i2c_dev_init(&device, &bus, I2C_GPS_ADDRESS);
   
   msgpack_sbuffer *msgpack_buf = msgpack_sbuffer_new();
   THROW_IF(msgpack_buf == NULL, -ENOMEM);
   msgpack_packer *pk = msgpack_packer_new(msgpack_buf, msgpack_sbuffer_write);
   THROW_IF(pk == NULL, -ENOMEM);

   while (1)
   {
      msleep(200);

      data_w[0] = I2C_GPS_STATUS;
      i2c_xfer(&device, 1, data_w, 1, data_r);
      
      msgpack_sbuffer_clear(msgpack_buf);
      int status = data_r[0];
      int fix = 0;
      if (status & I2C_GPS_STATUS_2DFIX)
         fix = 2;
      if (status & I2C_GPS_STATUS_3DFIX)
         fix = 3;

      if (fix == 2)
         msgpack_pack_array(pk, 7); /* 2d fix */
      else if (fix == 3)
         msgpack_pack_array(pk, 9); /* 3d fix */
      else
         msgpack_pack_array(pk, 1); /* no fix */
 
      data_w[0] = I2C_GPS_TIME;
      i2c_xfer(&device, 1, data_w, 4, data_r);
      long time = (((long) data_r[3]) << 24)
                | (((long) data_r[2]) << 16) 
                | (((long) data_r[1]) << 8) 
                | (data_r[0]);
      time /= 1000;
      char *time_str = ctime(&time);
      size_t len = strlen(time_str);
      msgpack_pack_raw(pk, len);
      msgpack_pack_raw_body(pk, time_str, len); /* gps array index 0 */

      if (fix == 2 || fix == 3)
      {
         data_w[0] = I2C_GPS_LOCATION;
         i2c_xfer(&device, 1, data_w, 8, data_r);
         PACKD(( (((long) data_r[3]) << 24) 
               | (((long) data_r[2]) << 16) 
               | (((long) data_r[1]) << 8) 
               | (data_r[0])) / 10000000.0); /* latitude, gps array index 1 */

         PACKD(( (((long) data_r[7]) << 24) 
               | (((long) data_r[6]) << 16) 
               | (((long) data_r[5]) << 8) 
               | (data_r[4])) / 10000000.0); /* logitude, gps array index 2 */

         PACKI((status & I2C_GPS_STATUS_NUMSATS) >> 4); /* gps array index 3 */
         
         data_w[0] = I2C_GPS_GROUND_SPEED;
         i2c_xfer(&device, 1, data_w, 2, data_r);
         PACKF(((float)((data_r[1] << 8) | data_r[0])) / 100.0 * 1.94384); /* gps array index 4 */
         
         data_w[0] = I2C_GPS_GROUND_SPEED;
         i2c_xfer(&device, 1, data_w, 2, data_r);
         PACKF((data_r[1] << 8) | data_r[0]); /* gps array index 5 */
         
         PACKF(0 /* HDOP */); /* gps array index 6 */
      }

      if (fix == 3)
      {
         data_w[0] = I2C_GPS_ALTITUDE;
         i2c_xfer(&device, 1, data_w, 2, data_r);
         PACKF(((data_r[1]) << 8) | data_r[0]);  /* gps array index 7 */
         PACKF(0 /* VDOP */); /* gps array index 8 */
      }
            
      scl_copy_send_dynamic(gps_socket, msgpack_buf->data, msgpack_buf->size);
   }
Example #30
0
void publish_proc_event(process_t *process, const char *name) {
  /* TODO(sissel): move this to a separate file for 'event' streaming */
  zmq_msg_t event;
  int rc;
  size_t msgsize;
  program_t *program = pn_proc_program(process);
  procnanny_t *pn = program->data;

  /* Fields:
   *  - program name
   *  - process instance
   *  - exit status
   *  - duration ?
   */

  msgpack_sbuffer *buffer = msgpack_sbuffer_new();
  msgpack_packer *output_msg = msgpack_packer_new(buffer, msgpack_sbuffer_write);

  msgpack_pack_map(output_msg, 5);
  msgpack_pack_string(output_msg, "event", -1);
  msgpack_pack_string(output_msg, name, -1);

  msgpack_pack_string(output_msg, "program", -1);
  msgpack_pack_string(output_msg, program->name, program->name_len);

  msgpack_pack_string(output_msg, "instance", -1);
  msgpack_pack_int(output_msg, process->instance);

  msgpack_pack_string(output_msg, "state", -1);
  switch (process->state) {
    case PROCESS_STATE_STARTING: msgpack_pack_string(output_msg, "starting", -1); break;
    case PROCESS_STATE_RUNNING: msgpack_pack_string(output_msg, "running", -1); break;
    case PROCESS_STATE_STOPPING: msgpack_pack_string(output_msg, "stopping", -1); break;
    case PROCESS_STATE_EXITED: msgpack_pack_string(output_msg, "exited", -1); break;
    case PROCESS_STATE_BACKOFF: msgpack_pack_string(output_msg, "backoff", -1); break;
    case PROCESS_STATE_NEW: msgpack_pack_string(output_msg, "new", -1); break;
    default: msgpack_pack_string(output_msg, "unknown", -1); break;
  }

  if (process->state == PROCESS_STATE_EXITED
      || process->state == PROCESS_STATE_BACKOFF) {
    msgpack_pack_string(output_msg, "code", -1);
    msgpack_pack_nil(output_msg);
  } else if (process->exit_signal == 0) {
    msgpack_pack_string(output_msg, "code", -1);
    msgpack_pack_int(output_msg, process->exit_status);
  } else {
    msgpack_pack_string(output_msg, "signal", -1);

    /* lol. */
    switch (process->exit_signal) {
      case SIGHUP: msgpack_pack_string(output_msg, "HUP", 3); break;
      case SIGINT: msgpack_pack_string(output_msg, "INT", 3); break;
      case SIGQUIT: msgpack_pack_string(output_msg, "QUIT", 4); break;
      case SIGILL: msgpack_pack_string(output_msg, "ILL", 3); break;
      case SIGTRAP: msgpack_pack_string(output_msg, "TRAP", 4); break;
      case SIGABRT: msgpack_pack_string(output_msg, "ABRT", 4); break;
      case SIGBUS: msgpack_pack_string(output_msg, "BUS", 3); break;
      case SIGFPE: msgpack_pack_string(output_msg, "FPE", 3); break;
      case SIGKILL: msgpack_pack_string(output_msg, "KILL", 4); break;
      case SIGUSR1: msgpack_pack_string(output_msg, "USR1", 4); break;
      case SIGSEGV: msgpack_pack_string(output_msg, "SEGV", 4); break;
      case SIGUSR2: msgpack_pack_string(output_msg, "USR2", 4); break;
      case SIGPIPE: msgpack_pack_string(output_msg, "PIPE", 4); break;
      case SIGALRM: msgpack_pack_string(output_msg, "ALRM", 4); break;
      case SIGTERM: msgpack_pack_string(output_msg, "TERM", 4); break;
      case SIGSTKFLT: msgpack_pack_string(output_msg, "STKFLT", 6); break;
      case SIGCHLD: msgpack_pack_string(output_msg, "CHLD", 4); break;
      case SIGCONT: msgpack_pack_string(output_msg, "CONT", 4); break;
      case SIGSTOP: msgpack_pack_string(output_msg, "STOP", 4); break;
      case SIGTSTP: msgpack_pack_string(output_msg, "TSTP", 4); break;
      case SIGTTIN: msgpack_pack_string(output_msg, "TTIN", 4); break;
      case SIGTTOU: msgpack_pack_string(output_msg, "TTOU", 4); break;
      case SIGURG: msgpack_pack_string(output_msg, "URG", 3); break;
      case SIGXCPU: msgpack_pack_string(output_msg, "XCPU", 4); break;
      case SIGXFSZ: msgpack_pack_string(output_msg, "XFSZ", 4); break;
      case SIGVTALRM: msgpack_pack_string(output_msg, "VTALRM", 5); break;
      case SIGPROF: msgpack_pack_string(output_msg, "PROF", 4); break;
      case SIGWINCH: msgpack_pack_string(output_msg, "WINCH", 5); break;
      case SIGPOLL: msgpack_pack_string(output_msg, "POLL", 4); break;
      case SIGPWR: msgpack_pack_string(output_msg, "PWR", 3); break;
      case SIGSYS: msgpack_pack_string(output_msg, "SYS", 3); break;
      default: msgpack_pack_string(output_msg, "unknown", -1); break;
    } /* switch process->exit_signal */
  } /* if process was killed by a signal */

  zmq_msg_init_data(&event, buffer->data, buffer->size, free_msgpack_buffer, buffer); 
  zmq_send(pn->eventsocket, &event, 0);
  zmq_msg_close(&event);

  msgpack_packer_free(output_msg);
} /* publish_output */