Johan Euphrosine | ee020ee | 2015-10-12 12:49:16 -0700 | [diff] [blame] | 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 | |
Vitaly Buka | 3d8feaf | 2015-10-28 13:10:53 -0700 | [diff] [blame] | 36 | void SetSimulateOffline(bool value) { |
| 37 | simulate_offline_ = value; |
| 38 | UpdateNetworkState(); |
| 39 | } |
| 40 | |
Johan Euphrosine | ee020ee | 2015-10-12 12:49:16 -0700 | [diff] [blame] | 41 | private: |
| 42 | void UpdateNetworkState(); |
| 43 | void UpdateNetworkStateCallback(provider::Network::State state); |
Vitaly Buka | 3d8feaf | 2015-10-28 13:10:53 -0700 | [diff] [blame] | 44 | bool simulate_offline_{false}; |
Johan Euphrosine | ee020ee | 2015-10-12 12:49:16 -0700 | [diff] [blame] | 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}; |
Vitaly Buka | 3d8feaf | 2015-10-28 13:10:53 -0700 | [diff] [blame] | 49 | std::unique_ptr<bufferevent, Deleter> connectivity_probe_; |
| 50 | |
Johan Euphrosine | ee020ee | 2015-10-12 12:49:16 -0700 | [diff] [blame] | 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_ |