bindings: replace pybindgen bindings support with cppyy bindings

This commit is contained in:
Gabriel Ferreira
2022-06-06 06:44:42 -07:00
parent c5d0c8efb4
commit 36df81be90
39 changed files with 750 additions and 3630 deletions

View File

@@ -13,6 +13,7 @@ if(${ENABLE_EXAMPLES})
CACHE INTERNAL "list of example folders"
)
endforeach()
scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR})
endif()
scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -23,11 +23,7 @@
# - DropTail queues
# - Tracing of queues and packet receptions to file "udp-echo.tr"
import ns.applications
import ns.core
import ns.csma
import ns.internet
import ns.network
from ns import ns
def main(argv):
#
@@ -89,7 +85,7 @@ def main(argv):
packetSize = 1024
maxPacketCount = 500
interPacketInterval = ns.core.Seconds(0.01)
client = ns.applications.UdpEchoClientHelper(i.GetAddress (1), port)
client = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(i.GetAddress (1)), port)
client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
@@ -105,6 +101,7 @@ def main(argv):
# Now, do the actual simulation.
#
print ("Run Simulation.")
ns.core.Simulator.Stop(ns.Seconds(10))
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
print ("Done.")

View File

@@ -27,86 +27,77 @@
# router
#
import ns.internet_apps
import ns.core
import ns.csma
import ns.internet
import ns.network
from ns import ns
def main(argv):
cmd = ns.CommandLine()
cmd = ns.core.CommandLine();
cmd.Parse(argv);
cmd.Parse(argv)
# Create nodes
print ("Create nodes")
n0 = ns.network.Node();
r = ns.network.Node();
n1 = ns.network.Node();
print("Create nodes")
net1 = ns.network.NodeContainer();
net1.Add(n0);
net1.Add(r);
net2 = ns.network.NodeContainer();
net2.Add(r);
net2.Add(n1);
all = ns.network.NodeContainer();
all.Add(n0);
all.Add(r);
all.Add(n1);
all = ns.NodeContainer(3)
net1 = ns.NodeContainer()
net1.Add(all.Get(0))
net1.Add(all.Get(1))
net2 = ns.NodeContainer()
net2.Add(all.Get(1))
net2.Add(all.Get(2))
# Create IPv6 Internet Stack
internetv6 = ns.internet.InternetStackHelper();
internetv6.Install(all);
internetv6 = ns.InternetStackHelper()
internetv6.Install(all)
# Create channels
csma = ns.csma.CsmaHelper();
csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)));
csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
d1 = csma.Install(net1);
d2 = csma.Install(net2);
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
d1 = csma.Install(net1)
d2 = csma.Install(net2)
# Create networks and assign IPv6 Addresses
print ("Addressing")
ipv6 = ns.internet.Ipv6AddressHelper();
ipv6.SetBase(ns.network.Ipv6Address("2001:1::"), ns.network.Ipv6Prefix(64));
i1 = ipv6.Assign(d1);
i1.SetForwarding(1, True);
i1.SetDefaultRouteInAllNodes(1);
ipv6.SetBase(ns.network.Ipv6Address("2001:2::"), ns.network.Ipv6Prefix(64));
i2 = ipv6.Assign(d2);
i2.SetForwarding(0, True);
i2.SetDefaultRouteInAllNodes(0);
print("Addressing")
ipv6 = ns.Ipv6AddressHelper()
ipv6.SetBase(ns.Ipv6Address("2001:1::"), ns.Ipv6Prefix(64))
i1 = ipv6.Assign(d1)
i1.SetForwarding(1, True)
i1.SetDefaultRouteInAllNodes(1)
ipv6.SetBase(ns.Ipv6Address("2001:2::"), ns.Ipv6Prefix(64))
i2 = ipv6.Assign(d2)
i2.SetForwarding(0, True)
i2.SetDefaultRouteInAllNodes(0)
# Create a Ping6 application to send ICMPv6 echo request from n0 to n1 via r
print ("Application")
packetSize = 1024;
maxPacketCount = 5;
interPacketInterval = ns.core.Seconds(1.);
ping6 = ns.internet_apps.Ping6Helper();
print("Application")
packetSize = 1024
maxPacketCount = 5
interPacketInterval = ns.Seconds(1.)
ping6 = ns.Ping6Helper()
ping6.SetLocal(i1.GetAddress(0, 1));
ping6.SetRemote(i2.GetAddress(1, 1));
ping6.SetLocal(i1.GetAddress(0, 1))
ping6.SetRemote(i2.GetAddress(1, 1))
ping6.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount));
ping6.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval));
ping6.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize));
ping6.SetAttribute("MaxPackets", ns.UintegerValue(maxPacketCount))
ping6.SetAttribute("Interval", ns.TimeValue(interPacketInterval))
ping6.SetAttribute("PacketSize", ns.UintegerValue(packetSize))
apps = ping6.Install(ns.network.NodeContainer(net1.Get(0)));
apps.Start(ns.core.Seconds(2.0));
apps.Stop(ns.core.Seconds(20.0));
apps = ping6.Install(ns.NodeContainer(net1.Get(0)))
apps.Start(ns.Seconds(2.0))
apps.Stop(ns.Seconds(20.0))
print ("Tracing")
ascii = ns.network.AsciiTraceHelper()
print("Tracing")
ascii = ns.AsciiTraceHelper()
csma.EnableAsciiAll(ascii.CreateFileStream("simple-routing-ping6.tr"))
csma.EnablePcapAll("simple-routing-ping6", True)
# Run Simulation
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
ns.Simulator.Run()
ns.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)
main(sys.argv)

View File

@@ -13,11 +13,7 @@
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# */
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
from ns import ns
# // Default Network Topology
# //
@@ -53,7 +49,8 @@ serverApps = echoServer.Install(nodes.Get(1))
serverApps.Start(ns.core.Seconds(1.0))
serverApps.Stop(ns.core.Seconds(10.0))
echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
address = ns.addressFromIpv4Address(interfaces.GetAddress(1))
echoClient = ns.applications.UdpEchoClientHelper(address, 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))

View File

@@ -16,12 +16,7 @@
# * Ported to Python by Mohit P. Tahiliani
# */
import ns.core
import ns.network
import ns.csma
import ns.internet
import ns.point_to_point
import ns.applications
from ns import ns
import sys
# // Default Network Topology
@@ -31,21 +26,30 @@ import sys
# // point-to-point | | | |
# // ================
# // LAN 10.1.2.0
ns.cppyy.cppdef("""
using namespace ns3;
cmd = ns.core.CommandLine()
cmd.nCsma = 3
cmd.verbose = "True"
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices")
cmd.AddValue("verbose", "Tell echo applications to log if true")
CommandLine& GetCommandLine(std::string filename, int& nCsma, bool& verbose)
{
static CommandLine cmd = CommandLine(filename);
cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma);
cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
return cmd;
}
""")
from ctypes import c_int, c_bool
nCsma = c_int(3)
verbose = c_bool(True)
cmd = ns.cppyy.gbl.GetCommandLine(__file__, nCsma, verbose)
cmd.Parse(sys.argv)
nCsma = int(cmd.nCsma)
verbose = cmd.verbose
nCsma = nCsma.value
verbose = verbose.value
if verbose == "True":
ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
nCsma = 1 if int(nCsma) == 0 else int(nCsma)
nCsma = 1 if nCsma == 0 else nCsma
p2pNodes = ns.network.NodeContainer()
p2pNodes.Create(2)
@@ -83,7 +87,7 @@ serverApps = echoServer.Install(csmaNodes.Get(nCsma))
serverApps.Start(ns.core.Seconds(1.0))
serverApps.Stop(ns.core.Seconds(10.0))
echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
echoClient = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(csmaInterfaces.GetAddress(nCsma)), 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))

View File

@@ -16,14 +16,7 @@
# * Ported to Python by Mohit P. Tahiliani
# */
import ns.core
import ns.network
import ns.point_to_point
import ns.applications
import ns.wifi
import ns.mobility
import ns.csma
import ns.internet
from ns import ns
import sys
# // Default Network Topology
@@ -37,23 +30,21 @@ import sys
# // ================
# // LAN 10.1.2.0
cmd = ns.core.CommandLine()
cmd.nCsma = 3
cmd.verbose = "True"
cmd.nWifi = 3
cmd.tracing = "False"
cmd = ns.getCommandLine(__file__)
nCsma ="3"
verbose = "True"
nWifi = "3"
tracing = "False"
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices")
cmd.AddValue("nWifi", "Number of wifi STA devices")
cmd.AddValue("verbose", "Tell echo applications to log if true")
cmd.AddValue("tracing", "Enable pcap tracing")
cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", ns.null_callback(), nCsma)
cmd.AddValue("nWifi", "Number of wifi STA devices", ns.null_callback(), nWifi)
cmd.AddValue("verbose", "Tell echo applications to log if true", ns.null_callback(), verbose)
cmd.AddValue("tracing", "Enable pcap tracing", ns.null_callback(), tracing)
cmd.Parse(sys.argv)
nCsma = int(cmd.nCsma)
verbose = cmd.verbose
nWifi = int(cmd.nWifi)
tracing = cmd.tracing
nCsma = int(nCsma)
nWifi = int(nWifi)
# The underlying restriction of 18 is due to the grid position
# allocator's configuration; the grid layout will exceed the
@@ -137,7 +128,7 @@ serverApps = echoServer.Install(csmaNodes.Get(nCsma))
serverApps.Start(ns.core.Seconds(1.0))
serverApps.Stop(ns.core.Seconds(10.0))
echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
echoClient = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(csmaInterfaces.GetAddress(nCsma)), 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))

View File

@@ -51,14 +51,7 @@
# +----------------+ +----------------+
#
import ns.applications
import ns.core
import ns.csma
import ns.internet
import ns.mobility
import ns.network
import ns.olsr
import ns.wifi
from ns import ns
# #
# # This function will be used below as a trace sink
@@ -76,11 +69,11 @@ def main(argv):
# simulation parameters.
#
cmd = ns.core.CommandLine()
cmd.backboneNodes = 10
cmd.infraNodes = 2
cmd.lanNodes = 2
cmd.stopTime = 20
cmd = ns.getCommandLine(__file__)
cmd.backboneNodes = "10"
cmd.infraNodes = "2"
cmd.lanNodes = "2"
cmd.stopTime = "20"
#
# Simulation defaults are typically set next, before command line
@@ -95,10 +88,10 @@ def main(argv):
# "--backboneNodes=20"
#
cmd.AddValue("backboneNodes", "number of backbone nodes")
cmd.AddValue("infraNodes", "number of leaf nodes")
cmd.AddValue("lanNodes", "number of LAN nodes")
cmd.AddValue("stopTime", "simulation stop time(seconds)")
cmd.AddValue("backboneNodes", "number of backbone nodes", ns.null_callback(), cmd.backboneNodes)
cmd.AddValue("infraNodes", "number of leaf nodes", ns.null_callback(), cmd.infraNodes)
cmd.AddValue("lanNodes", "number of LAN nodes", ns.null_callback(), cmd.lanNodes)
cmd.AddValue("stopTime", "simulation stop time(seconds)", ns.null_callback(), cmd.stopTime)
#
# The system global variables and the local values added to the argument
@@ -316,19 +309,26 @@ def main(argv):
appSource = ns.network.NodeList.GetNode(backboneNodes)
lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1
appSink = ns.network.NodeList.GetNode(lastNodeIndex)
# Let's fetch the IP address of the last node, which is on Ipv4Interface 1
remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
ns.cppyy.cppdef("""
Ipv4Address getIpv4AddressFromNode(Ptr<Node> node){
return node->GetObject<Ipv4>()->GetAddress(1,0).GetLocal();
}
""")
# Let's fetch the IP address of the last node, which is on Ipv4Interface 1
remoteAddr = ns.cppyy.gbl.getIpv4AddressFromNode(appSink)
socketAddr = ns.network.InetSocketAddress(remoteAddr, port)
genericAddress = ns.addressFromInetSocketAddress(socketAddr)
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory", genericAddress)
apps = onoff.Install(ns.network.NodeContainer(appSource))
apps.Start(ns.core.Seconds(3))
apps.Stop(ns.core.Seconds(stopTime - 1))
# Create a packet sink to receive these packets
sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
apps = sink.Install(ns.network.NodeContainer(appSink))
ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)))
sinkContainer = ns.network.NodeContainer(appSink)
apps = sink.Install(sinkContainer)
apps.Start(ns.core.Seconds(3))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # /
@@ -371,7 +371,6 @@ def main(argv):
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
if __name__ == '__main__':
import sys
main(sys.argv)

View File

@@ -22,13 +22,7 @@
import sys
import ns.applications
import ns.core
import ns.internet
import ns.mobility
import ns.network
import ns.point_to_point
import ns.wifi
from ns import ns
# void
# DevTxTrace (std::string context, Ptr<const Packet> p, Mac48Address address)
@@ -76,23 +70,17 @@ import ns.wifi
# std::cout << " start="<<start<<" duration="<<duration<<std::endl;
# }
def SetPosition(node, position):
mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
mobility.SetPosition(position)
def GetPosition(node):
mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
return mobility.GetPosition()
def AdvancePosition(node):
pos = GetPosition(node);
pos.x += 5.0
if pos.x >= 210.0:
return
SetPosition(node, pos)
ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, node)
ns.cppyy.cppdef("""
using namespace ns3;
void AdvancePosition(Ptr<Node> node){
Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
Vector pos = mob->GetPosition();
pos.x += 5.0;
if (pos.x >= 210.0)
return;
mob->SetPosition(pos);
Simulator::Schedule(Seconds(1.0), AdvancePosition, node);
}""")
def main(argv):
ns.core.CommandLine().Parse(argv)
@@ -133,14 +121,15 @@ def main(argv):
mobility.Install(stas)
mobility.Install(ap)
ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, ap.Get(0))
ns.core.Simulator.Schedule(ns.core.Seconds(1.0), ns.cppyy.gbl.AdvancePosition, ap.Get(0))
socket = ns.network.PacketSocketAddress()
socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
socket.SetProtocol(1)
onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", ns.network.Address(socket))
genericAddress = ns.addressFromPacketSocketAddress(socket)
onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", genericAddress)
onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))