00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ns3/string.h"
00020 #include "nsc-sysctl.h"
00021
00022 #include "sim_interface.h"
00023
00024 namespace ns3 {
00025
00026 class NscStackStringAccessor : public AttributeAccessor
00027 {
00028 public:
00029 NscStackStringAccessor (std::string name) : m_name (name) {}
00030
00031 virtual bool Set (ObjectBase * object, const AttributeValue &val) const;
00032 virtual bool Get (const ObjectBase * object, AttributeValue &val) const;
00033 virtual bool HasGetter (void) const;
00034 virtual bool HasSetter (void) const;
00035 private:
00036 std::string m_name;
00037 };
00038
00039 bool NscStackStringAccessor::HasGetter(void) const
00040 {
00041 return true;
00042 }
00043
00044 bool NscStackStringAccessor::HasSetter(void) const
00045 {
00046 return true;
00047 }
00048
00049
00050 bool NscStackStringAccessor::Set (ObjectBase * object, const AttributeValue & val) const
00051 {
00052 const StringValue *value = dynamic_cast<const StringValue *> (&val);
00053 if (value == 0)
00054 {
00055 return false;
00056 }
00057 Ns3NscStack *obj = dynamic_cast<Ns3NscStack *> (object);
00058 if (obj == 0)
00059 {
00060 return false;
00061 }
00062 obj->Set (m_name, value->Get ());
00063 return true;
00064 }
00065
00066 bool NscStackStringAccessor::Get (const ObjectBase * object, AttributeValue &val) const
00067 {
00068 StringValue *value = dynamic_cast<StringValue *> (&val);
00069 if (value == 0)
00070 {
00071 return false;
00072 }
00073 const Ns3NscStack *obj = dynamic_cast<const Ns3NscStack *> (object);
00074 if (obj == 0)
00075 {
00076 return false;
00077 }
00078 value->Set (obj->Get (m_name));
00079 return true;
00080 }
00081
00082
00083 TypeId
00084 Ns3NscStack::GetInstanceTypeId (void) const
00085 {
00086 if (m_stack == 0)
00087 {
00088
00089 return GetTypeId ();
00090 }
00091 std::string name = "ns3::Ns3NscStack<";
00092 name += m_stack->get_name ();
00093 name += ">";
00094 TypeId tid;
00095 if (TypeId::LookupByNameFailSafe (name, &tid))
00096 {
00097
00098 return tid;
00099 }
00100 else
00101 {
00102
00103
00104
00105
00106 tid = TypeId (name.c_str ());
00107 tid.SetParent<Ns3NscStack> ();
00108 char buf[256];
00109 for (int i=0; m_stack->sysctl_getnum(i, buf, sizeof(buf)) > 0 ;i++)
00110 {
00111 char value[256];
00112 if (m_stack->sysctl_get (buf, value, sizeof(value)) > 0)
00113 {
00114 tid.AddAttribute (buf, "Help text",
00115 StringValue (value),
00116 Create<NscStackStringAccessor> (buf),
00117 MakeStringChecker ());
00118 }
00119 }
00120 return tid;
00121 }
00122 }
00123
00124 std::string
00125 Ns3NscStack::Get (std::string name) const
00126 {
00127 char buf[512];
00128 if (m_stack->sysctl_get (name.c_str (), buf, sizeof(buf)) <= 0)
00129 {
00130 return NULL;
00131 }
00132 return std::string(buf);
00133 }
00134
00135 void
00136 Ns3NscStack::Set (std::string name, std::string value)
00137 {
00138 int ret = m_stack->sysctl_set (name.c_str (), value.c_str ());
00139 if (ret < 0)
00140 {
00141 NS_FATAL_ERROR ("setting " << name << " to " << value << "failed (retval " << ret << ")");
00142 }
00143 }
00144
00145 TypeId
00146 Ns3NscStack::Ns3NscStack::GetTypeId (void)
00147 {
00148 static TypeId tid = TypeId ("ns3::Ns3NscStack")
00149 .SetParent<Object> ()
00150 ;
00151 return tid;
00152 }
00153
00154 }