diff --git a/Platform/user/configm/config-server/localportal_config/localportal.c b/Platform/user/configm/config-server/localportal_config/localportal.c index abb08d055..aab5c40da 100644 --- a/Platform/user/configm/config-server/localportal_config/localportal.c +++ b/Platform/user/configm/config-server/localportal_config/localportal.c @@ -31,9 +31,9 @@ int _valid_ipv4_port(const char *str, int port) int i; volatile int local_errno; struct sockaddr_in addr; - fd = socket(AF_INET,SOCK_STREAM,0); //初始化socket + fd = socket(AF_INET,SOCK_STREAM,0); /*初始化*/ - if(fd ==-1) //检查是否正常初始化socket + if(fd ==-1) /*检查是否正常初始化socket */ { return -1; } @@ -47,8 +47,8 @@ int _valid_ipv4_port(const char *str, int port) { fprintf(stderr, "\"%s\" is a vaild IPv4 address\n", str); - addr.sin_family = AF_INET; //地址结构的协议簇 - addr.sin_port=htons(port); //地址结构的端口地址,网络字节序 + addr.sin_family = AF_INET; /*地址结构的协议簇 */ + addr.sin_port=htons(port); /*地址结构的端口地址,网络字节序 */ printf("the value of str:%s\n", str); i = (bind(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr))); printf("the value of i:%d\n", i); @@ -79,14 +79,32 @@ int _valid_ipv4_port(const char *str, int port) /*判断配置本地Portal服务器的IP地址是否有效,端口号是否被占用 */ +/*input格式 "{\"type\":0, \"ip\":1027824, \"port\":1010}"*/ ret_code portalserver_config_chk(uint source, uint *config_type, pointer input, int *input_len, pointer output, int *output_len) { ret_code ret = RET_OK; - localportal_configure_t *struct_portal; - struct_portal = (localportal_configure_t *)input; + cJSON *cjson, *res; + /*JSON字符串到JSON格式 */ + cjson = cJSON_Parse(input); + if(!cjson) + { + ret = RET_INPUTERR; + ASSERT_RET(ret); + return ret; + } + + /*创建freeauth_configure_t结构体对象 */ + s2j_create_struct_obj(struct_portal, localportal_configure_t); + + /*反序列化数据到freeauth_configure_t结构体对象 */ + s2j_struct_get_basic_element(struct_portal, cjson, int, ip); + s2j_struct_get_basic_element(struct_portal, cjson, int, port); + rpc_log_info("localport configure: ip: %d port: %d\n", + struct_portal->ip,struct_portal->port); + if(*input_len < sizeof(localportal_configure_t) ) { ret = RET_INPUTERR; @@ -112,23 +130,57 @@ ret_code portalserver_config_proc(uint source, uint config_type, pointer output, int *output_len) { ret_code ret = RET_OK; - int code; - localportal_configure_t *struct_portal; - struct_portal = (localportal_configure_t *)input; + cJSON *cjson, *res; + char * ret_char = NULL; + int * ret_int = NULL; + + /*JSON字符串到JSON格式 */ + cjson = cJSON_Parse(input); + if(!cjson) + { + ret = RET_INPUTERR; + ASSERT_RET(ret); + return ret; + } + + /*创建freeauth_configure_t结构体对象 */ + s2j_create_struct_obj(struct_portal, localportal_configure_t); + + /*反序列化数据到freeauth_configure_t结构体对象 */ + s2j_struct_get_basic_element(struct_portal, cjson, int, ip); + s2j_struct_get_basic_element(struct_portal, cjson, int, port); char str[32]; inet_ntop(AF_INET, (void *)&struct_portal->ip, str, 32); char *ip_addr = str; rpc_log_info("portalserver configure: ip: %s port: %d\n", - ip_addr, struct_portal->port); + ip_addr, struct_portal->port); - /*将配置信息发送到web server */ + /*将配置信息发送到web server,发送结果int类型表示,0表示发送成功,-1表示发送失败 */ + int r; + + /*创建json对象 */ + res = cJSON_CreateObject(); + if(!res) + { + ret = RET_ERR; + ASSERT_RET(ret); + return ret; + } + + cJSON_AddNumberToObject(res, "result", r); + + /*将json对象转换成json字符串 */ + ret_char = cJSON_PrintUnformatted(res); + ret_int = (int*)ret_char; + memcpy(output, ret_int, sizeof(ret_int)+1); + + free(ret_char); + cJSON_Delete(res); /*把本地Portal server的配置信息存入全局变量 */ localportal = struct_portal; - return RET_OK; } -