-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
161 lines (131 loc) · 4.42 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
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
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/config.hpp>
#include <iostream>
#include <map>
#include <vector>
#include <utility>
#include <memory>
#include <thread>
#include <stdexcept>
#include "ortools/base/init_google.h"
#include "sorter.h"
/* Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
* With modifications by Arnav Rawat
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
template <class Body, class Allocator>
http::message_generator
handle_request(http::request<Body, http::basic_fields<Allocator>>&& req) {
auto const bad_request = [&req](beast::string_view why) {
http::response<http::string_body> res{http::status::bad_request, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = std::string(why);
res.prepare_payload();
return res;
};
if(req.method() != http::verb::post || req.target() != "/schedule")
return bad_request("POST /schedule only");
http::string_body::value_type body;
try {
Sorter sort(req.body());
auto ret = sort.matchToSchedule();
if (ret == std::nullopt) {
body = "{}";
} else {
boost::json::object obj;
for (auto& [id, vec] : ret.value()) {
std::vector<boost::json::object> transformed;
transformed.reserve(vec.size());
std::transform(vec.begin(), vec.end(), std::back_inserter(transformed), [](std::tuple<int, int, int> tup) {
boost::json::object ret;
ret["id"] = std::get<0>(tup);
ret["start"] = std::get<1>(tup);
ret["end"] = std::get<2>(tup);
return ret;
});
obj[std::to_string(id)] = boost::json::value_from(transformed);
}
body = boost::json::serialize(obj);
}
} catch (std::exception e) {
return bad_request("Invalid JSON");
}
auto const size = body.size();
http::response<http::string_body> res {
std::piecewise_construct,
std::make_tuple(std::move(body)),
std::make_tuple(http::status::ok, req.version())
};
res.set(http::field::server, "lol");
res.set(http::field::content_type, "application/json");
res.content_length(size);
res.keep_alive(false);
return res;
}
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
void
do_session(tcp::socket& socket)
{
beast::error_code ec;
beast::flat_buffer buffer;
for(;;) {
http::request<http::string_body> req;
http::read(socket, buffer, req, ec);
if(ec == http::error::end_of_stream)
break;
if(ec)
return fail(ec, "read");
http::message_generator msg =
handle_request(std::move(req));
bool keep_alive = msg.keep_alive();
beast::write(socket, std::move(msg), ec);
if(ec)
return fail(ec, "write");
if(!keep_alive)
break;
}
socket.shutdown(tcp::socket::shutdown_send, ec);
}
int main(int argc, char* argv[])
{
InitGoogle(argv[0], &argc, &argv, true);
try {
if (argc != 3) {
std::cerr <<
"Usage: sched_converter <address> <port>\n" <<
"Example:\n" <<
"\tsched_converter 0.0.0.0 8080 .\n";
return EXIT_FAILURE;
}
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
net::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
for(;;) {
tcp::socket socket{ioc};
acceptor.accept(socket);
std::thread { std::bind(
&do_session,
std::move(socket)
)}.detach();
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}