mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Merge branch 'google3'
Change-Id: I8b28b1d3a5280581f20e4edf9144641a12df8879
This commit is contained in:
@@ -15,6 +15,7 @@ cc_library(
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
"//platform/impl/shared:atomic_boolean",
|
||||
"//platform/impl/shared:file",
|
||||
"//platform/impl/shared:posix_condition_variable",
|
||||
"//platform/impl/shared:posix_lock",
|
||||
"//platform/port:string",
|
||||
|
||||
@@ -18,6 +18,7 @@ target_link_libraries(platform_impl_g3
|
||||
"platform_types"
|
||||
"platform_api"
|
||||
"platform_impl_shared_atomic_boolean"
|
||||
"platform_impl_shared_file"
|
||||
"platform_impl_shared_posix_condition_variable"
|
||||
"platform_impl_shared_posix_lock"
|
||||
"platform_port_string"
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "platform/impl/g3/settable_future_impl.h"
|
||||
#include "platform/impl/g3/system_clock_impl.h"
|
||||
#include "platform/impl/shared/atomic_boolean_impl.h"
|
||||
#include "platform/impl/shared/file_impl.h"
|
||||
#include "platform/impl/shared/posix_condition_variable.h"
|
||||
#include "platform/impl/shared/posix_lock.h"
|
||||
#include "platform/port/string.h"
|
||||
@@ -36,6 +37,12 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace platform {
|
||||
|
||||
namespace {
|
||||
std::string getPayloadPath(std::int64_t payload_id) {
|
||||
return "/tmp/" + std::to_string(payload_id);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Ptr<SubmittableExecutor> ImplementationPlatform::createSingleThreadExecutor() {
|
||||
return Ptr<SubmittableExecutor>(/*new SingleThreadExecutorImpl()*/);
|
||||
}
|
||||
@@ -86,6 +93,16 @@ Ptr<AtomicBoolean> ImplementationPlatform::createAtomicBoolean(
|
||||
return Ptr<AtomicBoolean>(new AtomicBooleanImpl(initial_value));
|
||||
}
|
||||
|
||||
Ptr<InputFile> ImplementationPlatform::createInputFile(
|
||||
std::int64_t payload_id, std::int64_t total_size) {
|
||||
return MakePtr(new InputFileImpl(getPayloadPath(payload_id), total_size));
|
||||
}
|
||||
|
||||
Ptr<OutputFile> ImplementationPlatform::createOutputFile(
|
||||
std::int64_t payload_id) {
|
||||
return MakePtr(new OutputFileImpl(getPayloadPath(payload_id)));
|
||||
}
|
||||
|
||||
Ptr<BluetoothClassicMedium>
|
||||
ImplementationPlatform::createBluetoothClassicMedium() {
|
||||
return Ptr<BluetoothClassicMedium>();
|
||||
@@ -131,10 +148,6 @@ std::string ImplementationPlatform::getDeviceId() {
|
||||
return "google3";
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::getPayloadPath(int64_t payload_id) {
|
||||
return "/tmp/" + std::to_string(payload_id);
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -14,6 +14,7 @@ cc_library(
|
||||
"//platform:types",
|
||||
"//platform:utils",
|
||||
"//platform/api",
|
||||
"//platform/impl/shared:file",
|
||||
"//platform/impl/shared/sample:sample_wifi_medium",
|
||||
"//platform/port:string",
|
||||
"//absl/time",
|
||||
|
||||
@@ -12,6 +12,7 @@ target_link_libraries(platform_impl_sample
|
||||
PUBLIC
|
||||
absl::time
|
||||
platform_api
|
||||
platform_impl_shared_file
|
||||
platform_port_string
|
||||
platform_types
|
||||
platform_utils
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "platform/cancelable.h"
|
||||
#include "platform/impl/sample/atomic_reference_impl.h"
|
||||
#include "platform/impl/sample/settable_future_impl.h"
|
||||
#include "platform/impl/shared/file_impl.h"
|
||||
#include "platform/impl/shared/sample/sample_wifi_medium.h"
|
||||
#include "platform/port/string.h"
|
||||
#include "platform/ptr.h"
|
||||
@@ -30,6 +31,12 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace platform {
|
||||
|
||||
namespace {
|
||||
std::string getPayloadPath(std::int64_t payload_id) {
|
||||
return "/tmp/sample-" + std::to_string(payload_id);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Ptr<ScheduledExecutor> ImplementationPlatform::createScheduledExecutor() {
|
||||
return Ptr<ScheduledExecutor>{};
|
||||
}
|
||||
@@ -80,6 +87,16 @@ Ptr<AtomicBoolean> ImplementationPlatform::createAtomicBoolean(
|
||||
return Ptr<AtomicBoolean>{};
|
||||
}
|
||||
|
||||
Ptr<InputFile> ImplementationPlatform::createInputFile(
|
||||
std::int64_t payload_id, std::int64_t total_size) {
|
||||
return MakePtr(new InputFileImpl(getPayloadPath(payload_id), total_size));
|
||||
}
|
||||
|
||||
Ptr<OutputFile> ImplementationPlatform::createOutputFile(
|
||||
std::int64_t payload_id) {
|
||||
return MakePtr(new OutputFileImpl(getPayloadPath(payload_id)));
|
||||
}
|
||||
|
||||
Ptr<BluetoothClassicMedium>
|
||||
ImplementationPlatform::createBluetoothClassicMedium() {
|
||||
return Ptr<BluetoothClassicMedium>();
|
||||
@@ -116,10 +133,6 @@ Ptr<HashUtils> ImplementationPlatform::createHashUtils() {
|
||||
|
||||
std::string ImplementationPlatform::getDeviceId() { return "sample"; }
|
||||
|
||||
std::string ImplementationPlatform::getPayloadPath(int64_t payload_id) {
|
||||
return "/tmp/sample-" + std::to_string(payload_id);
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -43,3 +43,31 @@ cc_library(
|
||||
],
|
||||
deps = ["//platform/api"],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "file",
|
||||
srcs = ["file_impl.cc"],
|
||||
hdrs = ["file_impl.h"],
|
||||
visibility = [
|
||||
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
|
||||
"//core:__subpackages__",
|
||||
"//platform/impl:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
"//platform:types",
|
||||
"//platform/api",
|
||||
],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "file_test",
|
||||
timeout = "short",
|
||||
srcs = [
|
||||
"file_impl_test.cc",
|
||||
],
|
||||
deps = [
|
||||
":file",
|
||||
"//file/util:temp_path",
|
||||
"//testing/base/public:gunit_main",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -41,6 +41,26 @@ target_link_libraries(platform_impl_shared_posix_condition_variable
|
||||
absl::raw_logging_internal
|
||||
)
|
||||
|
||||
add_library(platform_impl_shared_file STATIC)
|
||||
|
||||
target_sources(platform_impl_shared_file
|
||||
PRIVATE
|
||||
"file_impl.cc"
|
||||
PUBLIC
|
||||
"file_impl.h"
|
||||
)
|
||||
|
||||
target_include_directories(platform_impl_shared_file
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(platform_impl_shared_file
|
||||
PUBLIC
|
||||
platform_api
|
||||
platform_types
|
||||
)
|
||||
|
||||
add_library(platform_impl_shared_atomic_boolean STATIC)
|
||||
|
||||
target_sources(platform_impl_shared_atomic_boolean
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "platform/impl/shared/file_impl.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
// InputFile
|
||||
|
||||
InputFileImpl::InputFileImpl(const std::string& path, std::int64_t size)
|
||||
: file_(path), path_(path), total_size_(size) {}
|
||||
|
||||
ExceptionOr<ConstPtr<ByteArray>> InputFileImpl::read(int64_t size) {
|
||||
if (!file_.is_open()) {
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
|
||||
}
|
||||
|
||||
if (file_.peek() == EOF) {
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(ConstPtr<ByteArray>());
|
||||
}
|
||||
|
||||
if (!file_.good()) {
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> read_bytes {new char [size]};
|
||||
file_.read(read_bytes.get(), static_cast<ptrdiff_t>(size));
|
||||
auto num_bytes_read = file_.gcount();
|
||||
if (num_bytes_read == 0) {
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(Exception::IO);
|
||||
}
|
||||
|
||||
return ExceptionOr<ConstPtr<ByteArray>>(
|
||||
MakeConstPtr(new ByteArray(read_bytes.get(), num_bytes_read)));
|
||||
}
|
||||
|
||||
std::string InputFileImpl::getFilePath() const { return path_; }
|
||||
|
||||
std::int64_t InputFileImpl::getTotalSize() const { return total_size_; }
|
||||
|
||||
void InputFileImpl::close() {
|
||||
if (file_.is_open()) {
|
||||
file_.close();
|
||||
}
|
||||
}
|
||||
|
||||
// OutputFile
|
||||
|
||||
OutputFileImpl::OutputFileImpl(const std::string& path) : file_(path) {}
|
||||
|
||||
Exception::Value OutputFileImpl::write(ConstPtr<ByteArray> data) {
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_data(data);
|
||||
|
||||
if (!file_.is_open()) {
|
||||
return Exception::IO;
|
||||
}
|
||||
|
||||
if (!file_.good()) {
|
||||
return Exception::IO;
|
||||
}
|
||||
|
||||
file_.write(data->getData(), data->size());
|
||||
file_.flush();
|
||||
return file_.good() ? Exception::NONE : Exception::IO;
|
||||
}
|
||||
|
||||
void OutputFileImpl::close() {
|
||||
if (file_.is_open()) {
|
||||
file_.close();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef PLATFORM_IMPL_SHARED_FILE_IMPL_H_
|
||||
#define PLATFORM_IMPL_SHARED_FILE_IMPL_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
|
||||
#include "platform/api/input_file.h"
|
||||
#include "platform/api/output_file.h"
|
||||
#include "platform/exception.h"
|
||||
#include "platform/ptr.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
class InputFileImpl final : public InputFile {
|
||||
public:
|
||||
InputFileImpl(const std::string& path, std::int64_t size);
|
||||
~InputFileImpl() override {}
|
||||
|
||||
ExceptionOr<ConstPtr<ByteArray>> read(std::int64_t size) override;
|
||||
std::string getFilePath() const override;
|
||||
std::int64_t getTotalSize() const override;
|
||||
void close() override;
|
||||
|
||||
private:
|
||||
std::ifstream file_;
|
||||
const std::string path_;
|
||||
const std::int64_t total_size_;
|
||||
};
|
||||
|
||||
class OutputFileImpl final : public OutputFile {
|
||||
public:
|
||||
explicit OutputFileImpl(const std::string& path);
|
||||
~OutputFileImpl() override {}
|
||||
|
||||
Exception::Value write(ConstPtr<ByteArray> data) override;
|
||||
void close() override;
|
||||
|
||||
private:
|
||||
std::ofstream file_;
|
||||
};
|
||||
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_IMPL_SHARED_FILE_IMPL_H_
|
||||
@@ -0,0 +1,133 @@
|
||||
#include "platform/impl/shared/file_impl.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
|
||||
|
||||
class FileImplTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
path_ = std::tmpnam(nullptr);;
|
||||
std::ofstream output_file(path_);
|
||||
file_ = std::fstream(path_, std::fstream::in | std::fstream::out);
|
||||
}
|
||||
|
||||
void WriteToFile(const std::string& text) {
|
||||
file_ << text;
|
||||
file_.flush();
|
||||
size_ += text.size();
|
||||
}
|
||||
|
||||
size_t GetSize() const { return size_; }
|
||||
|
||||
void AssertEquals(const ExceptionOr<ConstPtr<ByteArray>>& bytes,
|
||||
const std::string& expected) {
|
||||
ASSERT_TRUE(bytes.ok());
|
||||
ScopedPtr<ConstPtr<ByteArray>> byte_array(bytes.result());
|
||||
ASSERT_STREQ(byte_array->getData(), expected.c_str());
|
||||
ASSERT_EQ(byte_array->size(), expected.length());
|
||||
}
|
||||
|
||||
void AssertNull(const ExceptionOr<ConstPtr<ByteArray>>& bytes) {
|
||||
ASSERT_TRUE(bytes.ok());
|
||||
ASSERT_TRUE(bytes.result().isNull());
|
||||
}
|
||||
|
||||
static constexpr int64_t kMaxSize = 3;
|
||||
|
||||
std::string path_;
|
||||
std::fstream file_;
|
||||
size_t size_ = 0;
|
||||
};
|
||||
|
||||
TEST_F(FileImplTest, InputFile_NonExistentPath) {
|
||||
InputFileImpl input_file("/not/a/valid/path.txt", GetSize());
|
||||
ExceptionOr<ConstPtr<ByteArray>> read_result = input_file.read(kMaxSize);
|
||||
ASSERT_FALSE(read_result.ok());
|
||||
ASSERT_EQ(read_result.exception(), Exception::IO);
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_GetFilePath) {
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
ASSERT_EQ(input_file.getFilePath(), path_);
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_EmptyFileEOF) {
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
AssertNull(input_file.read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_ReadWorks) {
|
||||
WriteToFile("abc");
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
auto read_data = input_file.read(kMaxSize);
|
||||
read_data.result().destroy();
|
||||
SUCCEED();
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_ReadUntilEOF) {
|
||||
WriteToFile("abc");
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
AssertEquals(input_file.read(kMaxSize), "abc");
|
||||
AssertNull(input_file.read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_ReadWithSize) {
|
||||
WriteToFile("abc");
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
AssertEquals(input_file.read(2), "ab");
|
||||
AssertEquals(input_file.read(1), "c");
|
||||
AssertNull(input_file.read(kMaxSize));
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_GetTotalSize) {
|
||||
WriteToFile("abc");
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
EXPECT_EQ(input_file.getTotalSize(), 3);
|
||||
AssertEquals(input_file.read(1), "a");
|
||||
EXPECT_EQ(input_file.getTotalSize(), 3);
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, InputFile_Close) {
|
||||
WriteToFile("abc");
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
input_file.close();
|
||||
ExceptionOr<ConstPtr<ByteArray>> read_result = input_file.read(kMaxSize);
|
||||
ASSERT_FALSE(read_result.ok());
|
||||
ASSERT_EQ(read_result.exception(), Exception::IO);
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, OutputFile_NonExistentPath) {
|
||||
OutputFileImpl output_file("/not/a/valid/path.txt");
|
||||
ConstPtr<ByteArray> bytes = MakeConstPtr(new ByteArray("a", 1));
|
||||
Exception::Value write_result = output_file.write(bytes);
|
||||
ASSERT_EQ(write_result, Exception::IO);
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, OutputFile_Write) {
|
||||
OutputFileImpl output_file(path_);
|
||||
ConstPtr<ByteArray> bytes1 = MakeConstPtr(new ByteArray("a", 1));
|
||||
ConstPtr<ByteArray> bytes2 = MakeConstPtr(new ByteArray("bc", 2));
|
||||
ASSERT_EQ(output_file.write(bytes1), Exception::NONE);
|
||||
ASSERT_EQ(output_file.write(bytes2), Exception::NONE);
|
||||
InputFileImpl input_file(path_, GetSize());
|
||||
AssertEquals(input_file.read(kMaxSize), "abc");
|
||||
}
|
||||
|
||||
TEST_F(FileImplTest, OutputFile_Close) {
|
||||
OutputFileImpl output_file(path_);
|
||||
output_file.close();
|
||||
ConstPtr<ByteArray> bytes = MakeConstPtr(new ByteArray("a", 1));
|
||||
ASSERT_EQ(output_file.write(bytes), Exception::IO);
|
||||
}
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
Reference in New Issue
Block a user