--- a/src/contrib/mvc-controller/attr.py Fri Mar 12 11:51:24 2010 -0800
+++ b/src/contrib/mvc-controller/attr.py Fri Mar 12 15:34:22 2010 -0800
@@ -12,106 +12,105 @@
import ns3
-class Entry:
- """A local representation of an ns-3 attribute."""
- def __init__ (self, name, help, type, initial):
- self.__name = name
- self.__help = help
- self.__type = type
- self.__initial = initial
-
- def GetName (self):
- """Return the name of the ns-3 attribute described by this Entry."""
- return self.__name
-
- def GetHelp (self):
- """Return the help string of the ns-3 attribute described by this
- Entry.
- """
- return self.__help
-
- def GetType (self):
- """Return the underlying type of the ns-3 attribute described by this
- Entry.
- """
- return self.__type
-
- def GetInitial (self):
- """Return the initial value of the ns-3 attribute described by this
- Entry.
- """
- return self.__initial
-
class Attributes:
"""A local representation of a collection of ns-3 attributes. Typically the
- attributes of an ns3 Object
+ attributes of an ns3 Object.
"""
+
def __init__ (self, object):
"""Initialize the local collection of attributes to echo the attributes
of the underlying ns-3 object.
"""
self.__object = object
- self.__attributeEntries = []
- self.__num = object.GetTypeId ().GetAttributeN ()
- for i in range (self.__num):
- name = object.GetTypeId ().GetAttributeName (i)
- help = object.GetTypeId ().GetAttributeHelp (i)
- initial = object.GetTypeId ().GetAttributeInitialValue (i)
- type = object.GetTypeId ().GetAttributeChecker (i).GetValueTypeName ()
- self.__attributeEntries.extend ([Entry (name, help, type, initial)])
def GetNumberAttributes (self):
"""Return the number of attributes managed in this collection."""
- return self.__num
+ return self.__object.GetTypeId ().GetAttributeN ()
def SetValue (self, i, value):
"""Set the current value of the ns-3 attribute described by the ith
- entry in the collection.
+ entry in the collection. If the attribute is not settable, this
+ call has no effect.
"""
#
+ # Do some preparatory checking to try and avoid inscrutable fatal
+ # error deep down in the guts of ns-3. If we can't set the value,
+ # we'll do nothing and return False
+ #
+ info = ns3.TypeId.AttributeInfo ()
+ self.__object.GetTypeId ().LookupAttributeByName (self.GetName (i), info)
+ if info.flags & ns3.TypeId.ATTR_SET == 0:
+ return False
+
+ accessor = self.__object.GetTypeId ().GetAttributeAccessor (i)
+ if not accessor.HasSetter():
+ return False
+
+ #
# We are always going to treat the value passed in as a StringValue
# representation of the underlying Attribute value. We do some simple
- # checking to try and avoid inscrutable fatal errors down in ns-3 if
- # the value cannot be converted.
+ # type checking to try and avoid inscrutable fatal errors down in ns-3
+ # if the value cannot be converted.
#
valstr = str(value)
stringValue = ns3.StringValue ()
rc = stringValue.DeserializeFromString (valstr, self.__object.GetTypeId ().GetAttributeChecker (i))
if rc == False:
- raise ValueError
- self.__object.SetAttribute (self.__attributeEntries[i].GetName (), stringValue)
+ return False
+
+ #
+ # SetAttribute will just crash the system if it can't do it.
+ #
+ self.__object.SetAttribute (self.GetName (i), stringValue)
+ return True
def GetValue (self, i):
"""Return the current value of the ns-3 attribute described by the ith
entry in the collection.
"""
#
+ # Do some preparatory checking to try and avoid inscrutable fatal
+ # error deep down in the guts of ns-3. If we can't return the
+ # actual value, we'll return "Not Gettable".
+ #
+ info = ns3.TypeId.AttributeInfo ()
+ self.__object.GetTypeId ().LookupAttributeByName (self.GetName (i), info)
+ if info.flags & ns3.TypeId.ATTR_GET == 0:
+ return str(None)
+
+ accessor = self.__object.GetTypeId ().GetAttributeAccessor (i)
+ if not accessor.HasGetter():
+ return str(None)
+
+ #
# We are always going to treat the returned value as a StringValue.
#
stringValue = ns3.StringValue()
- self.__object.GetAttribute (self.__attributeEntries[i].GetName (), stringValue)
+ self.__object.GetAttribute (self.GetName (i), stringValue)
return stringValue.Get()
def GetName (self, i):
"""Return the name of the ns-3 attribute described by the ith entry
in the collection.
"""
- return self.__attributeEntries[i].GetName ()
+ return self.__object.GetTypeId ().GetAttributeName (i)
def GetHelp (self, i):
"""Return the help string of the ns-3 attribute described by the ith
entry in the collection.
"""
- return self.__attributeEntries[i].GetHelp ()
+ return self.__object.GetTypeId ().GetAttributeHelp (i)
def GetType (self, i):
- """Return the type of the ns-3 AttributeValue subclass typename of the
- attribute described by the ith entry in the collection.
+ """Return the type of the ns-3 AttributeValue subclass of the attribute
+ described by the ith entry in the collection.
"""
- return self.__attributeEntries[i].GetType ()
+ return self.__object.GetTypeId ().GetAttributeChecker (i).GetValueTypeName ()
- def GetInitial (self, i):
- """Return the initial value of the ns-3 attribute described by the ith
- entry in the collection.
+ def GetTypeInformation (self, i):
+ """Return information about the type of the ns-3 AttributeValue subclass
+ of the attribute described by the ith entry in the collection.
"""
- return self.__attributeEntries[i].GetInitial ().Get ()
+ if self.__object.GetTypeId ().GetAttributeChecker (i).HasUnderlyingTypeInformation ():
+ return self.__object.GetTypeId ().GetAttributeChecker (i).GetUnderlyingTypeInformation ()
+ return str(None)
--- a/src/contrib/mvc-controller/obj.py Fri Mar 12 11:51:24 2010 -0800
+++ b/src/contrib/mvc-controller/obj.py Fri Mar 12 15:34:22 2010 -0800
@@ -65,8 +65,8 @@
"""
return self.__attributes.GetType (i)
- def GetAttributeInitial (self, i):
- """Return the initial value of the ns-3 attribute described by the ith
- entry in this Object.
+ def GetAttributeTypeInformation (self, i):
+ """Return the underlying type information of the ns-3 attribute described
+ by the ith entry in this Object.
"""
- return self.__attributes.GetInitial (i)
+ return self.__attributes.GetTypeInformation (i)
--- a/src/contrib/mvc-controller/testit.py Fri Mar 12 11:51:24 2010 -0800
+++ b/src/contrib/mvc-controller/testit.py Fri Mar 12 15:34:22 2010 -0800
@@ -17,22 +17,48 @@
def main (argv):
n = Node()
- nd = CsmaNetDevice ()
+ nd0 = CsmaNetDevice ()
+ nd1 = CsmaNetDevice ()
ch = CsmaChannel ()
- n.AddDevice ("eth0", nd)
+ n.AddDevice ("eth0", nd0)
+ n.AddDevice ("eth1", nd1)
print "node attributes"
for i in range (n.GetNumberAttributes ()):
- print n.GetAttributeName (i), ":", n.GetAttributeValue (i)
+ print " name", n.GetAttributeName (i)
+ print " value", n.GetAttributeValue (i)
+ print " help", n.GetAttributeHelp (i)
+ print " type", n.GetAttributeType (i)
+ print " type information", n.GetAttributeTypeInformation (i)
+ print " ----------"
- print "device attributes"
- for i in range (nd.GetNumberAttributes ()):
- print nd.GetAttributeName (i), ":", nd.GetAttributeValue (i)
+ print "device 0 attributes"
+ for i in range (nd0.GetNumberAttributes ()):
+ print " name", nd0.GetAttributeName (i)
+ print " value", nd0.GetAttributeValue (i)
+ print " help", nd0.GetAttributeHelp (i)
+ print " type", nd0.GetAttributeType (i)
+ print " type information", nd0.GetAttributeTypeInformation (i)
+ print " ----------"
+
+ print "device 1 attributes"
+ for i in range (nd1.GetNumberAttributes ()):
+ print " name", nd1.GetAttributeName (i)
+ print " value", nd1.GetAttributeValue (i)
+ print " help", nd1.GetAttributeHelp (i)
+ print " type", nd1.GetAttributeType (i)
+ print " type information", nd1.GetAttributeTypeInformation (i)
+ print " ----------"
print "channel attributes"
- for i in range (nd.GetNumberAttributes ()):
- print ch.GetAttributeName (i), ":", ch.GetAttributeValue (i)
+ for i in range (ch.GetNumberAttributes ()):
+ print " name", ch.GetAttributeName (i)
+ print " value", ch.GetAttributeValue (i)
+ print " help", ch.GetAttributeHelp (i)
+ print " type", ch.GetAttributeType (i)
+ print " type information", ch.GetAttributeTypeInformation (i)
+ print " ----------"
if __name__ == '__main__':
sys.exit(main(sys.argv))