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
28namespace ns3 {
29
30NS_LOG_COMPONENT_DEFINE ("SimpleChannel");
31
32NS_OBJECT_ENSURE_REGISTERED (SimpleChannel);
33
34TypeId
36{
37 static TypeId tid = TypeId ("ns3::SimpleChannel")
39 .SetGroupName("Network")
40 .AddConstructor<SimpleChannel> ()
41 .AddAttribute ("Delay", "Transmission delay through the channel",
42 TimeValue (Seconds (0)),
45 ;
46 return tid;
47}
48
50{
51 NS_LOG_FUNCTION (this);
52}
53
54void
55SimpleChannel::Send (Ptr<Packet> p, uint16_t protocol,
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
80void
82{
83 NS_LOG_FUNCTION (this << device);
84 m_devices.push_back (device);
85}
86
87std::size_t
89{
90 NS_LOG_FUNCTION (this);
91 return m_devices.size ();
92}
93
95SimpleChannel::GetDevice (std::size_t i) const
96{
97 NS_LOG_FUNCTION (this << i);
98 return m_devices[i];
99}
100
101void
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
118void
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
Abstract Channel Base Class.
Definition: channel.h:44
an EUI-48 address
Definition: mac48-address.h:44
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
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.
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
std::vector< Ptr< SimpleNetDevice > > m_devices
devices connected by the channel
static TypeId GetTypeId(void)
Get the type ID.
std::map< Ptr< SimpleNetDevice >, std::vector< Ptr< SimpleNetDevice > > > m_blackListedDevices
devices blocked on a device
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.
virtual void UnBlackList(Ptr< SimpleNetDevice > from, Ptr< SimpleNetDevice > to)
Un-Blocks the communications from a NetDevice to another NetDevice.
virtual std::size_t GetNDevices(void) const
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, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1309
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
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:536