secgateway/Platform/user/configm/config-api/configclient.c

118 lines
2.9 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <stdio.h>
#include "rpc.h"
#include "configmapi.h"
rpc_client *config_client = NULL;
rpc_client *config_client_get()
{
if(config_client == NULL)
{
config_client = rpc_client_connect_ex("ConfigManger#0");
if(config_client == NULL)
{
rpc_log_error("connect ConfigManger#0 error\n");
return NULL;
}
}
return config_client;
}
ret_code config_construct_msg(uint config_type, uint64 config_id,
char* config_data, int config_len,
config_msg_t **config_msg, int *msg_len)
{
config_msg_t *pconfig_msg;
if(config_data == NULL)
{
return RET_NULLP;
}
*msg_len = sizeof(config_msg_t) + config_len;
pconfig_msg= (config_msg_t *) malloc(*msg_len);
if(pconfig_msg == NULL)
{
return RET_NOMEM;
}
pconfig_msg->source = CONFIG_FROM_WEB;
pconfig_msg->config_type = config_type;
pconfig_msg->config_id = config_id;
memcpy(pconfig_msg->config_buff, config_data, config_len);
*config_msg = pconfig_msg;
return RET_OK;
}
ret_code config_destroy_msg(config_msg_t *config_msg, int msg_len)
{
if(config_msg)
{
memset(config_msg, 0, msg_len);
free(config_msg);
}
}
ret_code web_config_exec_sync(uint config_type, uint64 config_id,
char* config_data, int config_len,
char**output, int *output_len)
{
rpc_client * client = config_client_get();
ret_code code = RET_OK ;
config_msg_t *config_msg;
int msg_len = 0;
if(client == NULL)
{
return RET_ERR;
}
code = config_construct_msg(config_type, config_id, config_data,
config_len, &config_msg, &msg_len);
ASSERT_RET(code);
code = rpc_client_call(client, "ConfigManger#0", "cm_config_process",
config_msg, msg_len, (pointer)output, output_len);
ASSERT_RET_NO(code);
config_destroy_msg(config_msg, msg_len);
return code;
}
ret_code web_config_exec_async(uint config_type, uint64 config_id,
char* config_data, int config_len,
rpc_callback callback, pointer data)
{
rpc_client * client = config_client_get();
config_msg_t *config_msg;
int msg_len = 0;
ret_code ret = RET_OK ;
if(client == NULL)
{
return RET_ERR;
}
ret = config_construct_msg(config_type, config_id, config_data,
config_len, &config_msg, &msg_len);
ASSERT_RET(ret);
ret = rpc_client_call_async(client, "ConfigManger#0", "cm_config_process",
config_msg, msg_len, callback, data);
config_destroy_msg(config_msg, msg_len);
return ret;
}