A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
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: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "energy-source.h"
22 #include "ns3/log.h"
23 
24 NS_LOG_COMPONENT_DEFINE ("EnergySource");
25 
26 namespace ns3 {
27 
28 NS_OBJECT_ENSURE_REGISTERED (EnergySource);
29 
30 TypeId
32 {
33  static TypeId tid = TypeId ("ns3::EnergySource")
34  .SetParent<Object> ()
35  ;
36  return tid;
37 }
38 
40 {
41  NS_LOG_FUNCTION (this);
42 }
43 
45 {
46  NS_LOG_FUNCTION (this);
47 }
48 
49 void
51 {
52  NS_LOG_FUNCTION (this);
53  NS_ASSERT (node != NULL);
54  m_node = node;
55 }
56 
59 {
60  NS_LOG_FUNCTION (this);
61  return m_node;
62 }
63 
64 void
66 {
67  NS_LOG_FUNCTION (this << deviceEnergyModelPtr);
68  NS_ASSERT (deviceEnergyModelPtr != NULL); // model must exist
69  m_models.Add (deviceEnergyModelPtr);
70 }
71 
74 {
75  NS_LOG_FUNCTION (this << tid);
78  for (i = m_models.Begin (); i != m_models.End (); i++)
79  {
80  if ((*i)->GetInstanceTypeId () == tid)
81  {
82  container.Add (*i);
83  }
84  }
85  return container;
86 }
87 
90 {
91  NS_LOG_FUNCTION (this << name);
94  for (i = m_models.Begin (); i != m_models.End (); i++)
95  {
96  if ((*i)->GetInstanceTypeId ().GetName ().compare (name) == 0)
97  {
98  container.Add (*i);
99  }
100  }
101  return container;
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION (this);
108  /*
109  * Device models are not aggregated to the node, hence we have to manually
110  * call dispose method here.
111  */
113  for (i = m_models.Begin (); i != m_models.End (); i++)
114  {
115  (*i)->Initialize ();
116  }
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION (this);
123  /*
124  * Device models are not aggregated to the node, hence we have to manually
125  * call dispose method here.
126  */
128  for (i = m_models.Begin (); i != m_models.End (); i++)
129  {
130  (*i)->Dispose ();
131  }
132 }
133 
134 /*
135  * Private function starts here.
136  */
137 
138 void
140 {
141  NS_LOG_FUNCTION (this);
143 }
144 
145 /*
146  * Protected functions start here.
147  */
148 
149 double
151 {
152  NS_LOG_FUNCTION (this);
153  double totalCurrentA = 0.0;
155  for (i = m_models.Begin (); i != m_models.End (); i++)
156  {
157  totalCurrentA += (*i)->GetCurrentA ();
158  }
159  return totalCurrentA;
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION (this);
166  // notify all device energy models installed on node
168  for (i = m_models.Begin (); i != m_models.End (); i++)
169  {
170  (*i)->HandleEnergyDepletion ();
171  }
172 }
173 
174 void
176 {
177  NS_LOG_FUNCTION (this);
178  m_models.Clear ();
179  m_node = NULL;
180 }
181 
182 } // namespace ns3
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
#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
Ptr< Node > GetNode(void) const
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void Add(DeviceEnergyModelContainer container)
Holds a vector of ns3::DeviceEnergyModel pointers.
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
virtual void DoDispose(void)
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
static TypeId GetTypeId(void)
double CalculateTotalCurrent(void)
Iterator Begin(void) const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
void InitializeDeviceModels(void)
Calls Start () method of the device energy models.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
virtual ~EnergySource()
Ptr< Node > m_node
Pointer to node containing this EnergySource.
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Iterator End(void) const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
DeviceEnergyModelContainer m_models
List of device energy models installed on the same node.
void DisposeDeviceModels(void)
Calls Dispose () method of the device energy models.
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
a base class which provides memory management and object aggregation
Definition: object.h:64
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void Clear(void)
Removes all elements in the container.