DESERT 3.5.1
Loading...
Searching...
No Matches
uwaloha.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
40#ifndef UWALOHA_H_
41#define UWALOHA_H_
42
43#include <mmac.h>
44#include <iostream>
45#include <string>
46#include <map>
47#include <set>
48#include <queue>
49#include <fstream>
50
51#include <mphy.h>
52
53#define UWALOHA_DROP_REASON_WRONG_STATE "WST"
54#define UWALOHA_DROP_REASON_WRONG_RECEIVER "WRCV"
55#define UWALOHA_DROP_REASON_UNKNOWN_TYPE "UPT"
56#define UWALOHA_DROP_REASON_BUFFER_FULL "DBF"
57#define UWALOHA_DROP_REASON_ERROR "ERR"
58
59extern packet_t PT_MMAC_ACK;
60
61typedef int pktSeqNum;
62
68class UWAloha : public MMac
69{
70
71public:
75 UWAloha();
76
80 virtual ~UWAloha();
81
90 virtual int command(int argc, const char *const *argv);
91
92protected:
112
134
144
152
163
168 class UWAlohaTimer : public TimerHandler
169 {
170
171 public:
176 : TimerHandler()
177 , start_time(0.0)
178 , left_duration(0.0)
179 , counter(0)
180 , module(m)
182 {
183 assert(m != NULL);
184 }
185
190 {
191 }
192
200 virtual void
202 {
203 assert(timer_status == UWALOHA_RUNNING);
204 left_duration -= (NOW - start_time);
205 if (left_duration <= 0.0)
206 left_duration = module->mac2phy_delay_;
207 force_cancel();
209 }
214 virtual void
216 {
217 assert(timer_status == UWALOHA_FROZEN);
218 start_time = NOW;
219 assert(left_duration > 0);
220 sched(left_duration);
222 }
223
227 virtual void
229 {
231 force_cancel();
232 }
233
238 virtual void
239 schedule(double val)
240 {
241 start_time = NOW;
242 left_duration = val;
244 resched(val);
245 }
246
251 bool
253 {
254 return (timer_status == UWALOHA_IDLE);
255 }
256
261 bool
263 {
264 return (timer_status == UWALOHA_RUNNING);
265 }
266
271 bool
273 {
274 return (timer_status == UWALOHA_EXPIRED);
275 }
276
281 bool
283 {
284 return (timer_status == UWALOHA_FROZEN);
285 }
286
291 bool
293 {
294 return (timer_status == UWALOHA_FROZEN ||
296 }
297
301 void
303 {
304 counter = 0;
305 }
306
311 void
313 {
314 ++counter;
315 }
316
321 int
323 {
324 return counter;
325 }
326
331 double
333 {
334 return left_duration;
335 }
336
337 protected:
338 double start_time;
344 UWAloha *module;
347 };
348
352 class AckTimer : public UWAlohaTimer
353 {
354
355 public:
360 : UWAlohaTimer(m)
361 {
362 }
363
367 virtual ~AckTimer()
368 {
369 }
370
371 protected:
376 virtual void expire(Event *e);
377 };
378
383 {
384
385 public:
390 : UWAlohaTimer(m)
391 {
392 }
393
398 {
399 }
400
401 protected:
406 virtual void expire(Event *e);
407 };
408
414 virtual void recvFromUpperLayers(Packet *p);
415
420 virtual void Mac2PhyStartTx(Packet *p);
421
426 virtual void Phy2MacEndTx(const Packet *p);
427
432 virtual void Phy2MacStartRx(const Packet *p);
433
438 virtual void Phy2MacEndRx(Packet *p);
439
446 virtual double computeTxTime(UWALOHA_PKT_TYPE type);
447
458 virtual void initPkt(
459 Packet *p, UWALOHA_PKT_TYPE pkt_type, int dest_addr = 0);
460
466 virtual double getBackoffTime();
467
471 virtual void txData();
472
477 virtual void txAck(int dest_addr);
478
483 virtual void stateIdle();
484
488 virtual void stateRxIdle();
489
494 virtual void stateTxData();
495
500 virtual void stateTxAck(int dest_addr);
501
506 virtual void stateWaitAck();
507
514 virtual void stateRxWaitAck();
515
520 virtual void stateBackoff();
521
527 virtual void stateRxBackoff();
528
533 virtual void stateCheckAckExpired();
534
539 virtual void stateCheckBackoffExpired();
540
546 virtual void stateRxData(Packet *p);
547
553 virtual void stateRxAck(Packet *p);
554
558 virtual void exitBackoff();
559
564 virtual void printStateInfo(double delay = 0);
565
569 virtual void initInfo();
570
577 virtual void
584
589 virtual void
591 {
592 last_reason = reason;
593 }
594
599 virtual void
601 {
603 }
604
608 virtual void
610 {
611 curr_tx_rounds = 0;
612 }
613
619 virtual void updateRTT(double rtt);
620
625 virtual double
627 {
628 return (rttsamples > 0) ? sumrtt / rttsamples : 0;
629 }
630
634 virtual void updateAckTimeout(double rtt);
635
640 virtual void
642 {
643 last_data_id_rx = id;
644 }
645 virtual void waitForUser();
646
652 inline int
653 getPktSeqNum(Packet *p)
654 {
655 int seq_num;
656 hdr_cmn *ch = hdr_cmn::access(p);
657 seq_num = ch->uid();
658 return seq_num;
659 }
660
666 inline void
667 putPktInQueue(Packet *p)
668 {
669 mapPacket.insert(pair<pktSeqNum, Packet *>(getPktSeqNum(p), p));
670 }
671
676 inline void
678 {
679 map<pktSeqNum, Packet *>::iterator it_p;
680 it_p = mapPacket.find(seq_num);
681 if (it_p != mapPacket.end())
682 {
683 Packet::free((*it_p).second);
684 mapPacket.erase((*it_p).first);
685 }
686 }
687
692 inline void
693 putAckTimerInMap(int seq_num)
694 {
695 mapAckTimer.insert(pair<pktSeqNum, AckTimer>(seq_num, ack_timer));
696 }
697
702 inline void
704 {
705 map<pktSeqNum, AckTimer>::iterator it_a;
706 it_a = mapAckTimer.find(seq_num);
707 if (it_a != mapAckTimer.end())
708 {
709 mapAckTimer.erase((*it_a).first);
710 }
711 }
712
718 virtual int
720 {
721 return (up_data_pkts_rx - mapPacket.size());
722 }
723
727 virtual void
729 {
730 up_data_pkts_rx++;
731 }
732
733 inline int
735 {
736 return up_data_pkts_rx;
737 }
738
747 double ACK_timeout;
749 double alpha_;
755
756 static bool initialized;
764 bool
769 double srtt;
770 double sumrtt;
771 double sumrtt2;
781 int txsn;
783 static const double prop_speed;
802 static map<UWALOHA_STATUS, string> status_info;
805 static map<UWALOHA_REASON_STATUS, string>
808 static map<UWALOHA_PKT_TYPE, string>
812 map<pktSeqNum, Packet *>
814 map<pktSeqNum, AckTimer> mapAckTimer;
817 ofstream fout;
818};
819
820#endif /* UWUWALOHA_H_ */
Base class of AckTimer, which is a derived class of UWAlohaTimer.
Definition uwaloha.h:353
virtual void expire(Event *e)
What a node is going to do when a timer expire.
Definition uwaloha.cpp:68
virtual ~AckTimer()
Destructor of AckTimer Class.
Definition uwaloha.h:367
AckTimer(UWAloha *m)
Constructor of AckTimer Class.
Definition uwaloha.h:359
Base class of BackoffTimer.
Definition uwaloha.h:383
virtual void expire(Event *e)
What a node is going to do when a timer expire.
Definition uwaloha.cpp:90
BackOffTimer(UWAloha *m)
Constructor of BackOffTimer Class.
Definition uwaloha.h:389
virtual ~BackOffTimer()
Destructor of BackOffTimer.
Definition uwaloha.h:397
Base class of all the timer used in this protocol.
Definition uwaloha.h:169
int counter
How many times a timer ran.
Definition uwaloha.h:342
virtual void unFreeze()
It starts the timer from where it was stopped.
Definition uwaloha.h:215
double getDuration()
This methods provide the duration of a timer.
Definition uwaloha.h:332
virtual void stop()
Stop the timer any way.
Definition uwaloha.h:228
int getCounter()
It provides, how many times a timer ran.
Definition uwaloha.h:322
double start_time
Start time of a timer.
Definition uwaloha.h:338
virtual void freeze()
It freezes or in another word, it stops the timer for some time.
Definition uwaloha.h:201
bool isIdle()
It tells whether the timer is in Idle state or not.
Definition uwaloha.h:252
bool isRunning()
This method tells whether the timer is in Running state or not.
Definition uwaloha.h:262
void resetCounter()
Reset the timer counter.
Definition uwaloha.h:302
virtual ~UWAlohaTimer()
Destructor of UWAlohaTimer Class.
Definition uwaloha.h:189
double left_duration
How long a timer is going to run more.
Definition uwaloha.h:340
UWAloha *UWALOHA_TIMER_STATUS timer_status
< Pointer of UWAloha module.
Definition uwaloha.h:346
virtual void schedule(double val)
Schedule the time, i.e., how long a timer is going to run.
Definition uwaloha.h:239
UWAlohaTimer(UWAloha *m)
Constructor of UWAlohaTimer Class.
Definition uwaloha.h:175
void incrCounter()
Increment the timer counter.
Definition uwaloha.h:312
bool isActive()
It tells whether the timer is active or not.
Definition uwaloha.h:292
bool isExpired()
Tells whether the timer is expired or not.
Definition uwaloha.h:272
bool isFrozen()
It tells whether the timer is in freeze mode or not.
Definition uwaloha.h:282
This is the base class of UWAloha protocol, which is a derived class of MMac.
Definition uwaloha.h:69
void putAckTimerInMap(int seq_num)
Put acknowledgement timer in the container.
Definition uwaloha.h:693
int rttsamples
Number of RTT samples.
Definition uwaloha.h:772
int txsn
Sequence number of the packet which is transmitted.
Definition uwaloha.h:781
static map< UWALOHA_STATUS, string > status_info
Container which stores all the status information.
Definition uwaloha.h:802
double start_tx_time
Time when a packet start transmitting.
Definition uwaloha.h:768
virtual void waitForUser()
Definition uwaloha.cpp:840
static map< UWALOHA_REASON_STATUS, string > reason_info
Container which stores all the reason information.
Definition uwaloha.h:806
UWALOHA_STATUS prev_prev_state
Enum variable.
Definition uwaloha.h:796
BackOffTimer backoff_timer
An object of the backoff timer class.
Definition uwaloha.h:786
void eraseItemFromPktQueue(int seq_num)
It erases the packet from the container.
Definition uwaloha.h:677
virtual ~UWAloha()
Destructor of UWAloha Class.
Definition uwaloha.cpp:162
UWAloha()
Constructor of UWAloha Class.
Definition uwaloha.cpp:119
static bool initialized
It checks whether UWAloha protocol is initialized or not.
Definition uwaloha.h:756
virtual void stateTxData()
If a node has packet to transmits.
Definition uwaloha.cpp:661
int max_payload
Maximum number of payload in a packet.
Definition uwaloha.h:744
UWALOHA_TIMER_STATUS
Enumeration class of UWAloha timer status.
Definition uwaloha.h:157
@ UWALOHA_IDLE
Definition uwaloha.h:158
@ UWALOHA_FROZEN
Definition uwaloha.h:160
@ UWALOHA_RUNNING
Definition uwaloha.h:159
@ UWALOHA_EXPIRED
Definition uwaloha.h:161
UWALOHA_ACK_MODES
Enumeration class of UWAloha acknowledgement mode.
Definition uwaloha.h:151
@ UWALOHA_NO_ACK_MODE
Definition uwaloha.h:151
@ UWALOHA_ACK_MODE
Definition uwaloha.h:151
int last_sent_data_id
sequence number of the last sent packet
Definition uwaloha.h:761
virtual void txData()
This method transmits Data packets from MAC layer to PHY layer.
Definition uwaloha.cpp:524
virtual void Phy2MacStartRx(const Packet *p)
PHY layer informs the MAC layer that it is receiving a packet.
Definition uwaloha.cpp:457
virtual void stateRxData(Packet *p)
It process the packet which is received.
Definition uwaloha.cpp:764
virtual void stateRxAck(Packet *p)
The node comes to this state if it receives an ACK packet.
Definition uwaloha.cpp:789
UWALOHA_PKT_TYPE
Enumeration class of UWAloha packet type.
Definition uwaloha.h:139
@ UWALOHA_ACK_PKT
Definition uwaloha.h:140
@ UWALOHA_DATAMAX_PKT
Definition uwaloha.h:142
@ UWALOHA_DATA_PKT
Definition uwaloha.h:141
bool print_transitions
Whether to print the state of the nodes.
Definition uwaloha.h:763
int buffer_pkts
Number of packets a node can store in the container.
Definition uwaloha.h:748
double srtt
Smoothed Round Trip Time, calculated as for TCP.
Definition uwaloha.h:769
virtual void stateWaitAck()
After transmitting a Data packet, a node waits for the ACK packet.
Definition uwaloha.cpp:722
UWALOHA_STATUS prev_state
Enum variable.
Definition uwaloha.h:794
virtual void updateLastDataIdRx(int id)
It updates the sequence number of the last data packet rx.
Definition uwaloha.h:641
virtual void stateCheckAckExpired()
It checks whether the ack timer is already expired while it was busy with other activities.
Definition uwaloha.cpp:548
double ACK_timeout
ACK timeout for the initial packet.
Definition uwaloha.h:747
double backoff_tuner
Tunes the backoff duration.
Definition uwaloha.h:743
virtual void stateIdle()
Node is in Idle state.
Definition uwaloha.cpp:605
int getUpLayersDataPktsRx()
Definition uwaloha.h:734
virtual void resetCurrTxRounds()
If a node is going to transmit a new packet, it resets the tx counter.
Definition uwaloha.h:609
virtual void updateRTT(double rtt)
Update the Round Trip Time (RTT) which is necessary to compute the acknowledgement duration as well a...
Definition uwaloha.cpp:255
UWALOHA_REASON_STATUS
Enumeration class which tells the nodes the reason why it is in this state.
Definition uwaloha.h:117
@ UWALOHA_REASON_BACKOFF_PENDING
Definition uwaloha.h:132
@ UWALOHA_REASON_DATA_PENDING
Definition uwaloha.h:118
@ UWALOHA_REASON_DATA_EMPTY
Definition uwaloha.h:124
@ UWALOHA_REASON_WAIT_ACK_PENDING
Definition uwaloha.h:129
@ UWALOHA_REASON_DATA_TX
Definition uwaloha.h:120
@ UWALOHA_REASON_START_RX
Definition uwaloha.h:127
@ UWALOHA_REASON_BACKOFF_TIMEOUT
Definition uwaloha.h:131
@ UWALOHA_REASON_MAX_TX_TRIES
Definition uwaloha.h:126
@ UWALOHA_REASON_ACK_TX
Definition uwaloha.h:121
@ UWALOHA_REASON_NOT_SET
Definition uwaloha.h:125
@ UWALOHA_REASON_ACK_TIMEOUT
Definition uwaloha.h:123
@ UWALOHA_REASON_ACK_RX
Definition uwaloha.h:122
@ UWALOHA_REASON_PKT_ERROR
Definition uwaloha.h:130
@ UWALOHA_REASON_PKT_NOT_FOR_ME
Definition uwaloha.h:128
@ UWALOHA_REASON_DATA_RX
Definition uwaloha.h:119
virtual void initInfo()
This function is used to initialize the UWAloha protocol.
Definition uwaloha.cpp:208
virtual double computeTxTime(UWALOHA_PKT_TYPE type)
Compute the transmission time of a packet.
Definition uwaloha.cpp:310
int curr_tx_rounds
How many times a packet is transmitted.
Definition uwaloha.h:774
virtual void stateCheckBackoffExpired()
It checks whether the backoff timer is already expired while it was busy with other activities.
Definition uwaloha.cpp:578
virtual void stateRxIdle()
If a node start receiving a packet in Idle state.
Definition uwaloha.cpp:625
UWALOHA_STATUS curr_state
Enum variable.
Definition uwaloha.h:791
UWALOHA_STATUS
Enumeration class of UWAloha status.
Definition uwaloha.h:96
@ UWALOHA_STATE_TX_ACK
Definition uwaloha.h:100
@ UWALOHA_STATE_CHK_ACK_TIMEOUT
Definition uwaloha.h:105
@ UWALOHA_STATE_RX_BACKOFF
Definition uwaloha.h:109
@ UWALOHA_STATE_ACK_RX
Definition uwaloha.h:103
@ UWALOHA_STATE_RX_WAIT_ACK
Definition uwaloha.h:107
@ UWALOHA_STATE_TX_DATA
Definition uwaloha.h:99
@ UWALOHA_STATE_NOT_SET
Definition uwaloha.h:104
@ UWALOHA_STATE_IDLE
Definition uwaloha.h:97
@ UWALOHA_STATE_BACKOFF
Definition uwaloha.h:98
@ UWALOHA_STATE_DATA_RX
Definition uwaloha.h:102
@ UWALOHA_STATE_WAIT_ACK
Definition uwaloha.h:101
@ UWALOHA_STATE_CHK_BACKOFF_TIMEOUT
Definition uwaloha.h:108
@ UWALOHA_STATE_WRONG_PKT_RX
Definition uwaloha.h:110
@ UWALOHA_STATE_RX_IDLE
Definition uwaloha.h:106
double sumrtt
Sum of RTT samples.
Definition uwaloha.h:770
virtual void Phy2MacEndTx(const Packet *p)
It infroms that a packet transmission end.
Definition uwaloha.cpp:403
virtual double getRTT()
This method is used to get the average RTT over all the receives RTT.
Definition uwaloha.h:626
int getPktSeqNum(Packet *p)
This method is used to get the sequence number from a packet.
Definition uwaloha.h:653
virtual void stateRxBackoff()
If a node start receiving a packet when it is in backoff state.
Definition uwaloha.cpp:651
virtual void exitBackoff()
It stops the backoff timer.
Definition uwaloha.cpp:280
virtual int getRemainingPkts()
Number of packets which MAC layer receives form upper layer(s) but were not transmitted.
Definition uwaloha.h:719
int last_data_id_rx
The sequence number of last received packet.
Definition uwaloha.h:775
int max_backoff_counter
Maximum number of backoff it will consider while it increases the backoff exponentially.
Definition uwaloha.h:750
virtual void Phy2MacEndRx(Packet *p)
PHY layer informs the MAC layer that the reception of the packet is over.
Definition uwaloha.cpp:465
static const double prop_speed
Speed of the sound signal.
Definition uwaloha.h:783
bool has_buffer_queue
Whether the node has buffer to store data or not.
Definition uwaloha.h:765
virtual double getBackoffTime()
This function calculates the backoff duration and return the backoff time.It employs the exponential ...
Definition uwaloha.cpp:286
virtual void incrCurrTxRounds()
Increments the current transmission round of a packet.
Definition uwaloha.h:600
void putPktInQueue(Packet *p)
A node receives packet(s) from upper layer and store them in the container.
Definition uwaloha.h:667
AckTimer ack_timer
An object of the AckTimer class.
Definition uwaloha.h:785
virtual void Mac2PhyStartTx(Packet *p)
It informs that a packet transmission started.
Definition uwaloha.cpp:393
map< pktSeqNum, AckTimer > mapAckTimer
Container where acknowledgement timer(s) is stored.
Definition uwaloha.h:814
UWALOHA_ACK_MODES ack_mode
Enum variable.
Definition uwaloha.h:800
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
Definition uwaloha.cpp:169
int max_tx_tries
Maximum number of transmissions for one packet.
Definition uwaloha.h:740
static map< UWALOHA_PKT_TYPE, string > pkt_type_info
Container which stores all the packet type information of UWAloha.
Definition uwaloha.h:809
UWALOHA_REASON_STATUS last_reason
Enum variable which stores the last reason why a node changes its state.
Definition uwaloha.h:789
virtual void stateBackoff()
If ACK packet is not received within the acknowledgement expire time.
Definition uwaloha.cpp:634
virtual void initPkt(Packet *p, UWALOHA_PKT_TYPE pkt_type, int dest_addr=0)
This method, initialize the packet.
Definition uwaloha.cpp:369
void eraseItemFrommapAckTimer(int seq_num)
Erase an item from acknowledgement stored container.
Definition uwaloha.h:703
int ACK_size
Size of the ACK, if the node uses ARQ technique.
Definition uwaloha.h:746
double sumrtt2
Sum of (RTT^2)
Definition uwaloha.h:771
virtual void stateRxWaitAck()
If a node receives any packet while it was waiting for ACK packet, it moves to this state.
Definition uwaloha.cpp:741
int recv_data_id
The sequence number of the packet which is received.
Definition uwaloha.h:776
double wait_constant
This fixed time is used to componsate different time variations.
Definition uwaloha.h:741
map< pktSeqNum, Packet * > mapPacket
Container where Data packets are stored.
Definition uwaloha.h:813
virtual void incrUpperDataRx()
Increment the number of Data packet receive for the upper layer.
Definition uwaloha.h:728
virtual void refreshState(UWALOHA_STATUS state)
Refreshes the states of the node.
Definition uwaloha.h:578
int uwaloha_debug
Debuging Flag.
Definition uwaloha.h:753
virtual void recvFromUpperLayers(Packet *p)
This function receives the packet from upper layer and save it in the queue.
Definition uwaloha.cpp:339
ofstream fout
An object of ofstream class.
Definition uwaloha.h:817
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 ...
Definition uwaloha.cpp:750
virtual void updateAckTimeout(double rtt)
Like updateRTT() function.
Definition uwaloha.cpp:265
virtual void printStateInfo(double delay=0)
This methods print the state information of the nodes.
Definition uwaloha.cpp:815
virtual void refreshReason(UWALOHA_REASON_STATUS reason)
To know the reason why a node is in this current state.
Definition uwaloha.h:590
double alpha_
This variable is used to tune the RTT.
Definition uwaloha.h:749
Packet * curr_data_pkt
Pointer of the latest selected data packet.
Definition uwaloha.h:779
int HDR_size
Size of the HDR if any.
Definition uwaloha.h:745
virtual void txAck(int dest_addr)
This methods transmits ACK packet from MAC layer to PHY layer.
Definition uwaloha.cpp:538
int pktSeqNum
Definition uwaloha.h:61
std::pair< int, int > counter
counter of collisions