forked from tjjh89017/ezio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
60 lines (50 loc) · 1.59 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
#include "service.hpp"
EZIOServiceImpl::EZIOServiceImpl(lt::session &tmp) : ses(tmp) {}
Status EZIOServiceImpl::GetTorrentStatus(ServerContext* context, const UpdateRequest* request, UpdateStatus* status)
{
// get status from session
// we ignore request hashes first, always return all
auto hash = request->hashes();
for(auto h : hash){
//std::cout << h << std::endl;
// do some filter for below
}
auto &t_stats = *status->mutable_torrents();
std::stringstream ss;
std::vector<lt::torrent_handle> torrents = ses.get_torrents();
for(lt::torrent_handle const &h : torrents) {
auto hash = status->add_hashes();
ss.str("");
ss.clear();
ss << h.info_hash();
ss >> *hash;
//std::cout << *hash << std::endl;
// disable those info we don't need
lt::torrent_status t_stat = h.status();
//std::cout << t_stat.download_payload_rate << std::endl;
//std::cout << t_stat.upload_payload_rate << std::endl;
//std::cout << t_stat.progress << std::endl;
Torrent t;
t.set_hash(*hash);
t.set_name(t_stat.name);
t.set_progress(t_stat.progress);
t.set_download(t_stat.download_payload_rate);
t.set_upload(t_stat.upload_payload_rate);
t.set_active_time(t_stat.active_time);
t_stats[*hash] = t;
}
return Status::OK;
}
gRPCService::gRPCService(lt::session &tmp, std::string listen_address = "127.0.0.1:50051") :
server_address(listen_address),
service(tmp)
{
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server = builder.BuildAndStart();
}
void gRPCService::stop()
{
server->Shutdown();
}