source: benchmark/io/http/main.cfa @ 0aec496

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 0aec496 was 0aec496, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

First attempt at webserver, no option support yet

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[0aec496]1#define __USE_GNU
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7extern "C" {
8        #include <sys/socket.h>
9        #include <sys/types.h>
10        #include <netinet/in.h>
11}
12
13#include <kernel.hfa>
14#include <stats.hfa>
15#include <thread.hfa>
16
17#include "channel.hfa"
18#include "filecache.hfa"
19#include "options.hfa"
20#include "worker.hfa"
21
22//=============================================================================================
23// Globals
24//=============================================================================================
25Options options @= {
26        0,
27        42u,
28        0,
29        false,
30        false,
31        0
32};
33
34channel & wait_connect;
35
36struct ServerProc {
37        processor self;
38};
39
40void ?{}( ServerProc & this ) {
41        /* paranoid */ assert( options.the_cluster != 0p );
42        (this.self){ "Benchmark Processor", *options.the_cluster };
43
44        #if !defined(__CFA_NO_STATISTICS__)
45                if( options.procstats ) {
46                        print_stats_at_exit( this.self, options.the_cluster->print_stats );
47                }
48                if( options.viewhalts ) {
49                        print_halts( this.self );
50                }
51        #endif
52}
53
54//=============================================================================================
55// Main
56//============================================================================================='
57int main( int argc, char * argv[] ) {
58        int port      = 8080;
59        int ret       = 0;
60        int backlog   = 10;
61        int nprocs    = 1;
62        int nworkers  = 1;
63        int cl_flags  = 0;
64        int chan_size = 10;
65        const char * path = ".";
66        //===================
67        // Parse args
68
69        //===================
70        // Open Files
71        printf("Filling cache from %s\n", path);
72        fill_cache( path );
73
74        //===================
75        // Open Socket
76        printf("Listening on port %d\n", port);
77        int server_fd = socket(AF_INET, SOCK_STREAM, 0);
78        if(server_fd < 0) {
79                abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
80        }
81
82        struct sockaddr_in address;
83        int addrlen = sizeof(address);
84        memset( (char *)&address, '\0' );
85        address.sin_family = AF_INET;
86        address.sin_addr.s_addr = htonl(INADDR_ANY);
87        address.sin_port = htons( port );
88
89        ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
90        if(ret < 0) {
91                abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
92        }
93
94        ret = listen( server_fd, backlog );
95        if(ret < 0) {
96                abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
97        }
98
99        //===================
100        // Run Server Cluster
101        {
102                cluster cl = { "Server Cluster", cl_flags };
103                #if !defined(__CFA_NO_STATISTICS__)
104                        print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
105                #endif
106                options.the_cluster = &cl;
107
108                channel chan = { chan_size };
109                &wait_connect = &chan;
110
111                {
112                        ServerProc procs[nprocs];
113                        {
114                                Worker workers[nworkers];
115                                printf("%d workers started on %d processors\n", nworkers, nprocs);
116                                {
117                                        Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 };
118                                }
119                                printf("Shutting Down\n");
120
121                                // Clean-up the workers
122                                for(nworkers) {
123                                        put( wait_connect, -1 );
124                                }
125                        }
126                }
127        }
128
129        //===================
130        // Close Socket
131        printf("Closing Socket\n");
132        ret = close( server_fd );
133        if(ret < 0) {
134                abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
135        }
136
137        //===================
138        // Close Files
139        printf("Closing Files\n");
140        close_cache();
141}
Note: See TracBrowser for help on using the repository browser.