-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_task_mue.cpp
389 lines (334 loc) · 10.4 KB
/
udp_task_mue.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//
// 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 <cstdint>
#include <cstring>
#include <set>
#include <ue/nts.hpp>
#include <utils/common.hpp>
#include <utils/constants.hpp>
#include <iostream>
#include <thread>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
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ı
InetAddress peerAddress1;
uint8_t buffer1[BUFFER_SIZE];
uint8_t buffer[BUFFER_SIZE];
bool p_a=false, a= false, f= false;
int n;
int sockfd;
// uint8_t buffer[1024];
struct sockaddr_in servaddr, cliaddr;
socklen_t len;
#include <fcntl.h>
/*
void udpListener() {
int sockfd;
char buffer[1024];
struct sockaddr_in servaddr, cliaddr;
// 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));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(12000);
// Bind the socket with the server address
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
std::cerr << "Bind failed" << std::endl;
close(sockfd);
return;
}
socklen_t len;
int n;
len = sizeof(cliaddr); // len is value/result
std::cout << "Listening on port 12000" << std::endl;
while (true) {
n = recvfrom(sockfd, (char *)buffer, 1024, MSG_WAITALL, (struct sockaddr *)&cliaddr, &len);
buffer[n] = '\0';
std::cout << "Received: " << buffer << std::endl;
m_server->Send(peerAddress1, buffer, static_cast<size_t>(buffer.length()));
// Process the received packet here
}
close(sockfd);
}
*/
namespace nr::ue
{
RlsUdpTask::RlsUdpTask(TaskBase *base, RlsSharedContext *shCtx, const std::vector<std::string> &searchSpace)
: m_server{}, m_ctlTask{}, m_shCtx{shCtx}, m_searchSpace{}, m_cells{}, m_cellIdToSti{}, m_lastLoop{},
m_cellIdCounter{}
{
m_logger = base->logBase->makeUniqueLogger(base->config->getLoggerPrefix() + "rls-udp");
m_server = new udp::UdpServer();
for (auto &ip : searchSpace)
m_searchSpace.emplace_back(ip, cons::RadioLinkPort);
m_simPos = Vector3{};
}
void RlsUdpTask::udpListener() {
// int sockfd;
// uint8_t buffer[1024];
// struct sockaddr_in servaddr, cliaddr;
// int flags = fcntl(sockfd, F_GETFL, 0);
// fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
// 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));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(12000);
// Bind the socket with the server address
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
std::cerr << "Bind failed" << std::endl;
close(sockfd);
return;
}
// socklen_t len;
InetAddress addr;
len = sizeof(cliaddr); // len is value/result
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
// std::cout << "Listening on port 12000" << std::endl;
while (true) {
n = recvfrom(sockfd, buffer1, BUFFER_SIZE, MSG_WAITALL, (struct sockaddr *)&cliaddr, &len);
// buffer1[n] = '\0';
if(n > 0)
{
m_logger->debug( "Received:[%s] ", buffer1);
auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer1, static_cast<size_t>(n)});
if (rlsMsg == nullptr)
m_logger->err("Unable to decode RLS message");
if (rlsMsg->msgType == rls::EMessageType::HEARTBEAT)
{
for (auto &addr : m_searchSpace)
{
m_logger->debug("sending heartbeat message");
m_server->Send(addr, buffer1, static_cast<size_t>(sizeof(buffer1)));
}
}
else {
//f= true;
m_logger->debug("non HB");
if(p_a)
{
m_logger->debug("the problem may be here");
m_server->Send(peerAddress1, buffer1, static_cast<size_t>(sizeof(buffer1)));
// f= false;
}
}
}
/*
if (a)
{
m_logger->debug(" sending reply[%s]", buffer);
sendto(sockfd, buffer, BUFFER_SIZE, MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len);// the buffersize may be issue
//auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer, static_cast<size_t>(size)});
// m_server->Send(peerAddress1, buffer, static_cast<size_t>(n+1));
auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer, static_cast<size_t>(sizeof(buffer))});
if (rlsMsg == nullptr)
m_logger->err("Unable to decode RLS message");
else
{
if(rlsMsg->msgType == rls::EMessageType::HEARTBEAT_ACK)
{
m_logger->debug("ack");
}
else
{
m_logger->debug("sending non-ack");
}
}
a= false;
// Process the received packet here
}*/
}
m_logger->debug("closing connection");
close(sockfd);
}
void RlsUdpTask::onStart()
{
//std::thread listenerThread(udpListener);
// Ensure the listener thread runs alongside the main UE functionality
// listenerThread.detach();
std::thread listenerThread([this]() { this->udpListener(); });
listenerThread.detach(); // Detach the thread to run independently
}
bool nt = true;
void RlsUdpTask::onLoop()
{
auto current = utils::CurrentTimeMillis();
// heartbeatCycle(current, m_simPos);
/*if (current - m_lastLoop > LOOP_PERIOD)
{
m_lastLoop = current;
heartbeatCycle(current, m_simPos);
}
*/
// uint8_t buffer[BUFFER_SIZE];
InetAddress peerAddress;
int size = m_server->Receive(buffer, BUFFER_SIZE, RECEIVE_TIMEOUT, peerAddress);
if(!p_a)
{
peerAddress1 = peerAddress;
}
// m_logger->debug("peeraddress received");
if (size > 0)
{
m_logger->debug(" message received from gnb[%s] ", buffer);
a= true;
p_a = true;
sendto(sockfd, buffer, size, MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len);
/* if(f)
{
m_server->Send(peerAddress1, buffer1, static_cast<size_t>(sizeof(buffer1)));
f= false;
}*/
}
auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer, static_cast<size_t>(size)});
if (rlsMsg == nullptr)
m_logger->err("Unable to decode RLS message");
else {
if (rlsMsg->msgType == rls::EMessageType::HEARTBEAT_ACK)
{
m_logger->debug("ack");
}
else
{
m_logger->debug("non-ack");
}
}
/* else
{
//std::cout<<"sent buffer"<<buffer;
// receiveRlsPdu(peerAddress, std::move(rlsMsg));
if(f)
{
m_logger->debug("sending buffer");
auto rlsMsg = rls::DecodeRlsMessage(OctetView{buffer1, static_cast<size_t>(n)});
OctetString stream;
rls::EncodeRlsMessage(*rlsMsg, stream);
// rls::RlsMessage message(buffer1, sizeof(buffer1));
// Now pass the rls::RlsMessage object to EncodeRlsMessage
//rls::EncodeRlsMessage(message, stream);
// rls::EncodeRlsMessage(buffer1, stream);
if (rlsMsg == nullptr)
m_logger->err("Unable to decode RLS message");
m_server->Send(peerAddress1, stream.data(), static_cast<size_t>(stream.length()));
f= false;
}
}*/
// receiveRlsPdu(peerAddress, std::move(rlsMsg));
}
//}
void RlsUdpTask::onQuit()
{
delete m_server;
}
void RlsUdpTask::sendRlsPdu(const InetAddress &addr, const rls::RlsMessage &msg)
{
OctetString stream;
rls::EncodeRlsMessage(msg, stream);
// std::cout<<addr.toString();
m_server->Send(addr, stream.data(), static_cast<size_t>(stream.length()));
}
void RlsUdpTask::send(int cellId, const rls::RlsMessage &msg)
{
if (m_cellIdToSti.count(cellId))
{
auto sti = m_cellIdToSti[cellId];
sendRlsPdu(m_cells[sti].address, msg);
}
}
void RlsUdpTask::receiveRlsPdu(const InetAddress &addr, std::unique_ptr<rls::RlsMessage> &&msg)
{
if (msg->msgType == rls::EMessageType::HEARTBEAT_ACK)
{
if (!m_cells.count(msg->sti))
{
m_cells[msg->sti].cellId = ++m_cellIdCounter;
m_cellIdToSti[m_cells[msg->sti].cellId] = msg->sti;
}
int oldDbm = INT32_MIN;
if (m_cells.count(msg->sti))
oldDbm = m_cells[msg->sti].dbm;
m_cells[msg->sti].address = addr;
m_cells[msg->sti].lastSeen = utils::CurrentTimeMillis();
int newDbm = ((const rls::RlsHeartBeatAck &)*msg).dbm;
m_cells[msg->sti].dbm = newDbm;
if (oldDbm != newDbm)
onSignalChangeOrLost(m_cells[msg->sti].cellId);
return;
}
if (!m_cells.count(msg->sti))
{
// if no HB-ACK received yet, and the message is not HB-ACK, then ignore the message
return;
}
auto w = std::make_unique<NmUeRlsToRls>(NmUeRlsToRls::RECEIVE_RLS_MESSAGE);
w->cellId = m_cells[msg->sti].cellId;
w->msg = std::move(msg);
//a= true;
// m_ctlTask->push(std::move(w));
}
void RlsUdpTask::onSignalChangeOrLost(int cellId)
{
int dbm = INT32_MIN;
if (m_cellIdToSti.count(cellId))
{
auto sti = m_cellIdToSti[cellId];
dbm = m_cells[sti].dbm;
}
auto w = std::make_unique<NmUeRlsToRls>(NmUeRlsToRls::SIGNAL_CHANGED);
w->cellId = cellId;
w->dbm = dbm;
m_ctlTask->push(std::move(w));
}
void RlsUdpTask::heartbeatCycle(uint64_t time, const Vector3 &simPos)
{
std::set<std::pair<uint64_t, int>> toRemove;
for (auto &cell : m_cells)
{
auto delta = time - cell.second.lastSeen;
if (delta > HEARTBEAT_THRESHOLD)
toRemove.insert({cell.first, cell.second.cellId});
}
for (auto cell : toRemove)
{
m_cells.erase(cell.first);
m_cellIdToSti.erase(cell.second);
}
for (auto cell : toRemove)
onSignalChangeOrLost(cell.second);
for (auto &addr : m_searchSpace)
{
rls::RlsHeartBeat msg{m_shCtx->sti};
msg.simPos = simPos;
sendRlsPdu(addr, msg);
}
}
void RlsUdpTask::initialize(NtsTask *ctlTask)
{
m_ctlTask = ctlTask;
}
} // namespace nr::ue