Skip to content

Commit

Permalink
Move "unix_time" option handling to OptionManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Dec 16, 2021
1 parent ee629c3 commit d75f0a4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions td/telegram/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "td/telegram/net/MtprotoHeader.h"
#include "td/telegram/net/NetQueryDispatcher.h"
#include "td/telegram/net/TempAuthKeyWatchdog.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/StateManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"

#include "td/actor/PromiseFuture.h"
Expand Down Expand Up @@ -149,7 +149,7 @@ void Global::update_server_time_difference(double diff) {
do_save_server_time_difference();

CHECK(Scheduler::instance());
send_closure(td(), &Td::on_update_server_time_difference);
send_closure(option_manager(), &OptionManager::on_update_server_time_difference);
}
}

Expand Down
25 changes: 22 additions & 3 deletions td/telegram/OptionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
#include "td/utils/misc.h"
#include "td/utils/SliceBuilder.h"

#include <cmath>
#include <limits>

namespace td {

OptionManager::OptionManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
send_unix_time_update();
}

void OptionManager::tear_down() {
Expand All @@ -41,6 +43,23 @@ void OptionManager::tear_down() {

OptionManager::~OptionManager() = default;

td_api::object_ptr<td_api::OptionValue> OptionManager::get_unix_time_option_value_object() {
return td_api::make_object<td_api::optionValueInteger>(G()->unix_time());
}

void OptionManager::send_unix_time_update() {
last_sent_server_time_difference_ = G()->get_server_time_difference();
td_->send_update(td_api::make_object<td_api::updateOption>("unix_time", get_unix_time_option_value_object()));
}

void OptionManager::on_update_server_time_difference() {
if (std::abs(G()->get_server_time_difference() - last_sent_server_time_difference_) < 0.5) {
return;
}

send_unix_time_update();
}

void OptionManager::clear_options() {
for (auto &option : G()->shared_config().get_options()) {
if (!is_internal_option(option.first)) {
Expand Down Expand Up @@ -296,7 +315,7 @@ void OptionManager::get_option(const string &name, Promise<td_api::object_ptr<td
break;
case 'u':
if (name == "unix_time") {
return promise.set_value(td_api::make_object<td_api::optionValueInteger>(G()->unix_time()));
return promise.set_value(get_unix_time_option_value_object());
}
break;
case 'v':
Expand Down Expand Up @@ -608,8 +627,8 @@ void OptionManager::get_current_state(vector<td_api::object_ptr<td_api::Update>>

updates.push_back(td_api::make_object<td_api::updateOption>(
"online", td_api::make_object<td_api::optionValueBoolean>(td_->is_online())));
updates.push_back(td_api::make_object<td_api::updateOption>(
"unix_time", td_api::make_object<td_api::optionValueInteger>(G()->unix_time())));

updates.push_back(td_api::make_object<td_api::updateOption>("unix_time", get_unix_time_option_value_object()));

for (const auto &option : G()->shared_config().get_options()) {
if (!is_internal_option(option.first)) {
Expand Down
8 changes: 8 additions & 0 deletions td/telegram/OptionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class OptionManager final : public Actor {
OptionManager &operator=(OptionManager &&) = delete;
~OptionManager() final;

void on_update_server_time_difference();

void on_option_updated(const string &name);

void get_option(const string &name, Promise<td_api::object_ptr<td_api::OptionValue>> &&promise);
Expand All @@ -43,8 +45,14 @@ class OptionManager final : public Actor {

static bool is_internal_option(Slice name);

static td_api::object_ptr<td_api::OptionValue> get_unix_time_option_value_object();

void send_unix_time_update();

Td *td_;
ActorShared<> parent_;

double last_sent_server_time_difference_ = 1e100;
};

} // namespace td
25 changes: 6 additions & 19 deletions td/telegram/Td.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
#include "td/utils/tl_parsers.h"
#include "td/utils/utf8.h"

#include <cmath>
#include <limits>
#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -2704,17 +2703,6 @@ void Td::on_alarm_timeout_callback(void *td_ptr, int64 alarm_id) {
send_closure_later(td_id, &Td::on_alarm_timeout, alarm_id);
}

void Td::on_update_server_time_difference() {
auto diff = G()->get_server_time_difference();
if (std::abs(diff - last_sent_server_time_difference_) < 0.5) {
return;
}

last_sent_server_time_difference_ = diff;
send_update(td_api::make_object<td_api::updateOption>(
"unix_time", td_api::make_object<td_api::optionValueInteger>(G()->unix_time())));
}

void Td::on_alarm_timeout(int64 alarm_id) {
if (alarm_id == ONLINE_ALARM_ID) {
on_online_updated(false, true);
Expand Down Expand Up @@ -3677,9 +3665,6 @@ Status Td::init(DbKey key) {
VLOG(td_init) << "Successfully inited database";

G()->init(parameters_, actor_id(this), r_td_db.move_as_ok()).ensure();
last_sent_server_time_difference_ = G()->get_server_time_difference();
send_update(td_api::make_object<td_api::updateOption>(
"unix_time", td_api::make_object<td_api::optionValueInteger>(G()->unix_time())));

init_options_and_network();

Expand Down Expand Up @@ -3865,11 +3850,16 @@ void Td::init_options_and_network() {
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());

VLOG(td_init) << "Create OptionManager";
option_manager_ = make_unique<OptionManager>(this, create_reference());
option_manager_actor_ = register_actor("OptionManager", option_manager_.get());
G()->set_option_manager(option_manager_actor_.get());

VLOG(td_init) << "Set ConfigShared callback";
class ConfigSharedCallback final : public ConfigShared::Callback {
public:
void on_option_updated(const string &name, const string &value) const final {
send_closure(G()->option_manager(), &OptionManager::on_option_updated, name);
send_closure_later(G()->option_manager(), &OptionManager::on_option_updated, name);
}
~ConfigSharedCallback() final {
LOG(INFO) << "Destroy ConfigSharedCallback";
Expand Down Expand Up @@ -4019,9 +4009,6 @@ void Td::init_managers() {
notification_manager_ = make_unique<NotificationManager>(this, create_reference());
notification_manager_actor_ = register_actor("NotificationManager", notification_manager_.get());
G()->set_notification_manager(notification_manager_actor_.get());
option_manager_ = make_unique<OptionManager>(this, create_reference());
option_manager_actor_ = register_actor("OptionManager", option_manager_.get());
G()->set_option_manager(option_manager_actor_.get());
poll_manager_ = make_unique<PollManager>(this, create_reference());
poll_manager_actor_ = register_actor("PollManager", poll_manager_.get());
sponsored_message_manager_ = make_unique<SponsoredMessageManager>(this, create_reference());
Expand Down
4 changes: 0 additions & 4 deletions td/telegram/Td.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class Td final : public Actor {

void on_result(NetQueryPtr query);

void on_update_server_time_difference();

void on_online_updated(bool force, bool send_update);

void on_update_status_success(bool is_online);
Expand Down Expand Up @@ -318,8 +316,6 @@ class Td final : public Actor {

TermsOfService pending_terms_of_service_;

double last_sent_server_time_difference_ = 1e100;

struct DownloadInfo {
int32 offset = -1;
int32 limit = -1;
Expand Down

0 comments on commit d75f0a4

Please sign in to comment.