worker.c 1.74 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/request/parser.h"
#include "http/response/writer.h"

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

#define  SHMN	"/worker_"
static
int
ctor(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(HttpRequestParser, this->pbuf);
	this->writer = new(HttpResponseWriter, this->wbuf);

	return 0;
}

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

	if (NULL != this->id)	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(HttpRequestParser, base->pbuf);
	this->writer = new(HttpResponseWriter, base->wbuf);
}

INIT_IFACE(Class, ctor, dtor, _clone);
INIT_IFACE(StreamReader, (fptr_streamReaderRead)httpWorkerProcess);
INIT_IFACE(StreamWriter, (fptr_streamWriterWrite)httpWorkerWrite);
CREATE_CLASS(
		HttpWorker,
		NULL, 
		IFACE(Class),
		IFACE(StreamReader),
		IFACE(StreamWriter));

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