A Discrete-Event Network Simulator
API
error-channel.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "ns3/simple-net-device.h"
22 #include "ns3/simulator.h"
23 #include "ns3/packet.h"
24 #include "ns3/node.h"
25 #include "ns3/log.h"
26 #include "error-channel.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("ErrorChannel");
31 
32 NS_OBJECT_ENSURE_REGISTERED (ErrorChannel);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::ErrorChannel")
39  .SetGroupName ("Network")
40  .AddConstructor<ErrorChannel> ()
41  ;
42  return tid;
43 }
44 
46 {
47  m_jumpingTime = Seconds (0.5);
48  m_jumping = false;
49  m_jumpingState = 0;
50  m_duplicateTime = Seconds (0.1);
51  m_duplicate = false;
52  m_duplicateState = 0;
53 }
54 
55 void
57 {
58  m_jumpingTime = delay;
59 }
60 
61 void
63 {
64  m_jumping = mode;
65  m_jumpingState = 0;
66 }
67 
68 void
70 {
71  m_duplicateTime = delay;
72 }
73 
74 void
76 {
77  m_duplicate = mode;
78  m_duplicateState = 0;
79 }
80 
81 void
82 ErrorChannel::Send (Ptr<Packet> p, uint16_t protocol,
83  Mac48Address to, Mac48Address from,
84  Ptr<SimpleNetDevice> sender)
85 {
86  NS_LOG_FUNCTION (p << protocol << to << from << sender);
87  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
88  {
89  Ptr<SimpleNetDevice> tmp = *i;
90  if (tmp == sender)
91  {
92  continue;
93  }
94  if (m_jumping)
95  {
96  if (m_jumpingState % 2)
97  {
98  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
99  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
100  }
101  else
102  {
103  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_jumpingTime,
104  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
105  }
106  m_jumpingState++;
107  }
108  else if (m_duplicate)
109  {
110  if (m_duplicateState % 2)
111  {
112  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
113  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
114  }
115  else
116  {
117  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
118  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
119  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_duplicateTime,
120  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
121  }
123  }
124  else
125  {
126  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
127  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
128  }
129  }
130 }
131 
132 void
134 {
135  m_devices.push_back (device);
136 }
137 
138 std::size_t
140 {
141  return m_devices.size ();
142 }
143 
145 ErrorChannel::GetDevice (std::size_t i) const
146 {
147  return m_devices[i];
148 }
149 
150 
151 } // namespace ns3
A Error channel, introducing deterministic delays on even/odd packets.
Definition: error-channel.h:40
Time m_duplicateTime
Duplicate time in Duplicate mode.
Definition: error-channel.h:88
virtual Ptr< NetDevice > GetDevice(std::size_t i) const
uint8_t m_duplicateState
Counter for even/odd packets in Duplicate mode.
Definition: error-channel.h:90
bool m_jumping
Flag for Jumping mode.
Definition: error-channel.h:87
std::vector< Ptr< SimpleNetDevice > > m_devices
devices connected by the channel
Definition: error-channel.h:84
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
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 SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.
void SetDuplicateMode(bool mode)
Set if the odd packets are duplicated (even ones are not duplicated ever)
Time m_jumpingTime
Delay time in Jumping mode.
Definition: error-channel.h:85
void SetDuplicateTime(Time delay)
Set the delay for the odd duplicate packets (even ones are not duplicated)
uint8_t m_jumpingState
Counter for even/odd packets in Jumping mode.
Definition: error-channel.h:86
bool m_duplicate
Flag for Duplicate mode.
Definition: error-channel.h:89
virtual std::size_t GetNDevices(void) const
static TypeId GetTypeId(void)
Get the type ID.
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.
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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.