These are the changes for Payload::Type => PayloadType for the flattening CL

PiperOrigin-RevId: 436767666
This commit is contained in:
jfcarroll
2022-03-23 10:11:15 -07:00
committed by Copybara-Service
parent 4acd3b1a1d
commit f4b80ccb59
16 changed files with 103 additions and 73 deletions
+1
View File
@@ -59,6 +59,7 @@ cc_library(
"out_of_band_connection_metadata.h",
"params.h",
"payload.h",
"payload_type.h",
"power_level.h",
"status.h",
"strategy.h",
+1 -1
View File
@@ -132,7 +132,7 @@ void Core::InitiateBandwidthUpgrade(absl::string_view endpoint_id,
void Core::SendPayload(absl::Span<const std::string> endpoint_ids,
Payload payload, ResultCallback callback) {
assert(payload.GetType() != Payload::Type::kUnknown);
assert(payload.GetType() != PayloadType::kUnknown);
assert(!endpoint_ids.empty());
router_->SendPayload(&client_, endpoint_ids, std::move(payload), callback);
@@ -49,7 +49,7 @@ namespace connections {
// PayloadManager::SendPayload() before control is transferred over to
// EndpointManager::SendPayloadChunk(). This work happens on one of three
// dedicated writer threads belonging to the PayloadManager. The writer thread
// that is used depends on the Payload::Type.
// that is used depends on the PayloadType.
//
// The EndpointManager has one dedicated reader thread for each registered
// endpoint, and the receiving of every incoming payload (and its subsequent
@@ -296,14 +296,14 @@ using location::nearby::api::OSName;
std::unique_ptr<InternalPayload> CreateOutgoingInternalPayload(
Payload payload) {
switch (payload.GetType()) {
case Payload::Type::kBytes:
case PayloadType::kBytes:
return absl::make_unique<BytesInternalPayload>(std::move(payload));
case Payload::Type::kFile: {
case PayloadType::kFile: {
return absl::make_unique<OutgoingFileInternalPayload>(std::move(payload));
}
case Payload::Type::kStream:
case PayloadType::kStream:
return absl::make_unique<OutgoingStreamInternalPayload>(
std::move(payload));
@@ -106,7 +106,7 @@ TEST(InternalPayloadFActoryTest, CanCreateIternalPayloadFromStreamMessage) {
EXPECT_EQ(payload.AsFile(), nullptr);
EXPECT_NE(payload.AsStream(), nullptr);
EXPECT_EQ(payload.AsBytes(), ByteArray());
EXPECT_EQ(payload.GetType(), Payload::Type::kStream);
EXPECT_EQ(payload.GetType(), PayloadType::kStream);
}
TEST(InternalPayloadFActoryTest, CanCreateIternalPayloadFromFileMessage) {
@@ -123,7 +123,7 @@ TEST(InternalPayloadFActoryTest, CanCreateIternalPayloadFromFileMessage) {
EXPECT_NE(payload.AsFile(), nullptr);
EXPECT_EQ(payload.AsStream(), nullptr);
EXPECT_EQ(payload.AsBytes(), ByteArray());
EXPECT_EQ(payload.GetType(), Payload::Type::kFile);
EXPECT_EQ(payload.GetType(), PayloadType::kFile);
}
void CreateFileWithContents(Payload::Id payload_id, const ByteArray& contents) {
+22 -22
View File
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -226,15 +226,15 @@ std::string PayloadManager::ToString(const EndpointIds& endpoint_ids) {
return endpoints_string;
}
std::string PayloadManager::ToString(Payload::Type type) {
std::string PayloadManager::ToString(PayloadType type) {
switch (type) {
case Payload::Type::kBytes:
case PayloadType::kBytes:
return std::string("Bytes");
case Payload::Type::kStream:
case PayloadType::kStream:
return std::string("Stream");
case Payload::Type::kFile:
case PayloadType::kFile:
return std::string("File");
case Payload::Type::kUnknown:
case PayloadType::kUnknown:
return std::string("Unknown");
}
}
@@ -355,14 +355,14 @@ void PayloadManager::SendPayload(ClientProxy* client,
// analytics.
std::int64_t payload_total_size;
switch (payload.GetType()) {
case connections::Payload::Type::kBytes:
case connections::PayloadType::kBytes:
payload_total_size = payload.AsBytes().size();
break;
case connections::Payload::Type::kFile:
case connections::PayloadType::kFile:
payload_total_size = payload.AsFile()->GetTotalSize();
break;
case connections::Payload::Type::kStream:
case connections::Payload::Type::kUnknown:
case connections::PayloadType::kStream:
case connections::PayloadType::kUnknown:
payload_total_size = -1;
break;
}
@@ -386,7 +386,7 @@ void PayloadManager::SendPayload(ClientProxy* client,
// other payload of the same type from even starting until this one is
// completely done with. If we ever want to provide isolation across
// ClientProxy objects this will need to be significantly re-architected.
Payload::Type payload_type = payload.GetType();
PayloadType payload_type = payload.GetType();
size_t resume_offset =
FeatureFlags::GetInstance().GetFlags().enable_send_payload_offset
? payload.GetOffset()
@@ -590,13 +590,13 @@ PayloadProgressInfo::Status PayloadManager::PayloadStatusToTransferUpdateStatus(
}
SingleThreadExecutor* PayloadManager::GetOutgoingPayloadExecutor(
Payload::Type payload_type) {
PayloadType payload_type) {
switch (payload_type) {
case Payload::Type::kBytes:
case PayloadType::kBytes:
return &bytes_payload_executor_;
case Payload::Type::kFile:
case PayloadType::kFile:
return &file_payload_executor_;
case Payload::Type::kStream:
case PayloadType::kStream:
return &stream_payload_executor_;
default:
return nullptr;
@@ -1109,7 +1109,7 @@ void PayloadManager::NotifyClientOfIncomingPayloadProgressInfo(
void PayloadManager::RecordPayloadStartedAnalytics(
ClientProxy* client, const EndpointIds& endpoint_ids,
std::int64_t payload_id, Payload::Type payload_type, std::int64_t offset,
std::int64_t payload_id, PayloadType payload_type, std::int64_t offset,
std::int64_t total_size) {
client->GetAnalyticsRecorder().OnOutgoingPayloadStarted(
endpoint_ids, payload_id, payload_type,
@@ -1118,7 +1118,7 @@ void PayloadManager::RecordPayloadStartedAnalytics(
void PayloadManager::RecordInvalidPayloadAnalytics(
ClientProxy* client, const EndpointIds& endpoint_ids,
std::int64_t payload_id, Payload::Type payload_type, std::int64_t offset,
std::int64_t payload_id, PayloadType payload_type, std::int64_t offset,
std::int64_t total_size) {
RecordPayloadStartedAnalytics(client, endpoint_ids, payload_id, payload_type,
offset, total_size);
@@ -1129,17 +1129,17 @@ void PayloadManager::RecordInvalidPayloadAnalytics(
}
}
Payload::Type PayloadManager::FramePayloadTypeToPayloadType(
PayloadType PayloadManager::FramePayloadTypeToPayloadType(
PayloadTransferFrame::PayloadHeader::PayloadType type) {
switch (type) {
case PayloadTransferFrame_PayloadHeader_PayloadType_BYTES:
return connections::Payload::Type::kBytes;
return connections::PayloadType::kBytes;
case PayloadTransferFrame_PayloadHeader_PayloadType_FILE:
return connections::Payload::Type::kFile;
return connections::PayloadType::kFile;
case PayloadTransferFrame_PayloadHeader_PayloadType_STREAM:
return connections::Payload::Type::kStream;
return connections::PayloadType::kStream;
default:
return connections::Payload::Type::kUnknown;
return connections::PayloadType::kUnknown;
}
}
+5 -5
View File
@@ -174,7 +174,7 @@ class PayloadManager : public EndpointManager::FrameProcessor {
using Endpoints = std::vector<const EndpointInfo*>;
static std::string ToString(const EndpointIds& endpoint_ids);
static std::string ToString(const Endpoints& endpoints);
static std::string ToString(Payload::Type type);
static std::string ToString(PayloadType type);
static std::string ToString(EndpointInfo::Status status);
// Splits the endpoints for this payload by availability.
@@ -277,7 +277,7 @@ class PayloadManager : public EndpointManager::FrameProcessor {
const PayloadProgressInfo& payload_transfer_update)
RUN_ON_PAYLOAD_STATUS_UPDATE_THREAD();
SingleThreadExecutor* GetOutgoingPayloadExecutor(Payload::Type payload_type);
SingleThreadExecutor* GetOutgoingPayloadExecutor(PayloadType payload_type);
void RunOnStatusUpdateThread(const std::string& name,
std::function<void()> runnable);
@@ -291,17 +291,17 @@ class PayloadManager : public EndpointManager::FrameProcessor {
void RecordPayloadStartedAnalytics(ClientProxy* client,
const EndpointIds& endpoint_ids,
std::int64_t payload_id,
Payload::Type payload_type,
PayloadType payload_type,
std::int64_t offset,
std::int64_t total_size);
void RecordInvalidPayloadAnalytics(ClientProxy* client,
const EndpointIds& endpoint_ids,
std::int64_t payload_id,
Payload::Type payload_type,
PayloadType payload_type,
std::int64_t offset,
std::int64_t total_size);
Payload::Type FramePayloadTypeToPayloadType(
PayloadType FramePayloadTypeToPayloadType(
PayloadTransferFrame::PayloadHeader::PayloadType type);
mutable Mutex mutex_;
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// Copyright 2020-2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
+15 -14
View File
@@ -26,43 +26,44 @@ Payload::~Payload() = default;
Payload& Payload::operator=(Payload&& other) noexcept = default;
// Default (invalid) payload.
Payload::Payload() : type_(Type::kUnknown), content_(absl::monostate()) {}
Payload::Payload()
: type_(PayloadType::kUnknown), content_(absl::monostate()) {}
// Constructors for outgoing payloads.
Payload::Payload(ByteArray&& bytes)
: type_(Type::kBytes), content_(std::move(bytes)) {}
: type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(const ByteArray& bytes)
: type_(Type::kBytes), content_(bytes) {}
: type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(InputFile input_file)
: id_(std::hash<std::string>()(input_file.GetFilePath())),
type_(Type::kFile),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(Id id, InputFile input_file)
: id_(id), type_(Type::kFile), content_(std::move(input_file)) {}
: id_(id), type_(PayloadType::kFile), content_(std::move(input_file)) {}
Payload::Payload(std::string parent_folder, std::string file_name,
InputFile input_file)
: id_(std::hash<std::string>()(input_file.GetFilePath())),
parent_folder_(parent_folder),
file_name_(file_name),
type_(Type::kFile),
type_(PayloadType::kFile),
content_(std::move(input_file)) {}
Payload::Payload(std::function<InputStream&()> stream)
: type_(Type::kStream), content_(std::move(stream)) {}
: type_(PayloadType::kStream), content_(std::move(stream)) {}
// Constructors for incoming payloads.
Payload::Payload(Id id, ByteArray&& bytes)
: id_(id), type_(Type::kBytes), content_(std::move(bytes)) {}
: id_(id), type_(PayloadType::kBytes), content_(std::move(bytes)) {}
Payload::Payload(Id id, const ByteArray& bytes)
: id_(id), type_(Type::kBytes), content_(bytes) {}
: id_(id), type_(PayloadType::kBytes), content_(bytes) {}
Payload::Payload(Id id, std::function<InputStream&()> stream)
: id_(id), type_(Type::kStream), content_(std::move(stream)) {}
: id_(id), type_(PayloadType::kStream), content_(std::move(stream)) {}
// Returns ByteArray payload, if it has been defined, or empty ByteArray.
const ByteArray& Payload::AsBytes() const& {
@@ -86,11 +87,11 @@ InputFile* Payload::AsFile() { return absl::get_if<InputFile>(&content_); }
Payload::Id Payload::GetId() const { return id_; }
// Returns Payload type.
Payload::Type Payload::GetType() const { return type_; }
PayloadType Payload::GetType() const { return type_; }
// Sets the payload offset in bytes
void Payload::SetOffset(size_t offset) {
CHECK(type_ == Type::kFile || type_ == Type::kStream);
CHECK(type_ == PayloadType::kFile || type_ == PayloadType::kStream);
InputFile* file = AsFile();
if (file != nullptr) {
CHECK(file->GetTotalSize() > 0 && offset < (size_t)file->GetTotalSize());
@@ -103,8 +104,8 @@ size_t Payload::GetOffset() { return offset_; }
// Generate Payload Id; to be passed to outgoing file constructor.
Payload::Id Payload::GenerateId() { return Prng().NextInt64(); }
Payload::Type Payload::FindType() const {
return static_cast<Type>(content_.index());
PayloadType Payload::FindType() const {
return static_cast<PayloadType>(content_.index());
}
const std::string& Payload::GetParentFolder() const { return parent_folder_; }
+4 -4
View File
@@ -21,6 +21,7 @@
#include <utility>
#include "absl/types/variant.h"
#include "connections/payload_type.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/core_config.h"
#include "internal/platform/file.h"
@@ -43,7 +44,6 @@ class DLL_API Payload {
// Enum values must match respective variant types.
using Content = absl::variant<absl::monostate, ByteArray,
std::function<InputStream&()>, InputFile>;
enum class Type { kUnknown = 0, kBytes = 1, kStream = 2, kFile = 3 };
Payload(Payload&& other) noexcept;
~Payload();
@@ -93,7 +93,7 @@ class DLL_API Payload {
Id GetId() const;
// Returns Payload type.
Type GetType() const;
PayloadType GetType() const;
// Sets the payload offset in bytes
void SetOffset(size_t offset);
@@ -107,7 +107,7 @@ class DLL_API Payload {
const std::string& GetParentFolder() const;
private:
Type FindType() const;
PayloadType FindType() const;
Id id_{GenerateId()};
size_t offset_{0};
@@ -115,7 +115,7 @@ class DLL_API Payload {
std::string parent_folder_;
std::string file_name_;
Type type_{FindType()};
PayloadType type_{FindType()};
Content content_;
};
+7 -7
View File
@@ -31,13 +31,13 @@ namespace connections {
TEST(PayloadTest, DefaultPayloadHasUnknownType) {
Payload payload;
EXPECT_EQ(payload.GetType(), Payload::Type::kUnknown);
EXPECT_EQ(payload.GetType(), PayloadType::kUnknown);
}
TEST(PayloadTest, SupportsByteArrayType) {
const ByteArray bytes("bytes");
Payload payload(bytes);
EXPECT_EQ(payload.GetType(), Payload::Type::kBytes);
EXPECT_EQ(payload.GetType(), PayloadType::kBytes);
EXPECT_EQ(payload.AsStream(), nullptr);
EXPECT_EQ(payload.AsFile(), nullptr);
EXPECT_EQ(payload.AsBytes(), bytes);
@@ -52,7 +52,7 @@ TEST(PayloadTest, SupportsFileType) {
Payload payload(payload_id, std::move(file));
payload.SetOffset(kOffset);
EXPECT_EQ(payload.GetType(), Payload::Type::kFile);
EXPECT_EQ(payload.GetType(), PayloadType::kFile);
EXPECT_EQ(payload.AsStream(), nullptr);
EXPECT_EQ(&payload.AsFile()->GetInputStream(), &stream);
EXPECT_EQ(payload.AsBytes(), ByteArray{});
@@ -72,7 +72,7 @@ TEST(PayloadTest, SupportsStreamType) {
});
payload.SetOffset(kOffset);
EXPECT_EQ(payload.GetType(), Payload::Type::kStream);
EXPECT_EQ(payload.GetType(), PayloadType::kStream);
EXPECT_EQ(payload.AsStream(), &pipe->GetInputStream());
EXPECT_EQ(payload.AsFile(), nullptr);
EXPECT_EQ(payload.AsBytes(), ByteArray{});
@@ -84,10 +84,10 @@ TEST(PayloadTest, PayloadIsMoveable) {
Payload payload2(ByteArray("bytes"));
auto id = payload2.GetId();
ByteArray bytes = payload2.AsBytes();
EXPECT_EQ(payload1.GetType(), Payload::Type::kUnknown);
EXPECT_EQ(payload2.GetType(), Payload::Type::kBytes);
EXPECT_EQ(payload1.GetType(), PayloadType::kUnknown);
EXPECT_EQ(payload2.GetType(), PayloadType::kBytes);
payload1 = std::move(payload2);
EXPECT_EQ(payload1.GetType(), Payload::Type::kBytes);
EXPECT_EQ(payload1.GetType(), PayloadType::kBytes);
EXPECT_EQ(payload1.AsBytes(), bytes);
EXPECT_EQ(payload1.GetId(), id);
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CORE_PAYLOAD_TYPE_H_
#define CORE_PAYLOAD_TYPE_H_
namespace location {
namespace nearby {
namespace connections {
enum class PayloadType { kUnknown = 0, kBytes = 1, kStream = 2, kFile = 3 };
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_PAYLOAD_TYPE_H_
+6 -6
View File
@@ -434,7 +434,7 @@ void AnalyticsRecorder::OnConnectionClosed(const std::string &endpoint_id,
}
void AnalyticsRecorder::OnIncomingPayloadStarted(
const std::string &endpoint_id, std::int64_t payload_id,
connections::Payload::Type type, std::int64_t total_size_bytes) {
connections::PayloadType type, std::int64_t total_size_bytes) {
MutexLock lock(&mutex_);
if (!CanRecordAnalyticsLocked("OnIncomingPayloadStarted")) {
return;
@@ -480,7 +480,7 @@ void AnalyticsRecorder::OnIncomingPayloadDone(const std::string &endpoint_id,
void AnalyticsRecorder::OnOutgoingPayloadStarted(
const std::vector<std::string> &endpoint_ids, std::int64_t payload_id,
connections::Payload::Type type, std::int64_t total_size_bytes) {
connections::PayloadType type, std::int64_t total_size_bytes) {
MutexLock lock(&mutex_);
if (!CanRecordAnalyticsLocked("OnOutgoingPayloadStarted")) {
return;
@@ -968,13 +968,13 @@ ConnectionsStrategy AnalyticsRecorder::StrategyToConnectionStrategy(
}
PayloadType AnalyticsRecorder::PayloadTypeToProtoPayloadType(
connections::Payload::Type type) {
connections::PayloadType type) {
switch (type) {
case connections::Payload::Type::kBytes:
case connections::PayloadType::kBytes:
return BYTES;
case connections::Payload::Type::kFile:
case connections::PayloadType::kFile:
return FILE;
case connections::Payload::Type::kStream:
case connections::PayloadType::kStream:
return STREAM;
default:
return UNKNOWN_PAYLOAD_TYPE;
+3 -3
View File
@@ -118,7 +118,7 @@ class AnalyticsRecorder {
// Payload
void OnIncomingPayloadStarted(const std::string &endpoint_id,
std::int64_t payload_id,
connections::Payload::Type type,
connections::PayloadType type,
std::int64_t total_size_bytes)
ABSL_LOCKS_EXCLUDED(mutex_);
void OnPayloadChunkReceived(const std::string &endpoint_id,
@@ -131,7 +131,7 @@ class AnalyticsRecorder {
ABSL_LOCKS_EXCLUDED(mutex_);
void OnOutgoingPayloadStarted(const std::vector<std::string> &endpoint_ids,
std::int64_t payload_id,
connections::Payload::Type type,
connections::PayloadType type,
std::int64_t total_size_bytes)
ABSL_LOCKS_EXCLUDED(mutex_);
void OnPayloadChunkSent(const std::string &endpoint_id,
@@ -315,7 +315,7 @@ class AnalyticsRecorder {
location::nearby::proto::connections::ConnectionsStrategy
StrategyToConnectionStrategy(connections::Strategy strategy);
location::nearby::proto::connections::PayloadType
PayloadTypeToProtoPayloadType(connections::Payload::Type type);
PayloadTypeToProtoPayloadType(connections::PayloadType type);
// Not owned by AnalyticsRecorder. Pointer must refer to a valid object
// that outlives the one constructed.
@@ -724,7 +724,7 @@ TEST(AnalyticsRecorderTest, OutgoingPayloadUpgraded) {
analytics_recorder.OnConnectionEstablished(endpoint_id, BLUETOOTH,
connection_token);
analytics_recorder.OnOutgoingPayloadStarted(
{endpoint_id}, payload_id, connections::Payload::Type::kFile, 50);
{endpoint_id}, payload_id, connections::PayloadType::kFile, 50);
analytics_recorder.OnPayloadChunkSent(endpoint_id, payload_id, 10);
analytics_recorder.OnPayloadChunkSent(endpoint_id, payload_id, 10);
analytics_recorder.OnConnectionClosed(endpoint_id, BLUETOOTH, UPGRADED);
@@ -46,7 +46,7 @@ void GNCPayloadListener::OnPayload(const std::string &endpoint_id, Payload paylo
NSMutableDictionary<NSNumber *, GNCPayloadInfo *> *payloads = payloads_provider_();
switch (payload.GetType()) {
case Payload::Type::kBytes: {
case PayloadType::kBytes: {
NSData *data = NSDataFromByteArray(payload.AsBytes()); // don't capture C++ object
// Wait for the payload transfer update to arrive before calling the Bytes payload handler.
@@ -66,7 +66,7 @@ void GNCPayloadListener::OnPayload(const std::string &endpoint_id, Payload paylo
break;
}
case Payload::Type::kStream:
case PayloadType::kStream:
if (handlers.streamPayloadHandler) {
// Make a pair of bound streams so data pumped into the output stream becomes
// available for reading from the input stream.
@@ -144,7 +144,7 @@ void GNCPayloadListener::OnPayload(const std::string &endpoint_id, Payload paylo
}
break;
case Payload::Type::kFile:
case PayloadType::kFile:
if (handlers.filePayloadHandler) {
InputFile *payloadInputFile = payload.AsFile();
NSURL *fileURL =