-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #253 from aligungr/dev
v3.1.0
- Loading branch information
Showing
89 changed files
with
3,779 additions
and
2,567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// 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 "base_app.hpp" | ||
|
||
#include <atomic> | ||
#include <csignal> | ||
#include <cstdio> | ||
#include <exception> | ||
#include <iostream> | ||
#include <utils/constants.hpp> | ||
#include <vector> | ||
|
||
static std::atomic_int g_instanceCount{}; | ||
static std::vector<void (*)()> g_runAtExit{}; | ||
static std::vector<std::string> g_deleteAtExit{}; | ||
|
||
extern "C" void BaseSignalHandler(int num) | ||
{ | ||
for (auto &fun : g_runAtExit) | ||
fun(); | ||
for (auto &file : g_deleteAtExit) | ||
std::remove(file.c_str()); | ||
|
||
if (num == SIGTERM || num == SIGINT) | ||
exit(0); | ||
} | ||
|
||
namespace app | ||
{ | ||
|
||
void Initialize() | ||
{ | ||
if (g_instanceCount++ != 0) | ||
std::terminate(); | ||
|
||
std::signal(SIGTERM, BaseSignalHandler); | ||
std::signal(SIGINT, BaseSignalHandler); | ||
} | ||
|
||
void RunAtExit(void (*fun)()) | ||
{ | ||
g_runAtExit.push_back(fun); | ||
} | ||
|
||
void DeleteAtExit(const std::string &file) | ||
{ | ||
g_deleteAtExit.push_back(file); | ||
} | ||
|
||
} // namespace app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// 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. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
namespace app | ||
{ | ||
|
||
void Initialize(); | ||
|
||
void RunAtExit(void (*fun)()); | ||
|
||
void DeleteAtExit(const std::string &file); | ||
|
||
} // namespace app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// 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 "cli_base.hpp" | ||
#include <utils/octet_string.hpp> | ||
#include <utils/octet_view.hpp> | ||
|
||
#define CMD_BUFFER_SIZE 8192 | ||
#define CMD_RCV_TIMEOUT 2500 | ||
#define CMD_MIN_LENGTH (3 + 4 + 4 + 1) | ||
|
||
namespace app | ||
{ | ||
|
||
InetAddress CliServer::assignedAddress() const | ||
{ | ||
return m_socket.getAddress(); | ||
} | ||
|
||
CliMessage CliServer::receiveMessage() | ||
{ | ||
uint8_t buffer[CMD_BUFFER_SIZE] = {0}; | ||
InetAddress address; | ||
|
||
int size = m_socket.receive(buffer, CMD_BUFFER_SIZE, CMD_RCV_TIMEOUT, address); | ||
if (size < CMD_MIN_LENGTH || size >= CMD_BUFFER_SIZE) | ||
return {}; | ||
|
||
OctetView v{buffer, static_cast<size_t>(size)}; | ||
if (v.readI() != cons::Major) | ||
return {}; | ||
if (v.readI() != cons::Minor) | ||
return {}; | ||
if (v.readI() != cons::Patch) | ||
return {}; | ||
|
||
CliMessage res{}; | ||
res.type = static_cast<CliMessage::Type>(v.readI()); | ||
int nodeNameLength = v.read4I(); | ||
res.nodeName = v.readUtf8String(nodeNameLength); | ||
int valueLength = v.read4I(); | ||
res.value = v.readUtf8String(valueLength); | ||
res.clientAddr = address; | ||
return res; | ||
} | ||
|
||
void CliServer::sendMessage(const CliMessage &msg) | ||
{ | ||
OctetString stream{}; | ||
stream.appendOctet(cons::Major); | ||
stream.appendOctet(cons::Minor); | ||
stream.appendOctet(cons::Patch); | ||
stream.appendOctet(static_cast<int>(msg.type)); | ||
stream.appendOctet4(static_cast<size_t>(msg.nodeName.size())); | ||
for (char c : msg.nodeName) | ||
stream.appendOctet(static_cast<uint8_t>(c)); | ||
stream.appendOctet4(static_cast<size_t>(msg.value.size())); | ||
for (char c : msg.value) | ||
stream.appendOctet(static_cast<uint8_t>(c)); | ||
|
||
m_socket.send(msg.clientAddr, stream.data(), static_cast<size_t>(stream.length())); | ||
} | ||
|
||
} // namespace app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// 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. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <udp/server.hpp> | ||
#include <utility> | ||
#include <utils/constants.hpp> | ||
#include <utils/network.hpp> | ||
#include <utils/nts.hpp> | ||
#include <vector> | ||
|
||
namespace app | ||
{ | ||
|
||
struct CliMessage | ||
{ | ||
enum class Type | ||
{ | ||
EMPTY = 0, | ||
ECHO, | ||
ERROR, | ||
RESULT, | ||
COMMAND | ||
} type{}; | ||
|
||
std::string nodeName{}; | ||
std::string value{}; | ||
InetAddress clientAddr{}; | ||
|
||
static CliMessage Error(InetAddress addr, std::string msg, std::string node = "") | ||
{ | ||
CliMessage m{}; | ||
m.type = Type::ERROR; | ||
m.value = std::move(msg); | ||
m.nodeName = std::move(node); | ||
m.clientAddr = addr; | ||
return m; | ||
} | ||
|
||
static CliMessage Result(InetAddress addr, std::string msg, std::string node = "") | ||
{ | ||
CliMessage m{}; | ||
m.type = Type::RESULT; | ||
m.value = std::move(msg); | ||
m.nodeName = std::move(node); | ||
m.clientAddr = addr; | ||
return m; | ||
} | ||
|
||
static CliMessage Echo(InetAddress addr, std::string msg) | ||
{ | ||
CliMessage m{}; | ||
m.type = Type::ECHO; | ||
m.value = std::move(msg); | ||
m.nodeName = ""; | ||
m.clientAddr = addr; | ||
return m; | ||
} | ||
|
||
static CliMessage Command(InetAddress addr, std::string msg, std::string node = "") | ||
{ | ||
CliMessage m{}; | ||
m.type = Type::COMMAND; | ||
m.value = std::move(msg); | ||
m.nodeName = std::move(node); | ||
m.clientAddr = addr; | ||
return m; | ||
} | ||
}; | ||
|
||
class CliServer | ||
{ | ||
private: | ||
Socket m_socket; | ||
|
||
public: | ||
explicit CliServer() : m_socket{Socket::CreateAndBindUdp({cons::CMD_SERVER_IP, 0})} | ||
{ | ||
} | ||
|
||
~CliServer() | ||
{ | ||
m_socket.close(); | ||
} | ||
|
||
[[nodiscard]] InetAddress assignedAddress() const; | ||
|
||
CliMessage receiveMessage(); | ||
void sendMessage(const CliMessage &msg); | ||
}; | ||
|
||
struct NwCliSendResponse : NtsMessage | ||
{ | ||
InetAddress address{}; | ||
std::string output{}; | ||
bool isError{}; | ||
|
||
NwCliSendResponse(const InetAddress &address, std::string output, bool isError) | ||
: NtsMessage(NtsMessageType::CLI_SEND_RESPONSE), address(address), output(std::move(output)), isError(isError) | ||
{ | ||
} | ||
}; | ||
|
||
class CliResponseTask : public NtsTask | ||
{ | ||
private: | ||
app::CliServer *cliServer; | ||
|
||
public: | ||
explicit CliResponseTask(CliServer *cliServer) : cliServer(cliServer) | ||
{ | ||
} | ||
|
||
protected: | ||
void onStart() override | ||
{ | ||
} | ||
void onLoop() override | ||
{ | ||
auto *msg = take(); | ||
if (msg == nullptr) | ||
return; | ||
if (msg->msgType == NtsMessageType::CLI_SEND_RESPONSE) | ||
{ | ||
auto *w = dynamic_cast<NwCliSendResponse *>(msg); | ||
cliServer->sendMessage(w->isError ? CliMessage::Error(w->address, w->output) | ||
: CliMessage::Result(w->address, w->output)); | ||
} | ||
delete msg; | ||
} | ||
void onQuit() override | ||
{ | ||
} | ||
}; | ||
|
||
} // namespace app |
Oops, something went wrong.