A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("PointToPointChannel");
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (PointToPointChannel);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::PointToPointChannel")
36  .SetParent<Channel> ()
37  .AddConstructor<PointToPointChannel> ()
38  .AddAttribute ("Delay", "Transmission delay through the channel",
39  TimeValue (Seconds (0)),
40  MakeTimeAccessor (&PointToPointChannel::m_delay),
41  MakeTimeChecker ())
42  .AddTraceSource ("TxRxPointToPoint",
43  "Trace source indicating transmission of packet from the PointToPointChannel, used by the Animation interface.",
45  ;
46  return tid;
47 }
48 
49 //
50 // By default, you get a channel that
51 // has an "infitely" fast transmission speed and zero delay.
53  :
54  Channel (),
55  m_delay (Seconds (0.)),
56  m_nDevices (0)
57 {
59 }
60 
61 void
63 {
64  NS_LOG_FUNCTION (this << device);
65  NS_ASSERT_MSG (m_nDevices < N_DEVICES, "Only two devices permitted");
66  NS_ASSERT (device != 0);
67 
68  m_link[m_nDevices++].m_src = device;
69 //
70 // If we have both devices connected to the channel, then finish introducing
71 // the two halves and set the links to IDLE.
72 //
73  if (m_nDevices == N_DEVICES)
74  {
75  m_link[0].m_dst = m_link[1].m_src;
76  m_link[1].m_dst = m_link[0].m_src;
77  m_link[0].m_state = IDLE;
78  m_link[1].m_state = IDLE;
79  }
80 }
81 
82 bool
84  Ptr<Packet> p,
86  Time txTime)
87 {
88  NS_LOG_FUNCTION (this << p << src);
89  NS_LOG_LOGIC ("UID is " << p->GetUid () << ")");
90 
91  NS_ASSERT (m_link[0].m_state != INITIALIZING);
92  NS_ASSERT (m_link[1].m_state != INITIALIZING);
93 
94  uint32_t wire = src == m_link[0].m_src ? 0 : 1;
95 
96  Simulator::ScheduleWithContext (m_link[wire].m_dst->GetNode ()->GetId (),
98  m_link[wire].m_dst, p);
99 
100  // Call the tx anim callback on the net device
101  m_txrxPointToPoint (p, src, m_link[wire].m_dst, txTime, txTime + m_delay);
102  return true;
103 }
104 
105 uint32_t
107 {
109  return m_nDevices;
110 }
111 
114 {
116  NS_ASSERT (i < 2);
117  return m_link[i].m_src;
118 }
119 
122 {
124  return GetPointToPointDevice (i);
125 }
126 
127 Time
129 {
130  return m_delay;
131 }
132 
135 {
136  return m_link[i].m_src;
137 }
138 
141 {
142  return m_link[i].m_dst;
143 }
144 
145 bool
147 {
148  NS_ASSERT (m_link[0].m_state != INITIALIZING);
149  NS_ASSERT (m_link[1].m_state != INITIALIZING);
150  return true;
151 }
152 
153 } // namespace ns3
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#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 the class in the ns-3 factory.
Definition: object-base.h:38
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:393
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...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Abstract Channel Base Class.
Definition: channel.h:43
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual uint32_t GetNDevices(void) const
Get number of devices on this channel.
void Attach(Ptr< PointToPointNetDevice > device)
Attach a given netdevice to this channel.
PointToPointChannel()
Create a PointToPointChannel.
hold objects of type ns3::Time
Definition: nstime.h:1008
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:905
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual Ptr< NetDevice > GetDevice(uint32_t i) const
#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:84
void Receive(Ptr< Packet > p)
Receive a packet from a connected PointToPointChannel.
Ptr< PointToPointNetDevice > GetSource(uint32_t i) const
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:441
virtual bool TransmitStart(Ptr< Packet > p, Ptr< PointToPointNetDevice > src, Time txTime)
Transmit a packet over this channel.
Ptr< PointToPointNetDevice > GetDestination(uint32_t i) const
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< PointToPointNetDevice > GetPointToPointDevice(uint32_t i) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610