-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.cpp
More file actions
103 lines (84 loc) · 2.07 KB
/
final.cpp
File metadata and controls
103 lines (84 loc) · 2.07 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
#include <iostream>
#include "final.h"
#include "server.h"
#ifdef _WIN32
#include "daemonize.h"
#else
#include "ccan_daemon_with_notify_daemon_with_notify.h"
#include <unistd.h> // sleep, dbg
#endif
// -h <ip> -p <port> -d <directory>
int main(int argc, char **argv)
{
std::string sIp = "127.0.0.1";
std::string sPort = "1415";
#ifdef _WIN32
std::string sDir = "d:\\srv";
for(int nArg = 1; nArg < argc; nArg += 2 )
{
if( nArg == argc - 1 )
break; // even check
if( strcmp(argv[nArg], "-h") == 0 )
sIp = argv[nArg + 1];
else if( strcmp(argv[nArg], "-p") == 0 )
sPort = argv[nArg + 1];
else if( strcmp(argv[nArg], "-d") == 0 )
sDir = argv[nArg + 1];
}
#else
std::string sDir = "/tmp";
int c;
while((c = getopt(argc, argv, "d:h:p:")) != -1) {
switch(c)
{
case 'd':
sDir = optarg;
break;
case 'p':
sPort = optarg;
break;
case 'h':
sIp = optarg;
break;
}
}
#endif
if( sDir.length() == 0 ) { std::cout << "Wrong path parameter" << std::endl; return -1; }
// remove trailing slash if any
std::string::iterator it = sDir.end() - 1;
if( *it == '/' || *it == '\\' )
sDir.erase(it);
std::cout << "Server parameters :" << std::endl << " host = " << sIp << ":" << sPort << std::endl << " dir = " << sDir << std::endl;
// run as background process
// NOTE not implemented for windows
int nDaemonizeRes = daemonize(1, 1, 1); // fork
if( nDaemonizeRes < 0 )
{
std::cout << "Can't daemonize, err = " << nDaemonizeRes << std::endl;
return -1;
}
else
std::cout << "Daemonized." << std::endl;
daemon_is_ready(); // tell parent to exit
HttpSrv srv;
int nInitRes = srv.init(sIp, std::stoi(sPort), sDir);
if( nInitRes < 0 )
{
std::cout << "Can't start server, err = " << nInitRes << std::endl;
return -1;
}
std::cout << "Server started." << std::endl;
while( 1 )
{
int nAcceptRes = srv.doAccept();
if( nAcceptRes < 0 )
{
std::cout << "Srv doAccept() fatal error, err = " << nAcceptRes << std::endl;
break;
}
}
std::cout << "Stopping server...";
srv.deinit();
std::cout << " done." << std::endl;
return 0;
}