src/node/nsc-sysctl.cc
changeset 4685 ae536d9e0d6d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/node/nsc-sysctl.cc	Wed Jul 15 18:46:14 2009 +0200
     1.3 @@ -0,0 +1,154 @@
     1.4 +/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/*
     1.6 + * This program is free software; you can redistribute it and/or modify
     1.7 + * it under the terms of the GNU General Public License version 2 as
     1.8 + * published by the Free Software Foundation;
     1.9 + *
    1.10 + * This program is distributed in the hope that it will be useful,
    1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.13 + * GNU General Public License for more details.
    1.14 + *
    1.15 + * You should have received a copy of the GNU General Public License
    1.16 + * along with this program; if not, write to the Free Software
    1.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.18 + *
    1.19 + * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    1.20 + */
    1.21 +
    1.22 +#include "ns3/string.h"
    1.23 +#include "nsc-sysctl.h"
    1.24 +
    1.25 +#include "sim_interface.h"
    1.26 +
    1.27 +namespace ns3 {
    1.28 +
    1.29 +class NscStackStringAccessor : public AttributeAccessor
    1.30 +{
    1.31 +  public:
    1.32 +  NscStackStringAccessor (std::string name) : m_name (name) {}
    1.33 +
    1.34 +  virtual bool Set (ObjectBase * object, const AttributeValue &val) const;
    1.35 +  virtual bool Get (const ObjectBase * object, AttributeValue &val) const;
    1.36 +  virtual bool HasGetter (void) const;
    1.37 +  virtual bool HasSetter (void) const;
    1.38 +  private:
    1.39 +  std::string m_name;
    1.40 +};
    1.41 +
    1.42 +bool NscStackStringAccessor::HasGetter(void) const
    1.43 +{
    1.44 +  return true;
    1.45 +}
    1.46 +
    1.47 +bool NscStackStringAccessor::HasSetter(void) const
    1.48 +{
    1.49 +  return true;
    1.50 +}
    1.51 +
    1.52 +
    1.53 +bool NscStackStringAccessor::Set (ObjectBase * object, const AttributeValue & val) const
    1.54 +{
    1.55 +  const StringValue *value = dynamic_cast<const StringValue *> (&val);
    1.56 +  if (value == 0)
    1.57 +    {
    1.58 +      return false;
    1.59 +    }
    1.60 +  Ns3NscStack *obj = dynamic_cast<Ns3NscStack *> (object);
    1.61 +  if (obj == 0)
    1.62 +    {
    1.63 +      return false;
    1.64 +    }
    1.65 +  obj->Set (m_name, value->Get ());
    1.66 +  return true;
    1.67 +}
    1.68 +
    1.69 +bool NscStackStringAccessor::Get (const ObjectBase * object, AttributeValue &val) const
    1.70 +{
    1.71 +  StringValue *value = dynamic_cast<StringValue *> (&val);
    1.72 +  if (value == 0)
    1.73 +    {
    1.74 +      return false;
    1.75 +    }
    1.76 +  const Ns3NscStack *obj = dynamic_cast<const Ns3NscStack *> (object);
    1.77 +  if (obj == 0)
    1.78 +    {
    1.79 +      return false;
    1.80 +    }
    1.81 +  value->Set (obj->Get (m_name));
    1.82 +  return true;
    1.83 +}
    1.84 +
    1.85 +
    1.86 +TypeId
    1.87 +Ns3NscStack::GetInstanceTypeId (void) const
    1.88 +{
    1.89 +  if (m_stack == 0)
    1.90 +    {
    1.91 +      // if we have no stack, we are a normal NscStack without any attributes.
    1.92 +      return GetTypeId ();
    1.93 +    }
    1.94 +  std::string name = "ns3::Ns3NscStack<";
    1.95 +  name += m_stack->get_name ();
    1.96 +  name += ">";
    1.97 +  TypeId tid;
    1.98 +  if (TypeId::LookupByNameFailSafe (name, &tid))
    1.99 +    {
   1.100 +      // if the relevant TypeId has already been registered, no need to do it again.
   1.101 +      return tid;
   1.102 +    }
   1.103 +  else
   1.104 +    {
   1.105 +      // Now, we register a new TypeId for this stack which will look
   1.106 +      // like a subclass of the Ns3NscStack. The class Ns3NscStack is effectively
   1.107 +      // mutating into a subclass of itself from the point of view of the TypeId
   1.108 +      // system _here_
   1.109 +      tid = TypeId (name.c_str ());
   1.110 +      tid.SetParent<Ns3NscStack> ();
   1.111 +      char buf[256];
   1.112 +      for (int i=0; m_stack->sysctl_getnum(i, buf, sizeof(buf)) > 0 ;i++)
   1.113 +        {
   1.114 +          char value[256];
   1.115 +          if (m_stack->sysctl_get (buf, value, sizeof(value)) > 0)
   1.116 +            {
   1.117 +              tid.AddAttribute (buf, "Help text",
   1.118 +                                StringValue (value),
   1.119 +                                Create<NscStackStringAccessor> (buf),
   1.120 +                                MakeStringChecker ());
   1.121 +            }
   1.122 +        }
   1.123 +      return tid;
   1.124 +    }
   1.125 +}
   1.126 +
   1.127 +std::string
   1.128 +Ns3NscStack::Get (std::string name) const
   1.129 +{
   1.130 +  char buf[512];
   1.131 +  if (m_stack->sysctl_get (name.c_str (), buf, sizeof(buf)) <= 0)
   1.132 +    { // name.c_str () is not a valid sysctl name, or internal NSC error (eg. error converting value)
   1.133 +      return NULL;
   1.134 +    }
   1.135 +  return std::string(buf);
   1.136 +}
   1.137 +
   1.138 +void
   1.139 +Ns3NscStack::Set (std::string name, std::string value)
   1.140 +{
   1.141 +  int ret = m_stack->sysctl_set (name.c_str (), value.c_str ());
   1.142 +  if (ret < 0)
   1.143 +    {
   1.144 +      NS_FATAL_ERROR ("setting " << name << " to " << value << "failed (retval " << ret << ")");
   1.145 +    }
   1.146 +}
   1.147 +
   1.148 +TypeId
   1.149 +Ns3NscStack::Ns3NscStack::GetTypeId (void)
   1.150 +{
   1.151 +  static TypeId tid = TypeId ("ns3::Ns3NscStack")
   1.152 +  .SetParent<Object> ()
   1.153 +  ;
   1.154 +  return tid;
   1.155 +}
   1.156 +
   1.157 +} // namespace ns3