DESERT 3.5.1
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 <mmac.h>
43#include <iostream>
44#include <string>
45#include <map>
46#include <set>
47#include <queue>
48#include <fstream>
49
50#include <mphy.h>
51
52#define CSMA_DROP_REASON_WRONG_STATE \
53 "WST"
55#define CSMA_DROP_REASON_WRONG_RECEIVER \
56 "WRCV"
57#define CSMA_DROP_REASON_UNKNOWN_TYPE \
58 "UPT"
59#define CSMA_DROP_REASON_BUFFER_FULL \
60 "DBF"
61#define CSMA_DROP_REASON_ERROR "ERR"
63extern packet_t PT_MMAC_ACK;
64
68class CsmaAloha : public MMac
69{
70public:
74 CsmaAloha();
78 virtual ~CsmaAloha();
89 virtual int command(int argc, const char *const *argv);
97 virtual int crLayCommand(ClMessage *m);
98
99protected:
100 static const double prop_speed;
123
145
148
151
159
163 class AlohaTimer : public TimerHandler
164 {
165 public:
171 : TimerHandler()
172 , start_time(0.0)
173 , left_duration(0.0)
174 , counter(0)
175 , module(m)
177 {
178 assert(m != NULL);
179 }
180
184 virtual ~AlohaTimer()
185 {
186 }
187
191 virtual void
193 {
194 assert(timer_status == CSMA_RUNNING);
195 left_duration -= (NOW - start_time);
196 if (left_duration <= 0.0)
197 left_duration = module->mac2phy_delay_;
198 force_cancel();
200 }
201
206 virtual void
208 {
209 assert(timer_status == CSMA_FROZEN);
210 start_time = NOW;
211 assert(left_duration > 0);
212 sched(left_duration);
214 }
215
219 virtual void
221 {
223 force_cancel();
224 }
225
230 virtual void
231 schedule(double val)
232 {
233 start_time = NOW;
234 left_duration = val;
236 resched(val);
237 }
238
243 bool
245 {
246 return (timer_status == CSMA_IDLE);
247 }
248
253 bool
255 {
256 return (timer_status == CSMA_RUNNING);
257 }
258
264 bool
266 {
267 return (timer_status == CSMA_EXPIRED);
268 }
269
274 bool
276 {
277 return (timer_status == CSMA_FROZEN);
278 }
279
285 bool
287 {
288 return (timer_status == CSMA_FROZEN ||
290 }
291
296 void
298 {
299 counter = 0;
300 }
301
306 void
308 {
309 ++counter;
310 }
311
317 int
319 {
320 return counter;
321 }
322
327 double
329 {
330 return left_duration;
331 }
332
333 protected:
334 double start_time;
340 CsmaAloha *module;
342 };
343
348 {
349 public:
355 : AlohaTimer(m)
356 {
357 }
358
363 {
364 }
365
366 protected:
371 virtual void expire(Event *e);
372 };
373
377 class AckTimer : public AlohaTimer
378 {
379 public:
385 : AlohaTimer(m)
386 {
387 }
388
392 virtual ~AckTimer()
393 {
394 }
395
396 protected:
401 virtual void expire(Event *e);
402 };
403
409 class ListenTimer : public AlohaTimer
410 {
411 public:
417 : AlohaTimer(m)
418 {
419 }
420
424 virtual ~ListenTimer()
425 {
426 }
427
428 protected:
433 virtual void expire(Event *e);
434 };
435
441 virtual void recvFromUpperLayers(Packet *p);
447 virtual void Mac2PhyStartTx(Packet *p);
453 virtual void Phy2MacEndTx(const Packet *p);
459 virtual void Phy2MacStartRx(const Packet *p);
465 virtual void Phy2MacEndRx(Packet *p);
471 virtual double computeTxTime(CSMA_PKT_TYPE type);
477 virtual void initPkt(Packet *p, CSMA_PKT_TYPE pkt_type, int dest_addr = 0);
485 virtual double getBackoffTime();
490 virtual void txData();
496 virtual void txAck(int dest_addr);
500 virtual void stateIdle();
504 virtual void stateRxIdle();
508 virtual void stateTxData();
513 virtual void stateTxAck(int dest_addr);
519 virtual void stateBackoff();
524 virtual void stateRxBackoff();
529 virtual void stateWaitAck();
533 virtual void stateRxWaitAck();
537 virtual void stateListen();
542 virtual void stateRxListen();
546 virtual void stateCheckListenExpired();
550 virtual void stateCheckAckExpired();
554 virtual void stateCheckBackoffExpired();
558 virtual void stateRxData(Packet *p);
562 virtual void stateRxAck(Packet *p);
566 virtual void stateRxPacketNotForMe(Packet *p);
574 virtual void printStateInfo(double delay = 0);
582 virtual void initInfo();
583
587 virtual void
594
598 virtual void
600 {
601 last_reason = reason;
602 }
606 virtual void exitBackoff();
607
612 virtual void
613 setSessionDistance(double distance)
614 {
615 session_distance = distance;
616 }
625 virtual bool keepDataPkt(int serial_number);
626
630 virtual void
632 {
634 }
635
639 virtual void
641 {
642 curr_tx_rounds = 0;
643 }
651 virtual void updateRTT(double rtt);
652
657 virtual double
659 {
660 return (rttsamples > 0) ? sumrtt / rttsamples : 0;
661 }
668 virtual void updateAckTimeout(double rtt);
669
673 virtual void
675 {
676 last_data_id_rx = id;
677 }
678
682 virtual void
683 queuePop(bool flag = true)
684 {
685 Packet::free(Q.front());
686 Q.pop();
687 waitEndTime(flag);
688 data_sn_queue.pop();
689 }
693 virtual void resetSession();
698 virtual void waitForUser();
699
700 // stats functions
701
708 virtual int
710 {
711 return (up_data_pkts_rx - Q.size());
712 }
713
717 virtual void
719 {
720 up_data_pkts_rx++;
721 }
722
723 virtual int
725 {
726 return up_data_pkts_rx;
727 }
728
738 double ACK_timeout;
740 double alpha_;
742 double listen_time;
744
745 std::queue<Packet *> Q;
746 std::queue<int>
749 static bool initialized;
751 static int u_pkt_id;
755 bool TxActive;
756 bool RxActive;
757 bool
761 bool
765 bool
771 double srtt;
772 double sumrtt;
773 double sumrtt2;
797 static map<CSMA_STATUS, string>
799 static map<CSMA_REASON_STATUS, string>
802 static map<CSMA_PKT_TYPE, string>
805 ofstream fout;
807};
808
809#endif /* CSMA_H */
Class used to handle the timer for waiting the ACK.
AckTimer(CsmaAloha *m)
Conscructor of AckTimer class.
virtual ~AckTimer()
Destructor of AckTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
Class that describes the timers in the node.
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 getDuration()
Returns the left duration of the timer.
double start_time
Start Time of the timer.
bool isActive()
Checks if the timer is ACTIVE.
int getCounter()
Returns the counter of the timer.
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
bool isIdle()
checks if the timer is IDLE
virtual ~AlohaTimer()
Destructor of the AlohaTimer class.
bool isExpired()
Checks if the timer is EXPIRED.
Class used to handle the timer of the backoff period.
virtual void expire(Event *e)
Method called when the timer expire.
virtual ~BackOffTimer()
Destructor of DataTimer class.
BackOffTimer(CsmaAloha *m)
Conscructor of BackOffTimer class.
Class used to handle the Listen Timer.
ListenTimer(CsmaAloha *m)
Conscructor of ListenTimer class.
virtual ~ListenTimer()
Destructor of AckTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
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.
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 waitForUser()
Used for debug purposes.
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 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_WRONG_PKT_RX
@ CSMA_STATE_CHK_BACKOFF_TIMEOUT
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 ~CsmaAloha()
Destructor of the CsmaAloha class.
virtual void stateWaitAck()
State in which a DATA packet is sent.
virtual int getRemainingPkts()
Return the number of packets not transmitted (remained in the protocol queue) at the end of the simul...
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.
virtual double getRTT()
get the value of RTT as mean of all the rttsamples
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
virtual void Phy2MacEndRx(Packet *p)
Method called when the Phy Layer finish to receive a Packet.
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...
static map< CSMA_REASON_STATUS, string > reason_info
Textual description of the protocol reason for the change of the state.
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.
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
virtual int crLayCommand(ClMessage *m)
Cross-Layer messages interpreter.
virtual void incrUpperDataRx()
Increase the number of Data packet Received from the Upper layers.
virtual void Phy2MacEndTx(const Packet *p)
Method called when the PHY layer finish to transmit the packet.
AckTimer ack_timer
Object that represents the ack timer.
int max_backoff_counter
Number of times a backoff is calculated.
virtual void recvFromUpperLayers(Packet *p)
Receives the packet from the upper layer (e.g.
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 Phy2MacStartRx(const Packet *p)
Method called when the Phy Layer start to receive a Packet.
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 int command(int argc, const char *const *argv)
TCL command interpreter.
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 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 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