아래 코드 참고
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* microhttpd */
#include <stdbool.h>
#include <microhttpd.h>
#define PORT 8080
#define POSTBUFFERSIZE 512
#define GET 0
#define POST 1
#define REQUEST_REFUSED_PAGE \
"<html><head><title> Request refused </title></head><body> Request Refused (file exist?) </body></html>"
const char *responseOK = "\"Result\":\"OK\"";
const char *responseExist = "\"Result\":\"Queue Exist\"";
struct postStatus {
bool status;
char *buff;
};
struct NODE {
struct NODE *next;
char msg[30];
int available;
char idx[5];
};
struct MULTI_MSG_QUEUE {
int cur_size;
int max_size;
char name[20];
struct NODE *msgq;
};
int cur_multi_queue_cnt =0;
struct MULTI_MSG_QUEUE multi_queue[10] = {0};
static struct MHD_Response *request_refused_response;
static void request_completed_callback (void *cls,
struct MHD_Connection *connection,
void **con_cls,
enum MHD_RequestTerminationCode toe)
{
printf("request_completed_callback \n");
}
static enum MHD_Result access_handler_callback ( void * cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size, void **con_cls) {
int ret;
printf("url - %s \n", url);
printf("method - %s \n", method);
printf("version - %s \n", version);
printf("upload_data - %s size - %d \n", upload_data, (int)*upload_data_size);
struct MHD_Response * response;
char url_cpy[50] = {0};
char cmd_list[3][20] = {0};
int cmd_cnt =0;
char *str_ptr = NULL;
char queue_size[10] ={0};
char queue_message[50] = {0};
strcpy(url_cpy,url);
if(url_cpy[0] == '/') {
str_ptr = strtok(&url_cpy[1], "/");
}else {
str_ptr = strtok(url_cpy, "/");
}
while(str_ptr != NULL) {
strcpy(cmd_list[cmd_cnt], str_ptr);
str_ptr = strtok(NULL, "/");
cmd_cnt++;
}
struct postStatus *post = NULL;
post = (struct postStatus *)*con_cls;
if(post == NULL) {
post = malloc(sizeof(struct postStatus));
post->status = false;
post->buff = NULL;
*con_cls = post;
}
printf("post->status = %d \n", post->status);
if(!post->status) {
post->status = true;
return MHD_YES;
}else {
if(*upload_data_size != 0) {
post->buff = malloc(*upload_data_size +1);
memset(post->buff, 0, *upload_data_size +1);
strncpy(post->buff, upload_data, *upload_data_size);
*upload_data_size =0;
return MHD_YES;
}else {
if(strncmp(cmd_list[0], "CREATE",6) == 0) {
json_object *root, *QueueSize;
root = json_tokener_parse(post->buff);
json_object_object_get_ex(root, "QueueSize", &QueueSize);
printf("QueueSize : %s \n", json_object_get_string(QueueSize));
strcpy(queue_size, json_object_get_string(QueueSize));
}else if(strncmp(cmd_list[0], "SEND",4) == 0) {
json_object *root, *q_msg;
root = json_tokener_parse(post->buff);
json_object_object_get_ex(root, "Message", &q_msg);
printf("Message : %s \n", json_object_get_string(q_msg));
strcpy(queue_message, json_object_get_string(q_msg));
}else if(strncmp(cmd_list[0], "RECEIVE",7) == 0) {
//[no body]
}
if(post->buff != NULL) {
free(post->buff);
}
}
}
if(post != NULL) {
free(post);
}
json_object *myobj = json_object_new_object();
if(strncmp(cmd_list[0], "CREATE",6) == 0) {
int is_exist =0;
for(int i=0; i<cur_multi_queue_cnt; i++) {
if(strncmp(multi_queue[i].name, cmd_list[1], strlne(cmd_list[i]) ) == 0) {
is_exist =1;
break;
}
}
if(is_exist) {
printf("Queue Exist \n");
json_object_object_add(myobj, "Result", json_object_new_string("Queue Exist"));
}else {
multi_queue[cur_multi_queue_cnt].max_size = atoi(queue_size);
strcpy(multi_queue[cur_multi_queue_cnt].name = cmd_list[1]);
multi_queue[cur_multi_queue_cnt].cur_size =0;
multi_queue[cur_multi_queue_cnt].msgq = (struct NODE *)malloc(sizeof(struct NODE));
multi_queue[cur_multi_queue_cnt].msgq->next = NULL;
cur_multi_queue_cnt++;
json_object_object_add(myobj, "Result", json_object_new_string("Ok"));
}
}else if(strncmp(cmd_list[0], "SEND",4) == 0) {
int q_cnt =-1;
for(int i=0; i<cur_multi_queue_cnt; i++) {
if(strncmp(multi_queue[i].name, cmd_list[1], strlne(cmd_list[i]) ) == 0) {
q_cnt =i;
break;
}
}
if(q_cnt != -1) {
if(multi_queue[q_cnt].cur_size < multi_queue[q_cnt].max_size) {
struct NODE *new = (struct NODE *)malloc(sizeof(struct NODE));
char msg_cpy[30] ={0};
memset(new->msg, 0, siezeof(new->msg));
new->next = NULL;
strcpy(new->msg, queue_message);
new->available = 1;
strcpy(msg_cpy,queue_message);
int msg_loop_cnt = 0;
str_ptr = strtok(msg_cpy, "#");
while(str_ptr != NULL) {
if(msg_loop_cnt == 0) {
}else if(msg_loop_cnt == 1) {
strcpy(new->idx, str_ptr);
}
str_ptr = strtok(NULL, "#");
msg_loop_cnt++;
}
}
struct NODE *cur = multi_queuep[q_cnt].msgq;
new->next= cur->next;
cur->next = new;
multi_queue[q_cnt].cur_size++;
json_object_object_add(myobj, "Result", json_object_new_string("Ok"));
}else {
printf("Queue Full \n");
json_object_object_add(myobj, "Result", json_object_get_string("Queue Full"));
}
}else if(strncmp(cmd_list[0], "RECEIVE",7) == 0) {
//similar with SEND
}
const char *result_json = json_object_to_json_string_ext(myobj, JSON_C_TO_STRING_PLAIN);
response = MHD_create_response_from_buffer (strlen(result_json), (void *)result_json,
MHD_RESPMEM_MUST_COPY);
if(!response) {
return MHD_NO;
}
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
int main(int argc, char *argv[]) {
struct MHD_Daemon *daemon = MHD_start_daemon (
MHD_USE_THREAD_PER_CONNECTION,
PORT,
NULL, NULL,
&access_handler_callback,
NULL,
MHD_OPTION_NOTIFY_COMPLETED,
&request_completed_callback,
NULL,
MHD_OPTION_END);
request_refused_response = MHD_create_response_from_buffer(strlen(REQUEST_REFUSED_PAGE),
(void *)REQUEST_REFUSED_PAGE,
MHD_RESPMEM_PERSISTENT);
if(daemon == NULL) {
fprintf(stderr, "MHD_start_daemon () error \n");
return EXIT_FAILURE;
}
while(true) {
getc(stdin);
}
//HTTP 데몬을 종료한다.
MHD_stop_daemon(daemon);
MHD_destroy_response(request_refused_response);
return EXIT_SUCCESS;
}
|
cs |
'프로그래밍 > C언어' 카테고리의 다른 글
코드에서 외부프로그램 실행 (0) | 2022.07.11 |
---|---|
curl 을 이용한 클라이언트 get , post 요청 (0) | 2022.07.11 |
Json C로 Json <-> string 변환 (0) | 2022.07.11 |
연결리스트로 스택 만들기 (0) | 2022.07.10 |
연결 리스트로 큐 생성 (0) | 2022.07.10 |