A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
error-channel.cc
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 "error-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
34 ErrorChannel::GetTypeId (void)
35 {
36  static TypeId tid = TypeId ("ns3::ErrorChannel")
37  .SetParent<Channel> ()
38  .AddConstructor<ErrorChannel> ()
39  ;
40  return tid;
41 }
42 
43 ErrorChannel::ErrorChannel ()
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<ErrorNetDevice> sender)
66 {
67  NS_LOG_FUNCTION (p << protocol << to << from << sender);
68  for (std::vector<Ptr<ErrorNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
69  {
70  Ptr<ErrorNetDevice> 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  &ErrorNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
79  jumpingState++;
80  }
81  else
82  {
83  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), jumpingTime,
84  &ErrorNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
85  jumpingState++;
86  }
87  }
88 }
89 
90 void
91 ErrorChannel::Add (Ptr<ErrorNetDevice> device)
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 
107 NS_OBJECT_ENSURE_REGISTERED (ErrorModel);
108 
109 TypeId BinaryErrorModel::GetTypeId (void)
110 {
111  static TypeId tid = TypeId ("ns3::BinaryErrorModel")
112  .SetParent<ErrorModel> ()
113  .AddConstructor<BinaryErrorModel> ()
114  ;
115  return tid;
116 }
117 
118 BinaryErrorModel::BinaryErrorModel ()
119 {
120  counter = 0;
121 }
122 
123 BinaryErrorModel::~BinaryErrorModel ()
124 {
125 }
126 
127 
128 bool
129 BinaryErrorModel::DoCorrupt (Ptr<Packet> p)
130 {
131  if (!IsEnabled ())
132  {
133  return false;
134  }
135  bool ret = counter%2;
136  counter++;
137  return ret;
138 }
139 
140 void
141 BinaryErrorModel::Reset (void)
142 {
143  DoReset();
144 }
145 
146 void
147 BinaryErrorModel::DoReset (void)
148 {
149  counter = 0;
150 }
151 
152 
153 } // namespace ns3
keep track of time unit.
Definition: nstime.h:149
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
General error model that can be used to corrupt packets.
Definition: error-model.h:115
bool IsEnabled(void) const
Definition: error-model.cc:138
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:900
void SetJumpingMode(bool mode)
Set if the odd packets are delayed (even ones are not delayed ever)
Ptr< Packet > Copy(void) const
Definition: packet.cc:131
an EUI-48 address
Definition: mac48-address.h:41
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void SetJumpingTime(Time delay)
Set the delay for the odd packets (even ones are not delayed)
virtual uint32_t GetNDevices(void) const
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
virtual Ptr< NetDevice > GetDevice(uint32_t i) const