-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (134 loc) · 4.35 KB
/
main.cpp
File metadata and controls
152 lines (134 loc) · 4.35 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
#include <iostream>
#include <string>
#include <pistache/common.h>
#include <pistache/cookie.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/http_headers.h>
#include <pistache/net.h>
#include <pistache/peer.h>
#include "nlohmann/json.hpp"
#include "src/Facade/videoservice.h"
using namespace Pistache;
using json = nlohmann::json;
using namespace std;
static VideoService service;
class MyHandler : public Http::Handler
{
HTTP_PROTOTYPE(MyHandler)
void onRequest(
const Http::Request& req,
Http::ResponseWriter response) override
{
if (req.resource() == "/open")
{
if (req.method() == Http::Method::Post)
{
cout << "open post start" <<endl;
using namespace Http;
json body = json::parse(req.body());
std::string url = body["url"];
cout << "url" << url <<endl;
bool res = service.open(url.c_str());
res ? response.send(Http::Code::Ok, "Ok") : response.send(Http::Code::Ok, "Failed");
}
}
else if (req.resource() == "/start")
{
if (req.method() == Http::Method::Post)
{
service.start();
response.send(Http::Code::Ok, req.body(), MIME(Text, Json));
}
}
else if (req.resource() == "/stop")
{
if (req.method() == Http::Method::Post)
{
service.stop();
response.send(Http::Code::Ok, "Ok");
}
}
else if (req.resource() == "/getParamInt")
{
if (req.method() == Http::Method::Post)
{
using namespace Http;
std::string target = req.body().data();
cout << "getParamInt target: " << target;
uint64_t result = service.getParameterInt((ParamInt)stoi(target));
response.send(Http::Code::Ok, to_string(result));
}
}
else if (req.resource() == "/getVideoRGBFrame")
{
if (req.method() == Http::Method::Post)
{
using namespace Http;
auto data = service.getVideoRGBFrame();
if(data){
string rgb = string((*data).first.begin(), (*data).first.end()).append("pts:");
int64_t pts = (*data).second;
rgb.append(to_string(pts));
response.send(Http::Code::Ok, rgb);
} else {
response.send(Http::Code::Ok, "null");
}
}
}
else if (req.resource() == "/getPcmData")
{
if (req.method() == Http::Method::Post)
{
using namespace Http;
auto data = service.getPcmData();
if(data){
string pcm = string((*data).first.begin(), (*data).first.end()).append("pts:");
int64_t pts = (*data).second;
pcm.append(to_string(pts));
response.send(Http::Code::Ok, pcm);
} else {
response.send(Http::Code::Ok, "null");
}
}
}
else if (req.resource() == "/exception")
{
throw std::runtime_error("Exception thrown in the handler");
}
else if (req.resource() == "/timeout")
{
response.timeoutAfter(std::chrono::seconds(2));
}
else
{
response.send(Http::Code::Not_Found);
}
}
void onTimeout(
const Http::Request& /*req*/,
Http::ResponseWriter response) override
{
response
.send(Http::Code::Request_Timeout, "Timeout")
.then([=](ssize_t) {}, PrintException());
}
};
int main(int argc, char* argv[])
{
Port port(9080);
int thr = 2;
if (argc >= 2)
{
port = static_cast<uint16_t>(std::stol(argv[1]));
if (argc == 3)
thr = std::stoi(argv[2]);
}
Address addr(Ipv4::any(), port);
auto server = std::make_shared<Http::Endpoint>(addr);
auto opts = Http::Endpoint::options()
.threads(thr);
server->init(opts);
server->setHandler(Http::make_handler<MyHandler>());
server->serve();
}