A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
point-to-point-channel.cc
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"
20 #include "point-to-point-net-device.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
33 PointToPointChannel::GetTypeId (void)
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 (),
97  txTime + m_delay, &PointToPointNetDevice::Receive,
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 
113 PointToPointChannel::GetPointToPointDevice (uint32_t i) const
114 {
116  NS_ASSERT (i < 2);
117  return m_link[i].m_src;
118 }
119 
120 Ptr<NetDevice>
122 {
124  return GetPointToPointDevice (i);
125 }
126 
127 Time
128 PointToPointChannel::GetDelay (void) const
129 {
130  return m_delay;
131 }
132 
133 Ptr<PointToPointNetDevice>
134 PointToPointChannel::GetSource (uint32_t i) const
135 {
136  return m_link[i].m_src;
137 }
138 
139 Ptr<PointToPointNetDevice>
140 PointToPointChannel::GetDestination (uint32_t i) const
141 {
142  return m_link[i].m_dst;
143 }
144 
145 bool
146 PointToPointChannel::IsInitialized (void) const
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
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
uint64_t GetUid(void) const
Definition: packet.cc:412
TracedCallback< Ptr< const Packet >, Ptr< NetDevice >, Ptr< NetDevice >, Time, Time > m_txrxPointToPoint
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
Abstract Channel Base Class.
Definition: channel.h:43
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
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.
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:900
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual Ptr< NetDevice > GetDevice(uint32_t i) const
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
virtual bool TransmitStart(Ptr< Packet > p, Ptr< PointToPointNetDevice > src, Time txTime)
Transmit a packet over this channel.