mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'master' into release
Change-Id: Ieb7f2b392864096e9cd783ab89f32442bc57abe7
This commit is contained in:
@@ -119,6 +119,7 @@ cc_library(
|
||||
"//platform_v2/api:comm",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/base:test_util",
|
||||
"//platform_v2/impl/shared:file",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/memory",
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "platform_v2/api/submittable_executor.h"
|
||||
#include "platform_v2/api/webrtc.h"
|
||||
#include "platform_v2/api/wifi.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/impl/g3/atomic_boolean.h"
|
||||
#include "platform_v2/impl/g3/atomic_reference.h"
|
||||
#include "platform_v2/impl/g3/bluetooth_adapter.h"
|
||||
@@ -147,7 +148,11 @@ std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
|
||||
}
|
||||
|
||||
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
if (MediumEnvironment::Instance().GetEnvironmentConfig().webrtc_enabled) {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Mutex> ImplementationPlatform::CreateMutex(Mutex::Mode mode) {
|
||||
|
||||
@@ -170,7 +170,7 @@ Exception WifiLanServerSocket::DoClose() {
|
||||
WifiLanMedium::WifiLanMedium() {
|
||||
service_.SetMedium(this);
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.RegisterWifiLanMedium(*this, service_);
|
||||
env.RegisterWifiLanMedium(*this);
|
||||
}
|
||||
|
||||
WifiLanMedium::~WifiLanMedium() {
|
||||
@@ -181,7 +181,6 @@ WifiLanMedium::~WifiLanMedium() {
|
||||
StopAdvertising(advertising_info_.service_id);
|
||||
StopDiscovery(discovering_info_.service_id);
|
||||
|
||||
accept_loops_runner_.Shutdown();
|
||||
NEARBY_LOG(INFO,
|
||||
"WifiLanMedium dtor advertising_accept_thread_running_ = %d",
|
||||
acceptance_thread_running_.load());
|
||||
@@ -195,12 +194,11 @@ WifiLanMedium::~WifiLanMedium() {
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiLanMedium::StartAdvertising(
|
||||
const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) {
|
||||
bool WifiLanMedium::StartAdvertising(const std::string& service_id,
|
||||
const std::string& service_info_name) {
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, service_name=%s",
|
||||
service_id.c_str(), wifi_lan_service_info_name.c_str());
|
||||
"G3 WifiLan StartAdvertising: service_id=%s, service_info_name=%s",
|
||||
service_id.c_str(), service_info_name.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, true);
|
||||
|
||||
@@ -230,8 +228,9 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (advertising_info_.Empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Can't stop advertising because we never started advertising.");
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Can't stop advertising because "
|
||||
"we never started advertising.");
|
||||
return false;
|
||||
}
|
||||
advertising_info_.Clear();
|
||||
@@ -241,14 +240,17 @@ bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
|
||||
env.UpdateWifiLanMediumForAdvertising(*this, service_, service_id, false);
|
||||
accept_loops_runner_.Shutdown();
|
||||
if (server_socket_ == nullptr) {
|
||||
NEARBY_LOG(ERROR, "Failed to find WifiLan Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
NEARBY_LOGS(ERROR) << "G3 WifiLan StopAdvertising: failed to find WifiLan "
|
||||
"Server socket: service_id="
|
||||
<< service_id;
|
||||
// Fall through for server socket not found.
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!server_socket_->Close().Ok()) {
|
||||
NEARBY_LOG(INFO, "Failed to close WifiLan server socket for %s.",
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopAdvertising: Failed to close WifiLan server "
|
||||
"socket for %s.",
|
||||
service_id.c_str());
|
||||
return false;
|
||||
}
|
||||
@@ -261,8 +263,8 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_id,
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartDiscovery: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id,
|
||||
std::move(callback), true);
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, std::move(callback),
|
||||
true);
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
discovering_info_.service_id = service_id;
|
||||
@@ -276,15 +278,16 @@ bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (discovering_info_.Empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Can't stop discovering because we never started discovering.");
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan StopDiscovery: Can't stop discovering because we "
|
||||
"never started discovering.");
|
||||
return false;
|
||||
}
|
||||
discovering_info_.Clear();
|
||||
}
|
||||
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id, {}, false);
|
||||
env.UpdateWifiLanMediumForDiscovery(*this, service_id, {}, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -293,8 +296,7 @@ bool WifiLanMedium::StartAcceptingConnections(
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StartAcceptingConnections: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_, service_id,
|
||||
callback);
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -302,7 +304,7 @@ bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
|
||||
NEARBY_LOG(INFO, "G3 WifiLan StopAcceptingConnections: service_id=%s",
|
||||
service_id.c_str());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_, service_id, {});
|
||||
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, {});
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -315,28 +317,31 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
|
||||
if (!medium) return {}; // Can't find medium. Bail out.
|
||||
|
||||
WifiLanServerSocket* server_socket = nullptr;
|
||||
WifiLanServerSocket* remote_server_socket = nullptr;
|
||||
NEARBY_LOG(INFO,
|
||||
"G3 WifiLan Connect [peer]: medium=%p, service=%p, service_id=%s",
|
||||
medium, &remote_service, service_id.c_str());
|
||||
// Then, find our server socket context in this medium.
|
||||
{
|
||||
absl::MutexLock medium_lock(&medium->mutex_);
|
||||
server_socket = medium->server_socket_.get();
|
||||
if (server_socket == nullptr) {
|
||||
NEARBY_LOG(ERROR, "Failed to find WifiLan Server socket: service_id=%s",
|
||||
remote_server_socket = medium->server_socket_.get();
|
||||
if (remote_server_socket == nullptr) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"G3 WifiLan Connect: Failed to find WifiLan Server socket: "
|
||||
"service_id=%s",
|
||||
service_id.c_str());
|
||||
// Fall through for server socket not found.
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
auto socket = std::make_unique<WifiLanSocket>();
|
||||
// Finally, Request to connect to this socket.
|
||||
if (!server_socket->Connect(*socket)) {
|
||||
NEARBY_LOG(
|
||||
ERROR,
|
||||
"Failed to connect to existing WifiLan Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
if (!remote_server_socket->Connect(*socket)) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"G3 WifiLan Connect: Failed to connect to existing WifiLan "
|
||||
"Server socket: service_id=%s",
|
||||
service_id.c_str());
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -34,21 +34,24 @@ namespace g3 {
|
||||
|
||||
class WifiLanMedium;
|
||||
|
||||
// Opaque wrapper over a WifiLan service which contains encoded WifiLan service
|
||||
// info name.
|
||||
// Opaque wrapper over a WifiLan service which contains packed
|
||||
// |WifiLanServiceInfo| string name.
|
||||
class WifiLanService : public api::WifiLanService {
|
||||
public:
|
||||
explicit WifiLanService(std::string name) : name_(std::move(name)) {}
|
||||
explicit WifiLanService(std::string service_info_name)
|
||||
: service_info_name_(std::move(service_info_name)) {}
|
||||
~WifiLanService() override = default;
|
||||
|
||||
void SetName(std::string name) { name_ = std::move(name); }
|
||||
std::string GetName() const override { return name_; }
|
||||
void SetName(std::string service_info_name) {
|
||||
service_info_name_ = std::move(service_info_name);
|
||||
}
|
||||
std::string GetName() const override { return service_info_name_; }
|
||||
|
||||
void SetMedium(WifiLanMedium* medium) { medium_ = medium; }
|
||||
WifiLanMedium* GetMedium() { return medium_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::string service_info_name_;
|
||||
WifiLanMedium* medium_ = nullptr;
|
||||
};
|
||||
|
||||
@@ -165,7 +168,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
~WifiLanMedium() override;
|
||||
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) override
|
||||
const std::string& service_info_name) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising(const std::string& service_id) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
@@ -215,7 +218,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
};
|
||||
|
||||
absl::Mutex mutex_;
|
||||
WifiLanService service_{"wifi_lan_service_info_name"};
|
||||
WifiLanService service_{"unknown G3 WifiLan service"};
|
||||
|
||||
// A thread pool dedicated to running all the accept loops from
|
||||
// StartAdvertising().
|
||||
@@ -225,8 +228,6 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
// A thread pool dedicated to wait to complete the accept_loops_runner_.
|
||||
MultiThreadExecutor close_accept_loops_runner_{kMaxConcurrentAcceptLoops};
|
||||
|
||||
// TODO(edwinwu): Extend it to hashmap to accept multiple sockets for multiple
|
||||
// entrance.
|
||||
// A server socket is established when start advertising.
|
||||
std::unique_ptr<WifiLanServerSocket> server_socket_;
|
||||
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
|
||||
|
||||
Reference in New Issue
Block a user