-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRlc.cpp
224 lines (182 loc) · 6.99 KB
/
Rlc.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
// The L-Band Digital Aeronautical Communications System (LDACS) Radio Link Control (RLC) implements the RLC protocol for the LDACS Air-Air Medium Access Control simulator.
// Copyright (C) 2023 Sebastian Lindner, Konrad Fuger, Musab Ahmed Eltayeb Ahmed, Andreas Timm-Giel, Institute of Communication Networks, Hamburg University of Technology, Hamburg, Germany
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Rlc.hpp"
#include "IArq.hpp"
#include <utility>
#include <string>
using namespace std;
using namespace TUHH_INTAIRNET_RLC;
void Rlc::receiveFromLower(L2Packet* packet) {
emit("rlc_bits_received_from_lower", (double) packet->getBits());
MacId src = packet->getOrigin();
auto process = getProcess(src);
if(process == nullptr) {
process = new RlcProcess(src, max_packet_size);
if(debugCallback) {
process->registerDebugMessageCallback(debugCallback);
process->registerEmitEventCallback(emitCallback);
}
processes.insert(make_pair(src, process));
}
auto headers = packet->getHeaders();
auto payloads = packet->getPayloads();
for(int i = 0; i< headers.size(); i++) {
if(headers[i]->frame_type == L2Header::FrameType::unicast || headers[i]->frame_type == L2Header::FrameType::broadcast) {
PacketFragment frag = make_pair(headers[i]->copy(), payloads[i] != nullptr? payloads[i]->copy(): nullptr);
process->receiveFromLower(frag);
}
}
delete packet;
L3Packet * pkt = process->getReassembledPacket();
auto nwLayer = getUpperLayer();
while (nwLayer && pkt != nullptr) {
debug("Rlc::passingToNWLayer" + to_string(pkt->size));
if(pkt->size > 0) {
emit("rlc_packet_sent_up", (double) pkt->size);
nwLayer->receiveFromLower(pkt);
} else {
delete pkt;
}
pkt = process->getReassembledPacket();
}
emit("rlc_awaiting_reassembly", (double) process->getNumReassembly());
}
void Rlc::receiveFromUpper(L3Packet *data, MacId dest, PacketPriority priority) {
emit("rlc_packet_received_from_upper", (double) data->size);
auto process = getProcess(dest);
if(process == nullptr) {
process = new RlcProcess(dest);
if(debugCallback) {
process->registerDebugMessageCallback(debugCallback);
process->registerEmitEventCallback(emitCallback);
}
processes.insert(make_pair(dest, process));
}
emit("rlc_packets_injected", (double)process->getNumInjectedPackets());
process->receiveFromUpper(data, priority);
IArq *arq = getLowerLayer();
if(arq) {
arq->notifyOutgoing(process->getQueuedBits(), dest);
}
emit("rlc_bits_to_send", (double)(getTotalBitsToSend()));
emit("rlc_packets_to_send", (double)(getTotalPacketsToSend()));
}
RlcProcess* Rlc::getProcess(MacId mac_id) const {
auto result = processes.find(mac_id);
if(result == processes.end()) {
return nullptr;
}
return result->second;
}
void Rlc::receiveInjectionFromLower(L2Packet *packet, PacketPriority priority) {
//emit("Rlc:packet_injected_from_lower(bits)", (double) packet->getBits());
MacId dest = packet->getDestination();
auto process = getProcess(dest);
if(process == nullptr) {
process = new RlcProcess(dest);
processes.insert(make_pair(dest, process));
}
process->receiveInjectionFromLower(packet, priority);
IArq *arq = getLowerLayer();
if(arq) {
arq->notifyOutgoing(process->getQueuedBits(), dest);
}
}
L2Packet * Rlc::requestSegment(unsigned int num_bits, const MacId &mac_id) {
emit("rlc_bits_requested_from_lower", (double) num_bits);
auto process = getProcess(mac_id);
bool is_broadcast = mac_id == SYMBOLIC_LINK_ID_BROADCAST;
if(process == nullptr) {
L2Packet* empty = new L2Packet();
if (is_broadcast)
empty->addMessage(new L2HeaderSH(), nullptr);
else
empty->addMessage(new L2HeaderPP(), nullptr);
return empty;
}
L2Packet* packet = process->getInjectedPacket();
if(packet == nullptr) {
packet = new L2Packet();
if (is_broadcast)
packet->addMessage(new L2HeaderSH(), nullptr);
else
packet->addMessage(new L2HeaderPP(), nullptr);
}
bool has_more_data = process->hasDataToSend();
int unicast_header_size = 82;
int broadcast_header_size = 25;
int header_size = mac_id != SYMBOLIC_LINK_ID_BROADCAST?
unicast_header_size :
broadcast_header_size;
int maxFragments = 100;
int fragkmentCounter = 0;
while(has_more_data && fragkmentCounter < maxFragments) {
pair<L2Header*, L2Packet::Payload*> data = process->getData(num_bits - packet->getBits());
packet->addMessage(data.first, data.second);
has_more_data = process->hasDataToSend();
// If the remaining space is too small for an additional header, we're done
if(packet->getBits() + header_size >= num_bits) {
has_more_data = false;
}
fragkmentCounter++;
emit("rlc_packet_sent_down", (double) packet->getBits());
emit("rlc_bits_to_send", (double)(getTotalBitsToSend()));
emit("rlc_packets_to_send", (double)(getTotalPacketsToSend()));
}
if(packet->getHeaders().size() <= 1) {
auto data = process->getEmptyData();
packet->addMessage(data.first, data.second);
}
return packet;
}
bool Rlc::isThereMoreData(const MacId& mac_id) const{
auto process = getProcess(mac_id);
if(!process) {
return false;
}
return process->hasDataToSend();
}
void Rlc::onEvent(double time) {
}
unsigned int Rlc::getQueuedDataSize(MacId dest) {
auto process = getProcess(dest);
if(!process) {
return 0;
}
return process->getQueuedBits();
}
int Rlc::getTotalBitsToSend() {
int total = 0;
for (auto const& entry : processes)
{
total += entry.second->getQueuedBits();
}
return total;
}
int Rlc::getTotalPacketsToSend() {
int total = 0;
for (auto const& entry : processes)
{
total += entry.second->getQueuedPackets();
}
return total;
}
Rlc::Rlc(int min_packet_size) {
this->max_packet_size = min_packet_size;
}
Rlc::~Rlc() {
for (auto it = processes.begin(); it != processes.end(); ++it)
{
delete it->second;
}
}