worker.c 1.76 KB
#include <stdlib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "class.h"
#include "http/worker.h"
#include "http/parser.h"
#include "http/response/writer.h"

#include "interface/class.h"
#include "interface/stream_reader.h"
#include "interface/stream_writer.h"

#include "utils/memory.h"

static
int
httpWorkerCtor(void * _this, va_list * params)
{
	HttpWorker this = _this;
	char *     id   = va_arg(*params, char *);
	int  *     val  = va_arg(*params, int *);
	char       cbuf_id[100];

	this->id  = malloc(strlen(id) + 1);
	strcpy(this->id, id);
	this->val = val;

	sprintf(cbuf_id, "%s_%s", "parser", id);
	this->pbuf   = new(Cbuf, cbuf_id, REQUEST_PARSER_BUFFER_MAX);

	sprintf(cbuf_id, "%s_%s", "writer", id);
	this->wbuf   = new(Cbuf, cbuf_id, RESPONSE_WRITER_MAX_BUF);

	this->parser = new(HttpParser, this->pbuf);
	this->writer = new(HttpResponseWriter, this->wbuf);

	return 0;
}

static
void
httpWorkerDtor(void * _this)
{
	HttpWorker this = _this;

	FREE(this->id);

	delete(this->parser);
	delete(this->writer);

	if (NULL != this->pbuf)	delete(this->pbuf);
	if (NULL != this->wbuf)	delete(this->wbuf);
}

static
void
_clone(void * _this, void * _base)
{
	HttpWorker this = _this;
	HttpWorker base = _base;

	this->id   = NULL;
	this->val  = base->val;
	this->pbuf = NULL;
	this->wbuf = NULL;

	this->parser = new(HttpParser, base->pbuf);
	this->writer = new(HttpResponseWriter, base->wbuf);
}

ssize_t httpWorkerProcess(void *, int);
ssize_t httpWorkerWrite(void *, int);

INIT_IFACE(Class, httpWorkerCtor, httpWorkerDtor, _clone);
INIT_IFACE(StreamReader, httpWorkerProcess);
INIT_IFACE(StreamWriter, httpWorkerWrite);
CREATE_CLASS(
		HttpWorker,
		NULL, 
		IFACE(Class),
		IFACE(StreamReader),
		IFACE(StreamWriter));

// vim: set ts=4 sw=4: