NS3(Network Simulator)

NS3 GlobalValue类API以及使用说明

2017-06-25  本文已影响177人  shawn168

GlobalValue类

位置:

core/model/global-value.cc

功能:

可从四个地方获取值:

    1  定义时设定的初始值
    2  从NS_GLOBAL_VALUE环境变量
    3 从命令行
    4 可明确调用SetValue()或者 Bind()函数

Instances of this class are expected to be allocated as static global variables and should be used to store configurable global state.

该类的实例应该被分配为一个静态的全局变量,并且应该被用于存储配置的全局状态。

// source.cc:
static GlobalValue g_myGlobal =
  GlobalValue ("myGlobal", "My global value for ...",
               IntegerValue (12),
               MakeIntegerChecker ());

GlobalValue::GlobalValue (std::string name, std::string help,
                          const AttributeValue &initialValue,
                          Ptr<const AttributeChecker> checker)

GlobalValue 定义包含四个参数:

第一个参数:变量名

第二个参数:该变量的帮助信息,一般说明定义该变量的意义

第三个参数:该变量的初始值,初始值不能为空,否则出错。

第四个参数:该变量的值的校验。该值不能为空,否则出错。这个参数是为了确保设置的值是合法的。AttributeChecker有许多子类。

函数:

void ns3::GlobalValue::Bind     (   std::string     name,
        const AttributeValue &      value 
    )   

bool ns3::GlobalValue::BindFailSafe     (   std::string     name,
        const AttributeValue &      value 
    )   

这两个函数都是设定对应的name的value值。
区别在于第二个函数返回bool值指示是否设定成功。第一个函数如果设定失败,直接崩溃。

void ns3::GlobalValue::GetValueByName   (   std::string     name,
        AttributeValue &    value 
    )   

bool ns3::GlobalValue::GetValueByNameFailSafe   (   std::string     name,
        AttributeValue &    value 
    )   

两个函数都是根据name获取对应的value值。
区别在于,第一个函数失败情况下,会触发NS_FATAL_ERROR
第二个函数返回一个bool值。指示成功与否。

使用

第一步:定义

使用全局变量,首先定义全局变量:

static ns3::GlobalValue g_rngSeed ("RngSeed", 
                                   "The global seed of all rng streams",
                                   ns3::IntegerValue(1),
                                   ns3::MakeIntegerChecker<uint32_t> ());

static ns3::GlobalValue g_rngRun ("RngRun", 
                                  "The substream index used for all streams",
                                  ns3::IntegerValue (1),
                                  ns3::MakeIntegerChecker<int64_t> ());

定义了两个全局变量:RngSeed和RngRun。

上面的代码位置/core/model/rng-seed-manager.cc

第二步设置变量值:

第一种方法:

Config::SetGlobal ("RngSeed", IntegerValue(seed));
}
Config::SetGlobal ("RngRun", IntegerValue (run));

第二种方法:
GlobalValue::Bind ("RngSeed", value);
GlobalValue::BindFailSafe ("RngRun", value);

第三种方法:
设置环境变量:
NS_GLOBAL_VALUE='RngSeed=Value;RngRun=OtherValue;'

第四种方法:
命令行方式
./waf --run="source file name --RngSeed=value --RngRun=OtherValue"

第三步:获取变量值

IntegerValue value
bool GlobalValue::GetValueByNameFailSafe ("RngSeed", value);
void GlobalValue::GetValueByName ("RngRun", value);

获取全部全局变量:

for (GlobalValue::Iterator i = GlobalValue::Begin (); i != GlobalValue::End (); ++i)
{
      //获取变量名
      std::stringstream ss;
      ss << "    --" << (*i)->GetName () << "=[";
      
      Ptr<const AttributeChecker> checker = (*i)->GetChecker ();
      
      //获取变量值
      StringValue v;
      (*i)->GetValue (v);
      ss << v.Get () << "]" << std::endl;

      //获取变量帮助信息
      ss << "        " << (*i)->GetHelp () << std::endl;
      globals.push_back (ss.str ());
}
上一篇下一篇

猜你喜欢

热点阅读