Chromium Code Reviews| Index: chromeos/dbus/packet_sender.cc |
| diff --git a/chromeos/dbus/packet_sender.cc b/chromeos/dbus/packet_sender.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9ee43a8dc8815ad89d7f38cab37a598c7edf1345 |
| --- /dev/null |
| +++ b/chromeos/dbus/packet_sender.cc |
| @@ -0,0 +1,62 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "packet_sender.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/chromeos/chromeos_version.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/strings/string_number_conversions.h" |
| + |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/debug_daemon_client.h" |
| +#include "chromeos/dbus/permission_broker_client.h" |
| + |
| +using base::Value; |
| + |
| +namespace chromeos { |
| + |
| +PacketSender::PacketSender() {} |
| + |
| +PacketSender::~PacketSender() {} |
| + |
| +PacketSender* PacketSender::GetInstance() { |
| + return Singleton<PacketSender>::get(); |
| +} |
| + |
| +void PacketSender::Send(std::string ip, std::string type, int ttl, int timeout, |
|
xiaowenx
2013/06/17 05:26:41
Use const std::string&?
Bei Zhang
2013/06/17 07:30:54
Done.
|
| + int size, const SendCallback& callback) { |
| + chromeos::DebugDaemonClient* debugd_client = |
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| + std::map<std::string, std::string> config; |
| + config["ttl"] = base::IntToString(ttl); |
| + config["timeout"] = base::IntToString(timeout); |
| + config["size"] = base::IntToString(size); |
| + debugd_client->TestICMPWithOptions(ip, config, |
| + base::Bind(&PacketSender::OnSend, |
| + base::Unretained(this), |
| + callback)); |
| +} |
| + |
| +void PacketSender::OnSend(const SendCallback& callback, |
| + bool succeeded, const std::string& status) { |
| + if (succeeded) { |
| + // Parse the result an gives ip and latency |
|
xiaowenx
2013/06/17 05:26:41
"an gives ip and latency" => "and return IP and la
Bei Zhang
2013/06/17 07:30:54
Done.
|
| + scoped_ptr<Value> parsed_value(base::JSONReader::Read(status)); |
| + base::DictionaryValue* result; |
| + std::string ip; |
| + int latency; |
| + |
| + if (parsed_value->GetAsDictionary(&result) && |
|
xiaowenx
2013/06/17 05:26:41
Need to check whether parsed_value is NULL
Bei Zhang
2013/06/17 07:30:54
Done.
|
| + result->GetString("ip", &ip) && result->GetInteger("ttl", &latency)) { |
|
xiaowenx
2013/06/17 05:26:41
It returns something like: { "<ip>" : { ... "time
Bei Zhang
2013/06/17 07:30:54
Done.
|
| + callback.Run(true, ip, latency); |
| + return; |
| + } |
| + } |
| + |
| + callback.Run(false, "", 0); |
| +} |
| + |
| +} // namespace chromeos |