A Discrete-Event Network Simulator
API
simple-channel.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #include "simple-channel.h"
20 
21 #include "simple-net-device.h"
22 
23 #include "ns3/log.h"
24 #include "ns3/node.h"
25 #include "ns3/packet.h"
26 #include "ns3/simulator.h"
27 
28 #include <algorithm>
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("SimpleChannel");
34 
35 NS_OBJECT_ENSURE_REGISTERED(SimpleChannel);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId("ns3::SimpleChannel")
41  .SetParent<Channel>()
42  .SetGroupName("Network")
43  .AddConstructor<SimpleChannel>()
44  .AddAttribute("Delay",
45  "Transmission delay through the channel",
46  TimeValue(Seconds(0)),
48  MakeTimeChecker());
49  return tid;
50 }
51 
53 {
54  NS_LOG_FUNCTION(this);
55 }
56 
57 void
59  uint16_t protocol,
60  Mac48Address to,
61  Mac48Address from,
62  Ptr<SimpleNetDevice> sender)
63 {
64  NS_LOG_FUNCTION(this << p << protocol << to << from << sender);
65  for (std::vector<Ptr<SimpleNetDevice>>::const_iterator i = m_devices.begin();
66  i != m_devices.end();
67  ++i)
68  {
69  Ptr<SimpleNetDevice> tmp = *i;
70  if (tmp == sender)
71  {
72  continue;
73  }
74  if (m_blackListedDevices.find(tmp) != m_blackListedDevices.end())
75  {
76  if (find(m_blackListedDevices[tmp].begin(), m_blackListedDevices[tmp].end(), sender) !=
77  m_blackListedDevices[tmp].end())
78  {
79  continue;
80  }
81  }
82  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
83  m_delay,
85  tmp,
86  p->Copy(),
87  protocol,
88  to,
89  from);
90  }
91 }
92 
93 void
95 {
96  NS_LOG_FUNCTION(this << device);
97  m_devices.push_back(device);
98 }
99 
100 std::size_t
102 {
103  NS_LOG_FUNCTION(this);
104  return m_devices.size();
105 }
106 
108 SimpleChannel::GetDevice(std::size_t i) const
109 {
110  NS_LOG_FUNCTION(this << i);
111  return m_devices[i];
112 }
113 
114 void
116 {
117  if (m_blackListedDevices.find(to) != m_blackListedDevices.end())
118  {
119  if (find(m_blackListedDevices[to].begin(), m_blackListedDevices[to].end(), from) ==
120  m_blackListedDevices[to].end())
121  {
122  m_blackListedDevices[to].push_back(from);
123  }
124  }
125  else
126  {
127  m_blackListedDevices[to].push_back(from);
128  }
129 }
130 
131 void
133 {
134  if (m_blackListedDevices.find(to) != m_blackListedDevices.end())
135  {
136  std::vector<Ptr<SimpleNetDevice>>::iterator iter;
137  iter = find(m_blackListedDevices[to].begin(), m_blackListedDevices[to].end(), from);
138  if (iter != m_blackListedDevices[to].end())
139  {
140  m_blackListedDevices[to].erase(iter);
141  }
142  }
143 }
144 
145 } // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
an EUI-48 address
Definition: mac48-address.h:46
Ptr< Packet > Copy() const
performs a COW copy of the packet.
Definition: packet.cc:131
A simple channel, for simple things and testing.
virtual void BlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Blocks the communications from a NetDevice to another NetDevice.
std::vector< Ptr< SimpleNetDevice > > m_devices
devices connected by the channel
Ptr< NetDevice > GetDevice(std::size_t i) const override
std::map< Ptr< SimpleNetDevice >, std::vector< Ptr< SimpleNetDevice > > > m_blackListedDevices
devices blocked on a device
static TypeId GetTypeId()
Get the type ID.
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.
Time m_delay
The assigned speed-of-light delay of the channel.
std::size_t GetNDevices() const override
virtual void UnBlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Un-Blocks the communications from a NetDevice to another NetDevice.
virtual void Send(Ptr< Packet > p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr< SimpleNetDevice > sender)
A packet is sent by a net device.
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:587
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
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:1424
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:1336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:535