Skip to content
Snippets Groups Projects
Commit 8e475e84 authored by Myles Watson's avatar Myles Watson
Browse files

RootCanal: Use weak_ptr for Phy, Device

Bug: 227487676
Test: cert/run
Change-Id: Ifb833c6109fc2dc4b0a1c0a14be185ecb1fd8296
parent dacb29a1
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,8 @@ void Device::RegisterPhyLayer(std::shared_ptr<PhyLayer> phy) {
}
void Device::UnregisterPhyLayers() {
for (auto phy : phy_layers_) {
for (auto weak_phy : phy_layers_) {
auto phy = weak_phy.lock();
if (phy != nullptr) {
phy->Unregister();
}
......@@ -40,7 +41,8 @@ void Device::UnregisterPhyLayers() {
}
void Device::UnregisterPhyLayer(Phy::Type phy_type, uint32_t factory_id) {
for (auto& phy : phy_layers_) {
for (auto& weak_phy : phy_layers_) {
auto phy = weak_phy.lock();
if (phy != nullptr && phy->IsFactoryId(factory_id) &&
phy->GetType() == phy_type) {
phy->Unregister();
......@@ -59,7 +61,8 @@ bool Device::IsAdvertisementAvailable() const {
void Device::SendLinkLayerPacket(
std::shared_ptr<model::packets::LinkLayerPacketBuilder> to_send,
Phy::Type phy_type) {
for (auto phy : phy_layers_) {
for (auto weak_phy : phy_layers_) {
auto phy = weak_phy.lock();
if (phy != nullptr && phy->GetType() == phy_type) {
phy->Send(to_send);
}
......@@ -68,7 +71,8 @@ void Device::SendLinkLayerPacket(
void Device::SendLinkLayerPacket(model::packets::LinkLayerPacketView to_send,
Phy::Type phy_type) {
for (auto phy : phy_layers_) {
for (auto weak_phy : phy_layers_) {
auto phy = weak_phy.lock();
if (phy != nullptr && phy->GetType() == phy_type) {
phy->Send(to_send);
}
......
......@@ -85,7 +85,7 @@ class Device {
void RegisterCloseCallback(std::function<void()>);
protected:
std::vector<std::shared_ptr<PhyLayer>> phy_layers_;
std::vector<std::weak_ptr<PhyLayer>> phy_layers_;
std::chrono::steady_clock::time_point last_advertisement_;
......
......@@ -88,13 +88,13 @@ void TestModel::Del(size_t dev_index) {
schedule_task_(model_user_id_, std::chrono::milliseconds(0),
[this, dev_index]() {
devices_[dev_index]->UnregisterPhyLayers();
devices_[dev_index] = nullptr;
devices_[dev_index].reset();
});
}
size_t TestModel::AddPhy(Phy::Type phy_type) {
size_t factory_id = phys_.size();
phys_.emplace_back(phy_type, factory_id);
phys_.push_back(std::make_shared<PhyLayerFactory>(phy_type, factory_id));
return factory_id;
}
......@@ -103,9 +103,11 @@ void TestModel::DelPhy(size_t phy_index) {
LOG_WARN("Unknown phy at index %zu", phy_index);
return;
}
schedule_task_(
model_user_id_, std::chrono::milliseconds(0),
[this, phy_index]() { phys_[phy_index].UnregisterAllPhyLayers(); });
schedule_task_(model_user_id_, std::chrono::milliseconds(0),
[this, phy_index]() {
phys_[phy_index]->UnregisterAllPhyLayers();
phys_[phy_index].reset();
});
}
void TestModel::AddDeviceToPhy(size_t dev_index, size_t phy_index) {
......@@ -118,9 +120,13 @@ void TestModel::AddDeviceToPhy(size_t dev_index, size_t phy_index) {
return;
}
auto dev = devices_[dev_index];
dev->RegisterPhyLayer(phys_[phy_index].GetPhyLayer(
[dev](model::packets::LinkLayerPacketView packet) {
dev->IncomingPacket(std::move(packet));
std::weak_ptr<Device> weak_dev = dev;
dev->RegisterPhyLayer(phys_[phy_index]->GetPhyLayer(
[weak_dev](model::packets::LinkLayerPacketView packet) {
auto device = weak_dev.lock();
if (device != nullptr) {
device->IncomingPacket(std::move(packet));
}
},
dev_index));
}
......@@ -137,8 +143,8 @@ void TestModel::DelDeviceFromPhy(size_t dev_index, size_t phy_index) {
schedule_task_(model_user_id_, std::chrono::milliseconds(0),
[this, dev_index, phy_index]() {
devices_[dev_index]->UnregisterPhyLayer(
phys_[phy_index].GetType(),
phys_[phy_index].GetFactoryId());
phys_[phy_index]->GetType(),
phys_[phy_index]->GetFactoryId());
});
}
......@@ -150,7 +156,7 @@ void TestModel::AddLinkLayerConnection(std::shared_ptr<Device> dev,
AsyncUserId user_id = get_user_id_();
for (size_t i = 0; i < phys_.size(); i++) {
if (phy_type == phys_[i].GetType()) {
if (phy_type == phys_[i]->GetType()) {
AddDeviceToPhy(index, i);
}
}
......@@ -202,8 +208,7 @@ void TestModel::OnConnectionClosed(size_t index, AsyncUserId user_id) {
}
cancel_tasks_from_user_(user_id);
devices_[index]->UnregisterPhyLayers();
devices_[index] = nullptr;
Del(index);
}
void TestModel::SetDeviceAddress(size_t index, Address address) {
......@@ -228,7 +233,11 @@ const std::string& TestModel::List() {
list_string_ += " Phys: \r\n";
for (size_t i = 0; i < phys_.size(); i++) {
list_string_ += " " + std::to_string(i) + ":";
list_string_ += phys_[i].ToString() + " \r\n";
if (phys_[i] == nullptr) {
list_string_ += " deleted \r\n";
} else {
list_string_ += phys_[i]->ToString() + " \r\n";
}
}
return list_string_;
}
......
......@@ -100,7 +100,7 @@ class TestModel {
void Reset();
private:
std::vector<PhyLayerFactory> phys_;
std::vector<std::shared_ptr<PhyLayerFactory>> phys_;
std::vector<std::shared_ptr<Device>> devices_;
std::string list_string_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment