comparison src/c/request.c @ 1446:36f7d1debb37

Each context gets its own non-repeating sequence of source numbers
author Adam Chlipala <adam@chlipala.net>
date Sat, 09 Apr 2011 14:36:47 -0400
parents 493e087f5479
children 4d0b80dd4c37
comparison
equal deleted inserted replaced
1445:6e6f1643c4e9 1446:36f7d1debb37
30 } 30 }
31 31
32 return r; 32 return r;
33 } 33 }
34 34
35 uw_context uw_request_new_context(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 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 ctx = uw_init(logger_data, log_debug); 36 uw_context ctx = uw_init(id, logger_data, log_debug);
37 int retries_left = MAX_RETRIES; 37 int retries_left = MAX_RETRIES;
38 uw_set_app(ctx, app); 38 uw_set_app(ctx, app);
39 39
40 while (1) { 40 while (1) {
41 failure_kind fk = uw_begin_init(ctx); 41 failure_kind fk = uw_begin_init(ctx);
82 void *logger_data; 82 void *logger_data;
83 uw_logger log_error, log_debug; 83 uw_logger log_error, log_debug;
84 } loggers; 84 } loggers;
85 85
86 typedef struct { 86 typedef struct {
87 int id;
87 loggers *ls; 88 loggers *ls;
88 uw_periodic pdic; 89 uw_periodic pdic;
89 } periodic; 90 } periodic;
90 91
91 static void *periodic_loop(void *data) { 92 static void *periodic_loop(void *data) {
92 periodic *p = (periodic *)data; 93 periodic *p = (periodic *)data;
93 uw_context ctx = uw_request_new_context(p->ls->app, p->ls->logger_data, p->ls->log_error, p->ls->log_debug); 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);
94 95
95 if (!ctx) 96 if (!ctx)
96 exit(1); 97 exit(1);
97 98
98 while (1) { 99 while (1) {
125 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) { 126 void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
126 uw_context ctx; 127 uw_context ctx;
127 failure_kind fk; 128 failure_kind fk;
128 uw_periodic *ps; 129 uw_periodic *ps;
129 loggers *ls = malloc(sizeof(loggers)); 130 loggers *ls = malloc(sizeof(loggers));
131 int id;
130 132
131 ls->app = app; 133 ls->app = app;
132 ls->logger_data = logger_data; 134 ls->logger_data = logger_data;
133 ls->log_error = log_error; 135 ls->log_error = log_error;
134 ls->log_debug = log_debug; 136 ls->log_debug = log_debug;
143 fprintf(stderr, "Error creating ticker thread\n"); 145 fprintf(stderr, "Error creating ticker thread\n");
144 exit(1); 146 exit(1);
145 } 147 }
146 } 148 }
147 149
148 ctx = uw_request_new_context(app, logger_data, log_error, log_debug); 150 ctx = uw_request_new_context(0, app, logger_data, log_error, log_debug);
149 151
150 if (!ctx) 152 if (!ctx)
151 exit(1); 153 exit(1);
152 154
153 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) { 155 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
162 exit(1); 164 exit(1);
163 } 165 }
164 166
165 uw_free(ctx); 167 uw_free(ctx);
166 168
169 id = 1;
167 for (ps = app->periodics; ps->callback; ++ps) { 170 for (ps = app->periodics; ps->callback; ++ps) {
168 pthread_t thread; 171 pthread_t thread;
169 periodic *arg = malloc(sizeof(periodic)); 172 periodic *arg = malloc(sizeof(periodic));
173 arg->id = id++;
170 arg->ls = ls; 174 arg->ls = ls;
171 arg->pdic = *ps; 175 arg->pdic = *ps;
172 176
173 if (pthread_create(&thread, NULL, periodic_loop, arg)) { 177 if (pthread_create(&thread, NULL, periodic_loop, arg)) {
174 fprintf(stderr, "Error creating periodic thread\n"); 178 fprintf(stderr, "Error creating periodic thread\n");
216 char *s; 220 char *s;
217 int had_error = 0; 221 int had_error = 0;
218 char errmsg[ERROR_BUF_LEN]; 222 char errmsg[ERROR_BUF_LEN];
219 223
220 uw_reset(ctx); 224 uw_reset(ctx);
225
221 rc->queryString[0] = 0; 226 rc->queryString[0] = 0;
222 227
223 for (s = path; *s; ++s) { 228 for (s = path; *s; ++s) {
224 if (s[0] == '%' && s[1] == '2' && s[2] == '7') { 229 if (s[0] == '%' && s[1] == '2' && s[2] == '7') {
225 s[0] = '\''; 230 s[0] = '\'';
544 } 549 }
545 } 550 }
546 551
547 void *client_pruner(void *data) { 552 void *client_pruner(void *data) {
548 loggers *ls = (loggers *)data; 553 loggers *ls = (loggers *)data;
549 uw_context ctx = uw_request_new_context(ls->app, ls->logger_data, ls->log_error, ls->log_debug); 554 uw_context ctx = uw_request_new_context(0, ls->app, ls->logger_data, ls->log_error, ls->log_debug);
550 555
551 if (!ctx) 556 if (!ctx)
552 exit(1); 557 exit(1);
553 558
554 while (1) { 559 while (1) {