A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
uan-transducer-hd.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "uan-transducer-hd.h"
22 #include "ns3/simulator.h"
23 #include "ns3/uan-prop-model.h"
24 #include "uan-phy.h"
25 #include "uan-channel.h"
26 #include "ns3/log.h"
27 #include "ns3/pointer.h"
28 
29 
30 NS_LOG_COMPONENT_DEFINE ("UanTransducerHd");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (UanTransducerHd);
35 UanTransducerHd::UanTransducerHd ()
36  : UanTransducer (),
37  m_state (RX),
38  m_endTxTime (Seconds (0)),
39  m_cleared (false)
40 {
41 }
42 
43 UanTransducerHd::~UanTransducerHd ()
44 {
45 }
46 
47 void
49 {
50  if (m_cleared)
51  {
52  return;
53  }
54  m_cleared = true;
55  if (m_channel)
56  {
57  m_channel->Clear ();
58  m_channel = 0;
59  }
60 
61  UanPhyList::iterator it = m_phyList.begin ();
62  for (; it != m_phyList.end (); it++)
63  {
64  if (*it)
65  {
66  (*it)->Clear ();
67  *it = 0;
68  }
69  }
70  ArrivalList::iterator ait = m_arrivalList.begin ();
71  for (; ait != m_arrivalList.end (); ait++)
72  {
73  ait->GetPacket () = 0;
74  }
75  m_phyList.clear ();
76  m_arrivalList.clear ();
77  m_endTxEvent.Cancel ();
78 }
79 
80 void
82 {
83  Clear ();
85 }
86 TypeId
87 UanTransducerHd::GetTypeId ()
88 {
89  static TypeId tid = TypeId ("ns3::UanTransducerHd")
90  .SetParent<Object> ()
91  .AddConstructor<UanTransducerHd> ()
92  ;
93  return tid;
94 }
95 
98 {
99  return m_state;
100 }
101 
102 
103 bool
105 {
106  return m_state == RX;
107 }
108 
109 bool
111 {
112  return m_state == TX;
113 
114 }
115 
118 {
119  return m_arrivalList;
120 }
121 
122 void
124  double rxPowerDb,
125  UanTxMode txMode,
126  UanPdp pdp)
127 {
128  UanPacketArrival arrival (packet,
129  rxPowerDb,
130  txMode,
131  pdp,
132  Simulator::Now ());
133 
134  m_arrivalList.push_back (arrival);
135  Time txDelay = Seconds (packet->GetSize () * 8.0 / txMode.GetDataRateBps ());
136  Simulator::Schedule (txDelay, &UanTransducerHd::RemoveArrival, this, arrival);
137  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Transducer in receive");
138  if (m_state == RX)
139  {
140  NS_LOG_DEBUG ("Transducer state = RX");
141  UanPhyList::const_iterator it = m_phyList.begin ();
142  for (; it != m_phyList.end (); it++)
143  {
144  NS_LOG_DEBUG ("Calling StartRx");
145  (*it)->StartRxPacket (packet, rxPowerDb, txMode, pdp);
146  }
147  }
148 }
149 
150 void
152  Ptr<Packet> packet,
153  double txPowerDb,
154  UanTxMode txMode)
155 {
156 
157  Time endTxTime;
158  if (m_state == TX)
159  {
160  Simulator::Remove (m_endTxEvent);
161  src->NotifyTxDrop(packet); // traced source netanim
162  }
163  else
164  {
165  m_state = TX;
166  src->NotifyTxBegin(packet); // traced source netanim
167  }
168 
169 
170  Time delay = Seconds (packet->GetSize () * 8.0 / txMode.GetDataRateBps ());
171  NS_LOG_DEBUG ("Transducer transmitting: TX delay = "
172  << delay << " seconds for packet size "
173  << packet->GetSize () << " bytes and rate = "
174  << txMode.GetDataRateBps () << " bps");
175  UanPhyList::const_iterator it = m_phyList.begin ();
176  for (; it != m_phyList.end (); it++)
177  {
178  if (src != (*it))
179  {
180  (*it)->NotifyTransStartTx (packet, txPowerDb, txMode);
181  }
182  }
183  m_channel->TxPacket (Ptr<UanTransducer> (this), packet, txPowerDb, txMode);
184 
185 
186  delay = std::max (delay, m_endTxTime - Simulator::Now ());
187 
188  m_endTxEvent = Simulator::Schedule (delay, &UanTransducerHd::EndTx, this);
189  m_endTxTime = Simulator::Now () + delay;
190  Simulator::Schedule(delay, &UanPhy::NotifyTxEnd, src, packet); // traced source netanim
191 }
192 
193 void
194 UanTransducerHd::EndTx (void)
195 {
196  NS_ASSERT (m_state == TX);
197  m_state = RX;
198  m_endTxTime = Seconds (0);
199 }
200 void
202 {
203  NS_LOG_DEBUG ("Transducer setting channel");
204  m_channel = chan;
205 
206 }
209 {
210  return m_channel;
211 }
212 void
214 {
215  m_phyList.push_back (phy);
216 }
217 
220 {
221  return m_phyList;
222 }
223 
224 void
225 UanTransducerHd::RemoveArrival (UanPacketArrival arrival)
226 {
227 
228  // Remove entry from arrival list
229  ArrivalList::iterator it = m_arrivalList.begin ();
230  for (; it != m_arrivalList.end (); it++)
231  {
232  if (it->GetPacket () == arrival.GetPacket ())
233  {
234  m_arrivalList.erase (it);
235  break;
236  }
237  }
238  UanPhyList::const_iterator ait = m_phyList.begin ();
239  for (; ait != m_phyList.end (); ait++)
240  {
241  (*ait)->NotifyIntChange ();
242  }
243 
244 }
245 
246 } // namespace ns3
keep track of time unit.
Definition: nstime.h:149
std::list< Ptr< UanPhy > > UanPhyList
UanPhyList is a standard template library list of UanPhy objects.
virtual const UanPhyList & GetPhyList(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
uint32_t GetSize(void) const
Definition: packet.h:620
virtual bool IsTx(void) const
virtual void DoDispose(void)
Definition: object.cc:335
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
virtual void Receive(Ptr< Packet > packet, double rxPowerDb, UanTxMode txMode, UanPdp pdp)
Receive Notify this object that a new packet has arrived at this nodes location.
virtual void DoDispose()
Ptr< Packet > GetPacket(void) const
virtual Ptr< UanChannel > GetChannel(void) const
uint32_t GetDataRateBps(void) const
Definition: uan-tx-mode.cc:45
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:36
std::list< UanPacketArrival > ArrivalList
Arrival list is a standard template library list of UanPacketArrivals objects.
virtual const ArrivalList & GetArrivalList(void) const
static void Remove(const EventId &id)
Definition: simulator.cc:257
static Time Now(void)
Definition: simulator.cc:179
virtual void SetChannel(Ptr< UanChannel > chan)
virtual void AddPhy(Ptr< UanPhy >)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
virtual void Clear(void)
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
virtual State GetState(void) const
void Cancel(void)
Definition: event-id.cc:47
State
Transducer state (receiving or transmitting)
a base class which provides memory management and object aggregation
Definition: object.h:63
virtual void Transmit(Ptr< UanPhy > src, Ptr< Packet > packet, double txPowerDb, UanTxMode txMode)
Transmit a packet from this transducer.
virtual bool IsRx(void) const
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
void NotifyTxEnd(Ptr< const Packet > packet)
Definition: uan-phy.cc:103
Class consisting of packet arrival information (Time, RxPower, mode, PDP)