-
Notifications
You must be signed in to change notification settings - Fork 21
/
service.cpp
132 lines (108 loc) · 3.33 KB
/
service.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
#include "service.hpp"
#include <spdlog/spdlog.h>
#include "daemon.hpp"
namespace ezio
{
gRPCService::gRPCService(ezio &daemon) :
daemon_(daemon)
{
}
void gRPCService::start(std::string listen_address)
{
ServerBuilder builder;
builder.AddListeningPort(listen_address, grpc::InsecureServerCredentials());
// set recv unlimit
builder.SetMaxReceiveMessageSize(-1);
builder.RegisterService(this);
server_ = builder.BuildAndStart();
}
void gRPCService::stop()
{
server_->Shutdown(std::chrono::system_clock::now() + std::chrono::seconds(10));
}
void gRPCService::wait()
{
server_->Wait();
}
Status gRPCService::Shutdown(ServerContext *context, const Empty *e1,
Empty *e2)
{
SPDLOG_INFO("shutdown");
daemon_.stop();
return Status::OK;
}
Status gRPCService::GetTorrentStatus(ServerContext *context,
const UpdateRequest *request,
UpdateStatus *response)
{
SPDLOG_DEBUG("GetTorrentStatus request: {}", request->DebugString());
std::vector<std::string> hashes(request->hashes().begin(), request->hashes().end());
auto result = daemon_.get_torrent_status(hashes);
for (const auto &iter : result) {
const auto &hash = iter.first;
const auto &t_stat = iter.second;
response->add_hashes(hash);
Torrent t;
t.set_hash(hash);
t.set_name(t_stat.name);
t.set_progress(t_stat.progress);
t.set_download_rate(t_stat.download_rate);
t.set_upload_rate(t_stat.upload_rate);
t.set_is_finished(t_stat.is_finished);
t.set_active_time(t_stat.active_time);
t.set_num_peers(t_stat.num_peers);
t.set_state(t_stat.state);
t.set_total_done(t_stat.total_done);
t.set_total(t_stat.total);
t.set_num_pieces(t_stat.num_pieces);
t.set_finished_time(t_stat.finished_time);
t.set_seeding_time(t_stat.seeding_time);
t.set_total_payload_download(t_stat.total_payload_download);
t.set_total_payload_upload(t_stat.total_payload_upload);
t.set_is_paused(t_stat.is_paused);
t.set_save_path(t_stat.save_path);
t.set_last_upload(t_stat.last_upload);
t.set_last_download(t_stat.last_download);
response->mutable_torrents()->insert({hash, t});
}
return Status::OK;
}
Status gRPCService::AddTorrent(ServerContext *context,
const AddRequest *request,
AddResponse *response)
{
SPDLOG_INFO("AddTorrent");
try {
daemon_.add_torrent(request->torrent(), request->save_path(), request->seeding_mode(), request->max_uploads(), request->max_connections(), request->sequential_download());
} catch (const std::exception &e) {
return Status(grpc::StatusCode::UNAVAILABLE, e.what());
}
return Status::OK;
}
Status gRPCService::PauseTorrent(ServerContext *context, const PauseTorrentRequest *request, PauseTorrentResponse *response)
{
SPDLOG_INFO("PauseTorrent");
try {
daemon_.pause_torrent(request->hash());
} catch (const std::exception &e) {
return Status(grpc::StatusCode::UNAVAILABLE, e.what());
}
return Status::OK;
}
Status gRPCService::ResumeTorrent(ServerContext *context, const ResumeTorrentRequest *request, ResumeTorrentResponse *response)
{
SPDLOG_INFO("ResumeTorrent");
try {
daemon_.resume_torrent(request->hash());
} catch (const std::exception &e) {
return Status(grpc::StatusCode::UNAVAILABLE, e.what());
}
return Status::OK;
}
Status gRPCService::GetVersion(ServerContext *context, const Empty *e, VersionResponse *response)
{
SPDLOG_INFO("GetVersion");
response->set_version(GIT_VERSION);
return Status::OK;
}
} // namespace ezio