worker.c
4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* \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/>.
*/
#define _GNU_SOURCE
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <search.h>
#include "trbase.h"
#include "trio.h"
#include "http/worker.h"
#include "http/parser.h"
#include "http/writer.h"
static
int
httpWorkerCtor(void * _this, va_list * params)
{
HttpWorker this = _this;
char * id = va_arg(*params, char *);
char cbuf_id[100];
this->id = TR_malloc(strlen(id) + 1);
strcpy(this->id, id);
this->asset_pool = TR_new(TR_Hash);
sprintf(cbuf_id, "%s_%s", "parser", id);
this->pbuf = TR_new(TR_Cbuf, cbuf_id, PARSER_MAX_BUF);
this->additional_headers = TR_new(TR_Queue);
this->parser = TR_new(HttpParser, this->pbuf);
this->writer = TR_new(HttpWriter);
return 0;
}
static
void
httpWorkerDtor(void * _this)
{
HttpWorker this = _this;
TR_MEM_FREE(this->id);
TR_delete(this->additional_headers);
TR_delete(this->parser);
TR_delete(this->writer);
if (NULL != this->pbuf) {
TR_delete(this->asset_pool);
TR_delete(this->pbuf); //!< cloned workers have NULL, so delete won't do anything
}
}
static
void
httpWorkerClone(void * _this, void * _base)
{
HttpWorker this = _this;
HttpWorker base = _base;
this->asset_pool = base->asset_pool;
this->application_adapter = base->application_adapter;
this->additional_headers = TR_new(TR_Queue);
this->parser = TR_new(HttpParser, base->pbuf);
/*
* I am pretty sure that it is not neccessary to have a
* separeate writer for each connection...
* Right now I leave it that way.
* TODO check this.
* OK some facts:
* - the stream as well as the worker are associated
* to the filehandle within the server.
* - the response queue is located within the writer.
* (this might be wrong...the response queue should
* be part of the worker. That way I could give it
* into the writer when writing. That way only one
* instance of the writer might be possible...)
* NO, the previous statement is wrong...this would
* involve much more organization overhead within
* the writer...queue change and such...
* At the end I think it might be best to leave it as
* it is.
*/
this->writer = TR_new(HttpWriter);
}
ssize_t httpWorkerProcess(void *, TR_Stream);
ssize_t httpWorkerWrite(void *, TR_Stream);
static
void
httpWorkerDetach(void * _this, void * adapter)
{
HttpWorker this = (HttpWorker)_this;
if (NULL != this->application_adapter) {
TR_delete(this->application_adapter);
}
}
static
void
httpWorkerAttach(void * _this, void * adapter)
{
HttpWorker this = (HttpWorker)_this;
/*
* right now only one adapter is allowed and the last
* added will be used....all others will be deleted in
* assumption that no other handle does exist anymore
* (because it was added as an adapter and thus is good
* for nothing else.)
*/
httpWorkerDetach(_this, adapter);
this->application_adapter = adapter;
}
static
void
httpWorkerNotify(void * _this)
{
HttpWorker this = (HttpWorker)_this;
TR_observerUpdate(this->application_adapter, _this);
}
TR_INIT_IFACE(TR_Class, httpWorkerCtor, httpWorkerDtor, httpWorkerClone);
TR_INIT_IFACE(TR_StreamReader, httpWorkerProcess);
TR_INIT_IFACE(TR_StreamWriter, httpWorkerWrite);
TR_INIT_IFACE(
TR_Subject,
httpWorkerAttach,
httpWorkerDetach,
httpWorkerNotify);
TR_CREATE_CLASS(
HttpWorker,
NULL,
TR_IF(TR_Class),
TR_IF(TR_StreamReader),
TR_IF(TR_StreamWriter),
TR_IF(TR_Subject));
// vim: set ts=4 sw=4: