Esempio n. 1
0
int main(void)
{
    msgpack_sbuffer sbuf;
    msgpack_packer pk;
    msgpack_zone mempool;
    msgpack_object deserialized;

    /* msgpack::sbuffer is a simple buffer implementation. */
    msgpack_sbuffer_init(&sbuf);

    /* serialize values into the buffer using msgpack_sbuffer_write callback function. */
    msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

    msgpack_pack_array(&pk, 3);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_true(&pk);
    msgpack_pack_str(&pk, 7);
    msgpack_pack_str_body(&pk, "example", 7);

    /* deserialize the buffer into msgpack_object instance. */
    /* deserialized object is valid during the msgpack_zone instance alive. */
    msgpack_zone_init(&mempool, 2048);

    msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

    /* print the deserialized object. */
    msgpack_object_print(stdout, deserialized);
    puts("");

    msgpack_zone_destroy(&mempool);
    msgpack_sbuffer_destroy(&sbuf);

    return 0;
}
Esempio n. 2
0
void msgpack_pack_string(msgpack_packer *pk, const char *str)
{
	size_t len = strlen(str);

	msgpack_pack_str(pk, len);
	msgpack_pack_str_body(pk, str, len);
}
void prepare(msgpack_sbuffer* sbuf) {
    msgpack_packer pk;

    msgpack_packer_init(&pk, sbuf, msgpack_sbuffer_write);
    /* 1st object */
    msgpack_pack_array(&pk, 3);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_true(&pk);
    msgpack_pack_str(&pk, 7);
    msgpack_pack_str_body(&pk, "example", 7);
    /* 2nd object */
    msgpack_pack_str(&pk, 6);
    msgpack_pack_str_body(&pk, "second", 6);
    /* 3rd object */
    msgpack_pack_array(&pk, 2);
    msgpack_pack_int(&pk, 42);
    msgpack_pack_false(&pk);
}
Esempio n. 4
0
/* pack the mail content */
void prepare_sbuf(msgpack_sbuffer* sbuf) {
	msgpack_packer pk;
	
	msgpack_packer_init(&pk, sbuf, msgpack_sbuffer_write);
	
	/* mail content */
	/* 4mail5Alice10helloworld */
	msgpack_pack_array(&pk, 3);

	msgpack_pack_str(&pk, 4)
	msgpack_pack_str_body(&pk, "mail", 4);
	
	msgpack_pack_str(&pk, 5)
	msgpack_pack_str_body(&pk, "Alice", 5);

	msgpack_pack_str(&pk, 10)
	msgpack_pack_str_body(&pk, "helloworld", 10);

}
Esempio n. 5
0
void receiver_init(receiver *r) {
    msgpack_packer pk;

    msgpack_sbuffer_init(&r->sbuf);
    msgpack_packer_init(&pk, &r->sbuf, msgpack_sbuffer_write);
    /* 1st object */
    msgpack_pack_array(&pk, 3);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_true(&pk);
    msgpack_pack_str(&pk, 7);
    msgpack_pack_str_body(&pk, "example", 7);
    /* 2nd object */
    msgpack_pack_str(&pk, 6);
    msgpack_pack_str_body(&pk, "second", 6);
    /* 3rd object */
    msgpack_pack_array(&pk, 2);
    msgpack_pack_int(&pk, 42);
    msgpack_pack_false(&pk);
    r->rest = r->sbuf.size;
}
Esempio n. 6
0
static void pack_string(msgpack_packer *packer, const char *string) {
  if(string == NULL) {
    msgpack_pack_nil(packer);
  } else {
    int length = (int)strlen(string);
#if defined(MSGPACK_VERSION_MAJOR) && MSGPACK_VERSION_MAJOR < 1
		msgpack_pack_raw(packer, length);
		msgpack_pack_raw_body(packer, string, length);
#else
		msgpack_pack_str(packer, length);
		msgpack_pack_str_body(packer, string, length);
#endif
  }
}
void ANetworkController::ReceivedGetGitHash() {
	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetGitHash);

	msgpack_pack_str(&pk, GIT_HASH_LEN);
	msgpack_pack_str_body(&pk, GIT_HASH, GIT_HASH_LEN);

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}
Esempio n. 8
0
void init_unpack_array(msgpack_object_type type)
{
  size_t off = 0;

  msgpack_sbuffer sbuf;
  msgpack_sbuffer_init(&sbuf);
  msgpack_packer pk;
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

  msgpack_pack_array(&pk, 3);
  if (type == MSGPACK_OBJECT_NIL) {
    msgpack_pack_nil(&pk);
  } else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
    msgpack_pack_int(&pk, 1);
  } else if (type == MSGPACK_OBJECT_BOOLEAN) {
    msgpack_pack_true(&pk);
  } else if (type == MSGPACK_OBJECT_MAP) {
    msgpack_pack_map(&pk, 1);
  } else if (type == MSGPACK_OBJECT_ARRAY) {
    msgpack_pack_array(&pk, 3);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_int(&pk, 1);
    msgpack_pack_int(&pk, 1);
  } else if (type == MSGPACK_OBJECT_FLOAT) {
    msgpack_pack_double(&pk, 1.2);
  }
  msgpack_pack_true(&pk);
  msgpack_pack_str(&pk, 7);
  msgpack_pack_str_body(&pk, "example", 7);

  msgpack_unpacked result;

  msgpack_unpacked_init(&result);
  msgpack_unpack_next(&result, sbuf.data, sbuf.size, &off);
  msgpack_sbuffer_destroy(&sbuf);
  deserialized = result.data;
}
Esempio n. 9
0
int msgpack_pack_string(msgpack_packer *pk, char const *str)
{
    size_t len = strlen(str);
    msgpack_pack_str(pk, len);
    return msgpack_pack_str_body(pk, str, len);
}
Esempio n. 10
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);
}
Esempio n. 11
0
/*
把字符串str通过msgpack打包。

*/
void msgpack_pack_string(msgpack_packer* pk, const char *str) {
	msgpack_pack_str(pk, strlen(str));
	msgpack_pack_str_body(pk, str, strlen(str));
}
void ANetworkController::SendData() {
	if (!TcpSocket) {
		return;
	}

	// Clear the data if there is anything on the stream
	TryReceiveData();

	AVehiclestats* Stats = NULL;
	for (TActorIterator<AVehiclestats> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		Stats = *ObjIt;
		break;
	}
	if (Stats == NULL) {
		return;
	}

	// Send crashlog

	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);


	for (auto Iter = Stats->IncidentLog.CreateConstIterator(); Iter; ++Iter) {
		msgpack_pack_array(&pk, 2);
		msgpack_pack_uint16(&pk, EPacketType::PT_IncidentLog);
		Pack(&pk, *Iter);
	}

	Stats->IncidentLog.Empty();


	// Send the statestics

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetStats);

	msgpack_pack_map(&pk, 6);

	msgpack_pack_str(&pk, 7);
	msgpack_pack_str_body(&pk, "spawned", 7);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesSpawned());

	msgpack_pack_str(&pk, 6);
	msgpack_pack_str_body(&pk, "onroad", 6);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesOnTheRoad());

	msgpack_pack_str(&pk, 7);
	msgpack_pack_str_body(&pk, "crashed", 7);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesCrashed());

	msgpack_pack_str(&pk, 9);
	msgpack_pack_str_body(&pk, "incidents", 9);
	msgpack_pack_uint32(&pk, Stats->GetNumVehiclesIncidents());

	TArray<float> Throughputs;

	for (TActorIterator<AMeasurementGate> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		AMeasurementGate* Gate = *ObjIt;
		Throughputs.Add(Gate->GetThroughput(0));
		Throughputs.Add(Gate->GetThroughput(1));
	}

	msgpack_pack_str(&pk, 11);
	msgpack_pack_str_body(&pk, "throughputs", 11);
	msgpack_pack_array(&pk, Throughputs.Num());
	for (auto Iter = Throughputs.CreateConstIterator(); Iter; ++Iter) {
		msgpack_pack_float(&pk, *Iter);
	}

	msgpack_pack_str(&pk, 4);
	msgpack_pack_str_body(&pk, "time", 4);
	msgpack_pack_float(&pk, Stats->GetTime());

	int32 BytesLeft = sbuf.size;
	while (BytesLeft > 0) {
		int32 Index = sbuf.size - BytesLeft;
		int32 BytesSent;
		TcpSocket->Send((uint8*)sbuf.data + Index, BytesLeft, BytesSent);
		BytesLeft = BytesLeft - BytesSent;
		if (BytesSent == -1) {
			break;
		}
	}

	msgpack_sbuffer_destroy(&sbuf);
}
Esempio n. 13
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;
}
Esempio n. 14
0
void write_raw_msgpack(FILE *out, conjugrad_float_t *x, int ncol, void *meta) {
	int nsingle = ncol * (N_ALPHA - 1);
	int nsingle_padded = nsingle + N_ALPHA_PAD - (nsingle % N_ALPHA_PAD);

	conjugrad_float_t *x1 = x;
	conjugrad_float_t *x2 = &x[nsingle_padded];
	(void)x2;

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

#ifdef JANSSON
	if(meta != NULL) {
		msgpack_pack_map(pk, 5);
		meta_write_msgpack(pk, (json_t *)meta);
	} else {
		msgpack_pack_map(pk, 4);
	}
#else
	msgpack_pack_map(pk, 4);
#endif

	msgpack_pack_str(pk, 6);
	msgpack_pack_str_body(pk, "format", 6);
	msgpack_pack_str(pk, 5);
	msgpack_pack_str_body(pk, "ccm-1", 5);

	msgpack_pack_str(pk, 4);
	msgpack_pack_str_body(pk, "ncol", 4);
	msgpack_pack_int32(pk, ncol);

	msgpack_pack_str(pk, 8);
	msgpack_pack_str_body(pk, "x_single", 8);
	msgpack_pack_array(pk, ncol * (N_ALPHA - 1));
	for(int i = 0; i < ncol; i++) {
		for(int a = 0; a < N_ALPHA - 1; a++) {
			#if CONJUGRAD_FLOAT == 32
				msgpack_pack_float(pk, V(i, a));
			#elif CONJUGRAD_FLOAT == 64
				msgpack_pack_double(pk, V(i, a));
			#endif
		}
	}

	msgpack_pack_str(pk, 6);
	msgpack_pack_str_body(pk, "x_pair", 6);

	int nedge = ncol * (ncol - 1) / 2;
	msgpack_pack_map(pk, nedge);

	char sbuf[8192];
	for(int i = 0; i < ncol; i++) {
		for(int j = i + 1; j < ncol; j++) {

			int nchar = snprintf(sbuf, 8192, "%d/%d", i, j);

			msgpack_pack_str(pk, nchar);
			msgpack_pack_str_body(pk, sbuf, nchar);

			msgpack_pack_map(pk, 3);
			
			msgpack_pack_str(pk, 1);
			msgpack_pack_str_body(pk, "i", 1);
			msgpack_pack_int32(pk, i);

			msgpack_pack_str(pk, 1);
			msgpack_pack_str_body(pk, "j", 1);
			msgpack_pack_int32(pk, j);

			msgpack_pack_str(pk, 1);
			msgpack_pack_str_body(pk, "x", 1);

			msgpack_pack_array(pk, N_ALPHA * N_ALPHA);
			for(int a = 0; a < N_ALPHA; a++) {
				for(int b = 0; b < N_ALPHA; b++) {
					#if CONJUGRAD_FLOAT == 32
						msgpack_pack_float(pk, W(b, j, a, i));
					#elif CONJUGRAD_FLOAT == 64
						msgpack_pack_double(pk, W(b, j, a, i));
					#endif
				}
			}
		}
	}


	fwrite(buffer->data, buffer->size, 1, out);

	msgpack_sbuffer_free(buffer);
	msgpack_packer_free(pk);
}
Esempio n. 15
0
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;
    }
}
Esempio n. 16
0
void cb_forward_flush(void *data, size_t bytes,
                      char *tag, int tag_len,
                      struct flb_input_instance *i_ins, void *out_context,
                      struct flb_config *config)
{
    int ret = -1;
    int entries = 0;
    size_t off = 0;
    size_t total;
    size_t bytes_sent;
    msgpack_packer   mp_pck;
    msgpack_sbuffer  mp_sbuf;
    msgpack_unpacked result;
    struct flb_out_forward_config *ctx = out_context;
    struct flb_upstream_conn *u_conn;
    (void) i_ins;
    (void) config;

    flb_debug("[out_forward] request %lu bytes to flush", bytes);

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

    /* Count number of entries, is there a better way to do this ? */
    msgpack_unpacked_init(&result);
    while (msgpack_unpack_next(&result, data, bytes, &off)) {
        entries++;
    }
    flb_debug("[out_fw] %i entries tag='%s' tag_len=%i",
              entries, tag, tag_len);
    msgpack_unpacked_destroy(&result);

    /* Output: root array */
    msgpack_pack_array(&mp_pck, 2);
    msgpack_pack_str(&mp_pck, tag_len);
    msgpack_pack_str_body(&mp_pck, tag, tag_len);
    msgpack_pack_array(&mp_pck, entries);

    /* Get a TCP connection instance */
    u_conn = flb_upstream_conn_get(ctx->u);
    if (!u_conn) {
        flb_error("[out_forward] no upstream connections available");
        msgpack_sbuffer_destroy(&mp_sbuf);
        FLB_OUTPUT_RETURN(FLB_RETRY);
    }

    /* Write message header */
    ret = flb_io_net_write(u_conn, mp_sbuf.data, mp_sbuf.size, &bytes_sent);
    if (ret == -1) {
        flb_error("[out_forward] could not write chunk header");
        msgpack_sbuffer_destroy(&mp_sbuf);
        flb_upstream_conn_release(u_conn);
        FLB_OUTPUT_RETURN(FLB_RETRY);
    }

    msgpack_sbuffer_destroy(&mp_sbuf);
    total = ret;

    /* Write body */
    ret = flb_io_net_write(u_conn, data, bytes, &bytes_sent);
    if (ret == -1) {
        flb_error("[out_forward] error writing content body");
        flb_upstream_conn_release(u_conn);
        FLB_OUTPUT_RETURN(FLB_RETRY);
    }

    total += bytes_sent;
    flb_upstream_conn_release(u_conn);
    flb_trace("[out_forward] ended write()=%d bytes", total);

    FLB_OUTPUT_RETURN(FLB_OK);
}
void ANetworkController::ReceivedGetBehaviors() {
	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetBehaviors);

	TArray<ABehaviours*> BehaviorsList;
	for (TActorIterator<ABehaviours> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		ABehaviours* Behaviors = *ObjIt;
		BehaviorsList.Add(Behaviors);
	}

	int32 BehaviorsListNum = BehaviorsList.Num();
	msgpack_pack_array(&pk, BehaviorsListNum);
	for (int i = 0; i < BehaviorsListNum; i++) {
		ABehaviours* Behaviors = BehaviorsList[i];
		msgpack_pack_map(&pk, 4);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "FlockingState", 13);
		msgpack_pack_uint8(&pk, (uint8)Behaviors->FlockingState);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "ThrottleGains", 13);
		Pack(&pk, Behaviors->ThrottleGains);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "SteeringGains", 13);
		Pack(&pk, Behaviors->SteeringGains);

		int32 BehaviorsNum = Behaviors->Behaviours.Num();
		msgpack_pack_str(&pk, 9);
		msgpack_pack_str_body(&pk, "Behaviors", 9);
		msgpack_pack_array(&pk, BehaviorsNum);
		for (int j = 0; j < BehaviorsNum; j++) {
			UBehaviour* Behavior = Behaviors->Behaviours[j];

			TArray<UProperty*> Properties;
			for (TFieldIterator<UProperty> PropIt(Behavior->GetClass()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				if (Cast<UNumericProperty>(Property) || Cast<UBoolProperty>(Property)) {
					Properties.Add(Property);
				}
			}

			msgpack_pack_map(&pk, Properties.Num() + 1);
			msgpack_pack_str(&pk, 4);
			msgpack_pack_str_body(&pk, "Name", 4);
			FString Name = Behavior->GetClass()->GetName();
			msgpack_pack_str(&pk, Name.Len());
			msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
			for (auto PropIt(Properties.CreateIterator()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				const void* Value = Property->ContainerPtrToValuePtr<uint8>(Behavior);
				FString Name = Property->GetName();
				msgpack_pack_str(&pk, Name.Len());
				msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
				if (UNumericProperty *NumericProperty = Cast<UNumericProperty>(Property)) {
					if (NumericProperty->IsFloatingPoint()) {
						msgpack_pack_double(&pk, NumericProperty->GetFloatingPointPropertyValue(Value));
					}
					else if (NumericProperty->IsInteger()) {
						msgpack_pack_int(&pk, NumericProperty->GetSignedIntPropertyValue(Value));
					}
				}
				else if (UBoolProperty *BoolProperty = Cast<UBoolProperty>(Property)) {
					if (BoolProperty->GetPropertyValue(Value)) {
						msgpack_pack_true(&pk);
					}
					else {
						msgpack_pack_false(&pk);
					}
				}
			}
		}
	}

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}