forked from hy0kl/event-json-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.c
132 lines (103 loc) · 3.37 KB
/
handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @describe:
* @author: Jerry Yang(hy0kle@gmail.com)
* */
#include "server.h"
#include "handler.h"
static const char *
get_error_message(error_code_e code)
{
switch (code)
{
case E_SUCCESS:
return "OK";
case SERVICE_IS_UNAVAILABLE:
return "后端服务不可用";
case E_CMD_UNDEFINED:
return "操作命令不存在";
}
return "未知错误";
}
static void
build_code_msg(cJSON *json, error_code_e code)
{
cJSON_AddItemToObject(json, RES_CODE, cJSON_CreateNumber(code));
cJSON_AddItemToObject(json, RES_MSG, cJSON_CreateString(get_error_message(code)));
return;
}
static void
rebuild_code_msg(cJSON *json, error_code_e code)
{
cJSON_ReplaceItemInObject(json, RES_CODE, cJSON_CreateNumber(code));
cJSON_ReplaceItemInObject(json, RES_MSG, cJSON_CreateString(get_error_message(code)));
return;
}
static void
rpc_cmd_echo(struct bufferq *bufferq)
{
cJSON *json = bufferq->response.json;
cJSON *data = cJSON_CreateObject();
cJSON_AddItemToObject(json, RES_DATA, data);
cJSON_AddItemToObject(data, "echo_data", cJSON_CreateString(bufferq->request.buf));
return;
}
static void
rpc_cmd_test(struct bufferq *bufferq)
{
cJSON *json = bufferq->response.json;
cJSON *data = cJSON_CreateObject();
char test[1024];
test[0] = '\0';
char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
time_t timep;
struct tm *tp;
struct timeval tv;
time(&timep); /* 获取 time_t 类型当前时间 */
//tp = gmtime(&timep); /* 转换为 struct tm 结构的UTC时间 */
/** 谷歌后得知: localtime() 返回的是静态指针,不需要 free() */
tp = localtime(&timep); /* 转换为 struct tm 结构的当地时间 */
gettimeofday(&tv, NULL);
snprintf(test, 1024, "Just for test. [timestamp: %ld] [%d/%02d/%02d %s %02d:%02d:%02d]",
GETSTIME(tv),
1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday,
wday[tp->tm_wday],
tp->tm_hour, tp->tm_min, tp->tm_sec);
cJSON_AddItemToObject(json, RES_DATA, data);
cJSON_AddItemToObject(data, "test", cJSON_CreateString(test));
cJSON_AddItemToObject(data, "timestamp", cJSON_CreateNumber(GETSTIME(tv)));
cJSON_AddItemToObject(data, "millisecond", cJSON_CreateNumber(GETMTIME(tv)));
cJSON_AddItemToObject(data, "microsecond", cJSON_CreateNumber(GETUTIME(tv)));
return;
}
static void
rpc_cmd_undefined(struct bufferq *bufferq)
{
cJSON *json = bufferq->response.json;
cJSON *data = cJSON_CreateObject();
rebuild_code_msg(json, E_CMD_UNDEFINED);
cJSON_AddItemToObject(json, RES_DATA, data);
return;
}
void
rpc_handler(struct bufferq *bufferq)
{
int cmd = cJSON_GetObjectItem(bufferq->request.json, "cmd")->valueint;
zlog_debug(g_zc, "[cmd: %d]", cmd);
/** 初始化响应体基本结构 {"code": 0, "message": "OK"} */
build_code_msg(bufferq->response.json, E_SUCCESS);
switch (cmd)
{
case CMD_ECHO:
rpc_cmd_echo(bufferq);
break;
case CMD_TEST:
rpc_cmd_test(bufferq);
break;
default:
rpc_cmd_undefined(bufferq);
}
/** 构造响应体的 json 包 */
bufferq->response.buf = cJSON_PrintUnformatted(bufferq->response.json);
bufferq->response.body_len = strlen(bufferq->response.buf);
return;
}