-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_task_gnb.cpp
303 lines (247 loc) · 8.21 KB
/
udp_task_gnb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// This file is a part of UERANSIM project.
// Copyright (c) 2023 ALİ GÜNGÖR.
//
// https://github.com/aligungr/UERANSIM/
// See README, LICENSE, and CONTRIBUTING files for licensing details.
//
#include "udp_task.hpp"
#include <cmath>
#include <cstdint>
#include <cstring>
#include <set>
#include <gnb/nts.hpp>
#include <utils/common.hpp>
#include <utils/constants.hpp>
#include <utils/libc_error.hpp>
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#define PORT 12000
int sockfd;
InetAddress peerAddress;
bool i_s = false;
struct sockaddr_in servaddr;
static constexpr const int BUFFER_SIZE = 16384;
static constexpr const int LOOP_PERIOD = 1000;
static constexpr const int RECEIVE_TIMEOUT = 200;
static constexpr const int HEARTBEAT_THRESHOLD = 2000; // (LOOP_PERIOD + RECEIVE_TIMEOUT)'dan büyük olmalı
static constexpr const int MIN_ALLOWED_DBM = -120;
static int EstimateSimulatedDbm(const Vector3 &myPos, const Vector3 &uePos)
{
int deltaX = myPos.x - uePos.x;
int deltaY = myPos.y - uePos.y;
int deltaZ = myPos.z - uePos.z;
int distance = static_cast<int>(std::sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ));
if (distance == 0)
return -1; // 0 may be confusing for people
return -distance;
}
namespace nr::gnb
{
RlsUdpTask::RlsUdpTask(TaskBase *base, uint64_t sti, Vector3 phyLocation)
: m_server{}, m_ctlTask{}, m_sti{sti}, m_phyLocation{phyLocation}, m_lastLoop{}, m_stiToUe{}, m_ueMap{}, m_newIdCounter{}
{
m_logger = base->logBase->makeUniqueLogger("rls-udp");
try
{
m_server = new udp::UdpServer(base->config->linkIp, cons::RadioLinkPort);
}
catch (const LibError &e)
{
m_logger->err("RLS failure [%s]", e.what());
quit();
return;
}
}
void RlsUdpTask::onStart()
{
/*
int sockfd;
struct sockaddr_in servaddr;
// Create socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cerr << "Socket creation failed" << std::endl;
return ;
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("192.168.56.140"); // IP address of VM2
*/
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cerr << "Socket creation failed" << std::endl;
return ;
}
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("192.168.56.140"); // IP address of VM2
}
void RlsUdpTask::onLoop()
{
auto current = utils::CurrentTimeMillis();
if (current - m_lastLoop > LOOP_PERIOD)
{
m_lastLoop = current;
heartbeatCycle(current);
}
uint8_t buffer[BUFFER_SIZE];
// InetAddress peerAddress;
int size = m_server->Receive(buffer, BUFFER_SIZE, RECEIVE_TIMEOUT, peerAddress);
if (size > 0)
{
auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer, static_cast<size_t>(size)});
/* int sockfd;
struct sockaddr_in servaddr;
// Create socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cerr << "Socket creation failed" << std::endl;
return ;
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr("192.168.56.140"); // IP address of VM2
OctetString stream;
rls::EncodeRlsMessage(*rlsMsg, stream);
std::string message = "Ping from VM1";
sendto(sockfd, buffer, static_cast<size_t>(size), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));
std::cout << "Ping message sent to VM2" << std::endl;*/
m_logger->debug("peer address set,sending message");
i_s = true;
sendto(sockfd, buffer, static_cast<size_t>(size), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));
/* socklen_t len = sizeof(servaddr);
int n = recvfrom(sockfd, buffer, 16384, MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
if(n>0)
{
std::cout << "Server: " << buffer << std::endl;
m_server->Send(peerAddress, buffer, static_cast<size_t>(n));
} */
// close(sockfd);
if (rlsMsg == nullptr)
m_logger->err("Unable to decode RLS message");
// else
// receiveRlsPdu(peerAddress, std::move(rlsMsg));
}
socklen_t len = sizeof(servaddr);
int n = recvfrom(sockfd, buffer, 16384, MSG_WAITALL, (struct sockaddr *) &servaddr, &len);
if(n>0 && i_s)
{
m_logger->debug("server [%s]", buffer);
m_server->Send(peerAddress, buffer, static_cast<size_t>(n));
}
}
void RlsUdpTask::onQuit()
{
delete m_server;
}
void RlsUdpTask::receiveRlsPdu(const InetAddress &addr, std::unique_ptr<rls::RlsMessage> &&msg)
{
if (msg->msgType == rls::EMessageType::HEARTBEAT)
{
int dbm = EstimateSimulatedDbm(m_phyLocation, ((const rls::RlsHeartBeat &)*msg).simPos);
if (dbm < MIN_ALLOWED_DBM)
{
// if the simulated signal strength is such low, then ignore this message
return;
}
if (m_stiToUe.count(msg->sti))
{
int ueId = m_stiToUe[msg->sti];
m_ueMap[ueId].address = addr;
m_ueMap[ueId].lastSeen = utils::CurrentTimeMillis();
}
else
{
int ueId = ++m_newIdCounter;
m_stiToUe[msg->sti] = ueId;
m_ueMap[ueId].address = addr;
m_ueMap[ueId].lastSeen = utils::CurrentTimeMillis();
auto w = std::make_unique<NmGnbRlsToRls>(NmGnbRlsToRls::SIGNAL_DETECTED);
w->ueId = ueId;
OctetString stream;
rls::EncodeRlsMessage(*msg, stream);
std::string message = "Ping from VM1";
sendto(sockfd, stream.data(), static_cast<size_t>(stream.length()), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));
std::cout << "Ping message sent to VM2" << std::endl;
m_ctlTask->push(std::move(w));
}
rls::RlsHeartBeatAck ack{m_sti};
ack.dbm = dbm;
sendRlsPdu(addr, ack);
return;
}
if (!m_stiToUe.count(msg->sti))
{
// if no HB received yet, and the message is not HB, then ignore the message
return;
}
OctetString stream;
rls::EncodeRlsMessage(*msg, stream);
std::string message = "Ping from VM1";
sendto(sockfd, stream.data(), static_cast<size_t>(stream.length()), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));
std::cout << "Ping message sent to VM2" << std::endl;
//close(sockfd);
auto w = std::make_unique<NmGnbRlsToRls>(NmGnbRlsToRls::RECEIVE_RLS_MESSAGE);
w->ueId = m_stiToUe[msg->sti];
w->msg = std::move(msg);
m_ctlTask->push(std::move(w));
}
void RlsUdpTask::sendRlsPdu(const InetAddress &addr, const rls::RlsMessage &msg)
{
OctetString stream;
rls::EncodeRlsMessage(msg, stream);
m_server->Send(addr, stream.data(), static_cast<size_t>(stream.length()));
}
void RlsUdpTask::heartbeatCycle(int64_t time)
{
std::set<int> lostUeId{};
std::set<uint64_t> lostSti{};
for (auto &item : m_ueMap)
{
if (time - item.second.lastSeen > HEARTBEAT_THRESHOLD)
{
lostUeId.insert(item.first);
lostSti.insert(item.second.sti);
}
}
for (uint64_t sti : lostSti)
m_stiToUe.erase(sti);
for (int ueId : lostUeId)
m_ueMap.erase(ueId);
for (int ueId : lostUeId)
{
auto w = std::make_unique<NmGnbRlsToRls>(NmGnbRlsToRls::SIGNAL_LOST);
w->ueId = ueId;
m_ctlTask->push(std::move(w));
}
}
void RlsUdpTask::initialize(NtsTask *ctlTask)
{
m_ctlTask = ctlTask;
}
void RlsUdpTask::send(int ueId, const rls::RlsMessage &msg)
{
if (ueId == 0)
{
for (auto &ue : m_ueMap)
send(ue.first, msg);
return;
}
if (!m_ueMap.count(ueId))
{
// ignore the message
return;
}
sendRlsPdu(m_ueMap[ueId].address, msg);
}
} // namespace nr::gnb