forked from SNodeC/snode.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (80 loc) · 3 KB
/
main.cpp
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
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <list>
#include <functional>
#include <map>
#include <string.h>
#include "TimerManager.h"
#include "ServerSocket.h"
#include "SocketManager.h"
#include "SocketMultiplexer.h"
#include "FileReader.h"
#include "Request.h"
#include "Response.h"
#include "ContinousTimer.h"
#include "SingleshotTimer.h"
int main(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
ServerSocket* serverSocket = ServerSocket::instance(8080);
serverSocket->get([] (Request& req, Response& res) -> void {
std::map<std::string, std::string>& header = req.header();
std::map<std::string, std::string>::iterator it;
for (it = header.begin(); it != header.end(); ++it) {
std::cout << (*it).first << ": " << (*it).second << std::endl;
}
/*
if (req.bodySize() > 0) {
const char* body = req.body();
int bodySize = req.bodySize();
char* str = new char[bodySize + 1];
memcpy(str, body, bodySize);
str[bodySize] = 0;
std::cout << str << std::endl;
res.send(str);
} else {
std::string document;
document = "<!DOCTYPE html>";
document += "<html><body>";
document += "<h1>My First Heading</h1>";
document += "<p>My first paragraph.</p>";
document += "</body></html>";
res.send(document);
} */
res.header();
FileReader::read("./index.html",
[&] (unsigned char* data, int length) -> void {
std::cout << "From FileReader" << std::endl;
if (length > 0) {
res.send((char*) data, length);
} else {
res.end();
}
},
[] (int err) -> void {
std::cout << "Error: " << strerror(err) << std::endl;
});
});
Timer& timer1 = Timer::continousTimer(
[] (void* arg) -> void {
std::cout << "Tick: " << (char*) arg << std::endl;
},
(struct timeval) {1, 0},
(void *) "Test 1"
);
Timer& timer2 = Timer::singleshotTimer(
[&timer1] (void* arg) -> void {
std::cout << "Tack: " << (char*) arg << std::endl;
Timer::cancel(&timer1);
},
(struct timeval) {1, 500000},
(void *) "Test 2"
);
SocketMultiplexer& sm = SocketMultiplexer::instance();
sm.getReadManager().manageSocket(serverSocket);
while(1) {
sm.tick();
}
return 0;
}