A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
30namespace ns3
31{
32
33NS_LOG_COMPONENT_DEFINE("SimpleChannel");
34
35NS_OBJECT_ENSURE_REGISTERED(SimpleChannel);
36
37TypeId
39{
40 static TypeId tid = TypeId("ns3::SimpleChannel")
42 .SetGroupName("Network")
43 .AddConstructor<SimpleChannel>()
44 .AddAttribute("Delay",
45 "Transmission delay through the channel",
49 return tid;
50}
51
53{
54 NS_LOG_FUNCTION(this);
55}
56
57void
59 uint16_t protocol,
60 Mac48Address to,
61 Mac48Address from,
63{
64 NS_LOG_FUNCTION(this << p << protocol << to << from << sender);
65 for (auto i = m_devices.begin(); i != m_devices.end(); ++i)
66 {
67 Ptr<SimpleNetDevice> tmp = *i;
68 if (tmp == sender)
69 {
70 continue;
71 }
72 if (m_blackListedDevices.find(tmp) != m_blackListedDevices.end())
73 {
74 if (find(m_blackListedDevices[tmp].begin(), m_blackListedDevices[tmp].end(), sender) !=
75 m_blackListedDevices[tmp].end())
76 {
77 continue;
78 }
79 }
80 Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
81 m_delay,
83 tmp,
84 p->Copy(),
85 protocol,
86 to,
87 from);
88 }
89}
90
91void
93{
94 NS_LOG_FUNCTION(this << device);
95 m_devices.push_back(device);
96}
97
98std::size_t
100{
101 NS_LOG_FUNCTION(this);
102 return m_devices.size();
103}
104
106SimpleChannel::GetDevice(std::size_t i) const
107{
108 NS_LOG_FUNCTION(this << i);
109 return m_devices[i];
110}
111
112void
114{
115 if (m_blackListedDevices.find(to) != m_blackListedDevices.end())
116 {
117 if (find(m_blackListedDevices[to].begin(), m_blackListedDevices[to].end(), from) ==
118 m_blackListedDevices[to].end())
119 {
120 m_blackListedDevices[to].push_back(from);
121 }
122 }
123 else
124 {
125 m_blackListedDevices[to].push_back(from);
126 }
127}
128
129void
131{
132 if (m_blackListedDevices.find(to) != m_blackListedDevices.end())
133 {
134 auto iter = find(m_blackListedDevices[to].begin(), m_blackListedDevices[to].end(), from);
135 if (iter != m_blackListedDevices[to].end())
136 {
137 m_blackListedDevices[to].erase(iter);
138 }
139 }
140}
141
142} // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
an EUI-48 address
Definition: mac48-address.h:46
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:588
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1434
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
#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:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.