Weave port [3/n]: Introduce weave packet counter generator

PiperOrigin-RevId: 523189849
This commit is contained in:
Anay Wadhera
2023-04-10 12:56:57 -07:00
committed by Copybara-Service
parent 574b88d7ef
commit a8bb29e1bf
5 changed files with 161 additions and 0 deletions
+1
View File
@@ -547,6 +547,7 @@ let package = Package(
"internal/test/fake_device_info_test.cc",
"internal/test/fake_task_runner_test.cc",
"internal/weave/packet_test.cc",
"internal/weave/packet_sequence_number_generator_test.cc",
"internal/weave/packetizer_test.cc",
// simulation
"connections/implementation/offline_simulation_user.cc",
+16
View File
@@ -2,15 +2,18 @@ cc_library(
name = "weave",
srcs = [
"packet.cc",
"packet_sequence_number_generator.cc",
"packetizer.cc",
],
hdrs = [
"packet.h",
"packet_sequence_number_generator.h",
"packetizer.h",
],
deps = [
"//internal/platform:base",
"//internal/platform:types",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
@@ -33,6 +36,19 @@ cc_test(
],
)
cc_test(
name = "packet_sequence_number_generator_test",
srcs = [
"packet_sequence_number_generator_test.cc",
],
deps = [
":weave",
"//internal/platform/implementation/g3", # build_cleaner: keep
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "packetizer_test",
srcs = [
@@ -0,0 +1,39 @@
// Copyright 2023 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/weave/packet_sequence_number_generator.h"
#include "internal/platform/mutex_lock.h"
#include "internal/weave/packet.h"
namespace nearby {
namespace weave {
int PacketSequenceNumberGenerator::Next() {
MutexLock lock(&mutex_);
int next_packet_counter = packet_counter_;
if (next_packet_counter == Packet::kMaxPacketCounter) {
packet_counter_ = 0;
} else {
++packet_counter_;
}
return next_packet_counter;
}
void PacketSequenceNumberGenerator::Reset() {
MutexLock lock(&mutex_);
packet_counter_ = 0;
}
} // namespace weave
} // namespace nearby
@@ -0,0 +1,45 @@
// Copyright 2023 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 THIRD_PARTY_NEARBY_INTERNAL_WEAVE_PACKET_SEQUENCE_NUMBER_GENERATOR_H_
#define THIRD_PARTY_NEARBY_INTERNAL_WEAVE_PACKET_SEQUENCE_NUMBER_GENERATOR_H_
#include "absl/base/thread_annotations.h"
#include "internal/platform/mutex.h"
namespace nearby {
namespace weave {
// Keeps a counter of the next Weave packet to set the new Weave packet sequence
// number in a connection. This counter automatically rolls over once it hits 7,
// since 3 bits are allocated in the packet to the counter, making its max value
// 0b111, or 7.
class PacketSequenceNumberGenerator {
public:
// Returns the next counter in the sequence, incrementing the counter
// internally.
int Next();
// Resets the counter to 0.
void Reset();
private:
Mutex mutex_;
int packet_counter_ ABSL_GUARDED_BY(mutex_) = 0;
};
} // namespace weave
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_WEAVE_PACKET_SEQUENCE_NUMBER_GENERATOR_H_
@@ -0,0 +1,60 @@
// Copyright 2023 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/weave/packet_sequence_number_generator.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
namespace nearby {
namespace weave {
namespace {
TEST(PacketSequenceNumberGeneratorTest, TestNext) {
PacketSequenceNumberGenerator generator;
EXPECT_EQ(generator.Next(), 0);
EXPECT_EQ(generator.Next(), 1);
EXPECT_EQ(generator.Next(), 2);
EXPECT_EQ(generator.Next(), 3);
}
TEST(PacketSequenceNumberGeneratorTest, TestNextWrapping) {
PacketSequenceNumberGenerator generator;
EXPECT_EQ(generator.Next(), 0);
EXPECT_EQ(generator.Next(), 1);
EXPECT_EQ(generator.Next(), 2);
EXPECT_EQ(generator.Next(), 3);
EXPECT_EQ(generator.Next(), 4);
EXPECT_EQ(generator.Next(), 5);
EXPECT_EQ(generator.Next(), 6);
EXPECT_EQ(generator.Next(), 7);
EXPECT_EQ(generator.Next(), 0);
EXPECT_EQ(generator.Next(), 1);
}
TEST(PacketSequenceNumberGeneratorTest, TestReset) {
PacketSequenceNumberGenerator generator;
EXPECT_EQ(generator.Next(), 0);
EXPECT_EQ(generator.Next(), 1);
EXPECT_EQ(generator.Next(), 2);
generator.Reset();
EXPECT_EQ(generator.Next(), 0);
EXPECT_EQ(generator.Next(), 1);
EXPECT_EQ(generator.Next(), 2);
}
} // namespace
} // namespace weave
} // namespace nearby