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 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::PointToPointChannel")
37  .SetParent<Channel> ()
38  .AddConstructor<PointToPointChannel> ()
39  .AddAttribute ("Delay", "Transmission delay through the channel",
40  TimeValue (Seconds (0)),
41  MakeTimeAccessor (&PointToPointChannel::m_delay),
42  MakeTimeChecker ())
43  .AddTraceSource ("TxRxPointToPoint",
44  "Trace source indicating transmission of packet from the PointToPointChannel, used by the Animation interface.",
46  ;
47  return tid;
48 }
49 
50 //
51 // By default, you get a channel that
52 // has an "infitely" fast transmission speed and zero delay.
54  :
55  Channel (),
56  m_delay (Seconds (0.)),
57  m_nDevices (0)
58 {
60 }
61 
62 void
64 {
65  NS_LOG_FUNCTION (this << device);
66  NS_ASSERT_MSG (m_nDevices < N_DEVICES, "Only two devices permitted");
67  NS_ASSERT (device != 0);
68 
69  m_link[m_nDevices++].m_src = device;
70 //
71 // If we have both devices connected to the channel, then finish introducing
72 // the two halves and set the links to IDLE.
73 //
74  if (m_nDevices == N_DEVICES)
75  {
76  m_link[0].m_dst = m_link[1].m_src;
77  m_link[1].m_dst = m_link[0].m_src;
78  m_link[0].m_state = IDLE;
79  m_link[1].m_state = IDLE;
80  }
81 }
82 
83 bool
85  Ptr<Packet> p,
87  Time txTime)
88 {
89  NS_LOG_FUNCTION (this << p << src);
90  NS_LOG_LOGIC ("UID is " << p->GetUid () << ")");
91 
92  NS_ASSERT (m_link[0].m_state != INITIALIZING);
93  NS_ASSERT (m_link[1].m_state != INITIALIZING);
94 
95  uint32_t wire = src == m_link[0].m_src ? 0 : 1;
96 
97  Simulator::ScheduleWithContext (m_link[wire].m_dst->GetNode ()->GetId (),
99  m_link[wire].m_dst, p);
100 
101  // Call the tx anim callback on the net device
102  m_txrxPointToPoint (p, src, m_link[wire].m_dst, txTime, txTime + m_delay);
103  return true;
104 }
105 
106 uint32_t
108 {
110  return m_nDevices;
111 }
112 
115 {
117  NS_ASSERT (i < 2);
118  return m_link[i].m_src;
119 }
120 
123 {
125  return GetPointToPointDevice (i);
126 }
127 
128 Time
130 {
131  return m_delay;
132 }
133 
136 {
137  return m_link[i].m_src;
138 }
139 
142 {
143  return m_link[i].m_dst;
144 }
145 
146 bool
148 {
149  NS_ASSERT (m_link[0].m_state != INITIALIZING);
150  NS_ASSERT (m_link[1].m_state != INITIALIZING);
151  return true;
152 }
153 
154 } // namespace ns3
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
uint64_t GetUid(void) const
A packet is allocated a new uid when it is created empty or with zero-filled payload.
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)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
Abstract Channel Base Class.
Definition: channel.h:43
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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:961
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:904
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual Ptr< NetDevice > GetDevice(uint32_t i) const
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
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:452
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:611
NS_LOG_COMPONENT_DEFINE("PointToPointChannel")