Commit d9bfa152 authored by aligungr's avatar aligungr

UE/gNB executable refactor

parent 54f7e1ff
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <utils/concurrent_map.hpp>
#include <vector> #include <vector>
namespace app namespace app
...@@ -41,4 +42,12 @@ inline void CreateProcTable(const std::unordered_map<std::string, T> &nodeMap, i ...@@ -41,4 +42,12 @@ inline void CreateProcTable(const std::unordered_map<std::string, T> &nodeMap, i
CreateProcTable(nodes, cmdPort); CreateProcTable(nodes, cmdPort);
} }
template <typename T>
inline void CreateProcTable(const ConcurrentMap<std::string, T> &nodeMap, int cmdPort)
{
std::vector<std::string> nodes{};
nodeMap.invokeForeach([&nodes](const auto &node) { nodes.push_back(node.first); });
CreateProcTable(nodes, cmdPort);
}
} // namespace app } // namespace app
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <ue/ue.hpp> #include <ue/ue.hpp>
#include <unistd.h> #include <unistd.h>
#include <utils/common.hpp> #include <utils/common.hpp>
#include <utils/concurrent_map.hpp>
#include <utils/constants.hpp> #include <utils/constants.hpp>
#include <utils/options.hpp> #include <utils/options.hpp>
#include <utils/yaml_utils.hpp> #include <utils/yaml_utils.hpp>
...@@ -22,7 +23,7 @@ ...@@ -22,7 +23,7 @@
static app::CliServer *g_cliServer = nullptr; static app::CliServer *g_cliServer = nullptr;
static nr::ue::UeConfig *g_refConfig = nullptr; static nr::ue::UeConfig *g_refConfig = nullptr;
static std::unordered_map<std::string, nr::ue::UserEquipment *> g_ueMap{}; static ConcurrentMap<std::string, nr::ue::UserEquipment *> g_ueMap{};
static app::CliResponseTask *g_cliRespTask = nullptr; static app::CliResponseTask *g_cliRespTask = nullptr;
static struct Options static struct Options
...@@ -282,13 +283,13 @@ static void ReceiveCommand(app::CliMessage &msg) ...@@ -282,13 +283,13 @@ static void ReceiveCommand(app::CliMessage &msg)
return; return;
} }
if (g_ueMap.count(msg.nodeName) == 0) auto *ue = g_ueMap.getOrDefault(msg.nodeName);
if (ue == nullptr)
{ {
g_cliServer->sendMessage(app::CliMessage::Error(msg.clientAddr, "Node not found: " + msg.nodeName)); g_cliServer->sendMessage(app::CliMessage::Error(msg.clientAddr, "Node not found: " + msg.nodeName));
return; return;
} }
auto *ue = g_ueMap[msg.nodeName];
ue->pushCommand(std::move(cmd), msg.clientAddr); ue->pushCommand(std::move(cmd), msg.clientAddr);
} }
...@@ -367,7 +368,7 @@ int main(int argc, char **argv) ...@@ -367,7 +368,7 @@ int main(int argc, char **argv)
{ {
auto *config = GetConfigByUe(i); auto *config = GetConfigByUe(i);
auto *ue = new nr::ue::UserEquipment(config, &g_ueController, nullptr, g_cliRespTask); auto *ue = new nr::ue::UserEquipment(config, &g_ueController, nullptr, g_cliRespTask);
g_ueMap[config->getNodeName()] = ue; g_ueMap.put(config->getNodeName(), ue);
} }
if (!g_options.disableCmd) if (!g_options.disableCmd)
...@@ -376,8 +377,7 @@ int main(int argc, char **argv) ...@@ -376,8 +377,7 @@ int main(int argc, char **argv)
g_cliRespTask->start(); g_cliRespTask->start();
} }
for (auto &ue : g_ueMap) g_ueMap.invokeForeach([](const auto &ue) { ue.second->start(); });
ue.second->start();
while (true) while (true)
Loop(); Loop();
......
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//
#include "concurrent_map.hpp"
//
// This file is a part of UERANSIM open source project.
// Copyright (c) 2021 ALİ GÜNGÖR.
//
// The software and all associated files are licensed under GPL-3.0
// and subject to the terms and conditions defined in LICENSE file.
//
#include <mutex>
#include <unordered_map>
#pragma once
template <typename TKey, typename TValue>
class ConcurrentMap
{
private:
mutable std::recursive_mutex m_mutex{};
std::unordered_map<TKey, TValue> m_map{};
public:
ConcurrentMap() = default;
public:
TValue getOrDefault(const TKey &key)
{
std::lock_guard lk(m_mutex);
if (m_map.count(key) != 0)
return m_map[key];
return TValue{};
}
void put(const TKey &key, const TValue &value)
{
std::lock_guard lk(m_mutex);
m_map[key] = value;
}
template <typename Fun>
void invokeForeach(const Fun &fun) const
{
std::lock_guard lk(m_mutex);
for (auto i : m_map)
fun(i);
}
};
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment