在学习NS-3 仿真过程中遇到问题:如何动态修改节点位置?如何设置节点颜色?
首先想到的是在visualize 里找相关的API,但未找到。之后搜索了整个NS-3 3.35工程,只发现了与NetAnim 相关的Color 操作,没发现与Visualize 相关的Color 操作。
NetAnim 似乎有设置节点颜色方法,如下:
AnimationInterface::SetNodeColor();
如果在Visualize 上找不到设置节点颜色方法,考虑转到NetAnim 上做。
找到相关例子程序:
生成XML文件并用NetAnim图形化的显示网络拓扑 - 灰信网(软件开发博客聚合)
https://www.freesion.com/article/5154439032/
调试该例子发现,如果在运行程序时加上--vis 则在点击visulize 仿真界面中的开始仿真Simulate(F3)按钮之前,first.xml 文件里是没有内容的,只有仿真开始才会往该文件里插入内容。在Simulator 仿真期间如果对节点进行移动等操作,会写入到first.xml 中,进而影响NetAnim 的仿真效果。
此外,anim.SetConstantPosition(nodes.Get(0), 10, 10); 该语句会对Simulator 产生影响,设置后在vis 可视化界面中的节点位置发生改变。
anim.UpdateNodeColor(nodes.Get(0), 255, 255, 22);该语句会对Simulator 不会产生影响,设置后在vis 可视化界面中节点颜色未发生改变,但在NetAnim 界面中颜色改变了。
如果不加--vis ,则会自动执行仿真,同时first.xml 里将会写入内容。
修改后的ns-3 3.35 源码包中的
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
/**
* Jerry:调试本源码的主要目的是尝试以下连接里关于NetAnim 相关的内容:
* 生成XML文件并用NetAnim图形化的显示网络拓扑 - 灰信网(软件开发博客聚合)
* https://www.freesion.com/article/5154439032/
*/
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("FirstScriptExample");
int main(int argc, char *argv[]) {
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
Time::SetResolution(Time::NS);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
AnimationInterface anim("first20220124.xml");
anim.UpdateNodeColor(nodes.Get(0), 255, 255, 22);
anim.SetConstantPosition(nodes.Get(0), 10, 10);
anim.SetConstantPosition(nodes.Get(1), 50, 50);
anim.SetConstantPosition(nodes.Get(0), 0, 0);
Simulator::Run();
Simulator::Destroy();
return 0;
}
example/tutorial/first.cc 源码如下: