A Discrete-Event Network Simulator
API
simple-channel.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include <algorithm>
21 #include "simple-channel.h"
22 #include "simple-net-device.h"
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/node.h"
26 #include "ns3/log.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("SimpleChannel");
31 
32 NS_OBJECT_ENSURE_REGISTERED (SimpleChannel);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::SimpleChannel")
38  .SetParent<Channel> ()
39  .SetGroupName("Network")
40  .AddConstructor<SimpleChannel> ()
41  .AddAttribute ("Delay", "Transmission delay through the channel",
42  TimeValue (Seconds (0)),
44  MakeTimeChecker ())
45  ;
46  return tid;
47 }
48 
50 {
51  NS_LOG_FUNCTION (this);
52 }
53 
54 void
55 SimpleChannel::Send (Ptr<Packet> p, uint16_t protocol,
56  Mac48Address to, Mac48Address from,
57  Ptr<SimpleNetDevice> sender)
58 {
59  NS_LOG_FUNCTION (this << p << protocol << to << from << sender);
60  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
61  {
62  Ptr<SimpleNetDevice> tmp = *i;
63  if (tmp == sender)
64  {
65  continue;
66  }
67  if (m_blackListedDevices.find (tmp) != m_blackListedDevices.end ())
68  {
69  if (find (m_blackListedDevices[tmp].begin (), m_blackListedDevices[tmp].end (), sender) !=
70  m_blackListedDevices[tmp].end () )
71  {
72  continue;
73  }
74  }
75  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_delay,
76  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
77  }
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION (this << device);
84  m_devices.push_back (device);
85 }
86 
87 std::size_t
89 {
90  NS_LOG_FUNCTION (this);
91  return m_devices.size ();
92 }
93 
95 SimpleChannel::GetDevice (std::size_t i) const
96 {
97  NS_LOG_FUNCTION (this << i);
98  return m_devices[i];
99 }
100 
101 void
103 {
104  if (m_blackListedDevices.find (to) != m_blackListedDevices.end ())
105  {
106  if (find (m_blackListedDevices[to].begin (), m_blackListedDevices[to].end (), from) ==
107  m_blackListedDevices[to].end () )
108  {
109  m_blackListedDevices[to].push_back (from);
110  }
111  }
112  else
113  {
114  m_blackListedDevices[to].push_back (from);
115  }
116 }
117 
118 void
120 {
121  if (m_blackListedDevices.find (to) != m_blackListedDevices.end ())
122  {
123  std::vector<Ptr<SimpleNetDevice> >::iterator iter;
124  iter = find (m_blackListedDevices[to].begin (), m_blackListedDevices[to].end (), from);
125  if (iter != m_blackListedDevices[to].end () )
126  {
127  m_blackListedDevices[to].erase (iter);
128  }
129  }
130 }
131 
132 
133 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Abstract Channel Base Class.
Definition: channel.h:43
virtual void Send(Ptr< Packet > p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr< SimpleNetDevice > sender)
A packet is sent by a net device.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:449
AttributeValue implementation for Time.
Definition: nstime.h:1126
virtual void UnBlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Un-Blocks the communications from a NetDevice to another NetDevice.
std::map< Ptr< SimpleNetDevice >, std::vector< Ptr< SimpleNetDevice > > > m_blackListedDevices
devices blocked on a device
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
an EUI-48 address
Definition: mac48-address.h:43
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:1127
std::vector< Ptr< SimpleNetDevice > > m_devices
devices connected by the channel
Time m_delay
The assigned speed-of-light delay of the channel.
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1484
virtual std::size_t GetNDevices(void) const
A simple channel, for simple things and testing.
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1064
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
virtual void BlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Blocks the communications from a NetDevice to another NetDevice.
static TypeId GetTypeId(void)
Get the type ID.