A Discrete-Event Network Simulator
API
point-to-point-channel.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007, 2008 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 
19 #include "point-to-point-channel.h"
21 #include "ns3/trace-source-accessor.h"
22 #include "ns3/packet.h"
23 #include "ns3/simulator.h"
24 #include "ns3/log.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("PointToPointChannel");
29 
30 NS_OBJECT_ENSURE_REGISTERED (PointToPointChannel);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::PointToPointChannel")
36  .SetParent<Channel> ()
37  .SetGroupName ("PointToPoint")
38  .AddConstructor<PointToPointChannel> ()
39  .AddAttribute ("Delay", "Propagation delay through the channel",
40  TimeValue (Seconds (0)),
42  MakeTimeChecker ())
43  .AddTraceSource ("TxRxPointToPoint",
44  "Trace source indicating transmission of packet "
45  "from the PointToPointChannel, used by the Animation "
46  "interface.",
48  "ns3::PointToPointChannel::TxRxAnimationCallback")
49  ;
50  return tid;
51 }
52 
53 //
54 // By default, you get a channel that
55 // has an "infitely" fast transmission speed and zero delay.
57  :
58  Channel (),
59  m_delay (Seconds (0.)),
60  m_nDevices (0)
61 {
63 }
64 
65 void
67 {
68  NS_LOG_FUNCTION (this << device);
69  NS_ASSERT_MSG (m_nDevices < N_DEVICES, "Only two devices permitted");
70  NS_ASSERT (device != 0);
71 
72  m_link[m_nDevices++].m_src = device;
73 //
74 // If we have both devices connected to the channel, then finish introducing
75 // the two halves and set the links to IDLE.
76 //
77  if (m_nDevices == N_DEVICES)
78  {
79  m_link[0].m_dst = m_link[1].m_src;
80  m_link[1].m_dst = m_link[0].m_src;
81  m_link[0].m_state = IDLE;
82  m_link[1].m_state = IDLE;
83  }
84 }
85 
86 bool
90  Time txTime)
91 {
92  NS_LOG_FUNCTION (this << p << src);
93  NS_LOG_LOGIC ("UID is " << p->GetUid () << ")");
94 
95  NS_ASSERT (m_link[0].m_state != INITIALIZING);
96  NS_ASSERT (m_link[1].m_state != INITIALIZING);
97 
98  uint32_t wire = src == m_link[0].m_src ? 0 : 1;
99 
100  Simulator::ScheduleWithContext (m_link[wire].m_dst->GetNode ()->GetId (),
102  m_link[wire].m_dst, p->Copy ());
103 
104  // Call the tx anim callback on the net device
105  m_txrxPointToPoint (p, src, m_link[wire].m_dst, txTime, txTime + m_delay);
106  return true;
107 }
108 
109 std::size_t
111 {
113  return m_nDevices;
114 }
115 
118 {
120  NS_ASSERT (i < 2);
121  return m_link[i].m_src;
122 }
123 
125 PointToPointChannel::GetDevice (std::size_t i) const
126 {
128  return GetPointToPointDevice (i);
129 }
130 
131 Time
133 {
134  return m_delay;
135 }
136 
139 {
140  return m_link[i].m_src;
141 }
142 
145 {
146  return m_link[i].m_dst;
147 }
148 
149 bool
151 {
152  NS_ASSERT (m_link[0].m_state != INITIALIZING);
153  NS_ASSERT (m_link[1].m_state != INITIALIZING);
154  return true;
155 }
156 
157 } // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:44
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:390
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
Simple Point To Point Channel.
virtual std::size_t GetNDevices(void) const
Get number of devices on this channel.
Link m_link[N_DEVICES]
Link model.
std::size_t m_nDevices
Devices of this channel.
void Attach(Ptr< PointToPointNetDevice > device)
Attach a given netdevice to this channel.
Ptr< PointToPointNetDevice > GetSource(uint32_t i) const
Get the net-device source.
PointToPointChannel()
Create a PointToPointChannel.
Ptr< PointToPointNetDevice > GetDestination(uint32_t i) const
Get the net-device destination.
@ IDLE
Idle state (no transmission from NetDevice)
@ INITIALIZING
Initializing state.
Ptr< PointToPointNetDevice > GetPointToPointDevice(std::size_t i) const
Get PointToPointNetDevice corresponding to index i on this channel.
bool IsInitialized(void) const
Check to make sure the link is initialized.
Time GetDelay(void) const
Get the delay associated with this channel.
static TypeId GetTypeId(void)
Get the TypeId.
TracedCallback< Ptr< const Packet >, Ptr< NetDevice >, Ptr< NetDevice >, Time, Time > m_txrxPointToPoint
The trace source for the packet transmission animation events that the device can fire.
Time m_delay
Propagation delay.
virtual bool TransmitStart(Ptr< const Packet > p, Ptr< PointToPointNetDevice > src, Time txTime)
Transmit a packet over this channel.
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
Get NetDevice corresponding to index i on this channel.
static const std::size_t N_DEVICES
Each point to point link has exactly two net devices.
void Receive(Ptr< Packet > p)
Receive a packet from a connected PointToPointChannel.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: nstime.h:1309
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:522