active_passive_out_of_credits_detector.h revision c54afe521739065a5d77e7c049acdb5e603f0592
1// Copyright (c) 2013 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#ifndef SHILL_CELLULAR_ACTIVE_PASSIVE_OUT_OF_CREDITS_DETECTOR_H_
6#define SHILL_CELLULAR_ACTIVE_PASSIVE_OUT_OF_CREDITS_DETECTOR_H_
7
8#include <memory>
9#include <string>
10
11#include <base/time/time.h>
12
13#include "shill/cellular/out_of_credits_detector.h"
14#include "shill/connection_health_checker.h"
15
16namespace shill {
17
18// Detects out-of-credits condition by monitoring for the following scenarios:
19//   - Passively watch for network congestion and launch active probes to
20//     determine if the network has stopped routing traffic.
21//   - Watch for connect/disconnect loop.
22class ActivePassiveOutOfCreditsDetector : public OutOfCreditsDetector {
23 public:
24  ActivePassiveOutOfCreditsDetector(EventDispatcher *dispatcher,
25                                    Manager *manager,
26                                    Metrics *metrics,
27                                    CellularService *service);
28  ~ActivePassiveOutOfCreditsDetector() override;
29
30  void ResetDetector() override;
31  bool IsDetecting() const override;
32  void NotifyServiceStateChanged(
33      Service::ConnectState old_state,
34      Service::ConnectState new_state) override;
35  void NotifySubscriptionStateChanged(uint32_t subscription_state) override {}
36
37  const TrafficMonitor *traffic_monitor() const {
38    return traffic_monitor_.get();
39  }
40
41  const std::string &GetServiceRpcIdentifier() const {
42    return service_rpc_identifier_;
43  }
44
45 private:
46  friend class ActivePassiveOutOfCreditsDetectorTest;
47  FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest,
48      ConnectDisconnectLoopDetectionIntermittentNetwork);
49  FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest,
50      ConnectDisconnectLoopDetectionNotSkippedAfterSlowResume);
51  FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest,
52      OnConnectionHealthCheckerResult);
53  FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest, OnNoNetworkRouting);
54  FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest, StopTrafficMonitor);
55
56  static const int64_t kOutOfCreditsConnectionDropSeconds;
57  static const int kOutOfCreditsMaxConnectAttempts;
58  static const int64_t kOutOfCreditsResumeIgnoreSeconds;
59
60  // Initiates traffic monitoring.
61  bool StartTrafficMonitor();
62
63  // Stops traffic monitoring.
64  void StopTrafficMonitor();
65
66  // Responds to a TrafficMonitor no-network-routing failure.
67  void OnNoNetworkRouting(int reason);
68
69  // Initializes and configures the connection health checker.
70  void SetupConnectionHealthChecker();
71
72  // Checks the network connectivity status by creating a TCP connection, and
73  // optionally sending a small amout of data.
74  void RequestConnectionHealthCheck();
75
76  // Responds to the result from connection health checker in a device specific
77  // manner.
78  void OnConnectionHealthCheckerResult(ConnectionHealthChecker::Result result);
79
80  // Performs out-of-credits detection by checking to see if we're stuck in a
81  // connect/disconnect loop.
82  void DetectConnectDisconnectLoop(Service::ConnectState curr_state,
83                                   Service::ConnectState new_state);
84  // Reconnects to the cellular service in the context of out-of-credits
85  // detection.
86  void OutOfCreditsReconnect();
87
88  // Ownership of |traffic_monitor| is taken.
89  void set_traffic_monitor(TrafficMonitor *traffic_monitor);
90
91  // Ownership of |healther_checker| is taken.
92  void set_connection_health_checker(ConnectionHealthChecker *health_checker);
93
94  base::WeakPtrFactory<ActivePassiveOutOfCreditsDetector> weak_ptr_factory_;
95
96  // Passively monitors network traffic for network failures.
97  std::unique_ptr<TrafficMonitor> traffic_monitor_;
98  // Determine network health through active probes.
99  std::unique_ptr<ConnectionHealthChecker> health_checker_;
100
101  // The following members are used by the connect/disconnect loop detection.
102  // Time when the last connect request started.
103  base::Time connect_start_time_;
104  // Number of connect attempts.
105  int num_connect_attempts_;
106  // Flag indicating whether out-of-credits detection is in progress.
107  bool out_of_credits_detection_in_progress_;
108
109  // String to hold service identifier for scoped logging.
110  std::string service_rpc_identifier_;
111
112  DISALLOW_COPY_AND_ASSIGN(ActivePassiveOutOfCreditsDetector);
113};
114
115}  // namespace shill
116
117#endif  // SHILL_CELLULAR_ACTIVE_PASSIVE_OUT_OF_CREDITS_DETECTOR_H_
118