|
#include <msgpack.h> |
|
#include <msgpack/fbuffer.h> |
|
#include <cstdlib> |
|
#include <cstring> |
|
#include <string> |
|
|
|
|
|
int main() |
|
{ |
|
const unsigned NUM_DATASTREAMS = 1; |
|
const char* STREAM_NAME = "component.port"; |
|
const char* META_STREAM_NAME = "component.port.meta"; |
|
const char* TYPENAME = "int"; |
|
const char* OUTPUT_FILE = "output.msg"; |
|
const unsigned STREAM_LENGTH = 2; // we have to know the stream length before we write the log... |
|
int data[2]; |
|
data[0] = 2; |
|
data[1] = 3; |
|
int timestamps[2]; |
|
timestamps[0] = 0; |
|
timestamps[1] = 1; |
|
|
|
FILE* fp = fopen(OUTPUT_FILE, "w"); |
|
|
|
msgpack_packer packer; |
|
msgpack_packer_init(&packer, fp, msgpack_fbuffer_write); |
|
|
|
msgpack_pack_map(&packer, 2 * NUM_DATASTREAMS); |
|
|
|
// Data stream (key) |
|
msgpack_pack_str(&packer, strlen(STREAM_NAME)); |
|
msgpack_pack_str_body(&packer, STREAM_NAME, strlen(STREAM_NAME)); |
|
|
|
// Data stream (content) |
|
msgpack_pack_array(&packer, STREAM_LENGTH); |
|
// Write whatever you want here |
|
for(unsigned i = 0; i < STREAM_LENGTH; i++) |
|
msgpack_pack_int64(&packer, data[i]); |
|
|
|
|
|
// Encode complex objects in maps, e.g., |
|
|
|
// msgpack_pack_map(&packer, NUM_FIELDS); |
|
|
|
// msgpack_pack_str(&packer, ...); |
|
// msgpack_pack_str_body(&packer, FIELD_NAME, ...); |
|
|
|
// msgpack_pack_...(&packer, ...); // field content |
|
|
|
|
|
// Meta data (key) |
|
msgpack_pack_str(&packer, strlen(META_STREAM_NAME)); |
|
msgpack_pack_str_body(&packer, META_STREAM_NAME, strlen(META_STREAM_NAME)); |
|
|
|
// Meta data (content) |
|
msgpack_pack_map(&packer, 2); |
|
// Timestamps |
|
const std::string TIME_KEY = "timestamps"; |
|
msgpack_pack_str(&packer, TIME_KEY.size()); |
|
msgpack_pack_str_body(&packer, TIME_KEY.c_str(), TIME_KEY.size()); |
|
msgpack_pack_array(&packer, STREAM_LENGTH); |
|
for(unsigned i = 0; i < STREAM_LENGTH; i++) |
|
msgpack_pack_int64(&packer, timestamps[i]); |
|
// Type name |
|
const std::string TYPE_KEY = "type"; |
|
msgpack_pack_str(&packer, TYPE_KEY.size()); |
|
msgpack_pack_str_body(&packer, TYPE_KEY.c_str(), TYPE_KEY.size()); |
|
msgpack_pack_str(&packer, strlen(TYPENAME)); |
|
msgpack_pack_str_body(&packer, TYPENAME, strlen(TYPENAME)); |
|
|
|
fclose(fp); |
|
|
|
return 0; |
|
} |