Fix initialization order.

GitOrigin-RevId: d27f3c18063093345436c55e3d4959297b7c234f
This commit is contained in:
levlam 2019-01-17 23:12:31 +03:00
parent 33657d521a
commit f633d754c7
5 changed files with 73 additions and 35 deletions

View File

@ -13,12 +13,20 @@
namespace td {
ConfigShared::ConfigShared(std::shared_ptr<KeyValueSyncInterface> config_pmc, unique_ptr<Callback> callback)
: config_pmc_(std::move(config_pmc)), callback_(std::move(callback)) {
ConfigShared::ConfigShared(std::shared_ptr<KeyValueSyncInterface> config_pmc) : config_pmc_(std::move(config_pmc)) {
}
void ConfigShared::set_callback(unique_ptr<Callback> callback) {
callback_ = std::move(callback);
if (callback_ == nullptr) {
return;
}
for (auto key_value : config_pmc_->get_all()) {
on_option_updated(key_value.first);
}
}
void ConfigShared::set_option_boolean(Slice name, bool value) {
if (set_option(name, value ? Slice("Btrue") : Slice("Bfalse"))) {
on_option_updated(name);
@ -134,7 +142,9 @@ tl_object_ptr<td_api::OptionValue> ConfigShared::get_option_value_object(Slice v
}
void ConfigShared::on_option_updated(Slice name) const {
callback_->on_option_updated(name.str(), get_option(name));
if (callback_ != nullptr) {
callback_->on_option_updated(name.str(), get_option(name));
}
}
} // namespace td

View File

@ -25,11 +25,15 @@ class ConfigShared {
Callback() = default;
Callback(const Callback &) = delete;
Callback &operator=(const Callback &) = delete;
Callback(Callback &&) = delete;
Callback &operator=(Callback &&) = delete;
virtual ~Callback() = default;
virtual void on_option_updated(const string &name, const string &value) const = 0;
};
ConfigShared(std::shared_ptr<KeyValueSyncInterface> config_pmc, unique_ptr<Callback> callback);
explicit ConfigShared(std::shared_ptr<KeyValueSyncInterface> config_pmc);
void set_callback(unique_ptr<Callback> callback);
void set_option_boolean(Slice name, bool value);
void set_option_empty(Slice name);

View File

@ -93,6 +93,7 @@ class Global : public ActorContext {
void set_net_query_dispatcher(unique_ptr<NetQueryDispatcher> net_query_dispatcher);
NetQueryDispatcher &net_query_dispatcher() {
CHECK(have_net_query_dispatcher());
return *net_query_dispatcher_;
}

View File

@ -3943,6 +3943,30 @@ Status Td::init(DbKey key) {
G()->set_state_manager(state_manager_.get());
connection_state_ = StateManager::State::Empty;
VLOG(td_init) << "Create ConfigShared";
G()->set_shared_config(td::make_unique<ConfigShared>(G()->td_db()->get_config_pmc_shared()));
if (G()->shared_config().have_option("language_database_path")) {
G()->shared_config().set_option_string("language_pack_database_path",
G()->shared_config().get_option_string("language_database_path"));
G()->shared_config().set_option_empty("language_database_path");
}
if (G()->shared_config().have_option("language_pack")) {
G()->shared_config().set_option_string("localization_target",
G()->shared_config().get_option_string("language_pack"));
G()->shared_config().set_option_empty("language_pack");
}
if (G()->shared_config().have_option("language_code")) {
G()->shared_config().set_option_string("language_pack_id", G()->shared_config().get_option_string("language_code"));
G()->shared_config().set_option_empty("language_code");
}
if (!G()->shared_config().have_option("message_text_length_max")) {
G()->shared_config().set_option_integer("message_text_length_max", 4096);
}
if (!G()->shared_config().have_option("message_caption_length_max")) {
G()->shared_config().set_option_integer("message_caption_length_max", 200);
}
VLOG(td_init) << "Create ConnectionCreator";
{
auto connection_creator = create_actor<ConnectionCreator>("ConnectionCreator", create_reference());
@ -3973,7 +3997,6 @@ Status Td::init(DbKey key) {
case td_api::removeProxy::ID:
case td_api::getProxies::ID:
case td_api::getProxyLink::ID:
case td_api::pingProxy::ID:
return true;
default:
return false;
@ -3985,36 +4008,28 @@ Status Td::init(DbKey key) {
auto temp_auth_key_watchdog = create_actor<TempAuthKeyWatchdog>("TempAuthKeyWatchdog");
G()->set_temp_auth_key_watchdog(std::move(temp_auth_key_watchdog));
VLOG(td_init) << "Create ConfigManager and ConfigShared";
VLOG(td_init) << "Create ConfigManager";
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());
VLOG(td_init) << "Set ConfigShared callback";
class ConfigSharedCallback : public ConfigShared::Callback {
public:
void on_option_updated(const string &name, const string &value) const override {
send_closure(G()->td(), &Td::on_config_option_updated, name);
}
~ConfigSharedCallback() override {
LOG(INFO) << "Destroy ConfigShared";
LOG(INFO) << "Destroy ConfigSharedCallback";
}
};
G()->set_shared_config(
td::make_unique<ConfigShared>(G()->td_db()->get_config_pmc_shared(), make_unique<ConfigSharedCallback>()));
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());
if (G()->shared_config().have_option("language_database_path")) {
G()->shared_config().set_option_string("language_pack_database_path",
G()->shared_config().get_option_string("language_database_path"));
G()->shared_config().set_option_empty("language_database_path");
}
if (G()->shared_config().have_option("language_pack")) {
G()->shared_config().set_option_string("localization_target",
G()->shared_config().get_option_string("language_pack"));
G()->shared_config().set_option_empty("language_pack");
}
if (G()->shared_config().have_option("language_code")) {
G()->shared_config().set_option_string("language_pack_id", G()->shared_config().get_option_string("language_code"));
G()->shared_config().set_option_empty("language_code");
}
// we need to set ConfigShared callback before td_api::getOption requests are processed for consistency
// TODO currently they will be inconsistent anyway, because td_api::getOption returns current value,
// but in td_api::updateOption there will be a newer value, obtained at the time of update creation
// so, there can be even two succesive updateOption with the same value
// we need to process td_api::getOption along with td_api::setOption for consistency
// we need to process td_api::setOption before managers and MTProto header are created,
// because their initialiation may be affected by the options
G()->shared_config().set_callback(make_unique<ConfigSharedCallback>());
complete_pending_preauthentication_requests([](int32 id) {
switch (id) {
@ -4032,17 +4047,15 @@ Status Td::init(DbKey key) {
// options_.proxy = Proxy();
G()->set_mtproto_header(make_unique<MtprotoHeader>(options_));
if (!G()->shared_config().have_option("message_text_length_max")) {
G()->shared_config().set_option_integer("message_text_length_max", 4096);
}
if (!G()->shared_config().have_option("message_caption_length_max")) {
G()->shared_config().set_option_integer("message_caption_length_max", 200);
}
VLOG(td_init) << "Create NetQueryDispatcher";
auto net_query_dispatcher = make_unique<NetQueryDispatcher>([&] { return create_reference(); });
G()->set_net_query_dispatcher(std::move(net_query_dispatcher));
complete_pending_preauthentication_requests([](int32 id) {
// pingProxy uses NetQueryDispatcher to get main_dc_id, so must be called after NetQueryDispatcher is created
return id == td_api::pingProxy::ID;
});
VLOG(td_init) << "Create AuthManager";
auth_manager_ = td::make_unique<AuthManager>(parameters_.api_id, parameters_.api_hash, create_reference());
auth_manager_actor_ = register_actor("AuthManager", auth_manager_.get());
@ -4203,6 +4216,8 @@ Status Td::init(DbKey key) {
complete_pending_preauthentication_requests([](int32 id) { return true; });
VLOG(td_init) << "Finish initialization";
state_ = State::Run;
return Status::OK();
}

View File

@ -594,7 +594,7 @@ class CliClient final : public Actor {
// LOG(INFO) << "on_result [" << generation << "][id=" << id << "] " << as_json_str;
if (generation != generation_) {
LOG(INFO) << "Drop unneded " << to_string(result);
LOG(INFO) << "Drop received from previous Client " << to_string(result);
return;
}
@ -801,6 +801,8 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::getOption>("use_pfs"));
send_request(td_api::make_object<td_api::setOption>(
"use_pfs", td_api::make_object<td_api::optionValueBoolean>(std::time(nullptr) / 86400 % 2 == 0)));
send_request(td_api::make_object<td_api::setOption>("notification_group_count_max",
td_api::make_object<td_api::optionValueInteger>(1)));
send_request(td_api::make_object<td_api::setOption>("use_storage_optimizer",
td_api::make_object<td_api::optionValueBoolean>(false)));
send_request(
@ -809,6 +811,11 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::setNetworkType>(td_api::make_object<td_api::networkTypeWiFi>()));
send_request(td_api::make_object<td_api::getNetworkStatistics>());
send_request(td_api::make_object<td_api::getCountryCode>());
send_request(
td_api::make_object<td_api::addProxy>("1.1.1.1", 1111, true, td_api::make_object<td_api::proxyTypeSocks5>()));
send_request(td_api::make_object<td_api::addProxy>("1.1.1.1", 1112, false,
td_api::make_object<td_api::proxyTypeSocks5>()));
send_request(td_api::make_object<td_api::pingProxy>(0));
auto bad_parameters = td_api::make_object<td_api::tdlibParameters>();
bad_parameters->database_directory_ = "/..";
@ -3812,6 +3819,7 @@ void main(int argc, char **argv) {
dump_memory_usage();
}
} // namespace td
int main(int argc, char **argv) {