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 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::ErrorChannel")
38  .SetParent<Channel> ()
39  .AddConstructor<ErrorChannel> ()
40  ;
41  return tid;
42 }
43 
45 {
46  jumping = false;
47  jumpingState = 0;
48 }
49 
50 void
52 {
53  jumpingTime = delay;
54 }
55 
56 void
58 {
59  jumping = mode;
60  jumpingState = 0;
61 }
62 
63 void
64 ErrorChannel::Send (Ptr<Packet> p, uint16_t protocol,
65  Mac48Address to, Mac48Address from,
66  Ptr<SimpleNetDevice> sender)
67 {
68  NS_LOG_FUNCTION (p << protocol << to << from << sender);
69  for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
70  {
71  Ptr<SimpleNetDevice> tmp = *i;
72  if (tmp == sender)
73  {
74  continue;
75  }
76  if( !jumping || jumpingState%2 )
77  {
78  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
79  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
80  jumpingState++;
81  }
82  else
83  {
84  Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), jumpingTime,
85  &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
86  jumpingState++;
87  }
88  }
89 }
90 
91 void
93 {
94  m_devices.push_back (device);
95 }
96 
97 uint32_t
99 {
100  return m_devices.size ();
101 }
103 ErrorChannel::GetDevice (uint32_t i) const
104 {
105  return m_devices[i];
106 }
107 
109  ;
110 
112 {
113  static TypeId tid = TypeId ("ns3::BinaryErrorModel")
114  .SetParent<ErrorModel> ()
115  .AddConstructor<BinaryErrorModel> ()
116  ;
117  return tid;
118 }
119 
121 {
122  counter = 0;
123 }
124 
126 {
127 }
128 
129 
130 bool
132 {
133  if (!IsEnabled ())
134  {
135  return false;
136  }
137  bool ret = counter%2;
138  counter++;
139  return ret;
140 }
141 
142 void
144 {
145  DoReset();
146 }
147 
148 void
150 {
151  counter = 0;
152 }
153 
154 
155 } // namespace ns3
virtual bool DoCorrupt(Ptr< Packet > p)
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
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.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
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)
NS_LOG_COMPONENT_DEFINE("ErrorChannel")
bool IsEnabled(void) const
Definition: error-model.cc:139
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:904
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: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:611
virtual Ptr< NetDevice > GetDevice(uint32_t i) const