• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_PASSIVE_LINK_MONITOR_H_
18 #define SHILL_PASSIVE_LINK_MONITOR_H_
19 
20 #include <base/callback.h>
21 #include <base/cancelable_callback.h>
22 
23 #include "shill/refptr_types.h"
24 
25 namespace shill {
26 
27 class ArpClient;
28 class EventDispatcher;
29 class IOHandler;
30 
31 // PassiveLinkMonitor tracks the status of a connection by monitoring ARP
32 // requests received on the given interface. Each cycle consist of 25 seconds,
33 // with at lease 5 ARP requests expected in a cycle, a callback indicating
34 // failure will be invoke if that expectation is not met. Caller can specify
35 // number of cycles to monitor, once that number is reached without any
36 // failures, a callback indicating success will be invoked. Monitor will
37 // automatically stop when the monitor results in either failure or success.
38 class PassiveLinkMonitor {
39  public:
40   typedef base::Callback<void(bool)> ResultCallback;
41 
42   // The default number of cycles to monitor for.
43   static const int kDefaultMonitorCycles;
44 
45   PassiveLinkMonitor(const ConnectionRefPtr& connection,
46                      EventDispatcher* dispatcher,
47                      const ResultCallback& result_callback);
48   virtual ~PassiveLinkMonitor();
49 
50   // Starts passive link-monitoring for the specified number of cycles.
51   virtual bool Start(int num_cycles);
52   // Stop passive link-monitoring. Clears any accumulated statistics.
53   virtual void Stop();
54 
55  private:
56   friend class PassiveLinkMonitorTest;
57 
58   // The number of milliseconds per cycle.
59   static const int kCyclePeriodMilliseconds;
60 
61   // Minimum number of ARP requests expected per cycle.
62   static const int kMinArpRequestsPerCycle;
63 
64   bool StartArpClient();
65   void StopArpClient();
66 
67   // Callback to be invoked whenever the ARP reception socket has data
68   // available to be received.
69   void ReceiveRequest(int fd);
70   // Callback to be invoked when cycle period is reached without receiving
71   // the expected number of ARP requests.
72   void CycleTimeoutHandler();
73   // Method to be called when the monitor is completed.
74   void MonitorCompleted(bool status);
75 
76   // The connection on which to perform passive link monitoring.
77   ConnectionRefPtr connection_;
78   // The dispatcher on which to create delayed tasks.
79   EventDispatcher* dispatcher_;
80   // ArpClient instance for monitoring ARP requests.
81   std::unique_ptr<ArpClient> arp_client_;
82   // Callback to be invoked when monitor is completed, either failure or
83   // success.
84   ResultCallback result_callback_;
85 
86   // Number of cycles to monitor for.
87   int num_cycles_to_monitor_;
88   // Number of ARP requests received in current cycle.
89   int num_requests_received_;
90   // Number of cycles passed so far.
91   int num_cycles_passed_;
92 
93   // IOCallback that fires when the socket associated with our ArpClient
94   // has a packet to be received.  Calls ReceiveRequest().
95   std::unique_ptr<IOHandler> receive_request_handler_;
96   // Callback for handling cycle timeout.
97   base::CancelableClosure monitor_cycle_timeout_callback_;
98   // Callback for handling monitor completed event.
99   base::CancelableClosure monitor_completed_callback_;
100 
101   DISALLOW_COPY_AND_ASSIGN(PassiveLinkMonitor);
102 };
103 
104 }  // namespace shill
105 
106 #endif  // SHILL_PASSIVE_LINK_MONITOR_H_
107