blob: 3aeac24348f9cc73102b6c238593b7840beeec6d [file] [log] [blame]
Johan Euphrosineee020ee2015-10-12 12:49:16 -07001// 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
12struct evdns_base;
13struct bufferevent;
14
15namespace weave {
16namespace examples {
17
18class EventTaskRunner;
19
20class 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 Buka3d8feaf2015-10-28 13:10:53 -070036 void SetSimulateOffline(bool value) {
37 simulate_offline_ = value;
38 UpdateNetworkState();
39 }
40
Johan Euphrosineee020ee2015-10-12 12:49:16 -070041 private:
42 void UpdateNetworkState();
43 void UpdateNetworkStateCallback(provider::Network::State state);
Vitaly Buka3d8feaf2015-10-28 13:10:53 -070044 bool simulate_offline_{false};
Johan Euphrosineee020ee2015-10-12 12:49:16 -070045 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 Buka3d8feaf2015-10-28 13:10:53 -070049 std::unique_ptr<bufferevent, Deleter> connectivity_probe_;
50
Johan Euphrosineee020ee2015-10-12 12:49:16 -070051 base::WeakPtrFactory<EventNetworkImpl> weak_ptr_factory_{this};
52};
53
54} // namespace examples
55} // namespace weave
56
57#endif // LIBWEAVE_EXAMPLES_UBUNTU_EVENT_NETWORK_H_