blob: fd988fdbd08c5fd0c8d8e65c2b3ee11613119100 [file] [log] [blame]
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -07001// 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
18using base::Bind;
19using base::Callback;
20using base::StringPrintf;
21using std::string;
22
23namespace shill {
24
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070025namespace Logging {
26static auto kModuleLogScope = ScopeLogger::kPortal;
27static string ObjectID(Connection *c) { return c->interface_name(); }
28}
29
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070030const int ConnectionTester::kTrialTimeoutSeconds = 5;
31
32ConnectionTester::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
47ConnectionTester::~ConnectionTester() {
48 Stop();
49 connectivity_trial_.reset();
50}
51
52void ConnectionTester::Start() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070053 SLOG(connection_, 3) << "In " << __func__;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070054 if (!connectivity_trial_->Start(ConnectivityTrial::kDefaultURL, 0))
55 LOG(ERROR) << StringPrintf("ConnectivityTrial failed to parse default "
56 "URL %s", ConnectivityTrial::kDefaultURL);
57}
58
59void ConnectionTester::Stop() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070060 SLOG(connection_, 3) << "In " << __func__;
Rebecca Silbersteinf4365a62014-09-16 11:40:32 -070061 connectivity_trial_->Stop();
62}
63
64void 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