forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeActor.h
146 lines (113 loc) · 3.91 KB
/
NodeActor.h
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
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#pragma once
#include "LoadSpeed.h"
#include "PartsHelper.h"
#include "PeerActor.h"
#include "Torrent.h"
#include "td/utils/Random.h"
#include <map>
namespace ton {
class NodeActor : public td::actor::Actor {
public:
class Callback {
public:
virtual ~Callback() {
}
virtual td::actor::ActorOwn<PeerActor> create_peer(PeerId self_id, PeerId peer_id,
td::SharedState<PeerState> state) = 0;
virtual void get_peers(td::Promise<std::vector<PeerId>> peers) = 0;
virtual void register_self(td::actor::ActorId<ton::NodeActor> self) = 0;
//TODO: proper callbacks
virtual void on_completed() = 0;
virtual void on_closed(ton::Torrent torrent) = 0;
};
NodeActor(PeerId self_id, ton::Torrent torrent, td::unique_ptr<Callback> callback, bool should_download = true);
void start_peer(PeerId peer_id, td::Promise<td::actor::ActorId<PeerActor>> promise);
ton::Torrent *with_torrent() {
return &torrent_;
}
std::string get_stats_str();
void set_file_priority(size_t i, td::uint8 priority);
void set_should_download(bool should_download);
private:
PeerId self_id_;
ton::Torrent torrent_;
std::vector<td::uint8> file_priority_;
td::unique_ptr<Callback> callback_;
bool should_download_{false};
class Notifier : public td::actor::Actor {
public:
Notifier(td::actor::ActorId<NodeActor> node, PeerId peer_id) : node_(std::move(node)), peer_id_(peer_id) {
}
void wake_up() override {
send_closure(node_, &NodeActor::on_signal_from_peer, peer_id_);
}
private:
td::actor::ActorId<NodeActor> node_;
PeerId peer_id_;
};
struct Peer {
td::actor::ActorOwn<PeerActor> actor;
td::actor::ActorOwn<Notifier> notifier;
td::SharedState<PeerState> state;
PartsHelper::PeerToken peer_token;
};
std::map<PeerId, Peer> peers_;
struct QueryId {
PeerId peer;
PartId part;
auto key() const {
return std::tie(peer, part);
}
bool operator<(const QueryId &other) const {
return key() < other.key();
}
};
struct PartsSet {
struct Info {
td::optional<PeerId> query_to_peer;
bool ready{false};
};
size_t total_queries{0};
std::vector<Info> parts;
};
PartsSet parts_;
PartsHelper parts_helper_;
std::vector<PartId> ready_parts_;
LoadSpeed download_;
td::Timestamp next_get_peers_at_;
bool has_get_peers_{false};
static constexpr double GET_PEER_RETRY_TIMEOUT = 5;
static constexpr double GET_PEER_EACH = 5;
bool is_completed_{false};
td::Timestamp will_upload_at_;
void on_signal_from_peer(PeerId peer_id);
void start_up() override;
void loop() override;
void tear_down() override;
void loop_start_stop_peers();
static constexpr size_t MAX_TOTAL_QUERIES = 20;
static constexpr size_t MAX_PEER_TOTAL_QUERIES = 5;
void loop_queries();
bool try_send_query();
bool try_send_part(PartId part_id);
void loop_get_peers();
void got_peers(td::Result<std::vector<PeerId>> r_peers);
void loop_peer(const PeerId &peer_id, Peer &peer);
void on_part_ready(PartId part_id);
void loop_will_upload();
};
} // namespace ton