A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
energy-source.cc
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
31 EnergySource::GetTypeId (void)
32 {
33  static TypeId tid = TypeId ("ns3::EnergySource")
34  .SetParent<Object> ()
35  ;
36  return tid;
37 }
38 
39 EnergySource::EnergySource ()
40 {
41  NS_LOG_FUNCTION (this);
42 }
43 
44 EnergySource::~EnergySource ()
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);
77  DeviceEnergyModelContainer::Iterator i;
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);
93  DeviceEnergyModelContainer::Iterator i;
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  */
112  DeviceEnergyModelContainer::Iterator i;
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  */
127  DeviceEnergyModelContainer::Iterator i;
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;
154  DeviceEnergyModelContainer::Iterator i;
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
167  DeviceEnergyModelContainer::Iterator i;
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
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
Ptr< Node > GetNode(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void Add(DeviceEnergyModelContainer container)
Holds a vector of ns3::DeviceEnergyModel pointers.
void BreakDeviceEnergyModelRefCycle(void)
virtual void DoDispose(void)
double CalculateTotalCurrent(void)
Iterator Begin(void) const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
void InitializeDeviceModels(void)
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
Ptr< Node > m_node
void NotifyEnergyDrained(void)
Iterator End(void) const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
DeviceEnergyModelContainer m_models
void DisposeDeviceModels(void)
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
a unique identifier for an interface.
Definition: type-id.h:44
void Clear(void)
Removes all elements in the container.