buffet: Allow configurable polling period While here, switch to using a repeating timer, which is cleaner. BUG=brillo:564,brillo:522 TEST=buffet_Registration passes. Can touch the polling period config flag and see that polling period changes. Change-Id: I8d0cca5d38e8ba91dad1a97311cc20603e300654 Reviewed-on: https://chromium-review.googlesource.com/260920 Tested-by: Christopher Wiley <wiley@chromium.org> Reviewed-by: Alex Vakulenko <avakulenko@chromium.org> Reviewed-by: Vitaly Buka <vitalybuka@chromium.org> Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/buffet/buffet_config.cc b/buffet/buffet_config.cc index 36acee9..e4f6209 100644 --- a/buffet/buffet_config.cc +++ b/buffet/buffet_config.cc
@@ -4,6 +4,9 @@ #include "buffet/buffet_config.h" +#include <base/logging.h> +#include <base/strings/string_number_conversions.h> + namespace buffet { namespace config_keys { @@ -18,6 +21,7 @@ const char kDefaultDescription[] = "default_description"; const char kDefaultLocation[] = "default_location"; const char kModelId[] = "model_id"; +const char kPollingPeriodMs[] = "polling_period_ms"; } // namespace config_keys @@ -40,6 +44,13 @@ store.GetString(config_keys::kDefaultDescription, &default_description_); store.GetString(config_keys::kDefaultLocation, &default_location_); store.GetString(config_keys::kModelId, &model_id_); + std::string polling_period_str; + if (store.GetString(config_keys::kPollingPeriodMs, &polling_period_str)) { + CHECK(base::StringToUint64(polling_period_str, &polling_period_ms_)) + << "Failed to parse polling period in configuration file."; + VLOG(1) << "Using a polling period of " << polling_period_ms_ + << " milliseconds."; + } } } // namespace buffet
diff --git a/buffet/buffet_config.h b/buffet/buffet_config.h index 539191e..550f0da 100644 --- a/buffet/buffet_config.h +++ b/buffet/buffet_config.h
@@ -30,6 +30,7 @@ std::string default_description() const { return default_description_; } std::string default_location() const { return default_location_; } std::string model_id() const { return model_id_; } + uint64_t polling_period_ms() const { return polling_period_ms_; } private: std::string client_id_{"58855907228.apps.googleusercontent.com"}; @@ -43,6 +44,7 @@ std::string default_description_{"A development device"}; std::string default_location_{"my desk"}; std::string model_id_{"AAA"}; + uint64_t polling_period_ms_{7000}; DISALLOW_COPY_AND_ASSIGN(BuffetConfig); };
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc index 3f7e17c..b16e626 100644 --- a/buffet/device_registration_info.cc +++ b/buffet/device_registration_info.cc
@@ -569,14 +569,6 @@ FROM_HERE, base::Bind(cb, base::Owned(value.release()))); } -void PostRepeatingTask(const tracked_objects::Location& from_here, - base::Closure task, - base::TimeDelta delay) { - task.Run(); - base::MessageLoop::current()->PostDelayedTask( - from_here, base::Bind(&PostRepeatingTask, from_here, task, delay), delay); -} - using ResponsePtr = scoped_ptr<chromeos::http::Response>; void SendRequestWithRetries( @@ -840,23 +832,22 @@ void DeviceRegistrationInfo::PeriodicallyPollCommands() { VLOG(1) << "Poll commands"; - PostRepeatingTask( + command_poll_timer_.Start( FROM_HERE, - base::Bind( - &DeviceRegistrationInfo::FetchCommands, - base::Unretained(this), - base::Bind(&DeviceRegistrationInfo::PublishCommands, - base::Unretained(this)), - base::Bind(&IgnoreCloudError)), - base::TimeDelta::FromSeconds(7)); + base::TimeDelta::FromMilliseconds(config_->polling_period_ms()), + base::Bind(&DeviceRegistrationInfo::FetchCommands, + base::Unretained(this), + base::Bind(&DeviceRegistrationInfo::PublishCommands, + base::Unretained(this)), + base::Bind(&IgnoreCloudError))); // TODO(antonm): Use better trigger: when StateManager registers new updates, // it should call closure which will post a task, probably with some // throttling, to publish state updates. - PostRepeatingTask( + state_push_timer_.Start( FROM_HERE, + base::TimeDelta::FromMilliseconds(config_->polling_period_ms()), base::Bind(&DeviceRegistrationInfo::PublishStateUpdates, - base::Unretained(this)), - base::TimeDelta::FromSeconds(7)); + base::Unretained(this))); } void DeviceRegistrationInfo::PublishCommands(const base::ListValue& commands) {
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h index 0eec540..7f5e2c6 100644 --- a/buffet/device_registration_info.h +++ b/buffet/device_registration_info.h
@@ -15,6 +15,7 @@ #include <base/memory/weak_ptr.h> #include <base/message_loop/message_loop.h> #include <base/time/time.h> +#include <base/timer/timer.h> #include <chromeos/data_encoding.h> #include <chromeos/errors/error.h> #include <chromeos/http/http_transport.h> @@ -240,6 +241,9 @@ RegistrationStatus registration_status_{RegistrationStatus::kUnconfigured}; base::Closure on_status_changed_; + base::RepeatingTimer<DeviceRegistrationInfo> command_poll_timer_; + base::RepeatingTimer<DeviceRegistrationInfo> state_push_timer_; + friend class TestHelper; base::WeakPtrFactory<DeviceRegistrationInfo> weak_factory_{this}; DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);