Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 1 | // Copyright 2015 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_PASSIVE_LINK_MONITOR_H_ |
| 6 | #define SHILL_PASSIVE_LINK_MONITOR_H_ |
| 7 | |
| 8 | #include <base/callback.h> |
| 9 | #include <base/cancelable_callback.h> |
| 10 | |
| 11 | #include "shill/refptr_types.h" |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | class ArpClient; |
| 16 | class EventDispatcher; |
| 17 | class IOHandler; |
| 18 | |
| 19 | // PassiveLinkMonitor tracks the status of a connection by monitoring ARP |
| 20 | // requests received on the given interface. Each cycle consist of 25 seconds, |
| 21 | // with at lease 5 ARP requests expected in a cycle, a callback indicating |
| 22 | // failure will be invoke if that expectation is not met. Caller can specify |
| 23 | // number of cycles to monitor, once that number is reached without any |
| 24 | // failures, a callback indicating success will be invoked. Monitor will |
| 25 | // automatically stop when the monitor results in either failure or success. |
| 26 | class PassiveLinkMonitor { |
| 27 | public: |
| 28 | typedef base::Callback<void(bool)> ResultCallback; |
| 29 | |
| 30 | // The default number of cycles to monitor for. |
| 31 | static const int kDefaultMonitorCycles; |
| 32 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | PassiveLinkMonitor(const ConnectionRefPtr& connection, |
| 34 | EventDispatcher* dispatcher, |
| 35 | const ResultCallback& result_callback); |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 36 | virtual ~PassiveLinkMonitor(); |
| 37 | |
| 38 | // Starts passive link-monitoring for the specified number of cycles. |
| 39 | virtual bool Start(int num_cycles); |
| 40 | // Stop passive link-monitoring. Clears any accumulated statistics. |
| 41 | virtual void Stop(); |
| 42 | |
| 43 | private: |
| 44 | friend class PassiveLinkMonitorTest; |
| 45 | |
| 46 | // The number of milliseconds per cycle. |
| 47 | static const int kCyclePeriodMilliseconds; |
| 48 | |
| 49 | // Minimum number of ARP requests expected per cycle. |
| 50 | static const int kMinArpRequestsPerCycle; |
| 51 | |
| 52 | bool StartArpClient(); |
| 53 | void StopArpClient(); |
| 54 | |
| 55 | // Callback to be invoked whenever the ARP reception socket has data |
| 56 | // available to be received. |
| 57 | void ReceiveRequest(int fd); |
| 58 | // Callback to be invoked when cycle period is reached without receiving |
| 59 | // the expected number of ARP requests. |
| 60 | void CycleTimeoutHandler(); |
| 61 | // Method to be called when the monitor is completed. |
| 62 | void MonitorCompleted(bool status); |
| 63 | |
| 64 | // The connection on which to perform passive link monitoring. |
| 65 | ConnectionRefPtr connection_; |
| 66 | // The dispatcher on which to create delayed tasks. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 67 | EventDispatcher* dispatcher_; |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 68 | // ArpClient instance for monitoring ARP requests. |
| 69 | std::unique_ptr<ArpClient> arp_client_; |
| 70 | // Callback to be invoked when monitor is completed, either failure or |
| 71 | // success. |
| 72 | ResultCallback result_callback_; |
| 73 | |
| 74 | // Number of cycles to monitor for. |
| 75 | int num_cycles_to_monitor_; |
| 76 | // Number of ARP requests received in current cycle. |
| 77 | int num_requests_received_; |
| 78 | // Number of cycles passed so far. |
| 79 | int num_cycles_passed_; |
| 80 | |
| 81 | // IOCallback that fires when the socket associated with our ArpClient |
| 82 | // has a packet to be received. Calls ReceiveRequest(). |
| 83 | std::unique_ptr<IOHandler> receive_request_handler_; |
| 84 | // Callback for handling cycle timeout. |
| 85 | base::CancelableClosure monitor_cycle_timeout_callback_; |
Peter Qiu | bf767f2 | 2015-03-10 16:55:19 -0700 | [diff] [blame] | 86 | // Callback for handling monitor completed event. |
| 87 | base::CancelableClosure monitor_completed_callback_; |
Peter Qiu | fcc00ee | 2015-01-16 15:38:45 -0800 | [diff] [blame] | 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(PassiveLinkMonitor); |
| 90 | }; |
| 91 | |
| 92 | } // namespace shill |
| 93 | |
| 94 | #endif // SHILL_PASSIVE_LINK_MONITOR_H_ |