处理复合属性

Ets_Obj 包中所有的属性 "get"、"set" 和 "exist" 函数都支持使用层次化属性名。这提供了一种与嵌套复合属性交互的简单方式。例如,可以调用:

Ets_Obj_Attr_Get (router_obj_hndl,
	"IP Router Parameters [0].Interface Information [3].Address", &address_str);

此示例从 "Interface Information" 属性的第 3 行获取 "Address" 字符串属性,而 "Interface Information" 是 "IP Router Parameters" 的嵌套复合属性。

作为便利写法,如果复合属性被约束为恰好只有一行,可以省略 "[0]"。例如:

"IP Router Parameters.Interface Information [3].Address"

图 9 中的示例演示了如何使用层次化名称设置复合属性的值。示例说明了两个要点:

  • 获取 count 属性以确定复合属性的行数。
  • 使用 sprintf 创建带有正确索引的属性名字符串。

图 9:设置复合属性值——使用层次化名称

/** Loop through all interfaces, setting the cost of all OSPF interfaces to 1.  **/

/* Get the number of rows in the OSPF Parameters' Interface Information */
/* compound attribute.                                                  */

num_intfs = 0;
Ets_Obj_Attr_Get (router_obj_hndl,
	"OSPF Parameters.Interface Information.count",
	&num_intfs);

for (intf_index = 0; intf_index < num_intfs; intf_index++)
	{
	/* prepare the cost attribute name. */
	sprintf (attr_name_str,
		"OSPF Parameters.Interface Information [%d].Interface Cost",
		intf_index);

	/* set this cost to 1. */
	Ets_Obj_Attr_Set (router_obj_hndl, attr_name_str, 1);
	}

在大多数情况下,层次化名称是访问复合属性子属性最有效的方式。然而,如果要在同一复合属性上反复设置许多子属性,下面的替代方法可能更高效。

  1. 使用 Ets_Obj_Attr_Get() 获取顶层属性的句柄。
  2. 使用步骤 1 获取的句柄调用 Ets_Topo_Child_Get(),获得子属性列表。再通过额外的 Ets_Topo_Child_Get() 调用可以进一步"深入"复合属性。

下面的代码片段说明了这一过程。

图 10:设置复合属性值——替代方法

// get handle for top-level "PBX Parameters" attribute, copy into pbx_parameters_list
Ets_Obj_Attr_Get (pbx_obj_hndl, "PBX Parameters", &pbx_parameters);

// Get list of "PBX Parameters" attributes
pbx_parameters_list = Ets_Topo_Child_Get (pbx_parameters, EtsC_Obj_Mtype_All, 0);
if (Ets_Obj_Attr_Exists (pbx_parameters, "Call Generation")
	{
	// Get list of "PBX Parameters.Call Generation" attributes
	call_parameters = Ets_Topo_Child_Get (pbx_parameters_list,
		EtsC_Obj_Mtype_All, 0);
	....
	}