A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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) 2011 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.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 ("ErrorChannel");
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (ErrorChannel);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::ErrorChannel")
37  .SetParent<Channel> ()
38  .AddConstructor<ErrorChannel> ()
39  ;
40  return tid;
41 }
42 
44 {
45  jumping = false;
46  jumpingState = 0;
47 }
48 
49 void
51 {
52  jumpingTime = delay;
53 }
54 
55 void
57 {
58  jumping = mode;
59  jumpingState = 0;
60 }
61 
62 void
63 ErrorChannel::Send (Ptr<Packet> p, uint16_t protocol,
64  Mac48Address to, Mac48Address from,
65  Ptr<SimpleNetDevice> sender)
66 {
67  NS_LOG_FUNCTION (p << protocol << to << from << sender);
68  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
69  {
70  Ptr<SimpleNetDevice> tmp = *i;
71  if (tmp == sender)
72  {
73  continue;
74  }
75  if( !jumping || jumpingState%2 )
76  {
77  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
78  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
79  jumpingState++;
80  }
81  else
82  {
83  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), jumpingTime,
84  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
85  jumpingState++;
86  }
87  }
88 }
89 
90 void
92 {
93  m_devices.push_back (device);
94 }
95 
96 uint32_t
98 {
99  return m_devices.size ();
100 }
102 ErrorChannel::GetDevice (uint32_t i) const
103 {
104  return m_devices[i];
105 }
106 
108 
110 {
111  static TypeId tid = TypeId ("ns3::BinaryErrorModel")
112  .SetParent<ErrorModel> ()
113  .AddConstructor<BinaryErrorModel> ()
114  ;
115  return tid;
116 }
117 
119 {
120  counter = 0;
121 }
122 
124 {
125 }
126 
127 
128 bool
130 {
131  if (!IsEnabled ())
132  {
133  return false;
134  }
135  bool ret = counter%2;
136  counter++;
137  return ret;
138 }
139 
140 void
142 {
143  DoReset();
144 }
145 
146 void
148 {
149  counter = 0;
150 }
151 
152 
153 } // namespace ns3
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
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
std::vector< Ptr< SimpleNetDevice > > m_devices
Definition: error-channel.h:66
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
static TypeId GetTypeId(void)
General error model that can be used to corrupt packets.
Definition: error-model.h:115
virtual void DoReset(void)
Re-initialize any state.
bool IsEnabled(void) const
Definition: error-model.cc:138
static TypeId GetTypeId(void)
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
void SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
an EUI-48 address
Definition: mac48-address.h:41
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
virtual void Add(Ptr< SimpleNetDevice > device)
Attached a net device to the channel.
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 uint32_t GetNDevices(void) const
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
virtual Ptr< NetDevice > GetDevice(uint32_t i) const