375 lines
9.6 KiB
C
375 lines
9.6 KiB
C
|
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include "rpc.h"
|
||
|
#include "configm.h"
|
||
|
|
||
|
|
||
|
config_service_t g_config_service[] = CONFIG_SERVICE_ARRAY;
|
||
|
|
||
|
void test_big_data(rpc_conn *conn, pointer input, int input_len, void* data) {
|
||
|
char buf[input_len];
|
||
|
memcpy(buf, input, input_len);
|
||
|
buf[input_len] = '\0';
|
||
|
printf("get data is %s\n", buf);
|
||
|
//sleep(15);
|
||
|
rpc_return_null(conn);
|
||
|
}
|
||
|
|
||
|
|
||
|
config_service_t *cm_config_service_get(uint64 config_id)
|
||
|
{
|
||
|
int len = sizeof(g_config_service) / sizeof(config_service_t);
|
||
|
int config_idx;
|
||
|
|
||
|
for(config_idx = 0; config_idx < len; config_idx++)
|
||
|
{
|
||
|
if(config_id == g_config_service[config_idx].config_id)
|
||
|
{
|
||
|
return &(g_config_service[config_idx]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
ret_code cm_config_rec_pre(char **input, short **input_len,
|
||
|
char **output, int *output_len)
|
||
|
{
|
||
|
/*if((access(CONFIG_RECOVERY_DONE, F_OK))!=-1)
|
||
|
{
|
||
|
return RET_ERR;
|
||
|
} */
|
||
|
|
||
|
*input = rpc_new(char, CM_BUFF_SIZE);
|
||
|
*input_len = rpc_new(short, CM_BUFF_SIZE);
|
||
|
*output = rpc_new(char, CM_BUFF_SIZE);
|
||
|
*output_len = CM_BUFF_SIZE;
|
||
|
|
||
|
if(*input == NULL
|
||
|
|| *input_len == NULL
|
||
|
|| *output == NULL)
|
||
|
{
|
||
|
return RET_NOMEM;
|
||
|
}
|
||
|
|
||
|
memset(*input, 0, CM_BUFF_SIZE);
|
||
|
memset(*input_len, 0, CM_BUFF_SIZE * sizeof(short));
|
||
|
memset(*output, 0, CM_BUFF_SIZE);
|
||
|
|
||
|
rpc_log_info("==>config revocery start\n");
|
||
|
|
||
|
return RET_OK;
|
||
|
}
|
||
|
|
||
|
ret_code cm_config_rec_done(char *input, short *input_len,char *output)
|
||
|
{
|
||
|
int fd;
|
||
|
|
||
|
memset(input, 0, CM_BUFF_SIZE);
|
||
|
memset(input_len, 0, CM_BUFF_SIZE * sizeof(short));
|
||
|
memset(output, 0, CM_BUFF_SIZE);
|
||
|
|
||
|
free(input);
|
||
|
free(input_len);
|
||
|
free(output);
|
||
|
|
||
|
fd = open(CONFIG_RECOVERY_DONE, O_CREAT, 0777);
|
||
|
if(fd <= 0)
|
||
|
{
|
||
|
rpc_log_error("create recovery file error");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
close(fd);
|
||
|
}
|
||
|
|
||
|
rpc_log_info("==>config revocery done!\n");
|
||
|
|
||
|
return RET_OK;
|
||
|
}
|
||
|
|
||
|
ret_code cm_config_get_allconfig(uint source,
|
||
|
config_service_t *config_svr,
|
||
|
char *input_array,
|
||
|
short *len_array,
|
||
|
int *total_len)
|
||
|
{
|
||
|
ret_code ret = RET_OK;
|
||
|
|
||
|
memset(input_array, 0, CM_BUFF_SIZE);
|
||
|
memset(len_array, 0, CM_BUFF_SIZE*sizeof(short));
|
||
|
*total_len = 0;
|
||
|
|
||
|
ret = config_svr->getall_callback(source, config_svr->config_id,
|
||
|
input_array, len_array, total_len);
|
||
|
ASSERT_RET(ret);
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
ret_code cm_config_config_rec(uint source,
|
||
|
config_service_t *config_svr,
|
||
|
char *input_array, short *len_array,
|
||
|
int total_len, char *output, int *output_len)
|
||
|
{
|
||
|
uint config_type = CM_CONFIG_SET;
|
||
|
ret_code ret = RET_OK;
|
||
|
int len_tem = 0;
|
||
|
int idx = 0;
|
||
|
|
||
|
if(config_svr->multi_inst == TRUE)
|
||
|
{
|
||
|
config_type = CM_CONFIG_ADD;
|
||
|
}
|
||
|
|
||
|
while(len_tem < total_len)
|
||
|
{
|
||
|
config_svr->proc_callback(source, config_type,
|
||
|
input_array, len_array[idx],
|
||
|
output, output_len);
|
||
|
|
||
|
input_array = input_array + len_array[idx];
|
||
|
len_tem = len_tem + len_array[idx];
|
||
|
|
||
|
idx++;
|
||
|
}
|
||
|
|
||
|
return RET_OK;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* 配置恢复 */
|
||
|
void cm_config_recovery()
|
||
|
{
|
||
|
int len = sizeof(g_config_service) / sizeof(config_service_t);
|
||
|
config_service_t *config_svr;
|
||
|
char *input_array = NULL;
|
||
|
short *len_array = NULL;
|
||
|
char *output = NULL;
|
||
|
int output_len;
|
||
|
|
||
|
int total_len = 0;
|
||
|
ret_code ret = RET_OK;
|
||
|
int config_idx;
|
||
|
int recover_idx = 1,recover_phase;
|
||
|
|
||
|
ret = cm_config_rec_pre(&input_array, &len_array, &output, &output_len);
|
||
|
ASSERT_RET_VOID(ret);
|
||
|
|
||
|
while(recover_idx <= 2)
|
||
|
{
|
||
|
if(recover_idx == 1)
|
||
|
{
|
||
|
recover_phase = CONFIG_FROM_RECOVER1;
|
||
|
}
|
||
|
else if(recover_idx == 2)
|
||
|
{
|
||
|
recover_phase = CONFIG_FROM_RECOVER2;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
for(config_idx = 0; config_idx < len; config_idx++)
|
||
|
{
|
||
|
config_svr = &(g_config_service[config_idx]);
|
||
|
|
||
|
if(config_svr->recovery != TRUE)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
cm_config_get_allconfig(recover_phase,
|
||
|
config_svr, input_array,
|
||
|
len_array, &total_len);
|
||
|
|
||
|
cm_config_config_rec(recover_phase, config_svr,
|
||
|
input_array, len_array, total_len,
|
||
|
output, &output_len);
|
||
|
}
|
||
|
|
||
|
recover_idx++;
|
||
|
}
|
||
|
|
||
|
cm_config_rec_done(input_array, len_array, output);
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* 配置处理入口函数,所有的配置,均在此函数中处理 */
|
||
|
void cm_config_process(rpc_conn *conn, pointer input, int input_len, void* data)
|
||
|
{
|
||
|
config_service_t *config_svr;
|
||
|
config_msg_t *config_msg;
|
||
|
char *cm_get_buff = NULL;
|
||
|
ret_code ret = RET_OK;
|
||
|
int config_len = 0;
|
||
|
short* single_len = NULL;
|
||
|
int buff_len = CM_BUFF_SIZE;
|
||
|
int index = 0;
|
||
|
|
||
|
if(conn == NULL || input == NULL)
|
||
|
{
|
||
|
rpc_return_error(conn, RET_NULLP, "NULL pointer");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*parse message*/
|
||
|
if(input_len < sizeof(config_msg_t))
|
||
|
{
|
||
|
rpc_return_error(conn, RET_NOMEM, "not enough memery");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
config_msg = (config_msg_t *)input;
|
||
|
config_len = input_len - sizeof(config_msg_t);
|
||
|
|
||
|
rpc_log_info("recv config from %d, config type is %d config id is 0x%llx ,len is %x\r\n",
|
||
|
config_msg->source, config_msg->config_type, config_msg->config_id,
|
||
|
config_len);
|
||
|
|
||
|
config_svr = cm_config_service_get(config_msg->config_id);
|
||
|
if(config_svr == NULL)
|
||
|
{
|
||
|
rpc_return_error(conn, RET_NULLP, "NULL pointer");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/*source check*/
|
||
|
if(!(config_svr->config_id & config_msg->config_id))
|
||
|
{
|
||
|
rpc_return_error(conn, RET_CHKERR, "source check error!\r\n");
|
||
|
return;
|
||
|
|
||
|
}
|
||
|
|
||
|
cm_get_buff = rpc_new(char, buff_len);
|
||
|
if(cm_get_buff == NULL)
|
||
|
{
|
||
|
rpc_return_error(conn, RET_NOMEM, "not enough memery");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
memset(cm_get_buff, 0, buff_len);
|
||
|
|
||
|
/*config check*/
|
||
|
if(config_svr->chk_callback)
|
||
|
{
|
||
|
ret = config_svr->chk_callback(config_msg->source,
|
||
|
config_msg->config_type,
|
||
|
config_msg->config_buff,
|
||
|
config_len,
|
||
|
cm_get_buff,
|
||
|
&buff_len);
|
||
|
if(ret != RET_OK)
|
||
|
{
|
||
|
rpc_return_error(conn, ret, cm_get_buff);
|
||
|
goto exit;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*config exec*/
|
||
|
switch(config_msg->config_type)
|
||
|
{
|
||
|
case CM_CONFIG_GET:
|
||
|
if(config_svr->get_callback)
|
||
|
{
|
||
|
ret = config_svr->get_callback(config_msg->source,
|
||
|
config_msg->config_buff,
|
||
|
config_len,
|
||
|
cm_get_buff,
|
||
|
&buff_len);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case CM_CONFIG_GET_ALL:
|
||
|
if(config_svr->getall_callback)
|
||
|
{
|
||
|
single_len = rpc_new(short, CM_BUFF_SIZE);
|
||
|
if(single_len == NULL)
|
||
|
{
|
||
|
rpc_return_error(conn, RET_NOMEM, "not enough memery");
|
||
|
goto exit;
|
||
|
}
|
||
|
|
||
|
ret = config_svr->getall_callback(config_msg->source,
|
||
|
config_svr->config_id,
|
||
|
cm_get_buff,
|
||
|
single_len,
|
||
|
&buff_len);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
if(config_svr->proc_callback)
|
||
|
{
|
||
|
ret = config_svr->proc_callback(config_msg->source,
|
||
|
config_msg->config_type,
|
||
|
config_msg->config_buff,
|
||
|
config_len,
|
||
|
cm_get_buff,
|
||
|
&buff_len);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if(buff_len > CM_BUFF_SIZE)
|
||
|
{
|
||
|
ret = RET_NOMEM;
|
||
|
}
|
||
|
|
||
|
if(ret != RET_OK)
|
||
|
{
|
||
|
rpc_return_error(conn, ret, cm_get_buff);
|
||
|
goto exit;
|
||
|
}
|
||
|
|
||
|
rpc_return(conn, cm_get_buff, buff_len);
|
||
|
|
||
|
exit:
|
||
|
rpc_free(cm_get_buff);
|
||
|
|
||
|
if(single_len != NULL)
|
||
|
{
|
||
|
rpc_free(single_len);
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char **argv)
|
||
|
{
|
||
|
rpc_server *server;
|
||
|
|
||
|
if (server= rpc_server_create_ex("ConfigManger#0"))
|
||
|
{
|
||
|
rpc_log_info("start server\n");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
rpc_log_error("start server error\n");
|
||
|
return EXIT_FAILURE;
|
||
|
}
|
||
|
|
||
|
/* 配置恢复 */
|
||
|
cm_config_recovery();
|
||
|
|
||
|
/* 注册配置处理函数 */
|
||
|
rpc_server_regservice(server, "ConfigManger#0", "cm_config_process", cm_config_process);
|
||
|
rpc_server_regservice(server, "ConfigManger#0", "test_big_data", test_big_data);
|
||
|
|
||
|
for(;;)
|
||
|
{
|
||
|
rpc_sleep(1000);
|
||
|
}
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|