source: benchmark/io/http/worker.cfa @ 8c58e73

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 8c58e73 was 8c58e73, checked in by Thierry Delisle <tdelisle@…>, 22 months ago

Removed webserver feature to have multiple clusters (it never actually worked)

  • Property mode set to 100644
File size: 5.8 KB
Line 
1#include "worker.hfa"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7
8#include <fstream.hfa>
9#include <iofwd.hfa>
10#include <mutex_stmt.hfa>
11
12#include "options.hfa"
13#include "protocol.hfa"
14#include "filecache.hfa"
15
16void ?{}( sendfile_stats_t & this ) {
17        this.calls = 0;
18        this.tries = 0;
19        this.header = 0;
20        this.splcin = 0;
21        this.splcot = 0;
22        for(i; zipf_cnts) {
23                this.avgrd[i].calls = 0;
24                this.avgrd[i].bytes = 0;
25        }
26}
27
28//=============================================================================================
29// Generic connection handling
30//=============================================================================================
31static void handle_connection( connection & this, volatile int & fd, char * buffer, size_t len, io_future_t * f ) {
32        REQUEST:
33        for() {
34                bool closed;
35                HttpCode code;
36                const char * file;
37                size_t name_size;
38
39                // Read the http request
40                if( options.log ) sout | "=== Reading request ===";
41                [code, closed, file, name_size] = http_read(fd, buffer, len, f);
42                f = 0p;
43
44                // if we are done, break out of the loop
45                if( closed ) break REQUEST;
46
47                // If this wasn't a request retrun 400
48                if( code != OK200 ) {
49                        sout | "=== Invalid Request :" | code_val(code) | "===";
50                        answer_error(fd, code);
51                        continue REQUEST;
52                }
53
54                if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
55                        if( options.log ) sout | "=== Request for /plaintext ===";
56
57                        int ret = answer_plaintext(fd);
58                        if( ret == -ECONNRESET ) break REQUEST;
59
60                        if( options.log ) sout | "=== Answer sent ===";
61                        continue REQUEST;
62                }
63
64                if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
65                        if( options.log ) sout | "=== Request for /ping ===";
66
67                        // Send the header
68                        int ret = answer_empty(fd);
69                        if( ret == -ECONNRESET ) break REQUEST;
70
71                        if( options.log ) sout | "=== Answer sent ===";
72                        continue REQUEST;
73                }
74
75                if( options.log ) {
76                        sout | "=== Request for file " | nonl;
77                        write(sout, file, name_size);
78                        sout | " ===";
79                }
80
81                if( !options.file_cache.path ) {
82                        if( options.log ) {
83                                sout | "=== File Not Found (" | nonl;
84                                write(sout, file, name_size);
85                                sout | ") ===";
86                        }
87                        answer_error(fd, E405);
88                        continue REQUEST;
89                }
90
91                // Get the fd from the file cache
92                int ans_fd;
93                size_t count;
94                [ans_fd, count] = get_file( file, name_size );
95
96                // If we can't find the file, return 404
97                if( ans_fd < 0 ) {
98                        if( options.log ) {
99                                sout | "=== File Not Found (" | nonl;
100                                write(sout, file, name_size);
101                                sout | ") ===";
102                        }
103                        answer_error(fd, E404);
104                        continue REQUEST;
105                }
106
107                // Send the desired file
108                int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile );
109                if( ret == -ECONNRESET ) break REQUEST;
110
111                if( options.log ) sout | "=== Answer sent ===";
112        }
113}
114
115//=============================================================================================
116// Self Accepting Worker Thread
117//=============================================================================================
118void ?{}( AcceptWorker & this ) {
119        ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 };
120        options.clopts.thrd_cnt++;
121        this.done = false;
122}
123
124void main( AcceptWorker & this ) {
125        park();
126        /* paranoid */ assert( this.conn.pipe[0] != -1 );
127        /* paranoid */ assert( this.conn.pipe[1] != -1 );
128        for() {
129                if( options.log ) sout | "=== Accepting connection ===";
130                int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY );
131                if(fd < 0) {
132                        if( errno == ECONNABORTED ) break;
133                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
134                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
135                }
136                if(this.done) break;
137
138                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
139                size_t len = options.socket.buflen;
140                char buffer[len];
141                handle_connection( this.conn, fd, buffer, len, 0p );
142
143                if( options.log ) sout | "=== Connection closed ===";
144        }
145}
146
147
148//=============================================================================================
149// Channel Worker Thread
150//=============================================================================================
151void ?{}( ChannelWorker & this ) {
152        ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 };
153        options.clopts.thrd_cnt++;
154        this.done = false;
155}
156
157void main( ChannelWorker & this ) {
158        park();
159        /* paranoid */ assert( this.conn.pipe[0] != -1 );
160        /* paranoid */ assert( this.conn.pipe[1] != -1 );
161        for() {
162                size_t len = options.socket.buflen;
163                char buffer[len];
164                PendingRead p;
165                p.in.buf = (void*)buffer;
166                p.in.len = len;
167                push(*this.queue, &p);
168
169                if( options.log ) sout | "=== Waiting new connection ===";
170                handle_connection( this.conn, p.out.fd, buffer, len, &p.f );
171
172                if( options.log ) sout | "=== Connection closed ===";
173                if(this.done) break;
174        }
175}
176
177extern "C" {
178extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
179}
180
181void ?{}( Acceptor & this ) {
182        ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 };
183        options.clopts.thrd_cnt++;
184        this.done = false;
185}
186
187void main( Acceptor & this ) {
188        park();
189        if( options.log ) sout | "=== Accepting connection ===";
190        for() {
191                int fd = accept4(this.sockfd, this.[addr, addrlen, flags]);
192                if(fd < 0) {
193                        if( errno == EWOULDBLOCK) {
194                                yield();
195                                continue;
196                        }
197                        if( errno == ECONNABORTED ) break;
198                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
199                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
200                }
201                if(this.done) return;
202
203                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
204
205                if(fd) {
206                        PendingRead * p = 0p;
207                        for() {
208                                if(this.done) return;
209                                p = pop(*this.queue);
210                                if(p) break;
211                                yield();
212                        };
213
214                        p->out.fd = fd;
215                        async_recv(p->f, p->out.fd, p->in.buf, p->in.len, 0, CFA_IO_LAZY);
216                }
217
218                if( options.log ) sout | "=== Accepting connection ===";
219        }
220}
Note: See TracBrowser for help on using the repository browser.