DESERT 3.5.1
Loading...
Searching...
No Matches
uw-ofdm-aloha.h
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
41#ifndef UWOFDMALOHA_H_
42#define UWOFDMALOHA_H_
43
44#include <mmac.h>
45#include <iostream>
46#include <string>
47#include <map>
48#include <set>
49#include <queue>
50#include <fstream>
51
52#include <mphy.h>
53#include "uwofdmphy_hdr.h"
54
55#define UWOFDMALOHA_DROP_REASON_WRONG_STATE "WST"
56#define UWOFDMALOHA_DROP_REASON_WRONG_RECEIVER "WRCV"
57#define UWOFDMALOHA_DROP_REASON_UNKNOWN_TYPE "UPT"
58#define UWOFDMALOHA_DROP_REASON_BUFFER_FULL "DBF"
59#define UWOFDMALOHA_DROP_REASON_ERROR "ERR"
60
61extern packet_t PT_MMAC_ACK;
62
63typedef int pktSeqNum;
64
65
66class UWOFDMAloha : public MMac
67{
68
69public:
74
78 virtual ~UWOFDMAloha();
79
88 virtual int command(int argc, const char *const *argv);
89
96 void init_macofdm_node(int subCarNum, double subCarSize, int ctrl_subCar, string modulation);
97
98protected:
120
143
153
161
172
177 class UWOFDMAlohaTimer : public TimerHandler
178 {
179
180 public:
185 : TimerHandler()
186 , start_time(0.0)
187 , left_duration(0.0)
188 , counter(0)
189 , module(m)
191 {
192 assert(m != NULL);
193 }
194
199 {
200 }
201
209 virtual void
211 {
213 left_duration -= (NOW - start_time);
214 if (left_duration <= 0.0)
215 left_duration = module->mac2phy_delay_;
216 force_cancel();
218 }
223 virtual void
225 {
227 start_time = NOW;
228 assert(left_duration > 0);
229 sched(left_duration);
231 }
232
236 virtual void
238 {
240 force_cancel();
241 }
242
247 virtual void
248 schedule(double val)
249 {
250 start_time = NOW;
251 left_duration = val;
253 resched(val);
254 }
255
260 bool
262 {
263 return (timer_status == UWOFDMALOHA_IDLE);
264 }
265
270 bool
272 {
274 }
275
280 bool
282 {
284 }
285
290 bool
292 {
294 }
295
300 bool
306
310 void
312 {
313 counter = 0;
314 }
315
320 void
322 {
323 ++counter;
324 }
325
330 int
332 {
333 return counter;
334 }
335
340 double
342 {
343 return left_duration;
344 }
345
346
347 protected:
348 double start_time;
354 UWOFDMAloha *module;
357 };
358
363 {
364
365 public:
371 {
372 }
373
377 virtual ~AckTimer()
378 {
379 }
380
381 protected:
386 virtual void expire(Event *e);
387 };
388
393 {
394
395 public:
401 {
402 }
403
408 {
409 }
410
411 protected:
416 virtual void expire(Event *e);
417 };
418
424 virtual void recvFromUpperLayers(Packet *p);
425
430 virtual void Mac2PhyStartTx(Packet *p);
431
436 virtual void Phy2MacEndTx(const Packet *p);
437
442 virtual void Phy2MacStartRx(const Packet *p);
443
448 virtual void Phy2MacEndRx(Packet *p);
449
456 virtual double computeTxTime(UWOFDMALOHA_PKT_TYPE type);
457
468 virtual void initPkt(
469 Packet *p, UWOFDMALOHA_PKT_TYPE pkt_type, int dest_addr = 0);
470
476 virtual double getBackoffTime();
477
481 virtual void txData();
482
487 virtual void txAck(int dest_addr);
488
493 virtual void stateIdle();
494
498 virtual void stateRxIdle();
499
504 virtual void stateTxData();
505
510 virtual void stateTxAck(int dest_addr);
511
516 virtual void stateWaitAck();
517
524 virtual void stateRxWaitAck();
525
530 virtual void stateBackoff();
531
537 virtual void stateRxBackoff();
538
543 virtual void stateCheckAckExpired();
544
549 virtual void stateCheckBackoffExpired();
550
556 virtual void stateRxData(Packet *p);
557
563 virtual void stateRxAck(Packet *p);
564
568 virtual void exitBackoff();
569
574 virtual void printStateInfo(double delay = 0);
575
579 virtual void initInfo();
580
587 virtual void
594
599 virtual void
601 {
602 last_reason = reason;
603 }
604
609 virtual void
611 {
613 }
614
618 virtual void
620 {
621 curr_tx_rounds = 0;
622 }
623
629 virtual void updateRTT(double rtt);
630
635 virtual double
637 {
638 return (rttsamples > 0) ? sumrtt / rttsamples : 0;
639 }
640
644 virtual void updateAckTimeout(double rtt);
645
650 virtual void
652 {
653 last_data_id_rx = id;
654 }
655
661 inline int
662 getPktSeqNum(Packet *p)
663 {
664 int seq_num;
665 hdr_cmn *ch = hdr_cmn::access(p);
666 seq_num = ch->uid();
667 return seq_num;
668 }
669
675 inline void
676 putPktInQueue(Packet *p)
677 {
678 mapPacket.insert(pair<pktSeqNum, Packet *>(getPktSeqNum(p), p));
680 std::cout <<"MAC " << addr << "putPktInQueue packet "<< getPktSeqNum(p) << std::endl;
681 }
682
687 inline void
689 {
690 map<pktSeqNum, Packet *>::iterator it_p;
691 it_p = mapPacket.find(seq_num);
693 std::cout << "MAC " << addr << "eraseItemFromPktQueue packet "<< seq_num << std::endl;
694 Packet::free((*it_p).second);
696 std::cout << "eraseItemFromPktQueue freed second " << std::endl;
697 mapPacket.erase((*it_p).first);
699 std::cout << "eraseItemFromPktQueue erased first " << std::endl;
700 }
701
706 inline void
707 putAckTimerInMap(int seq_num)
708 {
709 mapAckTimer.insert(pair<pktSeqNum, AckTimer>(seq_num, ack_timer));
710 }
711
716 inline void
718 {
719 map<pktSeqNum, AckTimer>::iterator it_a;
720 it_a = mapAckTimer.find(seq_num);
721 mapAckTimer.erase((*it_a).first);
722 }
723
729 virtual int
731 {
732 return (up_data_pkts_rx - mapPacket.size());
733 }
734
738 virtual void
740 {
741 up_data_pkts_rx++;
742 }
743
744 inline int
746 {
747 return up_data_pkts_rx;
748 }
749
753 inline void
755 {
756 hdr_OFDM* ofdmph = HDR_OFDM(p);
757
758 std::cout << NOW << "UwOFDMAloha ("<< addr <<")::displayCarriers";
759
760 for (int i=0; i< mac_ncarriers; i++)
761 std::cout << " car["<< i << "] = " << ofdmph->carriers[i];
762
763 std::cout <<" "<< std::endl;
764 return;
765
766 }
767
769 inline void
771 nouse_carriers.push_back(c);
772 }
773
781 double ACK_timeout;
783 double alpha_;
789
791 std::vector<string> mac_carMod; // Vector with carriers modulations
792 std::vector<int> mac_carVec; // Vector with carriers used/not used
793 int mac_ncarriers; // number of subcarriers
794 double mac_carrierSize; // size of each subcarrier
795 int ctrl_car; // number of control subcarriers
796 bool AckInBand = true; // if ACK uses full bandwidth
797 bool alohaOnCarrier = false;
798
799 int waitforpktnum = 0; //sequence number of the next expected packet
800
801 int n_receptions; //number of receptions at a certain moment
802
803 std::vector<Packet *> pkt_rcv_queue; //queue of packets being received
804
805 std::vector<int> nouse_carriers; //vectors of carriers that should not be used
806 bool disturbanceNode; // true if this is a disturbing node, not part of the
807 // main network
808
809 static bool initialized;
821 double srtt;
822 double sumrtt;
823 double sumrtt2;
833 int txsn;
835 static const double prop_speed;
854 static map<UWOFDMALOHA_STATUS, string> status_info;
857 static map<UWOFDMALOHA_REASON_STATUS, string>
860 static map<UWOFDMALOHA_PKT_TYPE, string>
864 map<pktSeqNum, Packet *>
866 map<pktSeqNum, AckTimer> mapAckTimer;
869 ofstream fout;
870};
871
872#endif /* UWUWOFDMALOHA_H_ */
Base class of AckTimer, which is a derived class of UWOFDMAlohaTimer.
virtual void expire(Event *e)
What a node is going to do when a timer expire.
virtual ~AckTimer()
Destructor of AckTimer Class.
AckTimer(UWOFDMAloha *m)
Constructor of AckTimer Class.
Base class of BackoffTimer.
BackOffTimer(UWOFDMAloha *m)
Constructor of BackOffTimer Class.
virtual ~BackOffTimer()
Destructor of BackOffTimer.
virtual void expire(Event *e)
What a node is going to do when a timer expire.
Base class of all the timer used in this protocol.
virtual void freeze()
It freezes or in another word, it stops the timer for some time.
void incrCounter()
Increment the timer counter.
bool isRunning()
This method tells whether the timer is in Running state or not.
bool isIdle()
It tells whether the timer is in Idle state or not.
bool isActive()
It tells whether the timer is active or not.
bool isExpired()
Tells whether the timer is expired or not.
bool isFrozen()
It tells whether the timer is in freeze mode or not.
virtual void schedule(double val)
Schedule the time, i.e., how long a timer is going to run.
int counter
How many times a timer ran.
UWOFDMAlohaTimer(UWOFDMAloha *m)
Constructor of UWOFDMAlohaTimer Class.
double start_time
Start time of a timer.
double left_duration
How long a timer is going to run more.
double getDuration()
This methods provide the duration of a timer.
virtual void stop()
Stop the timer any way.
virtual ~UWOFDMAlohaTimer()
Destructor of UWOFDMAlohaTimer Class.
virtual void unFreeze()
It starts the timer from where it was stopped.
void resetCounter()
Reset the timer counter.
int getCounter()
It provides, how many times a timer ran.
UWOFDMAloha *UWOFDMALOHA_TIMER_STATUS timer_status
< Pointer of UWOFDMAloha module.
virtual ~UWOFDMAloha()
Destructor of UWOFDMAloha Class.
UWOFDMALOHA_REASON_STATUS last_reason
Enum variable which stores the last reason why a node changes its state.
static map< UWOFDMALOHA_PKT_TYPE, string > pkt_type_info
Container which stores all the packet type information of UWOFDMAloha.
void eraseItemFromPktQueue(int seq_num)
It erases the packet from the container.
virtual void resetCurrTxRounds()
If a node is going to transmit a new packet, it resets the tx counter.
Packet * curr_data_pkt
Pointer of the latest selected data packet.
int getPktSeqNum(Packet *p)
This method is used to get the sequence number from a packet.
UWOFDMALOHA_REASON_STATUS
Enumeration class which tells the nodes the reason why it is in this state.
@ UWOFDMALOHA_REASON_ACK_TX
@ UWOFDMALOHA_REASON_BACKOFF_TIMEOUT
@ UWOFDMALOHA_REASON_DATA_RX
@ UWOFDMALOHA_REASON_PKT_RECEIVING
@ UWOFDMALOHA_REASON_DATA_EMPTY
@ UWOFDMALOHA_REASON_ACK_TIMEOUT
@ UWOFDMALOHA_REASON_ACK_RX
@ UWOFDMALOHA_REASON_DATA_PENDING
@ UWOFDMALOHA_REASON_PKT_ERROR
@ UWOFDMALOHA_REASON_MAX_TX_TRIES
@ UWOFDMALOHA_REASON_WAIT_ACK_PENDING
@ UWOFDMALOHA_REASON_BACKOFF_PENDING
@ UWOFDMALOHA_REASON_DATA_TX
@ UWOFDMALOHA_REASON_NOT_SET
@ UWOFDMALOHA_REASON_PKT_NOT_FOR_ME
@ UWOFDMALOHA_REASON_START_RX
virtual void txAck(int dest_addr)
This methods transmits ACK packet from MAC layer to PHY layer.
UWOFDMALOHA_ACK_MODES ack_mode
Enum variable.
UWOFDMALOHA_STATUS prev_state
Enum variable.
virtual void incrCurrTxRounds()
Increments the current transmission round of a packet.
UWOFDMAloha()
Constructor of UWOFDMAloha Class.
virtual void stateRxIdle()
If a node start receiving a packet in Idle state.
void init_macofdm_node(int subCarNum, double subCarSize, int ctrl_subCar, string modulation)
Initialize subCarriers parameters inside a node, default all carriers are used.
virtual void stateRxData(Packet *p)
It process the packet which is received.
double wait_constant
This fixed time is used to componsate different time variations.
virtual void stateCheckBackoffExpired()
It checks whether the backoff timer is already expired while it was busy with other activities.
int ACK_size
Size of the ACK, if the node uses ARQ technique.
map< pktSeqNum, Packet * > mapPacket
Container where Data packets are stored.
virtual void Phy2MacEndRx(Packet *p)
PHY layer informs the MAC layer that the reception of the packet is over.
int uwofdmaloha_debug
Debuging Flag.
virtual void refreshReason(UWOFDMALOHA_REASON_STATUS reason)
To know the reason why a node is in this current state.
double max_backoff_counter
Maximum number of backoff it will consider while it increases the backoff exponentially.
int rttsamples
Number of RTT samples.
std::vector< int > nouse_carriers
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void stateTxAck(int dest_addr)
If the protocl uses ARQ technique, in that case, after receiving a Data packet the node sends an ACK ...
double srtt
Smoothed Round Trip Time, calculated as for TCP.
std::vector< Packet * > pkt_rcv_queue
UWOFDMALOHA_ACK_MODES
Enumeration class of UWOFDMAloha acknowledgement mode.
virtual void exitBackoff()
It stops the backoff timer.
int last_sent_data_id
sequence number of the last sent packet
static const double prop_speed
Speed of the sound signal.
int last_data_id_rx
The sequence number of last received packet.
virtual double getBackoffTime()
This function calculates the backoff duration and return the backoff time.It employs the exponential ...
ofstream fout
An object of ofstream class.
virtual void updateLastDataIdRx(int id)
It updates the sequence number of the last data packet rx.
int curr_tx_rounds
How many times a packet is transmitted.
virtual void refreshState(UWOFDMALOHA_STATUS state)
Refreshes the states of the node.
virtual void initPkt(Packet *p, UWOFDMALOHA_PKT_TYPE pkt_type, int dest_addr=0)
This method, initialize the packet.
virtual void stateBackoff()
If ACK packet is not received within the acknowledgement expire time.
void addInvalidCarriers(int c)
Adds carriers that should not be used.
virtual double computeTxTime(UWOFDMALOHA_PKT_TYPE type)
Compute the transmission time of a packet.
UWOFDMALOHA_STATUS curr_state
Enum variable.
double start_tx_time
Time when a packet start transmitting.
double alpha_
This variable is used to tune the RTT.
virtual void Phy2MacEndTx(const Packet *p)
It infroms that a packet transmission end.
virtual void recvFromUpperLayers(Packet *p)
This function receives the packet from upper layer and save it in the queue.
double ACK_timeout
ACK timeout for the initial packet.
static map< UWOFDMALOHA_REASON_STATUS, string > reason_info
Container which stores all the reason information.
virtual void Mac2PhyStartTx(Packet *p)
It informs that a packet transmission started.
virtual void stateRxAck(Packet *p)
The node comes to this state if it receives an ACK packet.
static map< UWOFDMALOHA_STATUS, string > status_info
Container which stores all the status information.
virtual void txData()
This method transmits Data packets from MAC layer to PHY layer.
int buffer_pkts
Number of packets a node can store in the container.
virtual void updateRTT(double rtt)
Update the Round Trip Time (RTT) which is necessary to compute the acknowledgement duration as well a...
virtual double getRTT()
This method is used to get the average RTT over all the receives RTT.
map< pktSeqNum, AckTimer > mapAckTimer
Container where acknowledgement timer(s) is stored.
virtual void stateRxBackoff()
If a node start receiving a packet when it is in backoff state.
virtual void stateTxData()
If a node has packet to transmits.
std::vector< string > mac_carMod
--— OFDM PARAMS
UWOFDMALOHA_PKT_TYPE
Enumeration class of UWOFDMAloha packet type.
virtual int getRemainingPkts()
Number of packets which MAC layer receives form upper layer(s) but were not transmitted.
virtual void Phy2MacStartRx(const Packet *p)
PHY layer informs the MAC layer that it is receiving a packet.
void putPktInQueue(Packet *p)
A node receives packet(s) from upper layer and store them in the container.
int getUpLayersDataPktsRx()
bool print_transitions
Whether to print the state of the nodes.
double mac_carrierSize
AckTimer ack_timer
An object of the AckTimer class.
int recv_data_id
The sequence number of the packet which is received.
int max_tx_tries
Maximum number of retransmissions attempt.
UWOFDMALOHA_STATUS
Enumeration class of UWOFDMAloha status.
@ UWOFDMALOHA_STATE_NOT_SET
@ UWOFDMALOHA_STATE_BACKOFF
@ UWOFDMALOHA_STATE_CHK_BACKOFF_TIMEOUT
@ UWOFDMALOHA_STATE_WRONG_PKT_RX
@ UWOFDMALOHA_STATE_WAIT_ACK
@ UWOFDMALOHA_STATE_DATA_RX
@ UWOFDMALOHA_STATE_RX_IDLE
@ UWOFDMALOHA_STATE_RX_IN_PROGRESS
@ UWOFDMALOHA_STATE_TX_DATA
@ UWOFDMALOHA_STATE_CHK_ACK_TIMEOUT
@ UWOFDMALOHA_STATE_MULTIPLE_RX_IN_PROGRESS
@ UWOFDMALOHA_STATE_RX_BACKOFF
@ UWOFDMALOHA_STATE_RX_WAIT_ACK
int txsn
Sequence number of the packet which is transmitted.
virtual void stateRxWaitAck()
If a node receives any packet while it was waiting for ACK packet, it moves to this state.
double sumrtt
Sum of RTT samples.
bool has_buffer_queue
Whether the node has buffer to store data or not.
BackOffTimer backoff_timer
An object of the backoff timer class.
virtual void printStateInfo(double delay=0)
This methods print the state information of the nodes.
double backoff_tuner
Tunes the backoff duration.
double sumrtt2
Sum of (RTT^2)
virtual void updateAckTimeout(double rtt)
Like updateRTT() function.
int max_payload
Maximum number of payload in a packet.
static bool initialized
It checks whether UWOFDMAloha protocol is initialized or not.
virtual void incrUpperDataRx()
Increment the number of Data packet receive for the upper layer.
int HDR_size
Size of the HDR if any.
virtual void stateCheckAckExpired()
It checks whether the ack timer is already expired while it was busy with other activities.
void displayCarriers(Packet *p)
Prints the carriers as a sequence of 0s and 1s.
virtual void stateWaitAck()
After transmitting a Data packet, a node waits for the ACK packet.
std::vector< int > mac_carVec
UWOFDMALOHA_STATUS prev_prev_state
Enum variable.
UWOFDMALOHA_TIMER_STATUS
Enumeration class of UWOFDMAloha timer status.
virtual void initInfo()
This function is used to initialize the UWOFDMAloha protocol.
virtual void stateIdle()
Node is in Idle state.
void putAckTimerInMap(int seq_num)
Put acknowledgement timer in the container.
void eraseItemFrommapAckTimer(int seq_num)
Erase an item from acknowledgement stored container.
Header of the OFDM message with fields to implement a multi carrier system.
int carriers[MAX_CARRIERS]
int pktSeqNum
std::pair< int, int > counter
counter of collisions
Common structures and variables in the protocol.
#define HDR_OFDM(p)
alias defined to access the PROBE HEADER