-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.c
More file actions
171 lines (132 loc) · 3.27 KB
/
server.c
File metadata and controls
171 lines (132 loc) · 3.27 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/******************************************************************************
*
* FILENAME:
* server.c
*
* DESCRIPTION:
* The example of server using Unix domain socket.
*
* REVISION(MM/DD/YYYY):
* 12/02/2013 Shengkui Leng (lengshengkui@outlook.com)
* - Initial version
*
******************************************************************************/
#include <unistd.h>
#include <signal.h>
#include "common.h"
volatile sig_atomic_t loop_flag = 1;
/*
* Return the version of server.
*/
uds_command_t *cmd_get_version(void)
{
uds_response_version_t *ver;
printf("CMD_GET_VERSION\n");
ver = (uds_response_version_t *)malloc(sizeof(uds_response_version_t));
if (ver != NULL) {
ver->common.status = STATUS_SUCCESS;
ver->common.data_len = 2;
ver->major = 1;
ver->minor = 0;
}
return (uds_command_t *)ver;
}
/*
* Get a message string from server
*/
uds_command_t *cmd_get_msg(void)
{
uds_response_get_msg_t *res;
const char *str = "This is a message from the server.";
printf("CMD_GET_MESSAGE\n");
res = (uds_response_get_msg_t *)malloc(sizeof(uds_response_get_msg_t));
if (res != NULL) {
res->common.status = STATUS_SUCCESS;
res->common.data_len = strlen(str);
snprintf(res->data, UDS_GET_MSG_SIZE, "%s", str);
res->data[UDS_GET_MSG_SIZE-1] = 0;
}
return (uds_command_t *)res;
}
/*
* Send a message string to server
*/
uds_command_t *cmd_put_msg(uds_command_t *req)
{
uds_command_t *res;
uds_request_put_msg_t *put_msg = (uds_request_put_msg_t *)req;
printf("CMD_PUT_MESSAGE\n");
printf("Message: %s\n", (char *)put_msg->data);
res = (uds_command_t *)malloc(sizeof(uds_command_t));
if (res != NULL) {
res->status = STATUS_SUCCESS;
res->data_len = 0;
}
return (uds_command_t *)res;
}
/*
* Unknown request type
*/
uds_command_t *cmd_unknown(uds_command_t *req)
{
uds_command_t *res;
printf("Unknown request type\n");
res = (uds_command_t *)malloc(sizeof(uds_command_t));
if (res != NULL) {
res->status = STATUS_INVALID_COMMAND;
res->data_len = 0;
}
return res;
}
/*
* The handler to handle all requests from client
*/
uds_command_t *my_request_handler(uds_command_t *req)
{
uds_command_t *resp = NULL;
switch (req->command) {
case CMD_GET_VERSION:
resp = cmd_get_version();
break;
case CMD_GET_MESSAGE:
resp = cmd_get_msg();
break;
case CMD_PUT_MESSAGE:
resp = cmd_put_msg(req);
break;
default:
resp = cmd_unknown(req);
break;
}
return resp;
}
/*
* When user press CTRL+C, quit the server process.
*/
void handler_sigint(int sig)
{
loop_flag = 0;
}
void install_sig_handler()
{
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = handler_sigint;
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
}
int main(void)
{
uds_server_t *s;
s = server_init(UDS_SOCK_PATH, &my_request_handler);
if (s == NULL) {
printf("server: init error\n");
return STATUS_INIT_ERROR;
}
install_sig_handler();
while (loop_flag) {
server_accept_request(s);
}
server_close(s);
return STATUS_SUCCESS;
}