Void function creation

16 views
Skip to first unread message

Ma CAs

unread,
May 7, 2020, 2:38:00 PM5/7/20
to ns-3-users
Hello,

I am trying to calculate throughput and make void function so I could run it as Simulator::Schedule: 

void LTE (double starttime, double duration)
{

        Ptr<PacketSink> sink = serverApps.Get (0)->GetObject<PacketSink> ();
        double totalMbytes = sink->GetTotalRx ()/1000000;
        double lteThroughput = totalMbytes * 8.0 / (duration - starttime);
        std::cout <<"Total Mbytes: "<< totalMbytes <<"; Throughput " <<lteThroughput << " Mbps \t" << std::endl;
}

But I get problem with serverApps as it is not declarated. How could i do that in this function?

../scratch/lena-simple-epc.cc: In function ‘void LTE(double, double)’:
../scratch/lena-simple-epc.cc:43:32: error: ‘serverApps’ was not declared in this scope
         Ptr<PacketSink> sink = serverApps.Get (0)->GetObject<PacketSink> ();
                                ^~~~~~~~~~
../scratch/lena-simple-epc.cc:43:32: note: suggested alternative: ‘strverscmp’
         Ptr<PacketSink> sink = serverApps.Get (0)->GetObject<PacketSink> ();
                                ^~~~~~~~~~
                                strverscmp
../scratch/lena-simple-epc.cc:43:72: error: expected primary-expression before ‘>’ token
         Ptr<PacketSink> sink = serverApps.Get (0)->GetObject<PacketSink> ();
                                                                        ^
../scratch/lena-simple-epc.cc:43:75: error: expected primary-expression before ‘)’ token
         Ptr<PacketSink> sink = serverApps.Get (0)->GetObject<PacketSink> ();
                                                                           ^

Adil Alsuhaim

unread,
May 10, 2020, 11:14:44 AM5/10/20
to ns-3-users
serverApps is created in another function (I am guessing in main), so it's outside the scope of this void function. 

In ns-3, all nodes are contained in a global list of nodes called NodeList. A Node can contain a list of applications (also a list of devices). PacketSink is an application (it's a subclass of ns3::Application). If you want to access all the PacketSink applications in all the nodes, you can do this:

for (uint32_t i=0 ; i < NodeList::GetNNodes(); i++)
{
 
Ptr<Node> nodeI = NodeList::GetNode(i);
 
for (uint32_t j=0 ; j < nodeI->GetNApplications(); j++)
 
{
   
Ptr<Application> appI = nodeI->GetApplication (j);
   
if ( appI->GetInstanceTypeId () == PacketSink::GetTypeId ()) // checking before performing dynamic cast.
   
{
     
Ptr<PacketSink> sink = DynamicCast <PacketSink> (appI);
     
//do what you need to do
   
}
 
}
}

If you're sure that node 0 contains only one application, the packet sink, then you can just do
Ptr<PacketSink> sink = DynamicCast<PacketSink> (NodeList::GetNode(0)->GetApplication(0));

Cheers,
Adil


Reply all
Reply to author
Forward
0 new messages