Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1 | // Copyright (c) 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 | #include "shill/connection_tester.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/bind.h> |
| 10 | #include <base/strings/string_util.h> |
| 11 | #include <base/strings/stringprintf.h> |
| 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | |
| 14 | #include "shill/connection.h" |
| 15 | #include "shill/connectivity_trial.h" |
| 16 | #include "shill/logging.h" |
| 17 | |
| 18 | using base::Bind; |
| 19 | using base::Callback; |
| 20 | using base::StringPrintf; |
| 21 | using std::string; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 25 | namespace Logging { |
| 26 | static auto kModuleLogScope = ScopeLogger::kPortal; |
| 27 | static string ObjectID(Connection *c) { return c->interface_name(); } |
| 28 | } |
| 29 | |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 30 | const int ConnectionTester::kTrialTimeoutSeconds = 5; |
| 31 | |
| 32 | ConnectionTester::ConnectionTester( |
| 33 | ConnectionRefPtr connection, |
| 34 | EventDispatcher *dispatcher, |
| 35 | const Callback<void()> &callback) |
| 36 | : connection_(connection), |
| 37 | dispatcher_(dispatcher), |
| 38 | weak_ptr_factory_(this), |
| 39 | tester_callback_(callback), |
| 40 | connectivity_trial_( |
| 41 | new ConnectivityTrial(connection_, |
| 42 | dispatcher_, |
| 43 | kTrialTimeoutSeconds, |
| 44 | Bind(&ConnectionTester::CompleteTest, |
| 45 | weak_ptr_factory_.GetWeakPtr()))) { } |
| 46 | |
| 47 | ConnectionTester::~ConnectionTester() { |
| 48 | Stop(); |
| 49 | connectivity_trial_.reset(); |
| 50 | } |
| 51 | |
| 52 | void ConnectionTester::Start() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 53 | SLOG(connection_, 3) << "In " << __func__; |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 54 | if (!connectivity_trial_->Start(ConnectivityTrial::kDefaultURL, 0)) |
| 55 | LOG(ERROR) << StringPrintf("ConnectivityTrial failed to parse default " |
| 56 | "URL %s", ConnectivityTrial::kDefaultURL); |
| 57 | } |
| 58 | |
| 59 | void ConnectionTester::Stop() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 60 | SLOG(connection_, 3) << "In " << __func__; |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 61 | connectivity_trial_->Stop(); |
| 62 | } |
| 63 | |
| 64 | void ConnectionTester::CompleteTest(ConnectivityTrial::Result result) { |
| 65 | LOG(INFO) << StringPrintf("ConnectivityTester completed with phase==%s, " |
| 66 | "status==%s", |
| 67 | ConnectivityTrial::PhaseToString( |
| 68 | result.phase).c_str(), |
| 69 | ConnectivityTrial::StatusToString( |
| 70 | result.status).c_str()); |
| 71 | Stop(); |
| 72 | tester_callback_.Run(); |
| 73 | } |
| 74 | |
| 75 | } // namespace shill |
| 76 | |