how to get node id

3,032 views
Skip to first unread message

newns3u...@gmail.com

unread,
Jan 29, 2015, 2:56:33 PM1/29/15
to ns-3-...@googlegroups.com
Hi, 

I'm new to ns-3 and even worse, I'm new to C/C++. I've been reading lot trying to ramp up on ns-3, but it will take a while for me to really get familiar with ns-3 programming. Meanwhile I have a question and I guess it's very simple for experts in this group, so could you please help?

How to get node ID from YansWifiPhy?I added one line below in YansWifiPhy:EndRecive in order to get the node ID. But I keep getting errors. I'm assuming "this" is the pointer to the YanWifiPhy object which does have a member called m_device. What's wrong with this line of code? Thank you very much!

-Kelly

YansWifiPhy::EndReceive (Ptr<Packet> packet, Ptr<InterferenceHelper::Event> event)

{

....

 

 nodeID= this->m_device->GetObject<ns3::Node> ()->GetId ();


...

}


The error messages I got are below:


../src/wifi/model/yans-wifi-phy.cc:868:52: error: member access into incomplete type 'ns3::Node'

int nodeID= this->m_device->GetObject<ns3::Node> ()->GetId ();

                                                   ^

./ns3/net-device.h:35:7: note: forward declaration of 'ns3::Node'

class Node;

      ^

....

../src/wifi/model/yans-wifi-phy.cc:868:30: note: in instantiation of function template specialization 'ns3::Object::GetObject<ns3::Node>' requested here

int nodeID= this->m_device-->GetObject<ns3::Node> ()->GetId ();

                             ^

./ns3/net-device.h:35:7: note: forward declaration of 'ns3::Node'

class Node;

   ...




Tommaso Pecorella

unread,
Jan 30, 2015, 1:32:17 AM1/30/15
to ns-3-...@googlegroups.com
#include "ns3/node.h"

T.

newns3u...@gmail.com

unread,
Jan 30, 2015, 7:17:05 PM1/30/15
to ns-3-...@googlegroups.com
Thank you very much!

Samson Arun Raj

unread,
Dec 31, 2017, 11:13:11 AM12/31/17
to ns-3-users
Hi. I am new to this field of NS-3. I have the same question, how to retrive the node id's and how to display them ? 

I want to display 20 nodes with its unique id. I have created 20 nodes using for loop with the following command.

NodeContainer c;
c.Create (20);
int j;
for(j = 0; j < 20; j++)
{   
  m.Install (c.Get(j));
}
std::cout << "Node ID: %d" << ??? <<std::endl;
The above code created 20 nodes and seen in the simulation tool Netanim. Now I need to display each of these nodes with unique id in the terminal.
Can anyone tell me how to assign the node id?

pdbarnes

unread,
Dec 31, 2017, 3:37:35 PM12/31/17
to ns-3-users
You can’t assign the NodeId; it’s assigned automatically. You can look it up, however. The Id is returned by Node::GetId()
https://www.nsnam.org/doxygen/classns3_1_1_node.html#aaf49b64a843565ce3812326313b370ac

So from your NodeContainer ‘c’:

c.GetNode (j).GetId ();

(Also note that C-style output formatting “...%d” doesn’t work with C++ IO streams. Just stream the variable directly:
std::cout << “Node Id: “ << c.GetNode (j).GetId () << are::endl;

Peter

Samson Arun Raj

unread,
Jan 1, 2018, 6:30:42 AM1/1/18
to ns-3-users
I am sorry, I am getting errors. I will show my actual code to you. I have created a text file value.txt to store the details of the simulation. 

In that file, I can able to get the time, postion and velocity for xyz cordinates but I cant able to retrive the node ID. 

I also tried your method, but resulting factor leading to error. It is saying that the variable "c" cannot be declared outside the int main() function. Please help me in this sir. 

#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <string>
#include <cstdio>

#include "ns3/simulator.h"

#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/netanim-module.h"

#include "ns3/gauss-markov-mobility-model.h"
#include "ns3/position-allocator.h"

#include "ns3/network-module.h"

#include "ns3/node.h"

using namespace ns3;

static void 
PositionChange (std::string foo, Ptr<const MobilityModel> m)

{
FILE *fp;
double a,b,c,d,e,f;

fp=freopen("value.txt","a",stdout);
Vector pos = m->GetPosition();
Vector vel = m->GetVelocity();
a = pos.x;
b = pos.y;
c = pos.z;
d = vel.x;
e = vel.y;
f = vel.z;

std::cout << "Time: "<< Simulator::Now ().GetSeconds() << '\t';

fprintf(fp,"Node: \t POS-X: %lf \t POS-Y: %lf \t POS-Z: %lf \t VEL-X: %lf \t VEL-Y: %lf \t VEL-Z: %lf \n",a,b,c,d,e,f);



int main ()

{

remove("value.txt");

//created 20 nodes 

NodeContainer n;
n.Create (20);

// setting mobility

Config::SetDefault ("ns3::GaussMarkovMobilityModel::Bounds", StringValue ("0|200|0|200|0|200"));
Config::SetDefault ("ns3::GaussMarkovMobilityModel::TimeStep", StringValue ("1.0"));

MobilityHelper m;

m.SetPositionAllocator ("ns3::RandomBoxPositionAllocator",
"X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
"Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"),
"Z", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=200.0]"));

//// for creating mobile ground nodes

m.SetMobilityModel ("ns3::GaussMarkovMobilityModel",
"Bounds", StringValue ("0|200|0|200|0|200"),
"TimeStep", StringValue ("1.0"));

int i;
for(i=4;i<20;i++)
{
m.Install (n.Get(i));
}


///// for creating static nodes

m.SetMobilityModel ("ns3::ConstantPositionMobilityModel");

int j;
for(j=0;j<4;j++)
{
m.Install (n.Get(j));
}

// calling the above PostionChange function
Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange", MakeCallback (&PositionChange));


Simulator::Stop (Seconds (1000.0));

        AnimationInterface anim("7.xml");

anim.SetConstantPosition(n.Get(0),100,50,200);
anim.SetConstantPosition(n.Get(1),150,100,200);
anim.SetConstantPosition(n.Get(2),100,150,200);
anim.SetConstantPosition(n.Get(3),50,100,200);
Simulator::Run ();

Simulator::Destroy ();
return 0;

}

Tommaso Pecorella

unread,
Jan 3, 2018, 6:29:34 PM1/3/18
to ns-3-users
This script compiles just fine.

T.
Reply all
Reply to author
Forward
0 new messages