Merge branch 'google3'

Change-Id: Ibebf84b98939ee8e5006beedc34d225e8c2dd413
This commit is contained in:
Alexey Polyudov
2020-05-28 01:36:50 -07:00
337 changed files with 20487 additions and 1586 deletions
+7 -1
View File
@@ -9,6 +9,7 @@ cc_library(
hdrs = [
"atomic_boolean.h",
"atomic_reference.h",
"atomic_reference_def.h",
"ble.h",
"ble_v2.h",
"bluetooth_adapter.h",
@@ -25,12 +26,15 @@ cc_library(
"multi_thread_executor.h",
"output_file.h",
"output_stream.h",
"platform.h",
"scheduled_executor.h",
"server_sync.h",
"settable_future.h",
"settable_future_def.h",
"single_thread_executor.h",
"socket.h",
"submittable_executor.h",
"submittable_executor_def.h",
"system_clock.h",
"thread_utils.h",
"webrtc.h",
@@ -41,7 +45,9 @@ cc_library(
"//platform:types",
"//platform/port:down_cast",
"//platform/port:string",
"//webrtc/files/stable/webrtc/api:libjingle_peerconnection_api",
"//absl/strings",
"//absl/types:any",
"//webrtc/api:libjingle_peerconnection_api",
],
)
+34 -7
View File
@@ -1,21 +1,48 @@
#ifndef PLATFORM_API_ATOMIC_REFERENCE_H_
#define PLATFORM_API_ATOMIC_REFERENCE_H_
#include "platform/api/atomic_reference_def.h"
#include "platform/api/platform.h"
#include "platform/ptr.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
// An object reference that may be updated atomically.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
// "Common" part of implementation.
// Placed here for textual compatibility to minimize scope of changes.
// Can be (and should be) moved to a separate file outside "api" folder.
// TODO(apolyudov): for API v2.0
namespace platform {
namespace impl {
template <typename T>
class AtomicReference {
class AtomicReferenceImpl : public AtomicReference<T> {
public:
virtual ~AtomicReference() {}
explicit AtomicReferenceImpl(T initial_value) {
atomic_ = platform::ImplementationPlatform::createAtomicReferenceAny(
absl::any(initial_value));
}
virtual T get() = 0;
virtual void set(T value) = 0;
~AtomicReferenceImpl() override = default;
void set(T new_value) override { atomic_->set(absl::any(new_value)); }
T get() override { return absl::any_cast<T>(atomic_->get()); }
private:
Ptr<AtomicReference<absl::any>> atomic_;
};
} // namespace impl
template <typename T>
Ptr<AtomicReference<T>> ImplementationPlatform::createAtomicReference(
T initial_value) {
return Ptr<AtomicReference<T>>(
new impl::AtomicReferenceImpl<T>{initial_value});
}
} // namespace platform
} // namespace nearby
} // namespace location
+27
View File
@@ -0,0 +1,27 @@
#ifndef PLATFORM_API_ATOMIC_REFERENCE_DEF_H_
#define PLATFORM_API_ATOMIC_REFERENCE_DEF_H_
namespace location {
namespace nearby {
// An object reference that may be updated atomically.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
//
// Platform must implentent non-template static member functions
// Ptr<AtomicReference<size_t>> CreateAtomicReferenceSizeT()
// Ptr<AtomicReference<std::shared_ptr<void>>> CreateAtomicReferencePtr()
// in the location::nearby::platform::ImplementationPlatform class.
template <typename T>
class AtomicReference {
public:
virtual ~AtomicReference() = default;
virtual T get() = 0;
virtual void set(T value) = 0;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_API_ATOMIC_REFERENCE_DEF_H_
+1 -1
View File
@@ -24,7 +24,7 @@ namespace nearby {
struct BLEAdvertisementData {
typedef std::int8_t TXPowerLevel;
static const TXPowerLevel UNSPECIFIED_TX_POWER_LEVEL =
static constexpr TXPowerLevel UNSPECIFIED_TX_POWER_LEVEL =
std::numeric_limits<TXPowerLevel>::min();
bool is_connectable;
+2 -4
View File
@@ -10,11 +10,9 @@ namespace nearby {
// unbounded queue.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool-int-
template <typename ConcreteSubmittableExecutor>
class MultiThreadExecutor
: public SubmittableExecutor<ConcreteSubmittableExecutor> {
class MultiThreadExecutor : public SubmittableExecutor {
public:
~MultiThreadExecutor() override {}
~MultiThreadExecutor() override = default;
};
} // namespace nearby
+106
View File
@@ -0,0 +1,106 @@
#ifndef PLATFORM_API_PLATFORM_H_
#define PLATFORM_API_PLATFORM_H_
#include <cstdint>
#include "platform/api/atomic_boolean.h"
#include "platform/api/atomic_reference_def.h"
#include "platform/api/ble.h"
#include "platform/api/ble_v2.h"
#include "platform/api/bluetooth_adapter.h"
#include "platform/api/bluetooth_classic.h"
#include "platform/api/condition_variable.h"
#include "platform/api/count_down_latch.h"
#include "platform/api/hash_utils.h"
#include "platform/api/lock.h"
#include "platform/api/scheduled_executor.h"
#include "platform/api/server_sync.h"
#include "platform/api/settable_future_def.h"
#include "platform/api/submittable_executor_def.h"
#include "platform/api/system_clock.h"
#include "platform/api/thread_utils.h"
//#include "platform/api/webrtc.h"
#include "platform/api/wifi.h"
#include "platform/api/wifi_lan.h"
// Project-specific basic types, that are not part of API.
// TODO(apolyudov): replace with c++ standard types.
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace platform {
// API rework notes:
// https://docs.google.com/spreadsheets/d/1erZNkX7pX8s5jWTHdxgjntxTMor3BGiY2H_fC_ldtoQ/edit#gid=381357998
class ImplementationPlatform {
public:
// Class Templates in platform code.
//
// Platform interface does not support templates directly.
// This is a design decision. The purpose is to have type isolation
// between platform library (or simply platform) and core library.
// Another goal is to make a platform implementation a black box,
// which does not leak implementation details in any form, be that types,
// methods, or variables.
//
// Core library code does provide platform-specific class templates
// on top of (a non-templated) platform support.
//
// For every common library template that needs platform support,
// platform must provide an absl::any specialization of class template:
template <typename T>
static Ptr<AtomicReference<T>> createAtomicReference(T initial_value = T{});
template <typename T>
static Ptr<SettableFuture<T>> createSettableFuture();
// AtomicReference<T>
static Ptr<AtomicReference<absl::any>> createAtomicReferenceAny(
absl::any initial_value);
// SettableFuture<T>
static Ptr<SettableFuture<absl::any>> createSettableFutureAny();
// Non-template methods: general platform support.
static Ptr<AtomicBoolean> createAtomicBoolean(bool initial_value);
static Ptr<CountDownLatch> createCountDownLatch(std::int32_t count);
static Ptr<Lock> createLock();
static Ptr<ConditionVariable> createConditionVariable(Ptr<Lock> lock);
static Ptr<HashUtils> createHashUtils();
static Ptr<ThreadUtils> createThreadUtils();
static Ptr<SystemClock> createSystemClock();
// Java-like Executors
// Type aliases used to API 1.0 compatibility.
// They will be retired soon.
// TODO(apolyudov): cleanup.
using SingleThreadExecutorType = SubmittableExecutor;
using MultiThreadExecutorType = SubmittableExecutor;
using ScheduledExecutorType = ScheduledExecutor;
static Ptr<SubmittableExecutor> createSingleThreadExecutor();
static Ptr<SubmittableExecutor> createMultiThreadExecutor(
std::int32_t max_concurrency);
static Ptr<ScheduledExecutor> createScheduledExecutor();
// Protocol implementations, domain-specific support
static Ptr<BluetoothAdapter> createBluetoothAdapter();
static Ptr<WifiMedium> createWifiMedium();
static Ptr<BluetoothClassicMedium> createBluetoothClassicMedium();
static Ptr<BLEMedium> createBLEMedium();
static Ptr<BLEMediumV2> createBLEMediumV2();
static Ptr<ServerSyncMedium> createServerSyncMedium();
static Ptr<WifiLanMedium> createWifiLanMedium();
// static Ptr<WebRtcSignalingMessenger> createWebRtcSignalingMessenger(
// const std::string& self_id);
static std::string getDeviceId();
static std::string getPayloadPath(int64_t payload_id);
};
} // namespace platform
} // namespace nearby
} // namespace location
#endif // PLATFORM_API_PLATFORM_H_
+3 -3
View File
@@ -3,7 +3,7 @@
#include <cstdint>
#include "platform/api/executor.h"
#include "platform/api/submittable_executor_def.h"
#include "platform/cancelable.h"
#include "platform/ptr.h"
#include "platform/runnable.h"
@@ -15,9 +15,9 @@ namespace nearby {
// execute periodically.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html
class ScheduledExecutor : public Executor {
class ScheduledExecutor : public SubmittableExecutor {
public:
virtual ~ScheduledExecutor() {}
~ScheduledExecutor() override = default;
virtual Ptr<Cancelable> schedule(Ptr<Runnable> runnable,
std::int64_t delay_millis) = 0;
+1 -1
View File
@@ -22,7 +22,7 @@ class ServerSyncDevice {
virtual std::string getOwnGuid() = 0;
};
// Container of operations that can be performed over the Chrome Sync medium.
// Container of operations that can be performed over the Server Sync medium.
class ServerSyncMedium {
public:
virtual ~ServerSyncMedium() {}
+49 -8
View File
@@ -1,24 +1,65 @@
#ifndef PLATFORM_API_SETTABLE_FUTURE_H_
#define PLATFORM_API_SETTABLE_FUTURE_H_
#include "platform/api/listenable_future.h"
#include "platform/api/platform.h"
#include "platform/api/settable_future_def.h"
#include "platform/exception.h"
#include "platform/ptr.h"
#include "platform/runnable.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
// A SettableFuture is a type of Future whose result can be set.
//
// https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/SettableFuture.html
// "Common" part of implementation.
// Placed here for textual compatibility to minimize scope of changes.
// Can be (and should be) moved to a separate file outside "api" folder.
// TODO(apolyudov): for API v2.0
namespace platform {
namespace impl {
template <typename T>
class SettableFuture : public ListenableFuture<T> {
class SettableFutureImpl : public SettableFuture<T> {
public:
~SettableFuture() override {}
SettableFutureImpl() {
future_ = platform::ImplementationPlatform::createSettableFutureAny();
}
virtual bool set(T value) = 0;
~SettableFutureImpl() override = default;
virtual bool setException(Exception exception) = 0;
bool set(T value) override { return future_->set(absl::any(value)); }
bool setException(Exception exception) override {
return future_->setException(exception);
}
void addListener(Ptr<Runnable> runnable, Executor* executor) override {
future_->addListener(runnable, executor);
}
ExceptionOr<T> get() override { return CommonGet(future_->get()); }
ExceptionOr<T> get(std::int64_t timeout_ms) override {
return CommonGet(future_->get(timeout_ms));
}
private:
ExceptionOr<T> CommonGet(ExceptionOr<absl::any> ret_val) {
if (ret_val.exception() != Exception::kSuccess) {
return ExceptionOr<T>{ret_val.exception()};
}
return ExceptionOr<T>{absl::any_cast<T>(ret_val.result())};
}
Ptr<SettableFuture<absl::any>> future_;
};
} // namespace impl
template <typename T>
Ptr<SettableFuture<T>> ImplementationPlatform::createSettableFuture() {
return Ptr<SettableFuture<T>>(new impl::SettableFutureImpl<T>{});
}
} // namespace platform
} // namespace nearby
} // namespace location
+31
View File
@@ -0,0 +1,31 @@
#ifndef PLATFORM_API_SETTABLE_FUTURE_DEF_H_
#define PLATFORM_API_SETTABLE_FUTURE_DEF_H_
#include "platform/api/listenable_future.h"
#include "platform/exception.h"
namespace location {
namespace nearby {
// A SettableFuture is a type of Future whose result can be set.
//
// https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/SettableFuture.html
//
// Platform must implentent non-template static member functions
// Ptr<SettableFuture<size_t>> CreateSettableFutureSizeT()
// Ptr<SettableFuture<std::shared_ptr<void>>> CreateSettableFuturePtr()
// in the location::nearby::platform::ImplementationPlatform class.
template <typename T>
class SettableFuture : public ListenableFuture<T> {
public:
~SettableFuture() override = default;
virtual bool set(T value) = 0;
virtual bool setException(Exception exception) = 0;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_API_SETTABLE_FUTURE_DEF_H_
+2 -4
View File
@@ -10,11 +10,9 @@ namespace nearby {
// queue.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newSingleThreadExecutor--
template <typename ConcreteSubmittableExecutor>
class SingleThreadExecutor
: public SubmittableExecutor<ConcreteSubmittableExecutor> {
class SingleThreadExecutor : public SubmittableExecutor {
public:
~SingleThreadExecutor() override {}
~SingleThreadExecutor() override = default;
};
} // namespace nearby
+27 -24
View File
@@ -1,37 +1,40 @@
#ifndef PLATFORM_API_SUBMITTABLE_EXECUTOR_H_
#define PLATFORM_API_SUBMITTABLE_EXECUTOR_H_
#include <functional>
#include "platform/api/executor.h"
#include "platform/api/future.h"
#include "platform/callable.h"
#include "platform/port/down_cast.h"
#include "platform/ptr.h"
#include "platform/api/platform.h"
#include "platform/api/settable_future.h"
#include "platform/api/submittable_executor_def.h"
#include "platform/exception.h"
namespace location {
namespace nearby {
// Each per-platform concrete implementation is expected to extend from
// SubmittableExecutor and provide an override of its submit() method.
//
// e.g.
// class IOSSubmittableExecutor
// : public SubmittableExecutor<IOSSubmittableExecutor> {
// public:
// template <typename T>
// Ptr<Future<T> > submit(Ptr<Callable<T> > callable) {
// ...
// }
// }
template <typename ConcreteSubmittableExecutor>
class SubmittableExecutor : public Executor {
public:
~SubmittableExecutor() override {}
template <typename T>
Ptr<Future<T>> submit(Ptr<Callable<T>> callable) {
return DOWN_CAST<ConcreteSubmittableExecutor*>(this)->submit(callable);
// "Common" part of implementation.
// Placed here for textual compatibility to minimize scope of changes.
// Can be (and should be) moved to a separate file outside "api" folder.
// TODO(apolyudov): for API v2.0
template <typename T>
Ptr<Future<T>> SubmittableExecutor::submit(Ptr<Callable<T>> callable) {
using Platform = platform::ImplementationPlatform;
Ptr<SettableFuture<T>> future{Platform::createSettableFuture<T>()};
bool submitted = DoSubmit([callable, future]() {
ExceptionOr<T> result = callable->call();
if (result.ok()) {
future->set(std::move(result.result()));
} else {
future->setException({result.exception()});
}
});
if (!submitted) {
// Raise Exception::kExecution if we are shutting down.
future->setException({Exception::kExecution});
}
};
return future;
}
} // namespace nearby
} // namespace location
@@ -0,0 +1,35 @@
#ifndef PLATFORM_API_SUBMITTABLE_EXECUTOR_DEF_H_
#define PLATFORM_API_SUBMITTABLE_EXECUTOR_DEF_H_
#include <functional>
#include "platform/api/executor.h"
#include "platform/api/future.h"
#include "platform/callable.h"
#include "platform/ptr.h"
namespace location {
namespace nearby {
// Main interface to be used by platform as a base class for
// - MultiThreadExecutorWrapper
// - SingleThreadExecutorWrapper
// Platform must override bool submit(std::function<void()>) method.
class SubmittableExecutor : public Executor {
public:
~SubmittableExecutor() override = default;
template <typename T>
Ptr<Future<T>> submit(Ptr<Callable<T>> callable);
protected:
// Submit a callable (with no delay).
// Returns true, if callable was submitted, false otherwise.
// Callable is not submitted if shutdown is in progress.
virtual bool DoSubmit(std::function<void()> wrapped_callable) = 0;
};
} // namespace nearby
} // namespace location
#endif // PLATFORM_API_SUBMITTABLE_EXECUTOR_DEF_H_
+4 -3
View File
@@ -5,11 +5,11 @@
#include "platform/byte_array.h"
#include "platform/ptr.h"
#include "webrtc/files/stable/webrtc/api/peer_connection_interface.h"
//#include "webrtc/api/peer_connection_interface.h"
namespace location {
namespace nearby {
#if 0
class WebRtcSignalingMessenger {
public:
virtual ~WebRtcSignalingMessenger() = default;
@@ -33,12 +33,13 @@ class WebRtcSignalingMessenger {
virtual bool registerSignaling() = 0;
virtual bool unregisterSignaling() = 0;
virtual bool sendMessage(const string& peer_id,
virtual bool sendMessage(const std::string& peer_id,
ConstPtr<ByteArray> message) = 0;
virtual bool startReceivingMessages(
Ptr<SignalingMessageListener> listener) = 0;
virtual void getIceServers(Ptr<IceServersListener> ice_servers_listener) = 0;
};
#endif
} // namespace nearby
} // namespace location
+11 -9
View File
@@ -7,6 +7,7 @@
#include "platform/exception.h"
#include "platform/port/string.h"
#include "platform/ptr.h"
#include "absl/strings/string_view.h"
namespace location {
namespace nearby {
@@ -50,9 +51,10 @@ class WifiLanMedium {
public:
virtual ~WifiLanMedium() = default;
virtual bool StartAdvertising(const std::string& service_id,
const string& wifi_lan_service_info_name) = 0;
virtual void StopAdvertising(const std::string& service_id) = 0;
virtual bool StartAdvertising(
absl::string_view service_id,
absl::string_view wifi_lan_service_info_name) = 0;
virtual void StopAdvertising(absl::string_view service_id) = 0;
// Callback for WifiLan discover results.
class DiscoveredServiceCallback {
@@ -64,9 +66,9 @@ class WifiLanMedium {
};
virtual bool StartDiscovery(
const std::string& service_id,
absl::string_view service_id,
Ptr<DiscoveredServiceCallback> discovered_service_callback) = 0;
virtual void StopDiscovery(const std::string& service_id) = 0;
virtual void StopDiscovery(absl::string_view service_id) = 0;
class AcceptedConnectionCallback {
public:
@@ -76,16 +78,16 @@ class WifiLanMedium {
// destroyed) by the recipient of the callback methods (i.e. the creator of
// the concrete AcceptedConnectionCallback object).
virtual void OnConnectionAccepted(Ptr<WifiLanSocket> socket,
const string& service_id) = 0;
absl::string_view service_id) = 0;
};
virtual bool StartAcceptingConnections(
const std::string& service_id,
absl::string_view service_id,
Ptr<AcceptedConnectionCallback> accepted_connection_callback) = 0;
virtual void StopAcceptingConnections(const std::string& service_id) = 0;
virtual void StopAcceptingConnections(absl::string_view service_id) = 0;
virtual Ptr<WifiLanSocket> Connect(Ptr<WifiLanService> wifi_lan_service,
const std::string& service_id) = 0;
absl::string_view service_id) = 0;
};
} // namespace nearby