Commit a239fc98e8aded12a7259bc74fae2a45e617ba94
1 parent
514b7843
changed HttpMessageQueue to be a real queue and not a fixed size array
Showing
8 changed files
with
144 additions
and
46 deletions
@@ -26,17 +26,32 @@ | @@ -26,17 +26,32 @@ | ||
26 | #ifndef __HTTP_MESSAGE_QUEUE_H__ | 26 | #ifndef __HTTP_MESSAGE_QUEUE_H__ |
27 | #define __HTTP_MESSAGE_QUEUE_H__ | 27 | #define __HTTP_MESSAGE_QUEUE_H__ |
28 | 28 | ||
29 | +#include <sys/types.h> | ||
30 | + | ||
29 | #include "class.h" | 31 | #include "class.h" |
30 | #include "http/message.h" | 32 | #include "http/message.h" |
31 | - | ||
32 | -#define HTTP_MESSAGE_QUEUE_MAX 1024 | 33 | +#include "commons.h" |
33 | 34 | ||
34 | 35 | ||
35 | CLASS(HttpMessageQueue) { | 36 | CLASS(HttpMessageQueue) { |
36 | - HttpMessage msgs[HTTP_MESSAGE_QUEUE_MAX]; | ||
37 | - size_t nmsgs; | 37 | + HttpMessage msg; |
38 | + HttpMessageQueue next; | ||
39 | + | ||
40 | + /** | ||
41 | + * first and last are only available in the initial queue | ||
42 | + * element (the root). This elelment does not contain any message | ||
43 | + * and exists only for organizational purpose. | ||
44 | + */ | ||
45 | + HttpMessageQueue first; | ||
46 | + HttpMessageQueue last; | ||
47 | + size_t nmsg; | ||
38 | }; | 48 | }; |
39 | 49 | ||
50 | +void httpMessageQueuePut(HttpMessageQueue, HttpMessage); | ||
51 | +HttpMessage httpMessageQueueGet(HttpMessageQueue); | ||
52 | + | ||
53 | +#define httpMessageQueueEmpty(this) (0 >= (this)->nmsg) | ||
54 | + | ||
40 | #endif // __HTTP_MESSAGE_QUEUE_H__ | 55 | #endif // __HTTP_MESSAGE_QUEUE_H__ |
41 | 56 | ||
42 | // vim: set ts=4 sw=4: | 57 | // vim: set ts=4 sw=4: |
@@ -6,7 +6,9 @@ MSG = message.c \ | @@ -6,7 +6,9 @@ MSG = message.c \ | ||
6 | message/header_to_string.c \ | 6 | message/header_to_string.c \ |
7 | message/get_version.c \ | 7 | message/get_version.c \ |
8 | message/has_valid_version.c | 8 | message/has_valid_version.c |
9 | -MSGQ = message/queue.c | 9 | +MSGQ = message/queue.c \ |
10 | + message/queue/get.c \ | ||
11 | + message/queue/put.c | ||
10 | REQ = request.c \ | 12 | REQ = request.c \ |
11 | request/has_valid_method.c | 13 | request/has_valid_method.c |
12 | RESP = response.c \ | 14 | RESP = response.c \ |
@@ -38,10 +38,14 @@ void | @@ -38,10 +38,14 @@ void | ||
38 | messageQueueDtor(void * _this) | 38 | messageQueueDtor(void * _this) |
39 | { | 39 | { |
40 | HttpMessageQueue this = _this; | 40 | HttpMessageQueue this = _this; |
41 | + HttpMessageQueue node = this->first; | ||
41 | int i; | 42 | int i; |
42 | - | ||
43 | - for (i=0; i<this->nmsgs; i++) { | ||
44 | - delete((this->msgs)[i]); | 43 | + |
44 | + while (NULL != node) { | ||
45 | + HttpMessageQueue next = node->next; | ||
46 | + delete(node->msg); | ||
47 | + delete(node); | ||
48 | + node = next; | ||
45 | } | 49 | } |
46 | } | 50 | } |
47 | 51 |
src/http/message/queue/get.c
0 → 100644
1 | +/** | ||
2 | + * \file | ||
3 | + * | ||
4 | + * \author Georg Hopp | ||
5 | + * | ||
6 | + * \copyright | ||
7 | + * Copyright © 2012 Georg Hopp | ||
8 | + * | ||
9 | + * This program is free software: you can redistribute it and/or modify | ||
10 | + * it under the terms of the GNU General Public License as published by | ||
11 | + * the Free Software Foundation, either version 3 of the License, or | ||
12 | + * (at your option) any later version. | ||
13 | + * | ||
14 | + * This program is distributed in the hope that it will be useful, | ||
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | + * GNU General Public License for more details. | ||
18 | + * | ||
19 | + * You should have received a copy of the GNU General Public License | ||
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
21 | + */ | ||
22 | + | ||
23 | +#include "class.h" | ||
24 | +#include "http/message.h" | ||
25 | +#include "http/message/queue.h" | ||
26 | + | ||
27 | +HttpMessage | ||
28 | +httpMessageQueueGet(HttpMessageQueue this) | ||
29 | +{ | ||
30 | + HttpMessageQueue first; | ||
31 | + HttpMessage msg; | ||
32 | + | ||
33 | + if (NULL == this->first) { | ||
34 | + return NULL; | ||
35 | + } | ||
36 | + | ||
37 | + msg = this->first->msg; | ||
38 | + first = this->first->next; | ||
39 | + delete(this->first); | ||
40 | + | ||
41 | + this->first = first; | ||
42 | + this->nmsg--; | ||
43 | + | ||
44 | + return msg; | ||
45 | +} | ||
46 | + | ||
47 | +// vim: set ts=4 sw=4: |
src/http/message/queue/put.c
0 → 100644
1 | +/** | ||
2 | + * \file | ||
3 | + * | ||
4 | + * \author Georg Hopp | ||
5 | + * | ||
6 | + * \copyright | ||
7 | + * Copyright © 2012 Georg Hopp | ||
8 | + * | ||
9 | + * This program is free software: you can redistribute it and/or modify | ||
10 | + * it under the terms of the GNU General Public License as published by | ||
11 | + * the Free Software Foundation, either version 3 of the License, or | ||
12 | + * (at your option) any later version. | ||
13 | + * | ||
14 | + * This program is distributed in the hope that it will be useful, | ||
15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | + * GNU General Public License for more details. | ||
18 | + * | ||
19 | + * You should have received a copy of the GNU General Public License | ||
20 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
21 | + */ | ||
22 | + | ||
23 | +#include "class.h" | ||
24 | +#include "http/message.h" | ||
25 | +#include "http/message/queue.h" | ||
26 | + | ||
27 | +void | ||
28 | +httpMessageQueuePut(HttpMessageQueue this, HttpMessage msg) | ||
29 | +{ | ||
30 | + HttpMessageQueue node = (this->last)? this->last : this; | ||
31 | + | ||
32 | + node->next = new(HttpMessageQueue); | ||
33 | + this->last = node->next; | ||
34 | + | ||
35 | + if (node == this) { | ||
36 | + this->first = node->next; | ||
37 | + } | ||
38 | + | ||
39 | + node->next->msg = msg; | ||
40 | + this->nmsg++; | ||
41 | +} | ||
42 | + | ||
43 | +// vim: set ts=4 sw=4: |
@@ -167,16 +167,16 @@ httpParserParse(void * _this, Stream st) | @@ -167,16 +167,16 @@ httpParserParse(void * _this, Stream st) | ||
167 | httpParserPostVars(this); | 167 | httpParserPostVars(this); |
168 | } | 168 | } |
169 | 169 | ||
170 | - /** | ||
171 | - * enqueue current request | ||
172 | - */ | ||
173 | - this->queue->msgs[(this->queue->nmsgs)++] = this->current; | ||
174 | - this->current = NULL; | ||
175 | - | ||
176 | - /** | ||
177 | - * prepare for next request | ||
178 | - */ | ||
179 | - this->state = HTTP_MESSAGE_GARBAGE; | 170 | + /** |
171 | + * enqueue current request | ||
172 | + */ | ||
173 | + httpMessageQueuePut(this->queue, this->current); | ||
174 | + this->current = NULL; | ||
175 | + | ||
176 | + /** | ||
177 | + * prepare for next request | ||
178 | + */ | ||
179 | + this->state = HTTP_MESSAGE_GARBAGE; | ||
180 | } | 180 | } |
181 | break; | 181 | break; |
182 | 182 | ||
@@ -185,7 +185,7 @@ httpParserParse(void * _this, Stream st) | @@ -185,7 +185,7 @@ httpParserParse(void * _this, Stream st) | ||
185 | } | 185 | } |
186 | } | 186 | } |
187 | 187 | ||
188 | - return this->queue->nmsgs; | 188 | + return this->queue->nmsg; |
189 | } | 189 | } |
190 | 190 | ||
191 | // vim: set ts=4 sw=4: | 191 | // vim: set ts=4 sw=4: |
@@ -60,13 +60,11 @@ httpWorkerProcess(HttpWorker this, Stream st) | @@ -60,13 +60,11 @@ httpWorkerProcess(HttpWorker this, Stream st) | ||
60 | 60 | ||
61 | if (0 < (size = httpParserParse(this->parser, st))) { | 61 | if (0 < (size = httpParserParse(this->parser, st))) { |
62 | int i; | 62 | int i; |
63 | - HttpMessageQueue reqq = this->parser->queue; | ||
64 | - HttpMessageQueue respq = this->writer->queue; | ||
65 | 63 | ||
66 | - for (i=0; i<reqq->nmsgs; i++) { | ||
67 | - HttpMessage rmessage = reqq->msgs[i]; | ||
68 | - HttpRequest request = (HttpRequest)(reqq->msgs[i]); | ||
69 | - HttpMessage response = NULL; | 64 | + while (! httpMessageQueueEmpty(this->parser->queue)) { |
65 | + HttpRequest request = (HttpRequest)httpMessageQueueGet( | ||
66 | + this->parser->queue); | ||
67 | + HttpMessage response = NULL; | ||
70 | 68 | ||
71 | /** | 69 | /** |
72 | * \todo store the cookie count in the request to make a simple | 70 | * \todo store the cookie count in the request to make a simple |
@@ -238,19 +236,11 @@ httpWorkerProcess(HttpWorker this, Stream st) | @@ -238,19 +236,11 @@ httpWorkerProcess(HttpWorker this, Stream st) | ||
238 | 236 | ||
239 | httpWorkerAddCommonHeader((HttpMessage)request, response); | 237 | httpWorkerAddCommonHeader((HttpMessage)request, response); |
240 | 238 | ||
241 | - t = time(NULL); | ||
242 | - tmp = localtime(&t); | ||
243 | - strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | ||
244 | - httpHeaderAdd(&(response->header), | ||
245 | - new(HttpHeader, "Date", buffer)); | 239 | + delete(request); |
246 | 240 | ||
247 | - respq->msgs[(respq->nmsgs)++] = response; | 241 | + httpMessageQueuePut(this->writer->queue, response); |
248 | response = NULL; | 242 | response = NULL; |
249 | - delete((reqq->msgs)[i]); | ||
250 | } | 243 | } |
251 | - | ||
252 | - reqq->nmsgs = 0; | ||
253 | - size = respq->nmsgs; | ||
254 | } | 244 | } |
255 | 245 | ||
256 | return size; | 246 | return size; |
@@ -25,6 +25,7 @@ | @@ -25,6 +25,7 @@ | ||
25 | 25 | ||
26 | #include "class.h" | 26 | #include "class.h" |
27 | #include "http/message.h" | 27 | #include "http/message.h" |
28 | +#include "http/message/queue.h" | ||
28 | #include "http/writer.h" | 29 | #include "http/writer.h" |
29 | #include "cbuf.h" | 30 | #include "cbuf.h" |
30 | #include "stream.h" | 31 | #include "stream.h" |
@@ -35,9 +36,8 @@ | @@ -35,9 +36,8 @@ | ||
35 | ssize_t | 36 | ssize_t |
36 | httpWriterWrite(void * _this, Stream st) | 37 | httpWriterWrite(void * _this, Stream st) |
37 | { | 38 | { |
38 | - HttpWriter this = _this; | ||
39 | - HttpMessageQueue respq = this->queue; | ||
40 | - int cont = 1; | 39 | + HttpWriter this = _this; |
40 | + int cont = 1; | ||
41 | 41 | ||
42 | if (cbufIsLocked(this->buffer)) { | 42 | if (cbufIsLocked(this->buffer)) { |
43 | if (FALSE == this->ourLock) | 43 | if (FALSE == this->ourLock) |
@@ -51,8 +51,9 @@ httpWriterWrite(void * _this, Stream st) | @@ -51,8 +51,9 @@ httpWriterWrite(void * _this, Stream st) | ||
51 | while (cont) { | 51 | while (cont) { |
52 | switch (this->state) { | 52 | switch (this->state) { |
53 | case HTTP_WRITER_GET: | 53 | case HTTP_WRITER_GET: |
54 | - if (NULL == this->current && 0 < respq->nmsgs) { | ||
55 | - this->current = respq->msgs[0]; | 54 | + if (NULL == this->current && |
55 | + ! httpMessageQueueEmpty(this->queue)) { | ||
56 | + this->current = httpMessageQueueGet(this->queue); | ||
56 | 57 | ||
57 | this->written = 0; | 58 | this->written = 0; |
58 | this->nbody = 0; | 59 | this->nbody = 0; |
@@ -122,11 +123,7 @@ httpWriterWrite(void * _this, Stream st) | @@ -122,11 +123,7 @@ httpWriterWrite(void * _this, Stream st) | ||
122 | break; | 123 | break; |
123 | 124 | ||
124 | case HTTP_WRITER_DONE: | 125 | case HTTP_WRITER_DONE: |
125 | - this->state = HTTP_WRITER_GET; | ||
126 | - | ||
127 | - memmove(respq->msgs, | ||
128 | - &(respq->msgs[1]), | ||
129 | - sizeof(void*) * (--respq->nmsgs + 1)); | 126 | + this->state = HTTP_WRITER_GET; |
130 | 127 | ||
131 | cbufRelease(this->buffer); | 128 | cbufRelease(this->buffer); |
132 | this->ourLock = FALSE; | 129 | this->ourLock = FALSE; |
@@ -148,7 +145,7 @@ httpWriterWrite(void * _this, Stream st) | @@ -148,7 +145,7 @@ httpWriterWrite(void * _this, Stream st) | ||
148 | } | 145 | } |
149 | } | 146 | } |
150 | 147 | ||
151 | - return respq->nmsgs; | 148 | + return this->queue->nmsg; |
152 | } | 149 | } |
153 | 150 | ||
154 | // vim: set ts=4 sw=4: | 151 | // vim: set ts=4 sw=4: |
Please
register
or
login
to post a comment