Migrate Timer from location/nearby to third_party/nearby

PiperOrigin-RevId: 501928835
This commit is contained in:
Qin Wang
2023-01-13 13:44:17 -08:00
committed by Copybara-Service
parent 868e42f5f2
commit f94aa7548b
29 changed files with 165 additions and 102 deletions
+1
View File
@@ -34,6 +34,7 @@ cc_library(
"settable_future.h",
"submittable_executor.h",
"system_clock.h",
"timer.h",
],
defines = ["NO_WEBRTC"],
visibility = [
@@ -30,6 +30,7 @@ objc_library(
"multi_thread_executor.mm",
"platform.mm",
"scheduled_executor.mm",
"timer.mm",
"utils.mm",
"wifi_lan.mm",
],
@@ -40,6 +41,7 @@ objc_library(
"multi_thread_executor.h",
"scheduled_executor.h",
"single_thread_executor.h",
"timer.h",
"utils.h",
"wifi_lan.h",
],
@@ -27,6 +27,7 @@
#include "internal/platform/implementation/apple/mutex.h"
#import "internal/platform/implementation/apple/scheduled_executor.h"
#import "internal/platform/implementation/apple/single_thread_executor.h"
#include "internal/platform/implementation/apple/timer.h"
#import "internal/platform/implementation/apple/utils.h"
#include "internal/platform/implementation/apple/wifi_lan.h"
#include "internal/platform/implementation/mutex.h"
@@ -176,5 +177,10 @@ absl::StatusOr<WebResponse> ImplementationPlatform::SendRequest(
return absl::UnimplementedError("");
}
// TODO(b/261511529): Add implementation.
std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<apple::Timer>();
}
} // namespace api
} // namespace nearby
@@ -0,0 +1,43 @@
// 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.
// 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 PLATFORM_IMPL_APPLE_TIMER_H_
#define PLATFORM_IMPL_APPLE_TIMER_H_
#ifdef __cplusplus
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
namespace nearby {
namespace apple {
class Timer : public api::Timer {
public:
Timer() = default;
~Timer() override = default;
bool Create(int delay, int interval, std::function<void()> callback) override;
bool Stop() override;
bool FireNow() override;
};
} // namespace apple
} // namespace nearby
#endif
#endif // PLATFORM_IMPL_APPLE_TIMER_H_
@@ -0,0 +1,32 @@
// 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.
// 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.
#import "internal/platform/implementation/apple/timer.h"
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
namespace nearby {
namespace apple {
bool Timer::Create(int delay, int interval, std::function<void()> callback) { return true; }
bool Timer::Stop() { return true; }
bool Timer::FireNow() { return true; }
} // namespace apple
} // namespace nearby
@@ -31,6 +31,7 @@ cc_library(
"pipe.h",
"scheduled_executor.h",
"single_thread_executor.h",
"timer.h",
],
defines = ["NO_WEBRTC"],
visibility = ["//visibility:private"],
@@ -53,6 +53,7 @@
#include "internal/platform/implementation/g3/mutex.h"
#include "internal/platform/implementation/g3/scheduled_executor.h"
#include "internal/platform/implementation/g3/single_thread_executor.h"
#include "internal/platform/implementation/g3/timer.h"
#include "internal/platform/implementation/g3/wifi.h"
#include "internal/platform/implementation/g3/wifi_direct.h"
#include "internal/platform/implementation/g3/wifi_hotspot.h"
@@ -224,5 +225,9 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) {
new g3::ConditionVariable(static_cast<g3::Mutex*>(mutex)));
}
std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<g3::Timer>();
}
} // namespace api
} // namespace nearby
@@ -0,0 +1,71 @@
// 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.
// 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 PLATFORM_IMPL_G3_TIMER_H_
#define PLATFORM_IMPL_G3_TIMER_H_
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
namespace nearby {
namespace g3 {
class Timer : public api::Timer {
public:
Timer() = default;
~Timer() override = default;
bool Create(int delay, int interval,
std::function<void()> callback) override {
if (delay < 0 || interval < 0) {
return false;
}
callback_ = std::move(callback);
is_stopped_ = false;
return true;
}
bool Stop() override {
is_stopped_ = true;
return true;
}
bool FireNow() override {
if (is_stopped_ || !callback_) {
return false;
}
callback_();
return true;
}
// Mocked methods for test only
void TriggerCallback() {
if (is_stopped_ || callback_ == nullptr) {
return;
}
callback_();
}
private:
std::function<void()> callback_;
bool is_stopped_ = false;
};
} // namespace g3
} // namespace nearby
#endif // PLATFORM_IMPL_G3_TIMER_H_
@@ -41,6 +41,7 @@
#include "internal/platform/implementation/settable_future.h"
#include "internal/platform/implementation/submittable_executor.h"
#include "internal/platform/implementation/system_clock.h"
#include "internal/platform/implementation/timer.h"
#ifndef NO_WEBRTC
#include "internal/platform/implementation/webrtc.h"
#endif
@@ -128,6 +129,7 @@ class ImplementationPlatform {
static std::unique_ptr<WifiLanMedium> CreateWifiLanMedium();
static std::unique_ptr<WifiHotspotMedium> CreateWifiHotspotMedium();
static std::unique_ptr<WifiDirectMedium> CreateWifiDirectMedium();
static std::unique_ptr<Timer> CreateTimer();
#ifndef NO_WEBRTC
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
#endif
+48
View File
@@ -0,0 +1,48 @@
// 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.
// 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 PLATFORM_API_TIMER_H_
#define PLATFORM_API_TIMER_H_
#include <functional>
namespace nearby {
namespace api {
class Timer {
public:
virtual ~Timer() = default;
// Creates a timer based on interval.
//
// @param delay The amount of time in milliseconds relative to the current
// time that must elapse before the timer is signaled for the first
// time.
// @param interval The period of the timer, in milliseconds. If this parameter
// is zero, the timer is signaled once.
// @param callback it will be called when timer signaled.
//
// @return return true if success, otherwise false
virtual bool Create(int delay, int interval,
std::function<void()> callback) = 0;
// Stops timer. No timer signal is sent after the call.
virtual bool Stop() = 0;
virtual bool FireNow() = 0;
};
} // namespace api
} // namespace nearby
#endif // PLATFORM_API_TIMER_H_
@@ -17,6 +17,7 @@ cc_library(
name = "types",
srcs = [
"log_message.cc",
"timer.cc",
],
hdrs = [
"atomic_boolean.h",
@@ -33,13 +34,16 @@ cc_library(
"scheduled_executor.h",
"settable_future.h",
"submittable_executor.h",
"timer.h",
"utils.h",
],
deps = [
"//base",
"//base:stringprintf",
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform/implementation:types",
"//internal/platform/implementation/windows/generated:types",
],
)
@@ -221,6 +225,7 @@ cc_test(
"scheduled_executor_test.cc",
"submittable_executor_test.cc",
"thread_pool_test.cc",
"timer_test.cc",
"utils_test.cc",
],
copts = ["-Ithird_party/nearby/internal/platform/implementation/windows/generated -DCORE_ADAPTER_DLL"],
@@ -56,6 +56,7 @@
#include "internal/platform/implementation/windows/server_sync.h"
#include "internal/platform/implementation/windows/settable_future.h"
#include "internal/platform/implementation/windows/submittable_executor.h"
#include "internal/platform/implementation/windows/timer.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/implementation/windows/webrtc.h"
#include "internal/platform/implementation/windows/wifi.h"
@@ -307,5 +308,9 @@ absl::StatusOr<WebResponse> ImplementationPlatform::SendRequest(
return http_loader.GetResponse();
}
std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<windows::Timer>();
}
} // namespace api
} // namespace nearby
@@ -0,0 +1,101 @@
// 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.
// 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.
#include "internal/platform/implementation/windows/timer.h"
#include <functional>
#include <memory>
#include "internal/platform/logging.h"
namespace nearby {
namespace windows {
Timer::~Timer() { Stop(); }
bool Timer::Create(int delay, int interval, std::function<void()> callback) {
if ((delay < 0) || (interval < 0)) {
NEARBY_LOGS(WARNING) << "Delay and interval shouldn\'t be negative value.";
return false;
}
if (timer_queue_handle_ != nullptr) {
return false;
}
timer_queue_handle_ = CreateTimerQueue();
if (timer_queue_handle_ == nullptr) {
NEARBY_LOGS(ERROR) << "Failed to create timer queue.";
return false;
}
delay_ = delay;
interval_ = interval;
callback_ = std::move(callback);
if (!CreateTimerQueueTimer(&handle_, timer_queue_handle_,
static_cast<WAITORTIMERCALLBACK>(TimerRoutine),
&callback_, delay, interval, WT_EXECUTEDEFAULT)) {
if (!DeleteTimerQueueEx(timer_queue_handle_, nullptr)) {
NEARBY_LOGS(ERROR) << "Failed to create timer in timer queue.";
}
timer_queue_handle_ = nullptr;
return false;
}
return true;
}
bool Timer::Stop() {
if (timer_queue_handle_ == nullptr) {
return true;
}
if (!DeleteTimerQueueTimer(timer_queue_handle_, handle_, nullptr)) {
if (GetLastError() != ERROR_IO_PENDING) {
NEARBY_LOGS(ERROR) << "Failed to delete timer from timer queue.";
return false;
}
}
handle_ = nullptr;
if (!DeleteTimerQueueEx(timer_queue_handle_, nullptr)) {
NEARBY_LOGS(ERROR) << "Failed to delete timer queue.";
return false;
}
timer_queue_handle_ = nullptr;
return true;
}
bool Timer::FireNow() {
if (!timer_queue_handle_ || !callback_) {
return false;
}
callback_();
return true;
}
void CALLBACK Timer::TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) {
std::function<void()>* callback =
reinterpret_cast<std::function<void()>*>(lpParam);
if (*callback != NULL) {
(*callback)();
}
}
} // namespace windows
} // namespace nearby
@@ -0,0 +1,49 @@
// 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.
// 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 PLATFORM_IMPL_WINDOWS_TIMER_H_
#define PLATFORM_IMPL_WINDOWS_TIMER_H_
#include <windows.h>
#include <functional>
#include "internal/platform/implementation/timer.h"
namespace nearby {
namespace windows {
class Timer : public api::Timer {
public:
Timer() = default;
~Timer() override;
bool Create(int delay, int interval, std::function<void()> callback) override;
bool Stop() override;
bool FireNow() override;
private:
static void CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired);
int delay_;
int interval_;
std::function<void()> callback_;
HANDLE handle_ = NULL;
HANDLE timer_queue_handle_ = NULL;
};
} // namespace windows
} // namespace nearby
#endif // PLATFORM_IMPL_WINDOWS_TIMER_H_
@@ -0,0 +1,69 @@
// 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.
// 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.
#include "internal/platform/implementation/timer.h"
#include <chrono> // NOLINT
#include <functional> // NOLINT
#include <memory>
#include <thread> // NOLINT
#include "gtest/gtest.h"
#include "internal/platform/implementation/platform.h"
namespace nearby {
namespace windows {
namespace {
TEST(Timer, TestCreateTimer) {
int count = 0;
std::function<void()> callback = [&count]() { ++count; };
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_FALSE(timer->Create(-100, 0, callback));
EXPECT_TRUE(timer->Stop());
}
// This test case cannot run on Google3
TEST(Timer, DISABLED_TestRepeatTimer) {
int count = 0;
std::function<void()> callback = [&count]() { count++; };
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(300, 300, callback));
std::this_thread::sleep_for(std::chrono::seconds(1));
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 3);
}
TEST(Timer, DISABLED_TestFireNow) {
int count = 0;
std::function<void()> callback = [&count]() { ++count; };
auto timer = nearby::api::ImplementationPlatform::CreateTimer();
EXPECT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(3000, 3000, callback));
EXPECT_TRUE(timer->FireNow());
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 1);
}
} // namespace
} // namespace windows
} // namespace nearby