-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphController.cpp
More file actions
103 lines (100 loc) · 3.75 KB
/
Copy pathGraphController.cpp
File metadata and controls
103 lines (100 loc) · 3.75 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 "GraphController.h"
#include <string>
using std::string;
GraphController::GraphController(const string& address, const string& port, GraphService* graphService, EdmondsKarpService* edmondsKarpService, PushRelabelService* pushRelabelService) : BasicController(address, port)
{
this->graphService = graphService;
this->edmondsKarpService = edmondsKarpService;
this->pushRelabelService = pushRelabelService;
}
GraphController::~GraphController()
{
delete graphService;
delete edmondsKarpService;
delete pushRelabelService;
}
void GraphController::initRestOpHandlers()
{
listener.support(methods::GET, std::bind(&GraphController::handleGet, this, std::placeholders::_1));
}
void GraphController::handleGet(http_request message)
{
vector<utility::string_t> path = requestPath(message);
if (path.empty())
{
message.reply(status_codes::BadRequest);
}
else
{
if (path[0] == to_string_t("graph"))
{
auto queries = requestQuery(message);
int id = stoi(queries[to_string_t("id")]);
auto graph = graphService->getGraph(id);
if (graph == nullptr)
{
message.reply(status_codes::NotFound);
return;
}
json::value graphJson;
graphJson[to_string_t("graph")] = json::value::string(graph->toString());
message.reply(status_codes::OK, graphJson);
delete graph;
}
else if (path[0] == to_string_t("directedGraph"))
{
auto queries = requestQuery(message);
int id = stoi(queries[to_string_t("id")]);
auto directedGraph = graphService->getDirectedGraph(id);
if (directedGraph == nullptr)
{
message.reply(status_codes::NotFound);
return;
}
json::value graphJson;
graphJson[to_string_t("directepGraph")] = json::value::string(directedGraph->toString());
message.reply(status_codes::OK, graphJson);
delete directedGraph;
}
else if(path[0] == to_string_t("edmondsKarpMaxGraphFlow"))
{
auto queries = requestQuery(message);
int id = stoi(queries[to_string_t("id")]);
int source = stoi(queries[to_string_t("source")]);
int destination = stoi(queries[to_string_t("destination")]);
auto graph = graphService->getGraph(id);
if (graph == nullptr)
{
message.reply(status_codes::NotFound);
return;
}
auto result = edmondsKarpService->calculateMaxFlow(graph, source, destination);
json::value resultJson;
resultJson[to_string_t("result")] = json::value::number(result);
message.reply(status_codes::OK, resultJson);
delete graph;
}
else if (path[0] == to_string_t("pushRelabelMaxGraphFlow"))
{
auto queries = requestQuery(message);
int id = stoi(queries[to_string_t("id")]);
int source = stoi(queries[to_string_t("source")]);
int destination = stoi(queries[to_string_t("destination")]);
auto graph = graphService->getDirectedGraph(id);
if (graph == nullptr)
{
message.reply(status_codes::NotFound);
return;
}
auto result = pushRelabelService->calculateMaxFlow(graph, source, destination);
json::value resultJson;
resultJson[to_string_t("result")] = json::value::number(result);
message.reply(status_codes::OK, resultJson);
delete graph;
}
else
{
message.reply(status_codes::BadRequest);
}
}
}