Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This is a sample daemon that "handles" Buffet commands. |
| 6 | // It just prints the information about the command received to stdout and |
| 7 | // marks the command as processed. |
| 8 | |
| 9 | #include <string> |
| 10 | #include <sysexits.h> |
| 11 | |
| 12 | #include <base/bind.h> |
| 13 | #include <base/command_line.h> |
| 14 | #include <base/format_macros.h> |
| 15 | #include <chromeos/daemons/dbus_daemon.h> |
| 16 | #include <chromeos/map_utils.h> |
| 17 | #include <chromeos/strings/string_utils.h> |
| 18 | #include <chromeos/syslog_logging.h> |
| 19 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 20 | #include "buffet/dbus-proxies.h" |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 21 | |
| 22 | class Daemon : public chromeos::DBusDaemon { |
| 23 | public: |
| 24 | Daemon() = default; |
| 25 | |
| 26 | protected: |
| 27 | int OnInit() override; |
| 28 | void OnShutdown(int* return_code) override; |
| 29 | |
| 30 | private: |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 31 | std::unique_ptr<org::chromium::Buffet::ObjectManagerProxy> object_manager_; |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 32 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 33 | void OnBuffetCommand(org::chromium::Buffet::CommandProxy* command); |
| 34 | void OnBuffetCommandRemoved(const dbus::ObjectPath& object_path); |
| 35 | void OnPropertyChange(org::chromium::Buffet::CommandProxy* command, |
| 36 | const std::string& property_name); |
| 37 | void OnCommandProgress(org::chromium::Buffet::CommandProxy* command, |
| 38 | int progress); |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 41 | }; |
| 42 | |
| 43 | int Daemon::OnInit() { |
| 44 | int return_code = chromeos::DBusDaemon::OnInit(); |
| 45 | if (return_code != EX_OK) |
| 46 | return return_code; |
| 47 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 48 | object_manager_.reset(new org::chromium::Buffet::ObjectManagerProxy{bus_}); |
| 49 | object_manager_->SetCommandAddedCallback( |
| 50 | base::Bind(&Daemon::OnBuffetCommand, base::Unretained(this))); |
| 51 | object_manager_->SetCommandRemovedCallback( |
| 52 | base::Bind(&Daemon::OnBuffetCommandRemoved, base::Unretained(this))); |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 53 | |
| 54 | printf("Waiting for commands...\n"); |
| 55 | return EX_OK; |
| 56 | } |
| 57 | |
| 58 | void Daemon::OnShutdown(int* return_code) { |
| 59 | printf("Shutting down...\n"); |
| 60 | } |
| 61 | |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 62 | void Daemon::OnPropertyChange(org::chromium::Buffet::CommandProxy* command, |
| 63 | const std::string& property_name) { |
| 64 | printf("Notification: property '%s' on command '%s' changed.\n", |
| 65 | property_name.c_str(), command->id().c_str()); |
| 66 | printf(" Current command status: '%s' (%d)\n", |
| 67 | command->status().c_str(), command->progress()); |
| 68 | } |
| 69 | |
| 70 | void Daemon::OnBuffetCommand(org::chromium::Buffet::CommandProxy* command) { |
| 71 | command->SetPropertyChangedCallback(base::Bind(&Daemon::OnPropertyChange, |
| 72 | base::Unretained(this))); |
| 73 | printf("++++++++++++++++++++++++++++++++++++++++++++++++\n"); |
| 74 | printf("Command received: %s\n", command->name().c_str()); |
| 75 | printf("DBus Object Path: %s\n", command->GetObjectPath().value().c_str()); |
| 76 | printf(" category: %s\n", command->category().c_str()); |
| 77 | printf(" ID: %s\n", command->id().c_str()); |
| 78 | printf(" status: %s\n", command->status().c_str()); |
| 79 | printf(" # of parameters: %" PRIuS "\n", command->parameters().size()); |
Alex Vakulenko | 3755abe | 2014-12-15 11:02:56 -0800 | [diff] [blame] | 80 | auto keys = chromeos::GetMapKeysAsVector(command->parameters()); |
| 81 | std::string param_names = chromeos::string_utils::Join(", ", keys); |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 82 | printf(" parameter names: %s\n", param_names.c_str()); |
Alex Vakulenko | 420e49f | 2014-12-01 17:53:27 -0800 | [diff] [blame] | 83 | OnCommandProgress(command, 0); |
| 84 | } |
| 85 | |
| 86 | void Daemon::OnCommandProgress(org::chromium::Buffet::CommandProxy* command, |
| 87 | int progress) { |
| 88 | if (progress >= 100) { |
| 89 | command->Done(nullptr); |
| 90 | } else { |
| 91 | printf("Updating command '%s' progress to %d%%\n", |
| 92 | command->id().c_str(), progress); |
| 93 | command->SetProgress(progress, nullptr); |
| 94 | progress += 10; |
| 95 | base::MessageLoop::current()->PostDelayedTask( |
| 96 | FROM_HERE, |
| 97 | base::Bind(&Daemon::OnCommandProgress, |
| 98 | base::Unretained(this), |
| 99 | command, |
| 100 | progress), |
| 101 | base::TimeDelta::FromSeconds(1)); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Daemon::OnBuffetCommandRemoved(const dbus::ObjectPath& object_path) { |
| 106 | printf("------------------------------------------------\n"); |
| 107 | printf("Command removed\n"); |
| 108 | printf("DBus Object Path: %s\n", object_path.value().c_str()); |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int main(int argc, char* argv[]) { |
| 112 | CommandLine::Init(argc, argv); |
| 113 | chromeos::InitLog(chromeos::kLogToSyslog | |
| 114 | chromeos::kLogToStderr | |
| 115 | chromeos::kLogHeader); |
| 116 | Daemon daemon; |
| 117 | return daemon.Run(); |
| 118 | } |