blob: 69ee4546097c58eed061d1c2c835b1ded168be77 [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
25const int ConnectionTester::kTrialTimeoutSeconds = 5;
26
27ConnectionTester::ConnectionTester(
28 ConnectionRefPtr connection,
29 EventDispatcher *dispatcher,
30 const Callback<void()> &callback)
31 : connection_(connection),
32 dispatcher_(dispatcher),
33 weak_ptr_factory_(this),
34 tester_callback_(callback),
35 connectivity_trial_(
36 new ConnectivityTrial(connection_,
37 dispatcher_,
38 kTrialTimeoutSeconds,
39 Bind(&ConnectionTester::CompleteTest,
40 weak_ptr_factory_.GetWeakPtr()))) { }
41
42ConnectionTester::~ConnectionTester() {
43 Stop();
44 connectivity_trial_.reset();
45}
46
47void ConnectionTester::Start() {
48 SLOG(Portal, 3) << "In " << __func__;
49 if (!connectivity_trial_->Start(ConnectivityTrial::kDefaultURL, 0))
50 LOG(ERROR) << StringPrintf("ConnectivityTrial failed to parse default "
51 "URL %s", ConnectivityTrial::kDefaultURL);
52}
53
54void ConnectionTester::Stop() {
55 SLOG(Portal, 3) << "In " << __func__;
56 connectivity_trial_->Stop();
57}
58
59void ConnectionTester::CompleteTest(ConnectivityTrial::Result result) {
60 LOG(INFO) << StringPrintf("ConnectivityTester completed with phase==%s, "
61 "status==%s",
62 ConnectivityTrial::PhaseToString(
63 result.phase).c_str(),
64 ConnectivityTrial::StatusToString(
65 result.status).c_str());
66 Stop();
67 tester_callback_.Run();
68}
69
70} // namespace shill
71