A Discrete-Event Network Simulator
API
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 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("ErrorChannelSixlow");
30 
31 NS_OBJECT_ENSURE_REGISTERED (ErrorChannelSixlow);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::ErrorChannelSixlow")
38  .SetGroupName ("SixLowPan")
39  .AddConstructor<ErrorChannelSixlow> ()
40  ;
41  return tid;
42 }
43 
45 {
46  m_jumpingTime = Seconds (0.5);
47  m_jumping = false;
48  m_jumpingState = 0;
49  m_duplicateTime = Seconds (0.1);
50  m_duplicate = false;
51 }
52 
53 void
55 {
56  m_jumpingTime = delay;
57 }
58 
59 void
61 {
62  m_jumping = mode;
63  m_jumpingState = 0;
64 }
65 
66 void
68 {
69  m_duplicateTime = delay;
70 }
71 
72 void
74 {
75  m_duplicate = mode;
76  m_duplicateState = 0;
77 }
78 
79 
80 void
81 ErrorChannelSixlow::Send (Ptr<Packet> p, uint16_t protocol,
82  Mac48Address to, Mac48Address from,
83  Ptr<SimpleNetDevice> sender)
84 {
85  NS_LOG_FUNCTION (p << protocol << to << from << sender);
86  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
87  {
88  Ptr<SimpleNetDevice> tmp = *i;
89  if (tmp == sender)
90  {
91  continue;
92  }
93  if (m_jumping)
94  {
95  if (m_jumpingState % 2)
96  {
97  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
98  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
99  }
100  else
101  {
102  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_jumpingTime,
103  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
104  }
105  m_jumpingState++;
106  }
107  else if (m_duplicate)
108  {
109  if (m_duplicateState % 2)
110  {
111  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
112  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
113  }
114  else
115  {
116  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
117  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
118  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_duplicateTime,
119  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
120  }
122  }
123  else
124  {
125  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
126  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
127  }
128  }
129 }
130 
131 void
133 {
134  m_devices.push_back (device);
135 }
136 
137 uint32_t
139 {
140  return m_devices.size ();
141 }
144 {
145  return m_devices[i];
146 }
147 
149 
151 {
152  static TypeId tid = TypeId ("ns3::BinaryErrorSixlowModel")
153  .SetParent<ErrorModel> ()
154  .SetGroupName ("SixLowPan")
155  .AddConstructor<BinaryErrorSixlowModel> ()
156  ;
157  return tid;
158 }
159 
161 {
162  m_counter = 0;
163 }
164 
166 {
167 }
168 
169 
170 bool
172 {
173  if (!IsEnabled ())
174  {
175  return false;
176  }
177  bool ret = m_counter % 2;
178  m_counter++;
179  return ret;
180 }
181 
182 void
184 {
185  DoReset ();
186 }
187 
188 void
190 {
191  m_counter = 0;
192 }
193 
194 
195 } // namespace ns3
virtual uint32_t GetNDevices(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#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:44
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:201
void SetDuplicateTime(Time delay)
Set the delay for the odd duplicate packets (even ones are not duplicated)
A Error channel, introducing deterministic delays on even/odd packets.
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
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void DoReset(void)
Re-initialize any state.
an EUI-48 address
Definition: mac48-address.h:43
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.
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1319
A simple channel, for simple things and testing.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
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:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
std::vector< Ptr< SimpleNetDevice > > m_devices
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.