A Discrete-Event Network Simulator
API
nsc-sysctl.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
17  */
18 
19 #include "ns3/string.h"
20 #include "nsc-sysctl.h"
21 
22 #include "sim_interface.h"
23 
24 namespace ns3 {
25 
33 {
34 public:
39  NscStackStringAccessor (std::string name) : m_name (name) {}
40 
41  virtual bool Set (ObjectBase * object, const AttributeValue &val) const;
42  virtual bool Get (const ObjectBase * object, AttributeValue &val) const;
43  virtual bool HasGetter (void) const;
44  virtual bool HasSetter (void) const;
45 private:
46  std::string m_name;
47 };
48 
50 {
51  return true;
52 }
53 
55 {
56  return true;
57 }
58 
59 
60 bool NscStackStringAccessor::Set (ObjectBase * object, const AttributeValue & val) const
61 {
62  const StringValue *value = dynamic_cast<const StringValue *> (&val);
63  if (value == 0)
64  {
65  return false;
66  }
67  Ns3NscStack *obj = dynamic_cast<Ns3NscStack *> (object);
68  if (obj == 0)
69  {
70  return false;
71  }
72  obj->Set (m_name, value->Get ());
73  return true;
74 }
75 
76 bool NscStackStringAccessor::Get (const ObjectBase * object, AttributeValue &val) const
77 {
78  StringValue *value = dynamic_cast<StringValue *> (&val);
79  if (value == 0)
80  {
81  return false;
82  }
83  const Ns3NscStack *obj = dynamic_cast<const Ns3NscStack *> (object);
84  if (obj == 0)
85  {
86  return false;
87  }
88  value->Set (obj->Get (m_name));
89  return true;
90 }
91 
92 
93 TypeId
95 {
96  if (m_stack == 0)
97  {
98  // if we have no stack, we are a normal NscStack without any attributes.
99  return GetTypeId ();
100  }
101  std::string name = "ns3::Ns3NscStack<";
102  name += m_stack->get_name ();
103  name += ">";
104  TypeId tid;
105  if (TypeId::LookupByNameFailSafe (name, &tid))
106  {
107  // if the relevant TypeId has already been registered, no need to do it again.
108  return tid;
109  }
110  else
111  {
112  // Now, we register a new TypeId for this stack which will look
113  // like a subclass of the Ns3NscStack. The class Ns3NscStack is effectively
114  // mutating into a subclass of itself from the point of view of the TypeId
115  // system _here_
116  tid = TypeId (name.c_str ());
117  tid.SetParent<Ns3NscStack> ();
118  char buf[256];
119  for (int i=0; m_stack->sysctl_getnum (i, buf, sizeof(buf)) > 0; i++)
120  {
121  char value[256];
122  if (m_stack->sysctl_get (buf, value, sizeof(value)) > 0)
123  {
124  tid.AddAttribute (buf, "Help text",
125  StringValue (value),
126  Create<NscStackStringAccessor> (buf),
127  MakeStringChecker ());
128  }
129  }
130  return tid;
131  }
132 }
133 
134 std::string
135 Ns3NscStack::Get (std::string name) const
136 {
137  char buf[512];
138  if (m_stack->sysctl_get (name.c_str (), buf, sizeof(buf)) <= 0)
139  { // name.c_str () is not a valid sysctl name, or internal NSC error (eg. error converting value)
140  return NULL;
141  }
142  return std::string (buf);
143 }
144 
145 void
146 Ns3NscStack::Set (std::string name, std::string value)
147 {
148  int ret = m_stack->sysctl_set (name.c_str (), value.c_str ());
149  if (ret < 0)
150  {
151  NS_FATAL_ERROR ("setting " << name << " to " << value << "failed (retval " << ret << ")");
152  }
153 }
154 
156 
157 TypeId
158 Ns3NscStack::Ns3NscStack::GetTypeId (void)
159 {
160  static TypeId tid = TypeId ("ns3::Ns3NscStack")
161  .SetParent<Object> ()
162  ;
163  return tid;
164 }
165 
166 } // namespace ns3
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker, SupportLevel supportLevel=SUPPORTED, const std::string &supportMsg="")
Record in this TypeId the fact that a new attribute exists.
Definition: type-id.cc:1006
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Hold variables of type string.
Definition: string.h:41
void Set(const std::string &value)
Set the value.
Definition: string.cc:31
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: nsc-sysctl.cc:94
Hold a value for an Attribute.
Definition: attribute.h:68
This object represent the underlying nsc stack attributes and provide a ns-3-like system to access th...
Definition: nsc-sysctl.cc:32
This object represents the underlying nsc stack, which is aggregated to a Node object, and which provides access to the sysctls of the nsc stack through attributes.
Definition: nsc-sysctl.h:35
virtual bool Set(ObjectBase *object, const AttributeValue &val) const
Definition: nsc-sysctl.cc:60
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:832
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Definition: nsc-sysctl.cc:76
virtual bool HasSetter(void) const
Definition: nsc-sysctl.cc:54
virtual bool HasGetter(void) const
Definition: nsc-sysctl.cc:49
std::string Get(void) const
Definition: string.cc:31
virtual const char * get_name()=0
Return a short one-word name of the stack.
allow setting and getting the value of an attribute.
Definition: attribute.h:114
NscStackStringAccessor(std::string name)
Constructor.
Definition: nsc-sysctl.cc:39
virtual int sysctl_set(const char *name, const char *value)
Set system parameters using sysctl.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Get the type ID.
std::string m_name
name of the attribute
Definition: nsc-sysctl.cc:46
virtual int sysctl_getnum(size_t idx, char *name, size_t len)
Tell the cradle code to put the name of sysctl number &#39;idx&#39; into name[].
void Set(std::string name, std::string value)
Set an attribute.
Definition: nsc-sysctl.cc:146
std::string Get(std::string name) const
Get an attribute.
Definition: nsc-sysctl.cc:135
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
INetStack * m_stack
the underlying stack
Definition: nsc-sysctl.h:68
virtual int sysctl_get(const char *name, char *value, size_t len)
Get system parameters using sysctl.