mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
Merge branch 'master' into release
Change-Id: I56ea2217899e92bdd9d6cb56797ac9895e194fff
This commit is contained in:
@@ -25,6 +25,7 @@ cc_library(
|
||||
"future.h",
|
||||
"input_file.h",
|
||||
"listenable_future.h",
|
||||
"log_message.h",
|
||||
"mutex.h",
|
||||
"output_file.h",
|
||||
"scheduled_executor.h",
|
||||
@@ -76,12 +77,14 @@ cc_library(
|
||||
"platform.h",
|
||||
],
|
||||
visibility = [
|
||||
"//platform_v2/base:__pkg__",
|
||||
"//platform_v2/impl:__subpackages__",
|
||||
"//platform_v2/public:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":comm",
|
||||
":types",
|
||||
"//platform_v2/base",
|
||||
"//absl/strings",
|
||||
"//absl/types:any",
|
||||
],
|
||||
|
||||
@@ -15,22 +15,22 @@
|
||||
#ifndef PLATFORM_V2_API_ATOMIC_REFERENCE_H_
|
||||
#define PLATFORM_V2_API_ATOMIC_REFERENCE_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
// An object reference that may be updated atomically.
|
||||
//
|
||||
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
|
||||
template <typename T>
|
||||
class AtomicReference {
|
||||
// Type that allows 32-bit atomic reads and writes.
|
||||
class AtomicUint32 {
|
||||
public:
|
||||
virtual ~AtomicReference() = default;
|
||||
virtual ~AtomicUint32() = default;
|
||||
|
||||
virtual T Get() const & = 0;
|
||||
virtual T Get() && = 0;
|
||||
virtual void Set(const T& value) = 0;
|
||||
virtual void Set(T&& value) = 0;
|
||||
// Atomically reads and returns stored value.
|
||||
virtual std::uint32_t Get() const = 0;
|
||||
|
||||
// Atomically stores value.
|
||||
virtual void Set(std::uint32_t value) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_V2_API_CONDITION_VARIABLE_H_
|
||||
|
||||
#include "platform_v2/base/exception.h"
|
||||
#include "absl/time/clock.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -29,10 +30,19 @@ class ConditionVariable {
|
||||
public:
|
||||
virtual ~ConditionVariable() {}
|
||||
|
||||
// https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--
|
||||
// Notifies all the waiters that condition state has changed.
|
||||
virtual void Notify() = 0;
|
||||
// https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--
|
||||
virtual Exception Wait() = 0; // throws Exception::kInterrupted
|
||||
|
||||
// Waits indefinitely for Notify to be called.
|
||||
// May return prematurely in case of interrupt, if supported by platform.
|
||||
// Returns kSuccess, or kInterrupted on interrupt.
|
||||
virtual Exception Wait() = 0;
|
||||
|
||||
// Waits while timeout has not expired for Notify to be called.
|
||||
// May return prematurely in case of interrupt, if supported by platform.
|
||||
// Returns kSuccess, or kInterrupted on interrupt.
|
||||
// If Timeout expired, and Notify was not called, returns kTimeout.
|
||||
virtual Exception Wait(absl::Duration timeout) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef PLATFORM_V2_API_LOG_MESSAGE_H_
|
||||
#define PLATFORM_V2_API_LOG_MESSAGE_H_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
// A log message that prints to appropraite destination when ~LogMessage() is
|
||||
// called.
|
||||
class LogMessage {
|
||||
public:
|
||||
enum class Severity {
|
||||
kInfo = 0,
|
||||
kWarning = 1,
|
||||
kError = 2,
|
||||
kFatal = 3, // Terminates the process after logging
|
||||
};
|
||||
|
||||
// Configures minimum severity to be logged.
|
||||
static void SetMinLogSeverity(Severity severity);
|
||||
|
||||
// Returns if a log with |severity| should be logged based on
|
||||
// SetMinLogSeverity and additional platform requirements.
|
||||
static bool ShouldCreateLogMessage(Severity severity);
|
||||
|
||||
virtual ~LogMessage() = default;
|
||||
|
||||
// Printf like logging.
|
||||
virtual void Print(const char* format, ...) = 0;
|
||||
|
||||
// Returns a stream for std::cout like logging.
|
||||
virtual std::ostream& Stream() = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_V2_API_LOG_MESSAGE_H_
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "platform_v2/api/count_down_latch.h"
|
||||
#include "platform_v2/api/crypto.h"
|
||||
#include "platform_v2/api/input_file.h"
|
||||
#include "platform_v2/api/log_message.h"
|
||||
#include "platform_v2/api/mutex.h"
|
||||
#include "platform_v2/api/output_file.h"
|
||||
#include "platform_v2/api/scheduled_executor.h"
|
||||
@@ -39,8 +40,8 @@
|
||||
#include "platform_v2/api/webrtc.h"
|
||||
#include "platform_v2/api/wifi.h"
|
||||
#include "platform_v2/api/wifi_lan.h"
|
||||
#include "platform_v2/base/payload_id.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/any.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -58,18 +59,32 @@ class ImplementationPlatform {
|
||||
// - Future<T> : to synchronize on Callable<T> schduled to execute.
|
||||
// - CountDownLatch : to ensure at least N threads are waiting.
|
||||
// - file I/O
|
||||
static std::unique_ptr<AtomicReference<absl::any>> CreateAtomicReferenceAny(
|
||||
absl::any initial_value);
|
||||
static std::unique_ptr<SettableFuture<absl::any>> CreateSettableFutureAny();
|
||||
// - Logging
|
||||
|
||||
// Atomics:
|
||||
// =======
|
||||
|
||||
// Atomic boolean: special case. Uses native platform atomics.
|
||||
// Does not use locking.
|
||||
// Does not use dynamic memory allocations in operations.
|
||||
static std::unique_ptr<AtomicBoolean> CreateAtomicBoolean(bool initial_value);
|
||||
|
||||
// Supports enums and integers up to 32-bit.
|
||||
// Does not use locking, if platform supports 32-bit atimics natively.
|
||||
// Does not use dynamic memory allocations in operations.
|
||||
static std::unique_ptr<AtomicUint32>
|
||||
CreateAtomicUint32(std::uint32_t value);
|
||||
|
||||
static std::unique_ptr<CountDownLatch> CreateCountDownLatch(
|
||||
std::int32_t count);
|
||||
static std::unique_ptr<Mutex> CreateMutex(Mutex::Mode mode);
|
||||
static std::unique_ptr<ConditionVariable> CreateConditionVariable(
|
||||
Mutex* mutex);
|
||||
static std::unique_ptr<InputFile> CreateInputFile(std::int64_t payload_id,
|
||||
static std::unique_ptr<InputFile> CreateInputFile(PayloadId payload_id,
|
||||
std::int64_t total_size);
|
||||
static std::unique_ptr<OutputFile> CreateOutputFile(std::int64_t payload_id);
|
||||
static std::unique_ptr<OutputFile> CreateOutputFile(PayloadId payload_id);
|
||||
static std::unique_ptr<LogMessage> CreateLogMessage(
|
||||
const char* file, int line, LogMessage::Severity severity);
|
||||
|
||||
// Java-like Executors
|
||||
static std::unique_ptr<SubmittableExecutor> CreateSingleThreadExecutor();
|
||||
@@ -88,7 +103,6 @@ class ImplementationPlatform {
|
||||
static std::unique_ptr<WifiMedium> CreateWifiMedium();
|
||||
static std::unique_ptr<WifiLanMedium> CreateWifiLanMedium();
|
||||
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
|
||||
static std::string GetDeviceId();
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -30,8 +30,15 @@ class SettableFuture : public ListenableFuture<T> {
|
||||
public:
|
||||
~SettableFuture() override = default;
|
||||
|
||||
virtual bool Set(const T& value) = 0;
|
||||
virtual bool Set(T&& value) = 0;
|
||||
// Completes the future successfully. The value is returned to any waiters.
|
||||
// Returns true, if value was set.
|
||||
// Returns false, if Future is already in "done" state.
|
||||
virtual bool Set(T value) = 0;
|
||||
|
||||
// Completes the future unsuccessfully. The exception value is returned to any
|
||||
// waiters.
|
||||
// Returns true, if exception was set.
|
||||
// Returns false, if Future is already in "done" state.
|
||||
virtual bool SetException(Exception exception) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
#include <string>
|
||||
|
||||
#include "platform_v2/base/byte_array.h"
|
||||
#include "platform_v2/base/exception.h"
|
||||
#include "platform_v2/base/input_stream.h"
|
||||
#include "platform_v2/base/listeners.h"
|
||||
#include "platform_v2/base/output_stream.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
|
||||
@@ -32,25 +32,33 @@ class WifiLanService {
|
||||
public:
|
||||
virtual ~WifiLanService() = default;
|
||||
|
||||
virtual std::string GetName() = 0;
|
||||
virtual std::string GetName() const = 0;
|
||||
};
|
||||
|
||||
class WifiLanSocket {
|
||||
public:
|
||||
virtual ~WifiLanSocket() = default;
|
||||
|
||||
// Returns the InputStream of the WifiLanSocket, empty std::unique_ptr<>
|
||||
// on error.
|
||||
virtual std::unique_ptr<InputStream> GetInputStream() = 0;
|
||||
// Returns the InputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
virtual InputStream& GetInputStream() = 0;
|
||||
|
||||
// Returns the OutputStream of the WifiLanSocket, empty std::unique_ptr<>
|
||||
// on error.
|
||||
virtual std::unique_ptr<OutputStream> GetOutputStream() = 0;
|
||||
// Returns the OutputStream of the WifiLanSocket.
|
||||
// On error, returned stream will report Exception::kIo on any operation.
|
||||
//
|
||||
// The returned object is not owned by the caller, and can be invalidated once
|
||||
// the WifiLanSocket object is destroyed.
|
||||
virtual OutputStream& GetOutputStream() = 0;
|
||||
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
virtual Exception::Value Close() = 0;
|
||||
virtual Exception Close() = 0;
|
||||
|
||||
virtual WifiLanService& GetRemoteWifiLanService() = 0;
|
||||
// Returns valid WifiLanService pointer if there is a connection, and
|
||||
// nullptr otherwise.
|
||||
virtual WifiLanService* GetRemoteWifiLanService() = 0;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the WifiLan medium.
|
||||
@@ -59,39 +67,51 @@ class WifiLanMedium {
|
||||
virtual ~WifiLanMedium() = default;
|
||||
|
||||
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;
|
||||
const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) = 0;
|
||||
virtual bool StopAdvertising(const std::string& service_id) = 0;
|
||||
|
||||
// Callback for WifiLan discover results.
|
||||
class DiscoveredServiceCallback {
|
||||
public:
|
||||
virtual ~DiscoveredServiceCallback() = default;
|
||||
|
||||
virtual void OnServiceDiscovered(WifiLanService* wifi_lan_service) = 0;
|
||||
virtual void OnServiceLost(WifiLanService* wifi_lan_service) = 0;
|
||||
struct DiscoveredServiceCallback {
|
||||
// The WifiLanService* is not owned by callbacks.
|
||||
// It is passed to give access to its non-const methods.
|
||||
// It is guaranteed to be valid for the duration of call.
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_discovered_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
std::function<void(WifiLanService& wifi_lan_service,
|
||||
const std::string& service_id)>
|
||||
service_lost_cb =
|
||||
DefaultCallback<WifiLanService&, const std::string&>();
|
||||
};
|
||||
|
||||
virtual bool StartDiscovery(
|
||||
absl::string_view service_id,
|
||||
DiscoveredServiceCallback* discovered_service_callback) = 0;
|
||||
virtual void StopDiscovery(absl::string_view service_id) = 0;
|
||||
// Returns true once the WifiLan discovery has been initiated.
|
||||
virtual bool StartDiscovery(const std::string& service_id,
|
||||
DiscoveredServiceCallback callback) = 0;
|
||||
|
||||
class AcceptedConnectionCallback {
|
||||
public:
|
||||
virtual ~AcceptedConnectionCallback() = default;
|
||||
// Returns true once WifiLan discovery for service_id is well and truly
|
||||
// stopped; after this returns, there must be no more invocations of the
|
||||
// DiscoveredServiceCallback passed in to StartDiscovery() for service_id.
|
||||
virtual bool StopDiscovery(const std::string& service_id) = 0;
|
||||
|
||||
virtual void OnConnectionAccepted(WifiLanSocket* socket,
|
||||
absl::string_view service_id) = 0;
|
||||
// Callback that is invoked when a new connection is accepted.
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(WifiLanSocket& socket, const std::string& service_id)>
|
||||
accepted_cb = DefaultCallback<WifiLanSocket&, const std::string&>();
|
||||
};
|
||||
|
||||
// Returns true once WifiLan socket connection requests to service_id can be
|
||||
// accepted.
|
||||
virtual bool StartAcceptingConnections(
|
||||
absl::string_view service_id,
|
||||
AcceptedConnectionCallback* accepted_connection_callback) = 0;
|
||||
virtual void StopAcceptingConnections(absl::string_view service_id) = 0;
|
||||
const std::string& service_id,
|
||||
AcceptedConnectionCallback callback) = 0;
|
||||
virtual bool StopAcceptingConnections(const std::string& service_id) = 0;
|
||||
|
||||
virtual WifiLanSocket* Connect(WifiLanService* wifi_lan_service,
|
||||
absl::string_view service_id) = 0;
|
||||
// Connects to a WifiLan service.
|
||||
// On success, returns a new WifiLanSocket.
|
||||
// On error, returns nullptr.
|
||||
virtual std::unique_ptr<WifiLanSocket> Connect(
|
||||
WifiLanService& service, const std::string& service_id) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
Reference in New Issue
Block a user