Browse Source

changed HttpMessageQueue to be a real queue and not a fixed size array

master
Georg Hopp 14 years ago
parent
commit
a239fc98e8
  1. 23
      include/http/message/queue.h
  2. 4
      src/http/Makefile.am
  3. 8
      src/http/message/queue.c
  4. 47
      src/http/message/queue/get.c
  5. 43
      src/http/message/queue/put.c
  6. 4
      src/http/parser/parse.c
  7. 20
      src/http/worker/process.c
  8. 13
      src/http/writer/write.c

23
include/http/message/queue.h

@ -26,17 +26,32 @@
#ifndef __HTTP_MESSAGE_QUEUE_H__
#define __HTTP_MESSAGE_QUEUE_H__
#include <sys/types.h>
#include "class.h"
#include "http/message.h"
#define HTTP_MESSAGE_QUEUE_MAX 1024
#include "commons.h"
CLASS(HttpMessageQueue) {
HttpMessage msgs[HTTP_MESSAGE_QUEUE_MAX];
size_t nmsgs;
HttpMessage msg;
HttpMessageQueue next;
/**
* first and last are only available in the initial queue
* element (the root). This elelment does not contain any message
* and exists only for organizational purpose.
*/
HttpMessageQueue first;
HttpMessageQueue last;
size_t nmsg;
};
void httpMessageQueuePut(HttpMessageQueue, HttpMessage);
HttpMessage httpMessageQueueGet(HttpMessageQueue);
#define httpMessageQueueEmpty(this) (0 >= (this)->nmsg)
#endif // __HTTP_MESSAGE_QUEUE_H__
// vim: set ts=4 sw=4:

4
src/http/Makefile.am

@ -6,7 +6,9 @@ MSG = message.c \
message/header_to_string.c \
message/get_version.c \
message/has_valid_version.c
MSGQ = message/queue.c
MSGQ = message/queue.c \
message/queue/get.c \
message/queue/put.c
REQ = request.c \
request/has_valid_method.c
RESP = response.c \

8
src/http/message/queue.c

@ -38,10 +38,14 @@ void
messageQueueDtor(void * _this)
{
HttpMessageQueue this = _this;
HttpMessageQueue node = this->first;
int i;
for (i=0; i<this->nmsgs; i++) {
delete((this->msgs)[i]);
while (NULL != node) {
HttpMessageQueue next = node->next;
delete(node->msg);
delete(node);
node = next;
}
}

47
src/http/message/queue/get.c

@ -0,0 +1,47 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "class.h"
#include "http/message.h"
#include "http/message/queue.h"
HttpMessage
httpMessageQueueGet(HttpMessageQueue this)
{
HttpMessageQueue first;
HttpMessage msg;
if (NULL == this->first) {
return NULL;
}
msg = this->first->msg;
first = this->first->next;
delete(this->first);
this->first = first;
this->nmsg--;
return msg;
}
// vim: set ts=4 sw=4:

43
src/http/message/queue/put.c

@ -0,0 +1,43 @@
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "class.h"
#include "http/message.h"
#include "http/message/queue.h"
void
httpMessageQueuePut(HttpMessageQueue this, HttpMessage msg)
{
HttpMessageQueue node = (this->last)? this->last : this;
node->next = new(HttpMessageQueue);
this->last = node->next;
if (node == this) {
this->first = node->next;
}
node->next->msg = msg;
this->nmsg++;
}
// vim: set ts=4 sw=4:

4
src/http/parser/parse.c

@ -170,7 +170,7 @@ httpParserParse(void * _this, Stream st)
/**
* enqueue current request
*/
this->queue->msgs[(this->queue->nmsgs)++] = this->current;
httpMessageQueuePut(this->queue, this->current);
this->current = NULL;
/**
@ -185,7 +185,7 @@ httpParserParse(void * _this, Stream st)
}
}
return this->queue->nmsgs;
return this->queue->nmsg;
}
// vim: set ts=4 sw=4:

20
src/http/worker/process.c

@ -60,12 +60,10 @@ httpWorkerProcess(HttpWorker this, Stream st)
if (0 < (size = httpParserParse(this->parser, st))) {
int i;
HttpMessageQueue reqq = this->parser->queue;
HttpMessageQueue respq = this->writer->queue;
for (i=0; i<reqq->nmsgs; i++) {
HttpMessage rmessage = reqq->msgs[i];
HttpRequest request = (HttpRequest)(reqq->msgs[i]);
while (! httpMessageQueueEmpty(this->parser->queue)) {
HttpRequest request = (HttpRequest)httpMessageQueueGet(
this->parser->queue);
HttpMessage response = NULL;
/**
@ -238,19 +236,11 @@ httpWorkerProcess(HttpWorker this, Stream st)
httpWorkerAddCommonHeader((HttpMessage)request, response);
t = time(NULL);
tmp = localtime(&t);
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp);
httpHeaderAdd(&(response->header),
new(HttpHeader, "Date", buffer));
delete(request);
respq->msgs[(respq->nmsgs)++] = response;
httpMessageQueuePut(this->writer->queue, response);
response = NULL;
delete((reqq->msgs)[i]);
}
reqq->nmsgs = 0;
size = respq->nmsgs;
}
return size;

13
src/http/writer/write.c

@ -25,6 +25,7 @@
#include "class.h"
#include "http/message.h"
#include "http/message/queue.h"
#include "http/writer.h"
#include "cbuf.h"
#include "stream.h"
@ -36,7 +37,6 @@ ssize_t
httpWriterWrite(void * _this, Stream st)
{
HttpWriter this = _this;
HttpMessageQueue respq = this->queue;
int cont = 1;
if (cbufIsLocked(this->buffer)) {
@ -51,8 +51,9 @@ httpWriterWrite(void * _this, Stream st)
while (cont) {
switch (this->state) {
case HTTP_WRITER_GET:
if (NULL == this->current && 0 < respq->nmsgs) {
this->current = respq->msgs[0];
if (NULL == this->current &&
! httpMessageQueueEmpty(this->queue)) {
this->current = httpMessageQueueGet(this->queue);
this->written = 0;
this->nbody = 0;
@ -124,10 +125,6 @@ httpWriterWrite(void * _this, Stream st)
case HTTP_WRITER_DONE:
this->state = HTTP_WRITER_GET;
memmove(respq->msgs,
&(respq->msgs[1]),
sizeof(void*) * (--respq->nmsgs + 1));
cbufRelease(this->buffer);
this->ourLock = FALSE;
@ -148,7 +145,7 @@ httpWriterWrite(void * _this, Stream st)
}
}
return respq->nmsgs;
return this->queue->nmsg;
}
// vim: set ts=4 sw=4:
Loading…
Cancel
Save