Home | History | Annotate | Download | only in provider
      1 // Copyright 2015 The Weave 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 LIBWEAVE_EXAMPLES_UBUNTU_EVENT_NETWORK_H_
      6 #define LIBWEAVE_EXAMPLES_UBUNTU_EVENT_NETWORK_H_
      7 
      8 #include <weave/provider/network.h>
      9 
     10 #include <base/memory/weak_ptr.h>
     11 
     12 struct evdns_base;
     13 struct bufferevent;
     14 
     15 namespace weave {
     16 namespace examples {
     17 
     18 class EventTaskRunner;
     19 
     20 class EventNetworkImpl : public weave::provider::Network {
     21   class Deleter {
     22    public:
     23     void operator()(evdns_base* dns_base);
     24     void operator()(bufferevent* bev);
     25   };
     26 
     27  public:
     28   explicit EventNetworkImpl(EventTaskRunner* task_runner_);
     29   void AddConnectionChangedCallback(
     30       const ConnectionChangedCallback& callback) override;
     31   State GetConnectionState() const override;
     32   void OpenSslSocket(const std::string& host,
     33                      uint16_t port,
     34                      const OpenSslSocketCallback& callback) override;
     35 
     36   void SetSimulateOffline(bool value) {
     37     simulate_offline_ = value;
     38     UpdateNetworkState();
     39   }
     40 
     41  private:
     42   void UpdateNetworkState();
     43   void UpdateNetworkStateCallback(provider::Network::State state);
     44   bool simulate_offline_{false};
     45   EventTaskRunner* task_runner_{nullptr};
     46   std::unique_ptr<evdns_base, Deleter> dns_base_;
     47   std::vector<ConnectionChangedCallback> callbacks_;
     48   provider::Network::State network_state_{provider::Network::State::kOffline};
     49   std::unique_ptr<bufferevent, Deleter> connectivity_probe_;
     50 
     51   base::WeakPtrFactory<EventNetworkImpl> weak_ptr_factory_{this};
     52 };
     53 
     54 }  // namespace examples
     55 }  // namespace weave
     56 
     57 #endif  // LIBWEAVE_EXAMPLES_UBUNTU_EVENT_NETWORK_H_
     58