examples/ubuntu: switch to libevent network provider
- remove network provider impl from network_manager
- rename network_manager to wifi_manager
Bug: 24466635
Change-Id: I23845a59f0602c3995f5ece07a31d4a81f23b224
Reviewed-on: https://weave-review.googlesource.com/1331
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/libweave/examples/ubuntu/main.cc b/libweave/examples/ubuntu/main.cc
index cd6dc06..fa1901e 100644
--- a/libweave/examples/ubuntu/main.cc
+++ b/libweave/examples/ubuntu/main.cc
@@ -13,9 +13,10 @@
#include "examples/ubuntu/bluez_client.h"
#include "examples/ubuntu/curl_http_client.h"
#include "examples/ubuntu/event_http_server.h"
+#include "examples/ubuntu/event_network.h"
#include "examples/ubuntu/event_task_runner.h"
#include "examples/ubuntu/file_config_store.h"
-#include "examples/ubuntu/network_manager.h"
+#include "examples/ubuntu/wifi_manager.h"
namespace {
@@ -266,7 +267,8 @@
weave::examples::FileConfigStore config_store{disable_security};
weave::examples::EventTaskRunner task_runner;
weave::examples::CurlHttpClient http_client{&task_runner};
- weave::examples::NetworkImpl network{&task_runner, force_bootstrapping};
+ weave::examples::EventNetworkImpl network{&task_runner};
+ weave::examples::WifiImpl wifi{&task_runner, force_bootstrapping};
weave::examples::AvahiClient dns_sd;
weave::examples::HttpServerImpl http_server{&task_runner};
weave::examples::BluetoothImpl bluetooth;
@@ -274,7 +276,7 @@
auto device = weave::Device::Create(
&config_store, &task_runner, &http_client, &network, &dns_sd,
&http_server,
- weave::examples::NetworkImpl::HasWifiCapability() ? &network : nullptr,
+ weave::examples::WifiImpl::HasWifiCapability() ? &wifi : nullptr,
&bluetooth);
if (!registration_ticket.empty()) {
diff --git a/libweave/examples/ubuntu/network_manager.h b/libweave/examples/ubuntu/network_manager.h
deleted file mode 100644
index 3769ab1..0000000
--- a/libweave/examples/ubuntu/network_manager.h
+++ /dev/null
@@ -1,69 +0,0 @@
-// Copyright 2015 The Weave Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef LIBWEAVE_EXAMPLES_UBUNTU_NETWORK_MANAGER_H_
-#define LIBWEAVE_EXAMPLES_UBUNTU_NETWORK_MANAGER_H_
-
-#include <string>
-#include <vector>
-
-#include <base/memory/weak_ptr.h>
-#include <base/time/time.h>
-#include <weave/provider/network.h>
-#include <weave/provider/wifi.h>
-
-namespace weave {
-
-namespace provider {
-class TaskRunner;
-}
-
-namespace examples {
-
-// Basic weave::Network implementation.
-// Production version of SSL socket needs secure server certificate check.
-class NetworkImpl : public provider::Network, public provider::Wifi {
- public:
- explicit NetworkImpl(provider::TaskRunner* task_runner,
- bool force_bootstrapping);
- ~NetworkImpl();
-
- // Network implementation.
- void AddConnectionChangedCallback(
- const ConnectionChangedCallback& callback) override;
- State GetConnectionState() const override;
- void OpenSslSocket(const std::string& host,
- uint16_t port,
- const OpenSslSocketCallback& callback) override;
-
- // Wifi implementation.
- void Connect(const std::string& ssid,
- const std::string& passphrase,
- const DoneCallback& callback) override;
- void StartAccessPoint(const std::string& ssid) override;
- void StopAccessPoint() override;
-
- static bool HasWifiCapability();
-
- private:
- void TryToConnect(const std::string& ssid,
- const std::string& passphrase,
- int pid,
- base::Time until,
- const DoneCallback& callback);
- void UpdateNetworkState();
-
- bool force_bootstrapping_{false};
- bool hostapd_started_{false};
- provider::TaskRunner* task_runner_{nullptr};
- std::vector<ConnectionChangedCallback> callbacks_;
- provider::Network::State network_state_{provider::Network::State::kOffline};
-
- base::WeakPtrFactory<NetworkImpl> weak_ptr_factory_{this};
-};
-
-} // namespace examples
-} // namespace weave
-
-#endif // LIBWEAVE_EXAMPLES_UBUNTU_NETWORK_MANAGER_H_
diff --git a/libweave/examples/ubuntu/weave.gyp b/libweave/examples/ubuntu/weave.gyp
index a561c9d..24e8f15 100644
--- a/libweave/examples/ubuntu/weave.gyp
+++ b/libweave/examples/ubuntu/weave.gyp
@@ -37,7 +37,7 @@
'file_config_store.cc',
'main.cc',
'event_network.cc',
- 'network_manager.cc',
+ 'wifi_manager.cc',
'ssl_stream.cc',
],
'dependencies': [
diff --git a/libweave/examples/ubuntu/network_manager.cc b/libweave/examples/ubuntu/wifi_manager.cc
similarity index 67%
rename from libweave/examples/ubuntu/network_manager.cc
rename to libweave/examples/ubuntu/wifi_manager.cc
index efc2d5c..be2953e 100644
--- a/libweave/examples/ubuntu/network_manager.cc
+++ b/libweave/examples/ubuntu/wifi_manager.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "examples/ubuntu/network_manager.h"
+#include "examples/ubuntu/wifi_manager.h"
#include <arpa/inet.h>
#include <linux/wireless.h>
@@ -39,25 +39,19 @@
} // namespace
-NetworkImpl::NetworkImpl(provider::TaskRunner* task_runner,
+WifiImpl::WifiImpl(provider::TaskRunner* task_runner,
bool force_bootstrapping)
: force_bootstrapping_{force_bootstrapping}, task_runner_{task_runner} {
SSL_load_error_strings();
SSL_library_init();
StopAccessPoint();
- UpdateNetworkState();
}
-NetworkImpl::~NetworkImpl() {
+WifiImpl::~WifiImpl() {
StopAccessPoint();
}
-void NetworkImpl::AddConnectionChangedCallback(
- const ConnectionChangedCallback& callback) {
- callbacks_.push_back(callback);
-}
-
-void NetworkImpl::TryToConnect(const std::string& ssid,
+void WifiImpl::TryToConnect(const std::string& ssid,
const std::string& passphrase,
int pid,
base::Time until,
@@ -100,12 +94,12 @@
task_runner_->PostDelayedTask(
FROM_HERE,
- base::Bind(&NetworkImpl::TryToConnect, weak_ptr_factory_.GetWeakPtr(),
+ base::Bind(&WifiImpl::TryToConnect, weak_ptr_factory_.GetWeakPtr(),
ssid, passphrase, pid, until, callback),
base::TimeDelta::FromSeconds(1));
}
-void NetworkImpl::Connect(const std::string& ssid,
+void WifiImpl::Connect(const std::string& ssid,
const std::string& passphrase,
const DoneCallback& callback) {
force_bootstrapping_ = false;
@@ -122,35 +116,10 @@
base::Time::Now() + base::TimeDelta::FromMinutes(1), callback);
}
-void NetworkImpl::UpdateNetworkState() {
- network_state_ = Network::State::kOffline;
- if (force_bootstrapping_)
- return;
- if (std::system("ping talk.google.com -c 1") == 0)
- network_state_ = State::kOnline;
- else if (std::system("nmcli dev"))
- network_state_ = State::kError;
- else if (std::system("nmcli dev | grep connecting") == 0)
- network_state_ = State::kConnecting;
-
- task_runner_->PostDelayedTask(FROM_HERE,
- base::Bind(&NetworkImpl::UpdateNetworkState,
- weak_ptr_factory_.GetWeakPtr()),
- base::TimeDelta::FromSeconds(10));
- for (const auto& cb : callbacks_)
- cb.Run();
-}
-
-provider::Network::State NetworkImpl::GetConnectionState() const {
- return network_state_;
-}
-
-void NetworkImpl::StartAccessPoint(const std::string& ssid) {
+void WifiImpl::StartAccessPoint(const std::string& ssid) {
if (hostapd_started_)
return;
- network_state_ = State::kOffline;
-
// Release wlan0 interface.
CHECK_EQ(0, std::system("nmcli nm wifi off"));
CHECK_EQ(0, std::system("rfkill unblock wlan"));
@@ -187,35 +156,16 @@
CHECK_EQ(0, std::system(("dnsmasq --conf-file=" + dnsmasq_conf).c_str()));
}
-void NetworkImpl::StopAccessPoint() {
+void WifiImpl::StopAccessPoint() {
base::IgnoreResult(std::system("pkill -f dnsmasq.*/tmp/weave"));
base::IgnoreResult(std::system("pkill -f hostapd.*/tmp/weave"));
CHECK_EQ(0, std::system("nmcli nm wifi on"));
hostapd_started_ = false;
}
-bool NetworkImpl::HasWifiCapability() {
+bool WifiImpl::HasWifiCapability() {
return std::system("nmcli dev | grep ^wlan0") == 0;
}
-void NetworkImpl::OpenSslSocket(const std::string& host,
- uint16_t port,
- const OpenSslSocketCallback& callback) {
- // Connect to SSL port instead of upgrading to TLS.
- std::unique_ptr<SSLStream> tls_stream{new SSLStream{task_runner_}};
-
- if (tls_stream->Init(host, port)) {
- task_runner_->PostDelayedTask(
- FROM_HERE, base::Bind(callback, base::Passed(&tls_stream), nullptr),
- {});
- } else {
- ErrorPtr error;
- Error::AddTo(&error, FROM_HERE, "tls", "tls_init_failed",
- "Failed to initialize TLS stream.");
- task_runner_->PostDelayedTask(
- FROM_HERE, base::Bind(callback, nullptr, base::Passed(&error)), {});
- }
-}
-
} // namespace examples
} // namespace weave
diff --git a/libweave/examples/ubuntu/wifi_manager.h b/libweave/examples/ubuntu/wifi_manager.h
new file mode 100644
index 0000000..3258374
--- /dev/null
+++ b/libweave/examples/ubuntu/wifi_manager.h
@@ -0,0 +1,55 @@
+// Copyright 2015 The Weave Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef LIBWEAVE_EXAMPLES_UBUNTU_WIFI_MANAGER_H_
+#define LIBWEAVE_EXAMPLES_UBUNTU_WIFI_MANAGER_H_
+
+#include <string>
+#include <vector>
+
+#include <base/memory/weak_ptr.h>
+#include <base/time/time.h>
+#include <weave/provider/wifi.h>
+
+namespace weave {
+
+namespace provider {
+class TaskRunner;
+}
+
+namespace examples {
+
+// Basic weave::Wifi implementation.
+// Production version of SSL socket needs secure server certificate check.
+class WifiImpl : public provider::Wifi {
+ public:
+ explicit WifiImpl(provider::TaskRunner* task_runner,
+ bool force_bootstrapping);
+ ~WifiImpl();
+
+ // Wifi implementation.
+ void Connect(const std::string& ssid,
+ const std::string& passphrase,
+ const DoneCallback& callback) override;
+ void StartAccessPoint(const std::string& ssid) override;
+ void StopAccessPoint() override;
+
+ static bool HasWifiCapability();
+
+ private:
+ void TryToConnect(const std::string& ssid,
+ const std::string& passphrase,
+ int pid,
+ base::Time until,
+ const DoneCallback& callback);
+ bool force_bootstrapping_{false};
+ bool hostapd_started_{false};
+ provider::TaskRunner* task_runner_{nullptr};
+ base::WeakPtrFactory<WifiImpl> weak_ptr_factory_{this};
+};
+
+} // namespace examples
+} // namespace weave
+
+#endif // LIBWEAVE_EXAMPLES_UBUNTU_WIFI_MANAGER_H_