Server 0.0.1
HTTP/REST server implementation

src/http/worker.c

Go to the documentation of this file.
00001 
00023 #define _GNU_SOURCE
00024 
00025 #include <stdarg.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <stdio.h>
00029 #include <search.h>
00030 
00031 #include "class.h"
00032 #include "stream.h"
00033 #include "http/worker.h"
00034 #include "http/parser.h"
00035 #include "http/writer.h"
00036 
00037 #include "utils/memory.h"
00038 
00039 static
00040 int
00041 httpWorkerCtor(void * _this, va_list * params)
00042 {
00043         HttpWorker this = _this;
00044         char *     id   = va_arg(*params, char *);
00045         char       cbuf_id[100];
00046 
00047         this->id  = malloc(strlen(id) + 1);
00048         strcpy(this->id, id);
00049 
00050         this->val = va_arg(*params, struct randval *);
00051 
00052         sprintf(cbuf_id, "%s_%s", "parser", id);
00053         this->pbuf   = new(Cbuf, cbuf_id, PARSER_MAX_BUF);
00054 
00055         sprintf(cbuf_id, "%s_%s", "writer", id);
00056         this->wbuf   = new(Cbuf, cbuf_id, WRITER_MAX_BUF);
00057 
00058         this->parser = new(HttpParser, this->pbuf);
00059         this->writer = new(HttpWriter, this->wbuf);
00060 
00061         this->sroot  = &(this->session);
00062         this->auth   = va_arg(* params, void *);
00063 
00064         return 0;
00065 }
00066 
00067 static
00068 inline
00069 void
00070 tDelete(void * node)
00071 {
00072         delete(node);
00073 }
00074 
00075 static
00076 void
00077 httpWorkerDtor(void * _this)
00078 {
00079         HttpWorker this = _this;
00080 
00081         FREE(this->id);
00082 
00083         delete(this->parser);
00084         delete(this->writer);
00085 
00086         if (NULL != this->pbuf) {
00087                 delete(this->pbuf); 
00088                 delete(this->wbuf); 
00089                 tdestroy(*(this->sroot), tDelete);
00090         }
00091 }
00092 
00093 static
00094 void
00095 httpWorkerClone(void * _this, void * _base)
00096 {
00097         HttpWorker this = _this;
00098         HttpWorker base = _base;
00099 
00100         this->val  = base->val;
00101 
00102         this->parser = new(HttpParser, base->pbuf);
00103         this->writer = new(HttpWriter, base->wbuf);
00104 
00105         this->sroot  = &(base->session);
00106         this->auth   = base->auth;
00107 }
00108 
00109 ssize_t httpWorkerProcess(void *, Stream);
00110 ssize_t httpWorkerWrite(void *, Stream);
00111 
00112 INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone);
00113 INIT_IFACE(StreamReader, httpWorkerProcess);
00114 INIT_IFACE(StreamWriter, httpWorkerWrite);
00115 CREATE_CLASS(
00116                 HttpWorker,
00117                 NULL, 
00118                 IFACE(Class),
00119                 IFACE(StreamReader),
00120                 IFACE(StreamWriter));
00121 
00122 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines