예제 #1
0
static inline int process_line(char *line, struct flb_in_serial_config *ctx)
{
    int line_len;
    char *p = line;
    char *end = NULL;
    char msg[1024];

    /* Increase buffer position */
    ctx->buffer_id++;

    line_len = strlen(p);
    strncpy(msg, p, line_len);
    msg[line_len] = '\0';

    /*
     * Store the new data into the MessagePack buffer,
     * we handle this as a list of maps.
     */
    msgpack_pack_array(&ctx->mp_pck, 2);
    msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

    msgpack_pack_map(&ctx->mp_pck, 1);
    msgpack_pack_bin(&ctx->mp_pck, 3);
    msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
    msgpack_pack_bin(&ctx->mp_pck, line_len);
    msgpack_pack_bin_body(&ctx->mp_pck, p, line_len);

    flb_debug("[in_serial] '%s'",
              (const char *) msg);

    return 0;
}
예제 #2
0
METHOD(bin, Binary_encode, CipherText)
{
    uint64_t length = be64toh(args.size);

    SUCCESS {
        msgpack_pack_bin(res, sizeof(uint64_t) + args.size);
        msgpack_pack_bin_body(res, &length, sizeof(uint64_t));
        msgpack_pack_bin_body(res, args.ptr, args.size);
    }
    return 0;
}
예제 #3
0
static inline int process_pack(struct tcp_conn *conn,
                               char *pack, size_t size)
{
    size_t off = 0;
    msgpack_unpacked result;
    msgpack_object entry;
    struct flb_in_tcp_config *ctx;

    ctx = conn->ctx;

    /* First pack the results, iterate concatenated messages */
    msgpack_unpacked_init(&result);
    while (msgpack_unpack_next(&result, pack, size, &off)) {
        entry = result.data;

        msgpack_pack_array(&ctx->mp_pck, 2);
        msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

        msgpack_pack_map(&ctx->mp_pck, 1);
        msgpack_pack_bin(&ctx->mp_pck, 3);
        msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
        msgpack_pack_object(&ctx->mp_pck, entry);

        ctx->buffer_id++;
    }

    msgpack_unpacked_destroy(&result);

    return 0;
}
예제 #4
0
/**
 * Handle request message
 *
 * [type(0), msgid(uint), method(bin), args([...])]
 */
void MsgpackIODevice::dispatchRequest(msgpack_object& req)
{
	uint64_t msgid = req.via.array.ptr[1].via.u64;
	QByteArray errmsg("Unknown method");
	QVariant params;
	QByteArray method;

	if (!m_reqHandler) {
		goto err;
	}

	if (decodeMsgpack(req.via.array.ptr[2], method)) {
		qDebug() << "Found unexpected method in request" << req;
		goto err;
	}
	if (decodeMsgpack(req.via.array.ptr[3], params)) {
		qDebug() << "Found unexpected parameters in request" << req;
		goto err;
	}
	m_reqHandler->handleRequest(this, msgid, method, params.toList());
	return;

err:
	// Send error reply [type(1), msgid, error, NIL]
	msgpack_pack_array(&m_pk, 4);
	msgpack_pack_int(&m_pk, 1);
	msgpack_pack_int(&m_pk, msgid);
	msgpack_pack_bin(&m_pk, errmsg.size());
	msgpack_pack_bin_body(&m_pk, errmsg.constData(), errmsg.size());
	msgpack_pack_nil(&m_pk);
}
예제 #5
0
static inline int process_line(char *line, struct flb_in_serial_config *ctx)
{
    int line_len;
    uint64_t val;
    char *p = line;
    char *end = NULL;
    char msg[1024];

    /* Increase buffer position */
    ctx->buffer_id++;

    errno = 0;
    val = strtol(p, &end, 10);
    if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))
        || (errno != 0 && val == 0)) {
        goto fail;
    }

    /* Now process the human readable message */

    line_len = strlen(p);
    strncpy(msg, p, line_len);
    msg[line_len] = '\0';

    /*
     * Store the new data into the MessagePack buffer,
     * we handle this as a list of maps.
     */
    msgpack_pack_array(&ctx->mp_pck, 2);
    msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

    msgpack_pack_map(&ctx->mp_pck, 1);
    msgpack_pack_bin(&ctx->mp_pck, 3);
    msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
    msgpack_pack_bin(&ctx->mp_pck, line_len);
    msgpack_pack_bin_body(&ctx->mp_pck, p, line_len);

    flb_debug("[in_serial] '%s'",
              (const char *) msg);

    return 0;

 fail:
    ctx->buffer_id--;
    return -1;
}
예제 #6
0
void MsgpackIODevice::sendError(uint64_t msgid, const QString& msg)
{
	// [type(1), msgid, error, result(nil)]
	msgpack_pack_array(&m_pk, 4);
	msgpack_pack_int(&m_pk, 1); // 1 = Response
	msgpack_pack_int(&m_pk, msgid);
	QByteArray utf8 = msg.toUtf8();
	msgpack_pack_bin(&m_pk, utf8.size());
	msgpack_pack_bin_body(&m_pk, utf8.constData(), utf8.size());
	msgpack_pack_nil(&m_pk);
}
예제 #7
0
METHOD(u64, Binary_encode, PacketKind)
{
    CHECK(args < sizeof packet_kinds / sizeof * packet_kinds);

    SUCCESS {
        uint8_t data[] = {packet_kinds[args]};
        msgpack_pack_bin(res, sizeof data);
        msgpack_pack_bin_body(res, data, sizeof data);
    }
    return 0;
}
예제 #8
0
void in_xbee_rx_queue_raw(struct flb_in_xbee_config *ctx, const char *buf ,int len)
{
    /* Increase buffer position */

    pthread_mutex_lock(&ctx->mtx_mp);

    in_xbee_flush_if_needed(ctx);

    ctx->buffer_id++;

    msgpack_pack_array(&ctx->mp_pck, 2);
    msgpack_pack_uint64(&ctx->mp_pck, time(NULL));
    msgpack_pack_map(&ctx->mp_pck, 1);
    msgpack_pack_bin(&ctx->mp_pck, 4);
    msgpack_pack_bin_body(&ctx->mp_pck, "data", 4);
    msgpack_pack_bin(&ctx->mp_pck, len);
    msgpack_pack_bin_body(&ctx->mp_pck, buf, len);

    pthread_mutex_unlock(&ctx->mtx_mp);
}
예제 #9
0
/* Callback to gather CPU usage between now and previous snapshot */
int in_cpu_collect(struct flb_config *config, void *in_context)
{
    int i;
    int maps;
    int len;
    double usage;
    double total;
    (void) config;
    struct flb_in_cpu_config *ctx = in_context;
    struct cpu_stats *cstats = &ctx->cstats;
    struct cpu_snapshot *s;

    /* Get the current CPU usage */
    total = proc_cpu_load(ctx->n_processors, cstats);
    cstats->load_now = total;

    /* Calculate the difference between the two samples */
    usage = fabs(cstats->load_now - cstats->load_pre) / ctx->cpu_ticks;
    total = (usage * 100) / ctx->n_processors;

    /* Put current load back */
    cstats->load_pre = cstats->load_now;

    /*
     * Store the new data into the MessagePack buffer,
     */
    msgpack_pack_array(&ctx->mp_pck, 2);
    msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

    msgpack_pack_map(&ctx->mp_pck, (ctx->n_processors * 7 ) + 1);
    msgpack_pack_bin(&ctx->mp_pck, 3);
    msgpack_pack_bin_body(&ctx->mp_pck, "cpu", 3);
    msgpack_pack_double(&ctx->mp_pck, total);

    for (i = 1; i < ctx->n_processors + 1; i++) {
        s = &cstats->info[i];

        CPU_PACK_SNAP(s, user);
        CPU_PACK_SNAP(s, nice);
        CPU_PACK_SNAP(s, system);
        CPU_PACK_SNAP(s, idle);
        CPU_PACK_SNAP(s, iowait);
        CPU_PACK_SNAP(s, irq);
        CPU_PACK_SNAP(s, softirq);
    }

    flb_debug("[in_cpu] CPU %0.2f%%", total);

    flb_stats_update(in_cpu_plugin.stats_fd, 0, 1);
    return 0;
}
예제 #10
0
static inline int process_line(char *line, int len,
                               struct flb_in_serial_config *ctx)
{
    /* Increase buffer position */
    ctx->buffer_id++;

    /*
     * Store the new data into the MessagePack buffer,
     * we handle this as a list of maps.
     */
    msgpack_pack_array(&ctx->mp_pck, 2);
    msgpack_pack_uint64(&ctx->mp_pck, time(NULL));

    msgpack_pack_map(&ctx->mp_pck, 1);
    msgpack_pack_bin(&ctx->mp_pck, 3);
    msgpack_pack_bin_body(&ctx->mp_pck, "msg", 3);
    msgpack_pack_bin(&ctx->mp_pck, len);
    msgpack_pack_bin_body(&ctx->mp_pck, line, len);

    flb_debug("[in_serial] message '%s'",
              (const char *) line);

    return 0;
}
예제 #11
0
/**
 * Start an RPC request
 *
 * Use send() to pass on each of the call parameters
 *
 * Returns a MsgpackRequest object. You can connect to
 * its finished() SIGNAL to handle the response
 */
MsgpackRequest* MsgpackIODevice::startRequestUnchecked(const QString& method, quint32 argcount)
{
	quint32 msgid = msgId();
	// [type(0), msgid, method, args]
	msgpack_pack_array(&m_pk, 4);
	msgpack_pack_int(&m_pk, 0);
	msgpack_pack_int(&m_pk, msgid);
	const QByteArray& utf8 = method.toUtf8();
	msgpack_pack_bin(&m_pk, utf8.size());
	msgpack_pack_bin_body(&m_pk, utf8.constData(), utf8.size());
	msgpack_pack_array(&m_pk, argcount);

	MsgpackRequest *r = new MsgpackRequest( msgid, this);
	connect(r, &MsgpackRequest::timeout,
			this, &MsgpackIODevice::requestTimeout);
	m_requests.insert(msgid, r);
	return r;
}
예제 #12
0
METHOD(bin, Binary_decode, CipherText)
{
    uint64_t length;
    uint64_t tmp;

    SUCCESS {
        memcpy(&tmp, args.ptr, sizeof(uint64_t));
        length = be64toh(tmp);

        if (args.size >= sizeof(uint64_t) && args.size == length + sizeof(uint64_t))
        {
            msgpack_pack_bin(res, args.size - sizeof(uint64_t));
            msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t));
        } else {
            msgpack_pack_nil(res);
        }
    }
    return 0;
}
예제 #13
0
METHOD(array, Binary_encode, KeyPair)
{
    CHECK_SIZE(args, 2);
    CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_BIN);
    CHECK_TYPE(args.ptr[1], MSGPACK_OBJECT_BIN);

    msgpack_object_bin secret_key = args.ptr[0].via.bin;
    msgpack_object_bin public_key = args.ptr[1].via.bin;

    CHECK_SIZE(secret_key, 32);
    CHECK_SIZE(public_key, 32);

    SUCCESS {
        uint8_t data[64];
        memcpy(data, secret_key.ptr, 32);
        memcpy(data + 32, public_key.ptr, 32);
        msgpack_pack_bin(res, 64);
        msgpack_pack_bin_body(res, data, 64);
    }

    return 0;
}
예제 #14
0
파일: td.c 프로젝트: pandax381/fluent-bit
/*
 * Convert the internal Fluent Bit data representation to the required
 * one by Treasure Data cloud service.
 *
 * This function returns a new msgpack buffer and store the bytes length
 * in the out_size variable.
 */
static char *td_format(void *data, size_t bytes, int *out_size)
{
    int i;
    int ret;
    int n_size;
    size_t off = 0;
    time_t atime;
    char *buf;
    struct msgpack_sbuffer mp_sbuf;
    struct msgpack_packer mp_pck;
    msgpack_unpacked result;
    msgpack_object root;
    msgpack_object map;
    msgpack_sbuffer *sbuf;

    /* Initialize contexts for new output */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

    /* Iterate the original buffer and perform adjustments */
    msgpack_unpacked_init(&result);

    /* Perform some format validation */
    ret = msgpack_unpack_next(&result, data, bytes, &off);
    if (!ret) {
        return NULL;
    }

    /* We 'should' get an array */
    if (result.data.type != MSGPACK_OBJECT_ARRAY) {
        /*
         * If we got a different format, we assume the caller knows what he is
         * doing, we just duplicate the content in a new buffer and cleanup.
         */
        buf = malloc(bytes);
        if (!buf) {
            return NULL;
        }

        memcpy(buf, data, bytes);
        *out_size = bytes;
        return buf;
    }

    root = result.data;
    if (root.via.array.size == 0) {
        return NULL;
    }

    off = 0;
    msgpack_unpacked_destroy(&result);
    msgpack_unpacked_init(&result);
    while (msgpack_unpack_next(&result, data, bytes, &off)) {
        if (result.data.type != MSGPACK_OBJECT_ARRAY) {
            continue;
        }

        /* Each array must have two entries: time and record */
        root = result.data;
        if (root.via.array.size != 2) {
            continue;
        }

        atime = root.via.array.ptr[0].via.u64;
        map   = root.via.array.ptr[1];

        n_size = map.via.map.size + 1;
        msgpack_pack_map(&mp_pck, n_size);
        msgpack_pack_bin(&mp_pck, 4);
        msgpack_pack_bin_body(&mp_pck, "time", 4);
        msgpack_pack_int32(&mp_pck, atime);

        for (i = 0; i < n_size - 1; i++) {
            msgpack_pack_object(&mp_pck, map.via.map.ptr[i].key);
            msgpack_pack_object(&mp_pck, map.via.map.ptr[i].val);
        }
    }
    msgpack_unpacked_destroy(&result);

    /* Create new buffer */
    sbuf = &mp_sbuf;
    *out_size = sbuf->size;
    buf = malloc(sbuf->size);
    if (!buf) {
        return NULL;
    }

    /* set a new buffer and re-initialize our MessagePack context */
    memcpy(buf, sbuf->data, sbuf->size);
    msgpack_sbuffer_destroy(&mp_sbuf);

    return buf;
}
예제 #15
0
static char * packStructure(char *serviceName, char *dest, char *trans_id, char *payload, char *contentType, unsigned int payload_len)
{           
    msgpack_sbuffer sbuf;
    msgpack_packer pk;  
    char* b64buffer =  NULL;
    size_t encodeSize = 0;
    char source[MAX_PARAMETERNAME_LEN/2] = {'\0'}; 
    int msg_type = 4;

    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, LMLite %s ENTER\n", __FUNCTION__ ));

    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, deviceMAC *********:%s\n",deviceMAC));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, serviceName :%s\n",serviceName));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, dest :%s\n",dest));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, transaction_id :%s\n",trans_id));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, contentType :%s\n",contentType));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, payload_len :%d\n",payload_len));

    snprintf(source, sizeof(source), "mac:%s/%s", deviceMAC, serviceName);

    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, Received DeviceMac from Atom side: %s\n",deviceMAC));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, Source derived is %s\n", source));
  
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, <======== Start of packStructure ======>\n"));
    
    // Start of msgpack encoding
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, -----------Start of msgpack encoding------------\n"));

    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
    msgpack_pack_map(&pk, WEBPA_MAP_SIZE);

    msgpack_pack_str(&pk, strlen(WEBPA_MSG_TYPE));
    msgpack_pack_str_body(&pk, WEBPA_MSG_TYPE,strlen(WEBPA_MSG_TYPE));
    msgpack_pack_int(&pk, msg_type);   
    
    msgpack_pack_str(&pk, strlen(WEBPA_SOURCE));
    msgpack_pack_str_body(&pk, WEBPA_SOURCE,strlen(WEBPA_SOURCE));
    msgpack_pack_str(&pk, strlen(source));
    msgpack_pack_str_body(&pk, source,strlen(source));
    
    msgpack_pack_str(&pk, strlen(WEBPA_DESTINATION));
    msgpack_pack_str_body(&pk, WEBPA_DESTINATION,strlen(WEBPA_DESTINATION));       
    msgpack_pack_str(&pk, strlen(dest));
    msgpack_pack_str_body(&pk, dest,strlen(dest));
    
    msgpack_pack_str(&pk, strlen(WEBPA_TRANSACTION_ID));
    msgpack_pack_str_body(&pk, WEBPA_TRANSACTION_ID,strlen(WEBPA_TRANSACTION_ID));
    msgpack_pack_str(&pk, strlen(trans_id));
    msgpack_pack_str_body(&pk, trans_id,strlen(trans_id));
     
    msgpack_pack_str(&pk, strlen(WEBPA_PAYLOAD));
    msgpack_pack_str_body(&pk, WEBPA_PAYLOAD,strlen(WEBPA_PAYLOAD));
       
    if(strcmp(contentType,"avro/binary") == 0)
    {
        CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, msg->payload binary\n"));
        msgpack_pack_bin(&pk, payload_len);
        msgpack_pack_bin_body(&pk, payload, payload_len);
    }
    else // string: "contentType :application/json"
    {
        CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, msg->payload string\n"));
        msgpack_pack_str(&pk, strlen(payload));
        msgpack_pack_str_body(&pk, payload,strlen(payload));
    }
  
    msgpack_pack_str(&pk, strlen(CONTENT_TYPE));
    msgpack_pack_str_body(&pk, CONTENT_TYPE,strlen(CONTENT_TYPE));
    msgpack_pack_str(&pk, strlen(contentType));
    msgpack_pack_str_body(&pk, contentType,strlen(contentType));
    
    /*if(consoleDebugEnable)
        fprintf(stderr, "RDK_LOG_DEBUG,msgpack encoded data contains %d bytes and data is : %s\n",(int)sbuf.size,sbuf.data);*/

    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,-----------End of msgpack encoding------------\n"));
    // End of msgpack encoding
    
    // Start of Base64 Encode
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,-----------Start of Base64 Encode ------------\n"));
    encodeSize = b64_get_encoded_buffer_size( sbuf.size );
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,encodeSize is %d\n", (int)encodeSize));
    b64buffer = malloc(encodeSize + 1); // one byte extra for terminating NULL byte
    b64_encode((const uint8_t *)sbuf.data, sbuf.size, (uint8_t *)b64buffer);
    b64buffer[encodeSize] = '\0' ;    
 
    
    /*if(consoleDebugEnable)
    {
    int i;
    fprintf(stderr, "RDK_LOG_DEBUG,\n\n b64 encoded data is : ");
    for(i = 0; i < encodeSize; i++)
        fprintf(stderr,"%c", b64buffer[i]);      

    fprintf(stderr,"\n\n");       
    }*/

    //CcspLMLiteTrace(("RDK_LOG_DEBUG,\nb64 encoded data length is %d\n",i));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,---------- End of Base64 Encode -------------\n"));
    // End of Base64 Encode
    
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,Destroying sbuf.....\n"));
    msgpack_sbuffer_destroy(&sbuf);
    
    //CcspLMLiteTrace(("RDK_LOG_DEBUG,Final Encoded data: %s\n",b64buffer));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,Final Encoded data length: %d\n",(int)strlen(b64buffer)));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG,<======== End of packStructure ======>\n"));
    CcspLMLiteConsoleTrace(("RDK_LOG_DEBUG, LMLite %s EXIT\n", __FUNCTION__ ));

    return b64buffer;
}
예제 #16
0
TEST(streaming, basic)
{
    msgpack_sbuffer* buffer = msgpack_sbuffer_new();

    msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

    // 1, 2, 3, "str", ["str_data"], "bin", ["bin_data"], {0.3: 0.4}
    EXPECT_EQ(0, msgpack_pack_int(pk, 1));
    EXPECT_EQ(0, msgpack_pack_int(pk, 2));
    EXPECT_EQ(0, msgpack_pack_int(pk, 3));
    EXPECT_EQ(0, msgpack_pack_str(pk, 3));
    EXPECT_EQ(0, msgpack_pack_str_body(pk, "str", 3));
    EXPECT_EQ(0, msgpack_pack_array(pk, 1));
    EXPECT_EQ(0, msgpack_pack_str(pk, 8));
    EXPECT_EQ(0, msgpack_pack_str_body(pk, "str_data", 8));
    EXPECT_EQ(0, msgpack_pack_bin(pk, 3));
    EXPECT_EQ(0, msgpack_pack_bin_body(pk, "bin", 3));
    EXPECT_EQ(0, msgpack_pack_array(pk, 1));
    EXPECT_EQ(0, msgpack_pack_bin(pk, 8));
    EXPECT_EQ(0, msgpack_pack_bin_body(pk, "bin_data", 8));
    EXPECT_EQ(0, msgpack_pack_map(pk, 1));
    EXPECT_EQ(0, msgpack_pack_float(pk, 0.4f));
    EXPECT_EQ(0, msgpack_pack_double(pk, 0.8));
    int max_count = 6;

    msgpack_packer_free(pk);

    const char* input = buffer->data;
    const char* const eof = input + buffer->size;

    msgpack_unpacker pac;
    msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);

    msgpack_unpacked result;
    msgpack_unpacked_init(&result);

    int count = 0;
    while(count < max_count) {
        bool unpacked = false;

        msgpack_unpacker_reserve_buffer(&pac, 32*1024);

        while(!unpacked) {
            /* read buffer into msgpack_unapcker_buffer(&pac) upto
             * msgpack_unpacker_buffer_capacity(&pac) bytes. */
            memcpy(msgpack_unpacker_buffer(&pac), input, 1);
            input += 1;

            EXPECT_TRUE(input <= eof);

            msgpack_unpacker_buffer_consumed(&pac, 1);

            while(msgpack_unpacker_next(&pac, &result) == MSGPACK_UNPACK_SUCCESS) {
                unpacked = 1;
                msgpack_object obj = result.data;
                msgpack_object e;
                switch(count++) {
                case 0:
                    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
                    EXPECT_EQ(1, obj.via.u64);
                    break;
                case 1:
                    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
                    EXPECT_EQ(2, obj.via.u64);
                    break;
                case 2:
                    EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, obj.type);
                    EXPECT_EQ(3, obj.via.u64);
                    break;
                case 3:
                    EXPECT_EQ(MSGPACK_OBJECT_STR, obj.type);
                    EXPECT_EQ(std::string("str",3), std::string(obj.via.str.ptr, obj.via.str.size));
                    break;
                case 4:
                    EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
                    EXPECT_EQ(1, obj.via.array.size);
                    e = obj.via.array.ptr[0];
                    EXPECT_EQ(MSGPACK_OBJECT_STR, e.type);
                    EXPECT_EQ(std::string("str_data",8), std::string(e.via.str.ptr, e.via.str.size));
                    break;
                case 5:
                    EXPECT_EQ(MSGPACK_OBJECT_BIN, obj.type);
                    EXPECT_EQ(std::string("bin",3), std::string(obj.via.bin.ptr, obj.via.bin.size));
                    break;
                case 6:
                    EXPECT_EQ(MSGPACK_OBJECT_ARRAY, obj.type);
                    EXPECT_EQ(1, obj.via.array.size);
                    e = obj.via.array.ptr[0];
                    EXPECT_EQ(MSGPACK_OBJECT_BIN, e.type);
                    EXPECT_EQ(std::string("bin_data",8), std::string(e.via.bin.ptr, e.via.bin.size));
                    break;
                case 7:
                    EXPECT_EQ(MSGPACK_OBJECT_MAP, obj.type);
                    EXPECT_EQ(1, obj.via.map.size);
                    e = obj.via.map.ptr[0].key;
                    EXPECT_EQ(MSGPACK_OBJECT_FLOAT, e.type);
                    ASSERT_FLOAT_EQ(0.4f, static_cast<float>(e.via.f64));
#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
                    EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type);
                    ASSERT_FLOAT_EQ(0.4f, static_cast<float>(e.via.dec));
#endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT
                    e = obj.via.map.ptr[0].val;
                    EXPECT_EQ(MSGPACK_OBJECT_FLOAT, e.type);
                    ASSERT_DOUBLE_EQ(0.8, e.via.f64);
#if defined(MSGPACK_USE_LEGACY_NAME_AS_FLOAT)
                    EXPECT_EQ(MSGPACK_OBJECT_DOUBLE, e.type);
                    ASSERT_DOUBLE_EQ(0.8, e.via.dec);
#endif // MSGPACK_USE_LEGACY_NAME_AS_FLOAT
                    break;
                }
            }
        }
    }

    msgpack_unpacker_destroy(&pac);
    msgpack_unpacked_destroy(&result);
    msgpack_sbuffer_free(buffer);
}
예제 #17
0
/*
 * This plugin accepts following formats of MessagePack:
 *     { map => val, map => val, map => val }
 *  or [ time, { map => val, map => val, map => val } ]
 */
int in_xbee_rx_queue_msgpack(struct flb_in_xbee_config *ctx, const char *buf ,int len)
{
    msgpack_unpacked record;
    msgpack_unpacked field;
    msgpack_unpacked_init(&record);
    msgpack_unpacked_init(&field);

    size_t off = 0;
    size_t start = 0;
    size_t off2;
    size_t mp_offset;
    int queued = 0;
    uint64_t t;

    pthread_mutex_lock(&ctx->mtx_mp);

    while (msgpack_unpack_next(&record, buf, len, &off)) {
        if (record.data.type == MSGPACK_OBJECT_ARRAY && record.data.via.array.size == 2) {
            /*  [ time, { map => val, map => val, map => val } ] */

            msgpack_unpacked_destroy(&field);
            msgpack_unpacked_init(&field);
            off2 = 0;

            if (! msgpack_unpack_next(&field, buf + 1, len - 1, &off2))
                break;

            if (field.data.type != MSGPACK_OBJECT_POSITIVE_INTEGER)
                break;

            t = field.data.via.u64;
            mp_offset = off2;

            if (! msgpack_unpack_next(&field, buf + 1, len - 1, &off2))
                break;

            if (field.data.type != MSGPACK_OBJECT_MAP)
                break;

            in_xbee_flush_if_needed(ctx);
            ctx->buffer_id++;

            msgpack_pack_array(&ctx->mp_pck, 2);
            msgpack_pack_uint64(&ctx->mp_pck, t);
            msgpack_pack_bin_body(&ctx->mp_pck, (char*) buf + 1 + mp_offset, off2 - mp_offset);

        } else if (record.data.type == MSGPACK_OBJECT_MAP) {
            /*  { map => val, map => val, map => val } */

            in_xbee_flush_if_needed(ctx);
            ctx->buffer_id++;

            msgpack_pack_array(&ctx->mp_pck, 2);
            msgpack_pack_uint64(&ctx->mp_pck, time(NULL));
            msgpack_pack_bin_body(&ctx->mp_pck, buf + start, off - start);

        } else {
            break;

        }
        start = off;
        queued++;
    }

    msgpack_unpacked_destroy(&record);
    msgpack_unpacked_destroy(&field);
    pthread_mutex_unlock(&ctx->mtx_mp);
    return queued;
}
예제 #18
0
/**
 * Serialise a value into the msgpack stream
 */
void MsgpackIODevice::send(const QByteArray& bin)
{
	msgpack_pack_bin(&m_pk, bin.size());
	msgpack_pack_bin_body(&m_pk, bin.constData(), bin.size());
}
예제 #19
0
/* It parse a JSON string and convert it to MessagePack format */
char *flb_pack_json(char *js, size_t len, int *size)
{
    int i;
    int flen;
    int arr_size;
    char *p;
    char *buf;
    jsmntok_t *t;
    jsmntok_t *tokens;
    msgpack_packer pck;
    msgpack_sbuffer sbuf;

    if (!js) {
        return NULL;
    }

    tokens = json_tokenise(js, len, &arr_size);
    if (!tokens) {
        return NULL;
    }

    flb_debug("JSON to pack: '%s'", js);

    /* initialize buffers */
    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);

    for (i = 0; i < arr_size ; i++) {
        t = &tokens[i];
        if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)) {
            break;
        }
        flen = (t->end - t->start);

        switch (t->type) {
        case JSMN_OBJECT:
            flb_debug("json_pack: token=%i is OBJECT (size=%i)", i, t->size);
            msgpack_pack_map(&pck, t->size);
            break;
        case JSMN_ARRAY:
            flb_debug("json_pack: token=%i is ARRAY (size=%i)", i, t->size);
            msgpack_pack_array(&pck, t->size);
            break;
        case JSMN_STRING:
            flb_debug("json_pack: token=%i is STRING (len=%i)\n", i, flen);
            msgpack_pack_bin(&pck, flen);
            msgpack_pack_bin_body(&pck, js + t->start, flen);
            break;
        case JSMN_PRIMITIVE:
            p = js + t->start;
            if (strncmp(p, "false", 5) == 0) {
                flb_debug("json_pack: token=%i is FALSE", i);
                msgpack_pack_false(&pck);
            }
            else if (strncmp(p, "true", 4) == 0) {
                flb_debug("json_pack: token=%i is TRUE", i);
                msgpack_pack_true(&pck);
            }
            else if (strncmp(p, "null", 4) == 0) {
                flb_debug("json_pack: token=%i is NULL", i);
                msgpack_pack_nil(&pck);
            }
            else {
                flb_debug("json_pack: token=%i is INT64", i);
                msgpack_pack_int64(&pck, atol(p));
            }
            break;
        }
    }

    /* dump data back to a new buffer */
    *size = sbuf.size;
    buf = malloc(sbuf.size);
    memcpy(buf, sbuf.data, sbuf.size);
    msgpack_sbuffer_destroy(&sbuf);

    free(tokens);
    return buf;
}
예제 #20
0
int cb_fluentd_flush(void *data, size_t bytes, void *out_context,
                     struct flb_config *config)
{
    int fd;
    int ret = -1;
    int maps = 0;
    size_t total;
    char *buf = NULL;
    msgpack_packer   mp_pck;
    msgpack_sbuffer  mp_sbuf;
    msgpack_unpacked mp_umsg;
    size_t mp_upos = 0;
    (void) out_context;
    (void) config;

    /*
     * The incoming data comes in Fluent Bit format an array of objects, as we
     * aim to send this information to Fluentd through it in_forward plugin, we
     * need to transform the data. The Fluentd in_forward plugin allows one
     * of the following formats:
     *
     *   1. [tag, time, record]
     *
     *    or
     *
     *   2. [tag, [[time,record], [time,record], ...]]
     *
     *   we use the format #2
     */

    /* Initialize packager */
    msgpack_sbuffer_init(&mp_sbuf);
    msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

    /*
     * Count the number of map entries
     *
     * FIXME: Fluent Bit should expose the number of maps into the
     * data, so we avoid this silly counting.
     */
    msgpack_unpacked_init(&mp_umsg);
    while (msgpack_unpack_next(&mp_umsg, data, bytes, &mp_upos)) {
        maps++;
    }
    msgpack_unpacked_destroy(&mp_umsg);

    /* Output: root array */
    msgpack_pack_array(&mp_pck, 2);
    msgpack_pack_bin(&mp_pck, sizeof(FLB_CONFIG_DEFAULT_TAG) - 1);
    msgpack_pack_bin_body(&mp_pck,
                          FLB_CONFIG_DEFAULT_TAG,
                          sizeof(FLB_CONFIG_DEFAULT_TAG) - 1);
    msgpack_pack_array(&mp_pck, maps);

    /* Allocate a new buffer to merge data */
    total = bytes + mp_sbuf.size;
    buf = malloc(total);
    if (!buf) {
        perror("malloc");
        return -1;
    }

    memcpy(buf, mp_sbuf.data, mp_sbuf.size);
    memcpy(buf + mp_sbuf.size, data, bytes);
    msgpack_sbuffer_destroy(&mp_sbuf);

    fd = flb_net_tcp_connect(out_fluentd_plugin.host,
                             out_fluentd_plugin.port);
    if (fd <= 0) {
        free(buf);
        return -1;
    }


    /* FIXME: plain TCP write */
    ret = write(fd, buf, total);
    close(fd);
    free(buf);

    return ret;
}
예제 #21
0
파일: objectc.c 프로젝트: piling/msgpack-c
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d)
{
    switch(d.type) {
    case MSGPACK_OBJECT_NIL:
        return msgpack_pack_nil(pk);

    case MSGPACK_OBJECT_BOOLEAN:
        if(d.via.boolean) {
            return msgpack_pack_true(pk);
        } else {
            return msgpack_pack_false(pk);
        }

    case MSGPACK_OBJECT_POSITIVE_INTEGER:
        return msgpack_pack_uint64(pk, d.via.u64);

    case MSGPACK_OBJECT_NEGATIVE_INTEGER:
        return msgpack_pack_int64(pk, d.via.i64);

    case MSGPACK_OBJECT_FLOAT32:
        return msgpack_pack_float(pk, (float)d.via.f64);

    case MSGPACK_OBJECT_FLOAT64:
        return msgpack_pack_double(pk, d.via.f64);

    case MSGPACK_OBJECT_STR:
        {
            int ret = msgpack_pack_str(pk, d.via.str.size);
            if(ret < 0) { return ret; }
            return msgpack_pack_str_body(pk, d.via.str.ptr, d.via.str.size);
        }

    case MSGPACK_OBJECT_BIN:
        {
            int ret = msgpack_pack_bin(pk, d.via.bin.size);
            if(ret < 0) { return ret; }
            return msgpack_pack_bin_body(pk, d.via.bin.ptr, d.via.bin.size);
        }

    case MSGPACK_OBJECT_EXT:
        {
            int ret = msgpack_pack_ext(pk, d.via.ext.size, d.via.ext.type);
            if(ret < 0) { return ret; }
            return msgpack_pack_ext_body(pk, d.via.ext.ptr, d.via.ext.size);
        }

    case MSGPACK_OBJECT_ARRAY:
        {
            int ret = msgpack_pack_array(pk, d.via.array.size);
            if(ret < 0) {
                return ret;
            }
            else {
                msgpack_object* o = d.via.array.ptr;
                msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
                for(; o != oend; ++o) {
                    ret = msgpack_pack_object(pk, *o);
                    if(ret < 0) { return ret; }
                }

                return 0;
            }
        }

    case MSGPACK_OBJECT_MAP:
        {
            int ret = msgpack_pack_map(pk, d.via.map.size);
            if(ret < 0) {
                return ret;
            }
            else {
                msgpack_object_kv* kv = d.via.map.ptr;
                msgpack_object_kv* const kvend = d.via.map.ptr + d.via.map.size;
                for(; kv != kvend; ++kv) {
                    ret = msgpack_pack_object(pk, kv->key);
                    if(ret < 0) { return ret; }
                    ret = msgpack_pack_object(pk, kv->val);
                    if(ret < 0) { return ret; }
                }

                return 0;
            }
        }

    default:
        return -1;
    }
}
예제 #22
0
/* Receive a tokenized JSON message and convert it to MsgPack */
static char *tokens_to_msgpack(char *js,
                               jsmntok_t *tokens, int arr_size, int *out_size)
{
    int i;
    int flen;
    char *p;
    char *buf;
    jsmntok_t *t;
    msgpack_packer pck;
    msgpack_sbuffer sbuf;

    /* initialize buffers */
    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);

    for (i = 0; i < arr_size ; i++) {
        t = &tokens[i];
        if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)) {
            break;
        }
        flen = (t->end - t->start);

        switch (t->type) {
        case JSMN_OBJECT:
            msgpack_pack_map(&pck, t->size);
            break;
        case JSMN_ARRAY:
            msgpack_pack_array(&pck, t->size);
            break;
        case JSMN_STRING:
            msgpack_pack_bin(&pck, flen);
            msgpack_pack_bin_body(&pck, js + t->start, flen);
            break;
        case JSMN_PRIMITIVE:
            p = js + t->start;
            if (*p == 'f') {
                msgpack_pack_false(&pck);
            }
            else if (*p == 't') {
                msgpack_pack_true(&pck);
            }
            else if (*p == 'n') {
                msgpack_pack_nil(&pck);
            }
            else {
                if (is_float(p, flen)) {
                    msgpack_pack_double(&pck, atof(p));
                }
                else {
                    msgpack_pack_int64(&pck, atol(p));
                }
            }
            break;
        case JSMN_UNDEFINED:
            msgpack_sbuffer_destroy(&sbuf);
            return NULL;
        }
    }

    /* dump data back to a new buffer */
    *out_size = sbuf.size;
    buf = malloc(sbuf.size);
    memcpy(buf, sbuf.data, sbuf.size);
    msgpack_sbuffer_destroy(&sbuf);

    return buf;
}
예제 #23
0
METHOD(array, Binary_encode, NodeInfo)
{
    CHECK_SIZE(args, 3);

    CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_POSITIVE_INTEGER);
    uint64_t protocol = args.ptr[0].via.u64;

    CHECK_TYPE(args.ptr[1], MSGPACK_OBJECT_ARRAY);
    msgpack_object_array address = args.ptr[1].via.array;

    CHECK_SIZE(address, 2);
    CHECK_TYPE(address.ptr[0], MSGPACK_OBJECT_ARRAY);
    msgpack_object_array host_address = address.ptr[0].via.array;

    CHECK_TYPE(address.ptr[1], MSGPACK_OBJECT_POSITIVE_INTEGER);
    uint64_t port_number = address.ptr[1].via.u64;

    CHECK_SIZE(host_address, 2);
    CHECK_TYPE(host_address.ptr[0], MSGPACK_OBJECT_POSITIVE_INTEGER);
    uint64_t address_family = host_address.ptr[0].via.u64;

    CHECK_TYPE(args.ptr[2], MSGPACK_OBJECT_BIN);
    msgpack_object_bin public_key = args.ptr[2].via.bin;

    CHECK_SIZE(public_key, crypto_box_PUBLICKEYBYTES);

    IP_Port ipp;
    ipp.port = htons(port_number);

    switch (address_family) {
        case 0: {
            /* IPv4*/
            if (protocol == 1) {
                ipp.ip.family = TCP_INET;
            } else {
                ipp.ip.family = AF_INET;
            }

            CHECK_TYPE(host_address.ptr[1], MSGPACK_OBJECT_POSITIVE_INTEGER);
            uint64_t addr = host_address.ptr[1].via.u64;

            ipp.ip.ip4.uint32 = htonl(addr);
            break;
        }

        case 1: {
            /* IPv6 */
            if (protocol == 1) {
                ipp.ip.family = TCP_INET6;
            } else {
                ipp.ip.family = AF_INET6;
            }

            CHECK_TYPE(host_address.ptr[1], MSGPACK_OBJECT_ARRAY);
            msgpack_object_array addr = host_address.ptr[1].via.array;

            int i;

            for (i = 0; i < 4; ++i) {
                CHECK_TYPE(addr.ptr[i], MSGPACK_OBJECT_POSITIVE_INTEGER);
                uint64_t component   = addr.ptr[i].via.u64;
                ipp.ip.ip6.uint32[i] = htonl(component);
            }

            break;
        }
    }

    Node_format node;
    node.ip_port = ipp;
    memcpy(&node.public_key, public_key.ptr, crypto_box_PUBLICKEYBYTES);

    /* We assume IP6 because it's bigger */
    uint8_t packed_node[PACKED_NODE_SIZE_IP6];

    int len = pack_nodes(packed_node, sizeof packed_node, &node, 1);

    if (len < 0) {
        return failure;
    }

    SUCCESS {
        msgpack_pack_bin(res, len);
        msgpack_pack_bin_body(res, packed_node, len);
    }
    return 0;
}