A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
error-channel-sixlow.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 #include "error-channel-sixlow.h"
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 
27 NS_LOG_COMPONENT_DEFINE ("ErrorChannelSixlow");
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (ErrorChannelSixlow);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::ErrorChannelSixlow")
37  .SetParent<Channel> ()
38  .AddConstructor<ErrorChannelSixlow> ()
39  ;
40  return tid;
41 }
42 
44 {
45  m_jumpingTime = Seconds (0.5);
46  m_jumping = false;
47  m_jumpingState = 0;
48  m_duplicateTime = Seconds (0.1);
49  m_duplicate = false;
50 }
51 
52 void
54 {
55  m_jumpingTime = delay;
56 }
57 
58 void
60 {
61  m_jumping = mode;
62  m_jumpingState = 0;
63 }
64 
65 void
67 {
68  m_duplicateTime = delay;
69 }
70 
71 void
73 {
74  m_duplicate = mode;
75  m_duplicateState = 0;
76 }
77 
78 
79 void
80 ErrorChannelSixlow::Send (Ptr<Packet> p, uint16_t protocol,
81  Mac48Address to, Mac48Address from,
82  Ptr<SimpleNetDevice> sender)
83 {
84  NS_LOG_FUNCTION (p << protocol << to << from << sender);
85  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
86  {
87  Ptr<SimpleNetDevice> tmp = *i;
88  if (tmp == sender)
89  {
90  continue;
91  }
92  if (m_jumping)
93  {
94  if (m_jumpingState % 2)
95  {
96  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
97  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
98  }
99  else
100  {
101  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_jumpingTime,
102  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
103  }
104  m_jumpingState++;
105  }
106  else if (m_duplicate)
107  {
108  if (m_duplicateState % 2)
109  {
110  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
111  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
112  }
113  else
114  {
115  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
116  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
117  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_duplicateTime,
118  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
119  }
121  }
122  else
123  {
124  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
125  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
126  }
127  }
128 }
129 
130 void
132 {
133  m_devices.push_back (device);
134 }
135 
136 uint32_t
138 {
139  return m_devices.size ();
140 }
143 {
144  return m_devices[i];
145 }
146 
148 
150 {
151  static TypeId tid = TypeId ("ns3::BinaryErrorSixlowModel")
152  .SetParent<ErrorModel> ()
153  .AddConstructor<BinaryErrorSixlowModel> ()
154  ;
155  return tid;
156 }
157 
159 {
160  m_counter = 0;
161 }
162 
164 {
165 }
166 
167 
168 bool
170 {
171  if (!IsEnabled ())
172  {
173  return false;
174  }
175  bool ret = m_counter % 2;
176  m_counter++;
177  return ret;
178 }
179 
180 void
182 {
183  DoReset ();
184 }
185 
186 void
188 {
189  m_counter = 0;
190 }
191 
192 
193 } // namespace ns3
virtual uint32_t GetNDevices(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
#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 the class in the ns-3 factory.
Definition: object-base.h:38
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
Abstract Channel Base Class.
Definition: channel.h:43
void SetDuplicateTime(Time delay)
Set the delay for the odd duplicate packets (even ones are not duplicated)
General error model that can be used to corrupt packets.
Definition: error-model.h:115
void SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
bool IsEnabled(void) const
Definition: error-model.cc:138
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:905
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
virtual void DoReset(void)
Re-initialize any state.
an EUI-48 address
Definition: mac48-address.h:41
virtual void Send(Ptr< Packet > p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr< SimpleNetDevice > sender)
A packet is sent by a net device.
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
virtual Ptr< NetDevice > GetDevice(uint32_t i) const
static TypeId GetTypeId(void)
void SetDuplicateMode(bool mode)
Set if the odd packets are duplicated (even ones are not duplicated ever)
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
std::vector< Ptr< SimpleNetDevice > > m_devices
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.