A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
point-to-point-channel.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007, 2008 University of Washington
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
19
21
22#include "ns3/log.h"
23#include "ns3/packet.h"
24#include "ns3/simulator.h"
25#include "ns3/trace-source-accessor.h"
26
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("PointToPointChannel");
31
32NS_OBJECT_ENSURE_REGISTERED(PointToPointChannel);
33
34TypeId
36{
37 static TypeId tid =
38 TypeId("ns3::PointToPointChannel")
40 .SetGroupName("PointToPoint")
41 .AddConstructor<PointToPointChannel>()
42 .AddAttribute("Delay",
43 "Propagation delay through the channel",
47 .AddTraceSource("TxRxPointToPoint",
48 "Trace source indicating transmission of packet "
49 "from the PointToPointChannel, used by the Animation "
50 "interface.",
52 "ns3::PointToPointChannel::TxRxAnimationCallback");
53 return tid;
54}
55
56//
57// By default, you get a channel that
58// has an "infitely" fast transmission speed and zero delay.
60 : Channel(),
61 m_delay(Seconds(0.)),
62 m_nDevices(0)
63{
65}
66
67void
69{
70 NS_LOG_FUNCTION(this << device);
71 NS_ASSERT_MSG(m_nDevices < N_DEVICES, "Only two devices permitted");
72 NS_ASSERT(device);
73
74 m_link[m_nDevices++].m_src = device;
75 //
76 // If we have both devices connected to the channel, then finish introducing
77 // the two halves and set the links to IDLE.
78 //
79 if (m_nDevices == N_DEVICES)
80 {
81 m_link[0].m_dst = m_link[1].m_src;
82 m_link[1].m_dst = m_link[0].m_src;
83 m_link[0].m_state = IDLE;
84 m_link[1].m_state = IDLE;
85 }
86}
87
88bool
90{
91 NS_LOG_FUNCTION(this << p << src);
92 NS_LOG_LOGIC("UID is " << p->GetUid() << ")");
93
94 NS_ASSERT(m_link[0].m_state != INITIALIZING);
95 NS_ASSERT(m_link[1].m_state != INITIALIZING);
96
97 uint32_t wire = src == m_link[0].m_src ? 0 : 1;
98
99 Simulator::ScheduleWithContext(m_link[wire].m_dst->GetNode()->GetId(),
100 txTime + m_delay,
102 m_link[wire].m_dst,
103 p->Copy());
104
105 // Call the tx anim callback on the net device
106 m_txrxPointToPoint(p, src, m_link[wire].m_dst, txTime, txTime + m_delay);
107 return true;
108}
109
110std::size_t
112{
114 return m_nDevices;
115}
116
119{
121 NS_ASSERT(i < 2);
122 return m_link[i].m_src;
123}
124
127{
129 return GetPointToPointDevice(i);
130}
131
132Time
134{
135 return m_delay;
136}
137
140{
141 return m_link[i].m_src;
142}
143
146{
147 return m_link[i].m_dst;
148}
149
150bool
152{
153 NS_ASSERT(m_link[0].m_state != INITIALIZING);
154 NS_ASSERT(m_link[1].m_state != INITIALIZING);
155 return true;
156}
157
158} // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
Simple Point To Point Channel.
Time GetDelay() const
Get the delay associated with this channel.
static TypeId GetTypeId()
Get the TypeId.
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.
std::size_t GetNDevices() const override
Get number of devices on this channel.
Ptr< NetDevice > GetDevice(std::size_t i) const override
Get NetDevice corresponding to index i on this channel.
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() const
Check to make sure the link is initialized.
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.
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:77
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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.