comparison src/c/request.c @ 1997:c93fbd139732

Define uw_loggers structure, allow FFI code to access it
author Sergey Mironov <grrwlf@gmail.com>
date Wed, 26 Feb 2014 08:21:52 +0000
parents d02c1a0d8082
children bcda3ae88677
comparison
equal deleted inserted replaced
1996:582ea3a4d622 1997:c93fbd139732
10 #include <signal.h> 10 #include <signal.h>
11 11
12 #include <pthread.h> 12 #include <pthread.h>
13 13
14 #include "urweb.h" 14 #include "urweb.h"
15 #include "request.h"
15 16
16 #define MAX_RETRIES 5 17 #define MAX_RETRIES 5
17 18
18 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2); 19 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2);
19 20
30 } 31 }
31 32
32 return r; 33 return r;
33 } 34 }
34 35
35 uw_context uw_request_new_context(int id, uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 36 uw_context uw_request_new_context(int id, uw_app *app, uw_loggers *ls) {
36 uw_context ctx = uw_init(id, logger_data, log_debug); 37 void *logger_data = ls->logger_data;
38 uw_logger log_debug = ls->log_debug;
39 uw_logger log_error = ls->log_error;
40 uw_context ctx = uw_init(id, ls);
37 int retries_left = MAX_RETRIES; 41 int retries_left = MAX_RETRIES;
38 uw_set_app(ctx, app); 42 uw_set_app(ctx, app);
39 43
40 while (1) { 44 while (1) {
41 failure_kind fk = uw_begin_init(ctx); 45 failure_kind fk = uw_begin_init(ctx);
76 80
77 return NULL; 81 return NULL;
78 } 82 }
79 83
80 typedef struct { 84 typedef struct {
85 int id;
86 uw_loggers *ls;
87 uw_periodic pdic;
81 uw_app *app; 88 uw_app *app;
82 void *logger_data;
83 uw_logger log_error, log_debug;
84 } loggers;
85
86 typedef struct {
87 int id;
88 loggers *ls;
89 uw_periodic pdic;
90 } periodic; 89 } periodic;
91 90
92 static void *periodic_loop(void *data) { 91 static void *periodic_loop(void *data) {
93 periodic *p = (periodic *)data; 92 periodic *p = (periodic *)data;
94 uw_context ctx = uw_request_new_context(p->id, p->ls->app, p->ls->logger_data, p->ls->log_error, p->ls->log_debug); 93 uw_context ctx = uw_request_new_context(p->id, p->app, p->ls);
95 94
96 if (!ctx) 95 if (!ctx)
97 exit(1); 96 exit(1);
98 97
99 while (1) { 98 while (1) {
143 } else { 142 } else {
144 return pthread_create(outThread, NULL, threadFunc, arg); 143 return pthread_create(outThread, NULL, threadFunc, arg);
145 } 144 }
146 } 145 }
147 146
148 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 147 void uw_request_init(uw_app *app, uw_loggers* ls) {
149 uw_context ctx; 148 uw_context ctx;
150 failure_kind fk; 149 failure_kind fk;
151 uw_periodic *ps; 150 uw_periodic *ps;
152 loggers *ls = malloc(sizeof(loggers));
153 int id; 151 int id;
154 char *stackSize_s; 152 char *stackSize_s;
153
154 uw_logger log_debug = ls->log_debug;
155 uw_logger log_error = ls->log_error;
156 void* logger_data = ls->logger_data;
155 157
156 if ((stackSize_s = getenv("URWEB_STACK_SIZE")) != NULL && stackSize_s[0] != 0) { 158 if ((stackSize_s = getenv("URWEB_STACK_SIZE")) != NULL && stackSize_s[0] != 0) {
157 stackSize = atoll(stackSize_s); 159 stackSize = atoll(stackSize_s);
158 160
159 if (stackSize <= 0) { 161 if (stackSize <= 0) {
160 fprintf(stderr, "Invalid stack size \"%s\"\n", stackSize_s); 162 fprintf(stderr, "Invalid stack size \"%s\"\n", stackSize_s);
161 exit(1); 163 exit(1);
162 } 164 }
163 } 165 }
164
165 ls->app = app;
166 ls->logger_data = logger_data;
167 ls->log_error = log_error;
168 ls->log_debug = log_debug;
169 166
170 uw_global_init(); 167 uw_global_init();
171 uw_app_init(app); 168 uw_app_init(app);
172 169
173 { 170 {
177 fprintf(stderr, "Error creating ticker thread\n"); 174 fprintf(stderr, "Error creating ticker thread\n");
178 exit(1); 175 exit(1);
179 } 176 }
180 } 177 }
181 178
182 ctx = uw_request_new_context(0, app, logger_data, log_error, log_debug); 179 ctx = uw_request_new_context(0, app, ls);
183 180
184 if (!ctx) 181 if (!ctx)
185 exit(1); 182 exit(1);
186 183
187 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) { 184 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
203 pthread_t thread; 200 pthread_t thread;
204 periodic *arg = malloc(sizeof(periodic)); 201 periodic *arg = malloc(sizeof(periodic));
205 arg->id = id++; 202 arg->id = id++;
206 arg->ls = ls; 203 arg->ls = ls;
207 arg->pdic = *ps; 204 arg->pdic = *ps;
205 arg->app = app;
208 206
209 if (pthread_create_big(&thread, NULL, periodic_loop, arg)) { 207 if (pthread_create_big(&thread, NULL, periodic_loop, arg)) {
210 fprintf(stderr, "Error creating periodic thread\n"); 208 fprintf(stderr, "Error creating periodic thread\n");
211 exit(1); 209 exit(1);
212 } 210 }
238 char *method, char *path, char *query_string, 236 char *method, char *path, char *query_string,
239 char *body, size_t body_len, 237 char *body, size_t body_len,
240 void (*on_success)(uw_context), void (*on_failure)(uw_context), 238 void (*on_success)(uw_context), void (*on_failure)(uw_context),
241 void *logger_data, uw_logger log_error, uw_logger log_debug, 239 void *logger_data, uw_logger log_error, uw_logger log_debug,
242 int sock, 240 int sock,
243 int (*send)(int sockfd, const void *buf, size_t len), 241 int (*send)(int sockfd, const void *buf, ssize_t len),
244 int (*close)(int fd)) { 242 int (*close)(int fd)) {
245 int retries_left = MAX_RETRIES; 243 int retries_left = MAX_RETRIES;
246 failure_kind fk; 244 failure_kind fk;
247 int is_post = 0; 245 int is_post = 0;
248 char *boundary = NULL; 246 char *boundary = NULL;
586 uw_reset_keep_request(ctx); 584 uw_reset_keep_request(ctx);
587 } 585 }
588 } 586 }
589 587
590 void *client_pruner(void *data) { 588 void *client_pruner(void *data) {
591 loggers *ls = (loggers *)data; 589 pruner_data *pd = (pruner_data *)data;
592 uw_context ctx = uw_request_new_context(0, ls->app, ls->logger_data, ls->log_error, ls->log_debug); 590 uw_context ctx = uw_request_new_context(0, pd->app, pd->loggers);
593 591
594 if (!ctx) 592 if (!ctx)
595 exit(1); 593 exit(1);
596 594
597 while (1) { 595 while (1) {