A Discrete-Event Network Simulator
API
error-channel.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 #include "error-channel.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/node.h"
24 #include "ns3/packet.h"
25 #include "ns3/simple-net-device.h"
26 #include "ns3/simulator.h"
27 
28 namespace ns3
29 {
30 
31 NS_LOG_COMPONENT_DEFINE("ErrorChannel");
32 
33 NS_OBJECT_ENSURE_REGISTERED(ErrorChannel);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId("ns3::ErrorChannel")
40  .SetGroupName("Network")
41  .AddConstructor<ErrorChannel>();
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
83  uint16_t protocol,
84  Mac48Address to,
85  Mac48Address from,
86  Ptr<SimpleNetDevice> sender)
87 {
88  NS_LOG_FUNCTION(p << protocol << to << from << sender);
89  for (std::vector<Ptr<SimpleNetDevice>>::const_iterator i = m_devices.begin();
90  i != m_devices.end();
91  ++i)
92  {
93  Ptr<SimpleNetDevice> tmp = *i;
94  if (tmp == sender)
95  {
96  continue;
97  }
98  if (m_jumping)
99  {
100  if (m_jumpingState % 2)
101  {
102  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
103  Seconds(0),
105  tmp,
106  p->Copy(),
107  protocol,
108  to,
109  from);
110  }
111  else
112  {
113  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
116  tmp,
117  p->Copy(),
118  protocol,
119  to,
120  from);
121  }
122  m_jumpingState++;
123  }
124  else if (m_duplicate)
125  {
126  if (m_duplicateState % 2)
127  {
128  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
129  Seconds(0),
131  tmp,
132  p->Copy(),
133  protocol,
134  to,
135  from);
136  }
137  else
138  {
139  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
140  Seconds(0),
142  tmp,
143  p->Copy(),
144  protocol,
145  to,
146  from);
147  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
150  tmp,
151  p->Copy(),
152  protocol,
153  to,
154  from);
155  }
157  }
158  else
159  {
160  Simulator::ScheduleWithContext(tmp->GetNode()->GetId(),
161  Seconds(0),
163  tmp,
164  p->Copy(),
165  protocol,
166  to,
167  from);
168  }
169  }
170 }
171 
172 void
174 {
175  m_devices.push_back(device);
176 }
177 
178 std::size_t
180 {
181  return m_devices.size();
182 }
183 
185 ErrorChannel::GetDevice(std::size_t i) const
186 {
187  return m_devices[i];
188 }
189 
190 } // namespace ns3
A Error channel, introducing deterministic delays on even/odd packets.
Definition: error-channel.h:41
Time m_duplicateTime
Duplicate time in Duplicate mode.
Definition: error-channel.h:92
uint8_t m_duplicateState
Counter for even/odd packets in Duplicate mode.
Definition: error-channel.h:94
static TypeId GetTypeId()
Get the type ID.
std::size_t GetNDevices() const override
bool m_jumping
Flag for Jumping mode.
Definition: error-channel.h:91
std::vector< Ptr< SimpleNetDevice > > m_devices
devices connected by the channel
Definition: error-channel.h:88
void Send(Ptr< Packet > p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr< SimpleNetDevice > sender) override
A packet is sent by a net device.
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
void SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
Ptr< NetDevice > GetDevice(std::size_t i) const override
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:89
void Add(Ptr< SimpleNetDevice > device) override
Attached a net device to the channel.
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:90
bool m_duplicate
Flag for Duplicate mode.
Definition: error-channel.h:93
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.
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
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
#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.