-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (160 loc) · 6.13 KB
/
Copy pathmain.cpp
File metadata and controls
175 lines (160 loc) · 6.13 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
175
#include <errno.h>
#include <sys/epoll.h>
#include "Server.hpp"
#include "Client.hpp"
#include "Http.hpp"
#include "Dns.hpp"
#include "Backend.hpp"
#include "Cache.hpp"
#include "Timer.hpp"
#include "Timer/DNSCache.hpp"
#include "Timer/DNSQuery.hpp"
#include <vector>
#include <cstring>
#include <cstdio>
#include <signal.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX_EVENTS 1024
void signal_callback_handler(int signum) {
printf("Caught signal SIGPIPE %d\n",signum);
}
int main(int argc, char *argv[])
{
/* Catch Signal Handler SIGPIPE */
if(signal(SIGPIPE, signal_callback_handler)==SIG_ERR)
{
std::cerr << "signal(SIGPIPE, signal_callback_handler)==SIG_ERR, errno: " << std::to_string(errno) << std::endl;
abort();
}
mkdir("cache", S_IRWXU);
std::vector <std::string> sources;
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--flatcache") {
Cache::hostsubfolder=false;
}/* else if (std::string(argv[i]) == "--maxiumSizeToBeSmallFile") {
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
std::string maxiumSizeToBeSmallFile = argv[i++]; // Increment 'i' so we don't get the argument as the next argv[i].
} else { // Uh-oh, there was no argument to the destination option.
std::cerr << "--maxiumSizeToBeSmallFile option requires one argument." << std::endl;
return 1;
}
} else if (std::string(argv[i]) == "--maxiumSmallFileCacheSize") {
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
std::string maxiumSmallFileCacheSize = argv[i++]; // Increment 'i' so we don't get the argument as the next argv[i].
} else { // Uh-oh, there was no argument to the destination option.
std::cerr << "--memorycachemap option requires one argument." << std::endl;
return 1;
}
}*/ else if (std::string(argv[i]) == "--help") {
std::cerr << "--flatcache: use flat cache, the host cache folder is not created" << std::endl;
//std::cerr << "--maxiumSizeToBeSmallFile: (TODO) if smaller than this size, save into memory" << std::endl;
//std::cerr << "--maxiumSmallFileCacheSize: (TODO) The maximum content stored in memory, this cache prevent syscall and disk seek" << std::endl;
return 1;
} else {
sources.push_back(argv[i]);
}
}
(void)argc;
(void)argv;
//the event loop
struct epoll_event ev, events[MAX_EVENTS];
memset(&ev,0,sizeof(ev));
int nfds, epollfd;
Http::fdRandom=open("/dev/urandom",O_RDONLY);
ev.events = EPOLLIN|EPOLLET;
if ((epollfd = epoll_create1(0)) == -1) {
printf("epoll_create1: %s", strerror(errno));
return -1;
}
EpollObject::epollfd=epollfd;
Dns::dns=new Dns();
DNSCache dnsCache;
dnsCache.start(3600*1000);
DNSQuery dnsQuery;
dnsQuery.start(10);
/* cachePath (content header, 64Bits aligned):
* 64Bits: access time
* 64Bits: last modification time check
* 64Bits: modification time */
/*Server *server=*///new Server("/run/fastcgicdn.sock");
Server s("fastcgicdn.sock");
(void)s;
std::vector<Client *> newDeleteClient,oldDeleteClient;
std::vector<Backend *> newDeleteBackend,oldDeleteBackend;
for (;;) {
if ((nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1)) == -1)
printf("epoll_wait error %s", strerror(errno));
for(Client * client : oldDeleteClient)
delete client;
for(Backend * b : oldDeleteBackend)
delete b;
oldDeleteClient=newDeleteClient;
newDeleteClient.clear();
oldDeleteBackend=newDeleteBackend;
newDeleteBackend.clear();
for (int n = 0; n < nfds; ++n)
{
epoll_event &e=events[n];
switch(static_cast<EpollObject *>(e.data.ptr)->getKind())
{
case EpollObject::Kind::Kind_Server:
{
Server * server=static_cast<Server *>(e.data.ptr);
server->parseEvent(e);
}
break;
case EpollObject::Kind::Kind_Client:
{
Client * client=static_cast<Client *>(e.data.ptr);
if(!(e.events & EPOLLHUP))
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << std::endl;
#endif
/*client->disconnect();
newDeleteClient.push_back(client);*/
}
else
client->parseEvent(e);
if(!client->isValid())
{
//if(!deleteClient.empty() && deleteClient.back()!=client)
newDeleteClient.push_back(client);
client->disconnect();
}
}
break;
case EpollObject::Kind::Kind_Backend:
{
Backend * backend=static_cast<Backend *>(e.data.ptr);
backend->parseEvent(e);
/*if(!http->toRemove.empty())
newDeleteHttp.insert(newDeleteHttp.end(),http->toRemove.cbegin(),http->toRemove.cend());*/
if(!backend->isValid())
newDeleteBackend.push_back(backend);
}
break;
case EpollObject::Kind::Kind_Dns:
{
Dns * dns=static_cast<Dns *>(e.data.ptr);
dns->parseEvent(e);
}
break;
case EpollObject::Kind::Kind_Timer:
{
static_cast<Timer *>(e.data.ptr)->exec();
static_cast<Timer *>(e.data.ptr)->validateTheTimer();
}
break;
default:
break;
}
}
}
return 0;
}