DESERT 3.6.0
Loading...
Searching...
No Matches
uw-csma-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//
29
39#ifndef CSMA_H
40#define CSMA_H
41
42#include <fstream>
43#include <map>
44#include <queue>
45#include <string>
46
47#include <mphy.h>
48#include <mmac.h>
49
50#define CSMA_DROP_REASON_WRONG_STATE \
51 "WST"
52#define CSMA_DROP_REASON_WRONG_RECEIVER \
53 "WRCV"
54#define CSMA_DROP_REASON_UNKNOWN_TYPE \
55 "UPT"
56#define CSMA_DROP_REASON_BUFFER_FULL \
57 "DBF"
58#define CSMA_DROP_REASON_ERROR "ERR"
60extern packet_t PT_MMAC_ACK;
61
65class CsmaAloha : public MMac
66{
67public:
71 CsmaAloha();
75 virtual ~CsmaAloha() = default;
86 virtual int command(int argc, const char *const *argv) override;
87
88protected:
89 static const double prop_speed;
112
134
137
140
148
152 class AlohaTimer : public TimerHandler
153 {
154 public:
160 : TimerHandler()
161 , start_time(0.0)
162 , left_duration(0.0)
163 , counter(0)
164 , module(m)
166 {
167 assert(m != NULL);
168 }
169
173 virtual ~AlohaTimer() = default;
174
178 virtual void
180 {
181 assert(timer_status == CSMA_RUNNING);
182 left_duration -= (NOW - start_time);
183 if (left_duration <= 0.0)
184 left_duration = module->mac2phy_delay_;
185 force_cancel();
187 }
188
193 virtual void
195 {
196 assert(timer_status == CSMA_FROZEN);
197 start_time = NOW;
198 assert(left_duration > 0);
199 sched(left_duration);
201 }
202
206 virtual void
208 {
210 force_cancel();
211 }
212
217 virtual void
218 schedule(double val)
219 {
220 start_time = NOW;
221 left_duration = val;
223 resched(val);
224 }
225
230 bool
232 {
233 return (timer_status == CSMA_IDLE);
234 }
235
240 bool
242 {
243 return (timer_status == CSMA_RUNNING);
244 }
245
251 bool
253 {
254 return (timer_status == CSMA_EXPIRED);
255 }
256
261 bool
263 {
264 return (timer_status == CSMA_FROZEN);
265 }
266
271 bool
273 {
274 return (timer_status == CSMA_FROZEN ||
276 }
277
281 void
283 {
284 counter = 0;
285 }
286
290 void
292 {
293 ++counter;
294 }
295
300 int
302 {
303 return counter;
304 }
305
310 double
312 {
313 return left_duration;
314 }
315
316 protected:
317 double start_time;
323 CsmaAloha *module;
325 };
326
331 {
332 public:
338 : AlohaTimer(m)
339 {
340 }
341
345 virtual ~BackOffTimer() = default;
346
347 protected:
352 virtual void expire(Event *e);
353 };
354
358 class AckTimer : public AlohaTimer
359 {
360 public:
366 : AlohaTimer(m)
367 {
368 }
369
373 virtual ~AckTimer() = default;
374
375 protected:
380 virtual void expire(Event *e);
381 };
382
388 class ListenTimer : public AlohaTimer
389 {
390 public:
396 : AlohaTimer(m)
397 {
398 }
399
403 virtual ~ListenTimer() = default;
404
405 protected:
410 virtual void expire(Event *e);
411 };
412
418 virtual void recvFromUpperLayers(Packet *p) override;
419
425 virtual void Mac2PhyStartTx(Packet *p);
426
432 virtual void Phy2MacEndTx(const Packet *p) override;
433
439 virtual void Phy2MacStartRx(const Packet *p) override;
440
446 virtual void Phy2MacEndRx(Packet *p) override;
447
453 virtual double computeTxTime(CSMA_PKT_TYPE type);
454
462 virtual void initPkt(Packet *p, CSMA_PKT_TYPE pkt_type, int dest_addr = 0);
463
471 virtual double getBackoffTime();
472
477 virtual void txData();
478
484 virtual void txAck(int dest_addr);
485
489 virtual void stateIdle();
490
494 virtual void stateRxIdle();
495
499 virtual void stateTxData();
500
505 virtual void stateTxAck(int dest_addr);
506
512 virtual void stateBackoff();
513
518 virtual void stateRxBackoff();
519
524 virtual void stateWaitAck();
525
529 virtual void stateRxWaitAck();
530
534 virtual void stateListen();
535
540 virtual void stateRxListen();
541
545 virtual void stateCheckListenExpired();
546
550 virtual void stateCheckAckExpired();
551
555 virtual void stateCheckBackoffExpired();
556
560 virtual void stateRxData(Packet *p);
561
565 virtual void stateRxAck(Packet *p);
566
570 virtual void stateRxPacketNotForMe(Packet *p);
571
578 virtual void printStateInfo(double delay = 0);
579
585 virtual void initInfo();
586
590 virtual void
597
601 virtual void
603 {
604 last_reason = reason;
605 }
609 virtual void exitBackoff();
610
615 virtual void
616 setSessionDistance(double distance)
617 {
618 session_distance = distance;
619 }
620
629 virtual bool keepDataPkt(int serial_number);
630
634 virtual void
636 {
638 }
639
643 virtual void
645 {
646 curr_tx_rounds = 0;
647 }
648
656 virtual void updateRTT(double rtt);
657
662 virtual double
663 getRTT() const
664 {
665 return (rttsamples > 0) ? sumrtt / rttsamples : 0;
666 }
667
674 virtual void updateAckTimeout(double rtt);
675
679 virtual void
681 {
682 last_data_id_rx = id;
683 }
684
688 virtual void
689 queuePop(bool flag = true)
690 {
691 Packet::free(Q.front());
692 Q.pop();
693 waitEndTime(flag);
694 data_sn_queue.pop();
695 }
699 virtual void resetSession();
700
707 virtual int
709 {
710 return (up_data_pkts_rx - Q.size());
711 }
712
716 virtual void
718 {
719 up_data_pkts_rx++;
720 }
721
722 virtual int
724 {
725 return up_data_pkts_rx;
726 }
727
736 double ACK_timeout;
738 double alpha_;
740 double listen_time;
742 std::queue<Packet *> Q;
743 std::queue<int>
746 static bool initialized;
748 static int u_pkt_id;
752 bool TxActive;
753 bool RxActive;
765 double srtt;
766 double sumrtt;
767 double sumrtt2;
791 static map<CSMA_STATUS, string>
793 static map<CSMA_REASON_STATUS, string>
796 static map<CSMA_PKT_TYPE, string>
799 ofstream fout;
801};
802
803#endif /* CSMA_H */
Class used to handle the timer for waiting the ACK.
virtual ~AckTimer()=default
Destructor of AckTimer class.
AckTimer(CsmaAloha *m)
Conscructor of AckTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
Class that describes the timers in the node.
virtual ~AlohaTimer()=default
Destructor of the AlohaTimer class.
virtual void unFreeze()
unFreezes is used to resume the timer starting from the point where it was freezed
virtual void schedule(double val)
schedule a timer
void incrCounter()
Increments the counter of the timer.
virtual void stop()
stops the timer
CsmaAloha *CSMA_TIMER_STATUS timer_status
< Pointer to an object of type CsmaAloha
double left_duration
Left duration of the timer.
bool isFrozen()
Checks if the timer is FROZEN.
double start_time
Start Time of the timer.
bool isActive()
Checks if the timer is ACTIVE.
int counter
counter of the timer
void resetCounter()
Resets the counter of the timer.
virtual void freeze()
Freezes the timer.
AlohaTimer(CsmaAloha *m)
Constructor of the AlohaTimer class.
bool isRunning()
checks if the timer is RUNNING
int getCounter() const
Returns the counter of the timer.
double getDuration() const
Returns the left duration of the timer.
bool isIdle()
checks if the timer is IDLE
bool isExpired()
Checks if the timer is EXPIRED.
Class used to handle the timer of the backoff period.
virtual ~BackOffTimer()=default
Destructor of DataTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
BackOffTimer(CsmaAloha *m)
Conscructor of BackOffTimer class.
Class used to handle the Listen Timer.
ListenTimer(CsmaAloha *m)
Conscructor of ListenTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
virtual ~ListenTimer()=default
Destructor of AckTimer class.
Class that describes a CsmaAloha module.
int rttsamples
num of RTT samples
double ACK_timeout
Duration of the ACK waiting time.
int ACK_size
Size of the ACK message.
virtual void stateRxWaitAck()
State in which a reception is occuring while the node is waiting an ACK.
virtual void stateIdle()
IDLE state.
virtual int getRemainingPkts() override
Return the number of packets not transmitted (remained in the protocol queue) at the end of the simul...
std::queue< int > data_sn_queue
Queue of the sequence number of the packets.
virtual void txAck(int dest_addr)
Transmits the ACK packet (calling Mac2PhyStartTx) and increment the counter of transmitted ACK packet...
virtual void stateTxData()
State in which the protocol allows the node to transmit a data packet.
virtual void setSessionDistance(double distance)
Set the distance between the sender and the receiver.
virtual void stateCheckAckExpired()
Checks if the ACK reception timer is expired.
virtual void refreshState(CSMA_STATUS state)
Refresh the State of the protocol.
int buffer_pkts
Length of the data buffer in number of packets.
virtual void queuePop(bool flag=true)
Pop the first element of the data packet queue.
virtual void initInfo()
Initializes the protocol at the beginning of the simulation.
virtual void updateRTT(double rtt)
Update the RTT increasing the number of RTT samples and calculating the smoothed RTT using the formul...
virtual void updateAckTimeout(double rtt)
Updates the AckTimeout calling getRTT, where the ACK timeout is computed as srtt/rttsamples using the...
std::queue< Packet * > Q
Packet queue.
int last_sent_data_id
ID of the last sent packet.
CSMA_STATUS prev_prev_state
Previous previous state of the protocol.
virtual void stateRxAck(Packet *p)
state in which an ACK packet is received
virtual void resetSession()
Resets the current session (e.g.
virtual void Phy2MacStartRx(const Packet *p) override
Method called when the Phy Layer start to receive a Packet.
virtual int getUpLayersDataPktsRx()
Packet * curr_data_pkt
Pointer to the current data packet.
@ CSMA_STATE_CHK_ACK_TIMEOUT
@ CSMA_STATE_RX_WAIT_ACK
@ CSMA_STATE_RX_BACKOFF
@ CSMA_STATE_CHK_LISTEN_TIMEOUT
@ CSMA_STATE_WAIT_ACK
@ CSMA_STATE_WRONG_PKT_RX
@ CSMA_STATE_CHK_BACKOFF_TIMEOUT
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
double alpha_
smooth factor in the calculation of the RTT
bool RxActive
flag that indicates if a reception is occuring
@ CSMA_REASON_WAIT_ACK_PENDING
@ CSMA_REASON_DATA_PENDING
@ CSMA_REASON_BACKOFF_TIMEOUT
@ CSMA_REASON_DATA_EMPTY
@ CSMA_REASON_LISTEN_TIMEOUT
@ CSMA_REASON_PKT_NOT_FOR_ME
@ CSMA_REASON_BACKOFF_PENDING
@ CSMA_REASON_MAX_TX_TRIES
@ CSMA_REASON_ACK_TIMEOUT
@ CSMA_REASON_LISTEN_PENDING
@ CSMA_REASON_PKT_ERROR
virtual void initPkt(Packet *p, CSMA_PKT_TYPE pkt_type, int dest_addr=0)
Init the packet with the MAC address of the receiver and the sender, the size of the packet and the t...
static map< CSMA_PKT_TYPE, string > pkt_type_info
Textual description of the packet type.
virtual void stateWaitAck()
State in which a DATA packet is sent.
virtual ~CsmaAloha()=default
Destructor of the CsmaAloha class.
static const double prop_speed
Typical sound propagation speed in underwater enviroment.
virtual void stateRxData(Packet *p)
State in which a DATA packet is received.
bool has_buffer_queue
flag that indicates if a node has a buffer where store DATA packets
virtual void stateCheckListenExpired()
Checks if the Listen period is expired.
int last_data_id_rx
ID of the last DATA packet received.
static int u_pkt_id
simulation-unique packet ID
virtual void stateRxListen()
State in which a reception is occuring while the node is listening the channel.
virtual void refreshReason(CSMA_REASON_STATUS reason)
Refresh the reason for the change of state.
CSMA_ACK_MODES ack_mode
Variable that indicates if the protocol is in ACK or NO_ACK mode.
double wait_costant
Adding factor in the calculation of the listen time.
virtual void incrCurrTxRounds()
Increases the number of times a packet is re-transmitted.
virtual void stateRxPacketNotForMe(Packet *p)
state in which a wrong Packet is received
double session_distance
Distance between sender and the receiver involved in the current session.
CsmaAloha()
Constructor of the CsmaAloha class.
virtual void txData()
Transmits the DATA packet (calling Mac2PhyStartTx) and increment the counter of transmitted data pack...
virtual void Phy2MacEndTx(const Packet *p) override
Method called when the PHY layer finish to transmit the packet.
static map< CSMA_REASON_STATUS, string > reason_info
Textual description of the protocol reason for the change of the state.
virtual void Phy2MacEndRx(Packet *p) override
Method called when the Phy Layer finish to receive a Packet.
int max_payload
Maximum dimension of the data payload in bytes.
BackOffTimer backoff_timer
Object that represents the backoff timer.
virtual double getBackoffTime()
compute the BackOff time as backoff = backoff_tuner*random*2*ACK_timeout*2^(counter) where counter is...
virtual void printStateInfo(double delay=0)
Prints in a file the textual information of the current state and the transitions reasons.
virtual void recvFromUpperLayers(Packet *p) override
Receives the packet from the upper layer (e.g.
int HDR_size
Size (in bytes) of the header added by the protocol.
double sumrtt
sum of RTT samples
bool print_transitions
flag that indicates if the protocol is enabled to print its state transitions on a file
virtual double computeTxTime(CSMA_PKT_TYPE type)
Compute the time needed to transmit the packet (using a CrLayerMessage to ask the PHY to perform the ...
static bool initialized
true if the protocol is initialized
AckTimer ack_timer
Object that represents the ack timer.
int max_backoff_counter
Number of times a backoff is calculated.
int u_data_id
DATA packete ID.
virtual void updateLastDataIdRx(int id)
Updates the ID of the last DATA packet received.
virtual void stateBackoff()
BackOff STATE.
bool TxActive
flag that indicates if a transmission is occuring
virtual void exitBackoff()
Method called when the Backoff timer is expired.
virtual void stateRxBackoff()
State in which a reception is occurring while the protocol is in the backoff state.
bool session_active
flag that indicates if a session (i.e.
ListenTimer listen_timer
Object that represents the listen timer.
CSMA_STATUS prev_state
Previous state of the protocol.
virtual void stateListen()
State in which the node is listening the channel.
static map< CSMA_STATUS, string > status_info
Textual description of the protocol states.
double srtt
Smoothed Round Trip Time, calculated as for TCP.
int curr_tx_rounds
Number of current transmission of the same packet.
CSMA_REASON_STATUS last_reason
Reason for the state transitions.
double start_tx_time
timestamp in which the node stars to transmit a packet
virtual void resetCurrTxRounds()
Reset the number of times a data pacekt is re-transmitted.
double sumrtt2
sum of (RTT^2)
virtual void Mac2PhyStartTx(Packet *p)
Pass the packet to the PHY layer.
virtual double getRTT() const
get the value of RTT as mean of all the rttsamples
virtual bool keepDataPkt(int serial_number)
Checks if the data packet received is a double packet (using the serial number of the packet)
double backoff_tuner
Multiplier factor in the calculation of the backoff.
CSMA_STATUS curr_state
Current state of the protocol.
int max_tx_tries
Maximum number of transmissions for one packet.
virtual void incrUpperDataRx() override
Increase the number of Data packet Received from the Upper layers.
virtual void stateCheckBackoffExpired()
Checks if the Backoff period is expired.
ofstream fout
Object that handles the output file where the protocol writes the state transistions.
virtual void stateRxIdle()
A reception is occuring while the protocol is in IDLE state.
double listen_time
Time in which the node sense the channel.
virtual void stateTxAck(int dest_addr)
State in which the protocol set-up the ACK packet to transmit.
std::pair< int, int > counter
counter of collisions