DESERT 3.5.1
Loading...
Searching...
No Matches
uw-csma-aloha-trigger-node.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 UW_CS_ALOHA_TRIG_NODE_H
40#define UW_CS_ALOHA_TRIG_NODE_H
41
42#include <mmac.h>
43#include <mac.h>
44#include <mphy.h>
45#include <iostream>
46#include <string>
47#include <map>
48#include <set>
49#include <queue>
50#include <fstream>
51#include <cmath>
52#include <climits>
53#include <iomanip>
54#include <rng.h>
55
56#define UW_CS_ALOHA_TRIG_NODE_DROP_REASON_BUFFER_FULL \
57 "DBF"
58#define UW_CS_ALOHA_TRIG_NODE_DROP_REASON_ERROR "ERR"
60extern packet_t PT_MMAC_TRIGGER;
61
65class UwCsmaAloha_Trigger_NODE : public MMac
66{
67public:
86 virtual int command(int argc, const char *const *argv);
94 virtual int crLayCommand(ClMessage *m);
95
96protected:
97 static const double prop_speed;
113
130
138
142 class Csma_Aloha_Triggered_Timer : public TimerHandler
143 {
144 public:
150 : TimerHandler()
151 , start_time(0.0)
152 , left_duration(0.0)
153 , counter(0)
154 , module(m)
156 {
157 assert(m != NULL);
158 }
159
164 {
165 }
166
170 virtual void
172 {
174 left_duration -= (NOW - start_time);
175 if (left_duration <= 0.0)
176 left_duration = module->mac2phy_delay_;
177 force_cancel();
179 }
180
185 virtual void
187 {
189 start_time = NOW;
190 assert(left_duration > 0);
191 sched(left_duration);
193 }
194
198 virtual void
200 {
202 force_cancel();
203 }
204
209 virtual void
210 schedule(double val)
211 {
212 start_time = NOW;
213 left_duration = val;
215 resched(val);
216 }
217
222 bool
224 {
226 }
227
232 bool
237
242 bool
247
252 bool
254 {
256 }
257
262 bool
268
272 void
274 {
275 counter = 0;
276 }
277
282 void
284 {
285 ++counter;
286 }
287
292 int
294 {
295 return counter;
296 }
297
303 double
305 {
306 return left_duration;
307 }
308
309 protected:
310 double start_time;
320 };
321
326 {
327 public:
337
341 virtual ~ListenTimer()
342 {
343 }
344
345 protected:
350 virtual void expire(Event *e);
351 };
352
358 {
359 public:
368
373 {
374 }
375
376 protected:
381 virtual void expire(Event *e);
382 };
383
389 virtual void recvFromUpperLayers(Packet *p);
395 virtual void Mac2PhyStartTx(Packet *p);
401 virtual void Phy2MacEndTx(const Packet *p);
407 virtual void Phy2MacStartRx(const Packet *p);
413 virtual void Phy2MacEndRx(Packet *p);
419 virtual void initPkt(Packet *p);
424 virtual void txData();
425
429 virtual void stateIdle();
433 virtual void stateRxIdle();
437 virtual void stateTxData();
441 virtual void stateListen();
446 virtual void stateRxListen();
451 virtual void stateRxTrigger(Packet *p);
452
456 virtual void stateCheckListenExpired();
460 virtual void stateCheckTxTimerExpired();
465 virtual void stateRxData(Packet *p);
469 virtual void stateRxPacketNotForMe(Packet *p);
477 virtual void initInfo();
478
482 virtual void
489
493 virtual void
498
503 virtual void
505 {
506 last_data_id_rx = id;
507 }
508
512 virtual void
513 queuePop(bool flag = true)
514 {
515 Packet::free(Q.front());
516 Q.pop();
517 waitEndTime(flag);
518 data_sn_queue.pop();
519 }
524 virtual void waitForUser();
525
526 // stats functions
527
534 virtual int
536 {
537 return (up_data_pkts_rx - Q.size());
538 }
539
543 virtual void
545 {
546 up_data_pkts_rx++;
547 }
548
550
552
559 double listen_time;
563
564 std::queue<Packet *> Q;
565 std::queue<int>
568 static bool initialized;
570 static int u_pkt_id;
578 bool
584 static map<UW_CS_ALOHA_TRIG_NODE_STATUS, string>
586 static map<UW_CS_ALOHA_TRIG_NODE_REASON_STATUS, string>
599 ofstream fout;
601};
602
603#endif
Csma_Aloha_Triggered_Timer(UwCsmaAloha_Trigger_NODE *m)
Constructor of the AlohaTimer class.
virtual ~Csma_Aloha_Triggered_Timer()
Destructor of the AlohaTimer class.
UwCsmaAloha_Trigger_NODE *UW_CS_ALOHA_TRIG_NODE_TIMER_STATUS timer_status
< Pointer of an object of type UwCsmaAloha_Triggered
void incrCounter()
Increments the counter of the timer.
virtual void unFreeze()
unFreezes is used to resume the timer starting from the point where it was freezed
double getDuration()
Returns the left duration of the timer.
Class used to handle the timer to handle the listen time.
ListenTimer(UwCsmaAloha_Trigger_NODE *m)
Constructor of ListenTimer class.
virtual void expire(Event *e)
Method called when the timer expire.
virtual ~ListenTimer()
Destructor of the class ListenTimer.
Class used to handle the time in which the node is allowed to transmit to the sink.
virtual void expire(Event *e)
Method called when the timer expire.
virtual ~TransmissionTimer()
Destructor of the class TransmissionTimer.
TransmissionTimer(UwCsmaAloha_Trigger_NODE *m)
Constructor of the TransmissionTimer class.
Class that describes a CsmaAloha_TRIGGERED module of the node.
static bool initialized
true if the protocol is initialized
virtual void updateLastDataIdRx(int id)
Updates the ID of the last DATA packet received.
virtual int crLayCommand(ClMessage *m)
Cross-Layer messages interpreter.
double wait_costant
Adding factor in the calculation of the listen time.
static const double prop_speed
Typical sound propagation speed in underwater enviroment.
UW_CS_ALOHA_TRIG_NODE_STATUS prev_state
Previous state of the protocol.
virtual void stateRxData(Packet *p)
State in which a DATA packet is received.
virtual void refreshState(UW_CS_ALOHA_TRIG_NODE_STATUS state)
Refresh the State of the protocol.
UW_CS_ALOHA_TRIG_NODE_REASON_STATUS last_reason
Reason for the state transitions.
double listen_time
Time in which the node sense the channel.
virtual void waitForUser()
Used for debug purposes.
bool has_buffer_queue
flag that indicates if a node has a buffer where store DATA packets
virtual void Phy2MacStartRx(const Packet *p)
Method called when the Phy Layer start to receive a Packet.
UW_CS_ALOHA_TRIG_NODE_STATUS prev_prev_state
Previous previous state of the protocol.
UwCsmaAloha_Trigger_NODE()
Constructor of the UwCsmaAloha_Trigger_NODE class.
virtual void stateRxIdle()
A reception is occuring while the protocol is in IDLE state.
int buffer_pkts
Length of the data buffer in number of packets.
std::queue< int > data_sn_queue
Queue of the sequence number of the packets.
static map< UW_CS_ALOHA_TRIG_NODE_STATUS, string > status_info
Textual description of the protocol states.
int tx_timer_duration
Duration of the time in which the node is allowed to transmit.
virtual void stateRxListen()
State in which a reception is occuring while the node is listening the channel.
virtual void Mac2PhyStartTx(Packet *p)
Pass the packet to the PHY layer.
virtual void stateTxData()
State in which the protocol allows the node to transmit a data packet.
virtual void Phy2MacEndRx(Packet *p)
Method called when the Phy Layer finish to receive a Packet.
virtual void Phy2MacEndTx(const Packet *p)
Method called when the PHY layer finish to transmit the packet.
int HDR_size
Size (in bytes) of the header added by the protocol.
ofstream fout
Object that handles the output file where the protocol writes the state transistions.
virtual int getRemainingPkts()
Return the number of packets not transmitted (remained in the protocol queue) at the end of the simul...
virtual ~UwCsmaAloha_Trigger_NODE()
Destructor of the UwCsmaAloha_Trigger_NODE class.
virtual void txData()
Transmits the DATA packet (calling Mac2PhyStartTx) and increment the counter of transmitted data pack...
virtual void stateIdle()
IDLE state.
static map< UW_CS_ALOHA_TRIG_NODE_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.
static int u_pkt_id
simulation-unique packet ID
std::queue< Packet * > Q
Packet queue.
int last_sent_data_id
ID of the last sent packet.
Packet * curr_data_pkt
Pointer to the current data packet.
virtual void stateRxTrigger(Packet *p)
State of the protocol in which a TRIGGER packet is received.
virtual void recvFromUpperLayers(Packet *p)
Receives the packet from the upper layer (e.g.
UW_CS_ALOHA_TRIG_NODE_STATUS curr_state
Current state of the protocol.
virtual void incrUpperDataRx()
Increase the number of Data packet Received from the Upper layers.
virtual void initInfo()
Initializes the protocol at the beginning of the simulation.
virtual void stateCheckListenExpired()
Checks if the Listen period is expired.
bool can_transmit
Flag that indicates if the node can transmit data packets to the sink.
virtual void stateCheckTxTimerExpired()
Checks if the Time period is expired.
virtual void stateListen()
State in which the node is listening the channel.
virtual void initPkt(Packet *p)
Init the packet with the MAC address of the receiver and the sender, the size of the packet and the t...
virtual void stateRxPacketNotForMe(Packet *p)
state in which a wrong Packet is received
virtual int command(int argc, const char *const *argv)
TCL command interpreter.
virtual void queuePop(bool flag=true)
Pop the first element of the data packet queue.
int last_data_id_rx
ID of the last DATA packet received.
virtual void refreshReason(UW_CS_ALOHA_TRIG_NODE_REASON_STATUS reason)
Refresh the reason for the change of state.
std::pair< int, int > counter
counter of collisions