Skip to content

Commit aa4acf7

Browse files
committed
Implement tonlib::EngineConsoleClient
1 parent d2d9e93 commit aa4acf7

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

tonlib/tonlib/EngineConsoleClient.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include "adnl/adnl-ext-client.h"
2+
#include "auto/tl/ton_api.hpp"
3+
14
#include "EngineConsoleClient.h"
25

36
namespace tonlib {
@@ -11,13 +14,68 @@ bool is_engine_console_query(ton::tl_object_ptr<ton::ton_api::Function> const& f
1114
}
1215
}
1316

14-
EngineConsoleClient::EngineConsoleClient(td::IPAddress address, ton::PublicKey public_key,
15-
ton::PrivateKey private_key) {
17+
class EngineConsoleClientCallback : public ton::adnl::AdnlExtClient::Callback {
18+
public:
19+
EngineConsoleClientCallback(td::actor::ActorId<EngineConsoleClient> id) : id_(std::move(id)) {
20+
}
21+
22+
void on_ready() override {
23+
td::actor::send_closure(id_, &EngineConsoleClient::on_ready);
24+
}
25+
26+
void on_stop_ready() override {
27+
td::actor::send_closure(id_, &EngineConsoleClient::on_stop_ready);
28+
}
29+
30+
private:
31+
td::actor::ActorId<EngineConsoleClient> id_;
32+
};
33+
34+
EngineConsoleClient::EngineConsoleClient(td::IPAddress address, ton::PublicKey public_key, ton::PrivateKey private_key)
35+
: address_(address), public_key_(std::move(public_key)), private_key_(std::move(private_key)) {
36+
}
37+
38+
void EngineConsoleClient::on_ready() {
39+
ready_ = true;
40+
for (auto& promise : pending_ready_promises_) {
41+
promise.set_value(td::Unit());
42+
}
43+
pending_ready_promises_.clear();
44+
}
45+
46+
void EngineConsoleClient::on_stop_ready() {
47+
for (auto& promise : pending_ready_promises_) {
48+
promise.set_error(td::Status::Error("Connection closed"));
49+
}
50+
pending_ready_promises_.clear();
51+
ready_ = false;
52+
client_ = {};
1653
}
1754

1855
td::actor::Task<ton::tl_object_ptr<ton::ton_api::Object>> EngineConsoleClient::query(
1956
ton::tl_object_ptr<ton::ton_api::Function> object) {
20-
co_return td::Status::Error("Not implemented");
57+
if (!ready_) {
58+
if (client_.empty()) {
59+
client_ = ton::adnl::AdnlExtClient::create(ton::adnl::AdnlNodeIdFull{public_key_}, private_key_, address_,
60+
std::make_unique<EngineConsoleClientCallback>(actor_id(this)));
61+
}
62+
63+
auto [ready_awaiter, ready_promise] = td::actor::StartedTask<td::Unit>::make_bridge();
64+
pending_ready_promises_.push_back(std::move(ready_promise));
65+
co_await std::move(ready_awaiter);
66+
}
67+
68+
auto query_bytes = ton::serialize_tl_object(object, true);
69+
auto wrapped_query = ton::serialize_tl_object(
70+
ton::create_tl_object<ton::ton_api::engine_validator_controlQuery>(std::move(query_bytes)), true);
71+
72+
auto [response_awaiter, response_promise] = td::actor::StartedTask<td::BufferSlice>::make_bridge();
73+
td::actor::send_closure(client_, &ton::adnl::AdnlExtClient::send_query, "query", std::move(wrapped_query),
74+
td::Timestamp::in(10.0), std::move(response_promise));
75+
auto response = co_await std::move(response_awaiter);
76+
77+
auto result_obj = co_await ton::fetch_tl_object<ton::ton_api::Object>(response, true);
78+
co_return std::move(result_obj);
2179
}
2280

2381
} // namespace tonlib

tonlib/tonlib/EngineConsoleClient.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "adnl/adnl-ext-client.h"
34
#include "keys/keys.hpp"
45
#include "td/actor/actor.h"
56
#include "td/actor/coro_task.h"
@@ -13,7 +14,18 @@ class EngineConsoleClient : public td::actor::Actor {
1314
public:
1415
EngineConsoleClient(td::IPAddress address, ton::PublicKey public_key, ton::PrivateKey private_key);
1516

17+
void on_ready();
18+
void on_stop_ready();
19+
1620
td::actor::Task<ton::tl_object_ptr<ton::ton_api::Object>> query(ton::tl_object_ptr<ton::ton_api::Function> function);
21+
22+
private:
23+
td::IPAddress address_;
24+
ton::PublicKey public_key_;
25+
ton::PrivateKey private_key_;
26+
td::actor::ActorOwn<ton::adnl::AdnlExtClient> client_;
27+
bool ready_ = false;
28+
std::vector<td::Promise<td::Unit>> pending_ready_promises_;
1729
};
1830

1931
} // namespace tonlib

0 commit comments

Comments
 (0)