-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestService.cpp
More file actions
158 lines (136 loc) · 3.23 KB
/
Copy pathRestService.cpp
File metadata and controls
158 lines (136 loc) · 3.23 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
#define _CRT_SECURE_NO_WARNINGS
#include "Test.h"
#include <cpprest\http_listener.h>
#include <cpprest\json.h>
using namespace std;
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
#ifdef SEPARATE
#include <Windows.h>
#include <vector>
using Id_type = long long;
typedef vector<vector<Id_type>>(*FUNA)(Id_type, Id_type);
const char* dllName = "findPath.dll";
const char* funName = "findPath";
FUNA fp = nullptr;
#else
#include "Entity.h"
extern paths_t findPath(Id_type id1, Id_type id2);
#endif
void handleRequest(http_request req)
{
#ifndef COMPETE_TIME
AGGLog("**handling request...");
auto stime = clock();
#ifdef AGG_DEBUG_
//RSLog(L"\n" + req.to_string());
AGGLog(req.absolute_uri().to_string());
#endif // AGG_DEBUG_
#endif // COMPETE_TIME
auto q = uri::split_query(req.relative_uri().query());
Id_type start = stoll(q[U("id1")]);
Id_type end = stoll(q[U("id2")]);
#ifdef SEPARATE
auto path = fp(start, end); // findPath
#else
auto path = findPath(start, end); // findPath
#endif
json::value res;
for (size_t i = 0; i < path.size(); ++i) {
json::value tmp;
for (size_t j = 0; j < path[i].size(); ++j) {
tmp[j] = path[i].at(j);
}
res[i] = tmp;
}
req.reply(status_codes::OK, res);
#ifndef COMPETE_TIME
/*wstring strLog(L"input:");
strLog += to_wstring(start) + L":" + to_wstring(end);
RSLog(strLog);
strLog = L"\n&output json:";
strLog += res.serialize();
RSLog(strLog);*/
auto etime = clock();
AGGLog("**handling time:");
AGGLog(etime - stime);
AGGLog("**handling over.\n");
#endif // !COMPETE_TIME
}
#ifdef AGG_DEBUG_
void handleLog(http_request req)
{
ifstream logfile("REST.log");
if (!logfile) {
req.reply(status_codes::OK);
return;
}
string tmp((istreambuf_iterator<char>(logfile)), istreambuf_iterator<char>());
req.reply(status_codes::OK, tmp);
logfile.close();
}
#endif // AGG_DEBUG_
int main()
{
#ifdef AGG_DEBUG_
http_listener loger(U("http://*/log"));
http_listener cleaner(U("http://*/clean"));
loger.support(handleLog);
cleaner.support([](http_request req) {remove("REST.log"); });
#endif // AGG_DEBUG_
http_listener listener(U("http://*/test"));
listener.support(methods::GET, handleRequest);
#ifdef SEPARATE
HMODULE hDll = LoadLibrary(dllName);
if (hDll == NULL) {
#ifndef COMPETE_TIME
AGGLog("Load Dll Err.");
#endif // COMPETE_TIME
return 1;
}
fp = FUNA(GetProcAddress(hDll, funName));
if (fp == nullptr) {
#ifndef COMPETE_TIME
AGGLog("Load Function Err.");
#endif
FreeLibrary(hDll);
return 2;
}
#endif
try {
#ifdef AGG_DEBUG_
auto threadLog = loger.open();
thread httpLog([&] { threadLog.wait(); });
auto threadClean = cleaner.open();
thread httpClean([&] {threadClean.wait(); });
httpLog.join();
httpClean.join();
#endif // AGG_DEBUG_
listener.open()
#ifndef COMPETE_TIME
.then([=]() {AGGLog("###### start listening..."); })
#endif // !COMPETE_TIME
.get();
while (true);
}
catch (const std::exception& e) {
#ifdef AGG_DEBUG_
AGGLog("@@@@@@Exception: ");
AGGLog(e.what());
#endif // AGG_DEBUG_
#ifdef SEPARATE
FreeLibrary(hDll);
#endif
}
listener.close();
#ifdef SEPARATE
FreeLibrary(hDll);
#endif
#ifdef AGG_DEBUG_
loger.close();
cleaner.close();
system("pause");
#endif // AGG_DEBUG_
return 0;
}