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 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (ErrorChannelSixlow)
33  ;
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::ErrorChannelSixlow")
39  .SetParent<Channel> ()
40  .AddConstructor<ErrorChannelSixlow> ()
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 }
53 
54 void
56 {
57  m_jumpingTime = delay;
58 }
59 
60 void
62 {
63  m_jumping = mode;
64  m_jumpingState = 0;
65 }
66 
67 void
69 {
70  m_duplicateTime = delay;
71 }
72 
73 void
75 {
76  m_duplicate = mode;
77  m_duplicateState = 0;
78 }
79 
80 
81 void
82 ErrorChannelSixlow::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 uint32_t
140 {
141  return m_devices.size ();
142 }
145 {
146  return m_devices[i];
147 }
148 
150  ;
151 
153 {
154  static TypeId tid = TypeId ("ns3::BinaryErrorSixlowModel")
155  .SetParent<ErrorModel> ()
156  .AddConstructor<BinaryErrorSixlowModel> ()
157  ;
158  return tid;
159 }
160 
162 {
163  m_counter = 0;
164 }
165 
167 {
168 }
169 
170 
171 bool
173 {
174  if (!IsEnabled ())
175  {
176  return false;
177  }
178  bool ret = m_counter % 2;
179  m_counter++;
180  return ret;
181 }
182 
183 void
185 {
186  DoReset ();
187 }
188 
189 void
191 {
192  m_counter = 0;
193 }
194 
195 
196 } // namespace ns3
virtual uint32_t GetNDevices(void) const
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void Receive(Ptr< Packet > packet, uint16_t protocol, Mac48Address to, Mac48Address from)
Receive a packet from a connected SimpleChannel.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:171
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:139
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:904
Ptr< Packet > Copy(void) const
Definition: packet.cc:122
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)
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:611
std::vector< Ptr< SimpleNetDevice > > m_devices
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.