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

@@ -19,13 +19,8 @@
# Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
import unittest
from ns.core import Simulator, Seconds, Config, int64x64_t
import ns.core
import ns.network
import ns.internet
import ns.mobility
import ns.csma
import ns.applications
from ns import ns
import sys
UINT32_MAX = 0xFFFFFFFF
@@ -46,19 +41,27 @@ class TestSimulator(unittest.TestCase):
@param self this object
@return none
"""
def callback(args):
def callback(args: ns.cppyy.gbl.std.vector) -> None:
"""! Callback function
@param args arguments
return none
"""
self._args_received = args
self._cb_time = Simulator.Now()
Simulator.Destroy()
import copy
self._args_received = list(map(lambda x: x.decode("utf-8"), args))
self._cb_time = ns.Simulator.Now()
ns.Simulator.Destroy()
self._args_received = None
self._cb_time = None
Simulator.ScheduleNow(callback, "args")
Simulator.Run()
self.assertEqual(self._args_received, "args")
ns.cppyy.cppdef("""
EventImpl* pythonMakeEvent(void (*f)(std::vector<std::string>), std::vector<std::string> l)
{
return MakeEvent(f, l);
}
""")
event = ns.cppyy.gbl.pythonMakeEvent(callback, sys.argv)
ns.Simulator.ScheduleNow(event)
ns.Simulator.Run()
self.assertListEqual(self._args_received, sys.argv)
self.assertEqual(self._cb_time.GetSeconds(), 0.0)
def testSchedule(self):
@@ -66,19 +69,26 @@ class TestSimulator(unittest.TestCase):
@param self this object
@return none
"""
def callback(args):
def callback(args: ns.cppyy.gbl.std.vector):
"""! Callback function
@param args arguments
@return none
"""
self._args_received = args
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = list(map(lambda x: x.decode("utf-8"), args))
self._cb_time = ns.Simulator.Now()
ns.Simulator.Destroy()
self._args_received = None
self._cb_time = None
Simulator.Schedule(Seconds(123), callback, "args")
Simulator.Run()
self.assertEqual(self._args_received, "args")
ns.cppyy.cppdef("""
EventImpl* pythonMakeEvent2(void (*f)(std::vector<std::string>), std::vector<std::string> l)
{
return MakeEvent(f, l);
}
""")
event = ns.cppyy.gbl.pythonMakeEvent2(callback, sys.argv)
ns.Simulator.Schedule(ns.Seconds(123), event)
ns.Simulator.Run()
self.assertListEqual(self._args_received, sys.argv)
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
def testScheduleDestroy(self):
@@ -86,22 +96,29 @@ class TestSimulator(unittest.TestCase):
@param self this object
@return none
"""
def callback(args):
def callback(args: ns.cppyy.gbl.std.vector):
"""! Callback function
@param args
@return none
"""
self._args_received = args
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = list(map(lambda x: x.decode("utf-8"), args))
self._cb_time = ns.Simulator.Now()
ns.Simulator.Destroy()
self._args_received = None
self._cb_time = None
def null(): pass
Simulator.Schedule(Seconds(123), null)
Simulator.ScheduleDestroy(callback, "args")
Simulator.Run()
Simulator.Destroy()
self.assertEqual(self._args_received, "args")
ns.cppyy.cppdef("void null(){ return; }")
ns.Simulator.Schedule(ns.Seconds(123), ns.cppyy.gbl.null)
ns.cppyy.cppdef("""
EventImpl* pythonMakeEvent3(void (*f)(std::vector<std::string>), std::vector<std::string> l)
{
return MakeEvent(f, l);
}
""")
event = ns.cppyy.gbl.pythonMakeEvent3(callback, sys.argv)
ns.Simulator.ScheduleDestroy(event)
ns.Simulator.Run()
ns.Simulator.Destroy()
self.assertListEqual(self._args_received, sys.argv)
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
def testScheduleWithContext(self):
@@ -109,23 +126,30 @@ class TestSimulator(unittest.TestCase):
@param self this object
@return none
"""
def callback(context, args):
def callback(context, args: ns.cppyy.gbl.std.vector):
"""! Callback
@param context the cntet
@param args the arguments
@return none
"""
self._context_received = context
self._args_received = args
self._cb_time = Simulator.Now()
Simulator.Destroy()
self._args_received = list(map(lambda x: x.decode("utf-8"), args))
self._cb_time = ns.Simulator.Now()
ns.Simulator.Destroy()
self._args_received = None
self._cb_time = None
self._context_received = None
Simulator.ScheduleWithContext(54321, Seconds(123), callback, "args")
Simulator.Run()
ns.cppyy.cppdef("""
EventImpl* pythonMakeEvent4(void (*f)(uint32_t, std::vector<std::string>), uint32_t context, std::vector<std::string> l)
{
return MakeEvent(f, context, l);
}
""")
event = ns.cppyy.gbl.pythonMakeEvent4(callback, 54321, sys.argv)
ns.Simulator.ScheduleWithContext(54321, ns.Seconds(123), event)
ns.Simulator.Run()
self.assertEqual(self._context_received, 54321)
self.assertEqual(self._args_received, "args")
self.assertListEqual(self._args_received, sys.argv)
self.assertEqual(self._cb_time.GetSeconds(), 123.0)
def testTimeComparison(self):
@@ -133,29 +157,29 @@ class TestSimulator(unittest.TestCase):
@param self this object
@return none
"""
self.assertTrue(Seconds(123) == Seconds(123))
self.assertTrue(Seconds(123) >= Seconds(123))
self.assertTrue(Seconds(123) <= Seconds(123))
self.assertTrue(Seconds(124) > Seconds(123))
self.assertTrue(Seconds(123) < Seconds(124))
self.assertTrue(ns.Seconds(123) == ns.Seconds(123))
self.assertTrue(ns.Seconds(123) >= ns.Seconds(123))
self.assertTrue(ns.Seconds(123) <= ns.Seconds(123))
self.assertTrue(ns.Seconds(124) > ns.Seconds(123))
self.assertTrue(ns.Seconds(123) < ns.Seconds(124))
def testTimeNumericOperations(self):
"""! Test numeric operations
@param self this object
@return none
"""
self.assertEqual(Seconds(10) + Seconds(5), Seconds(15))
self.assertEqual(Seconds(10) - Seconds(5), Seconds(5))
self.assertEqual(ns.Seconds(10) + ns.Seconds(5), ns.Seconds(15))
self.assertEqual(ns.Seconds(10) - ns.Seconds(5), ns.Seconds(5))
v1 = int64x64_t(5.0)*int64x64_t(10)
self.assertEqual(v1, int64x64_t(50))
v1 = ns.int64x64_t(5.0)*ns.int64x64_t(10)
self.assertEqual(v1, ns.int64x64_t(50))
def testConfig(self):
"""! Test configuration
@param self this object
@return none
"""
Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
ns.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
# hm.. no Config.Get?
def testSocket(self):
@@ -163,27 +187,30 @@ class TestSimulator(unittest.TestCase):
@param self
@return none
"""
node = ns.network.Node()
internet = ns.internet.InternetStackHelper()
nc = ns.NodeContainer(1)
node = nc.Get(0)
internet = ns.CreateObject("InternetStackHelper")
internet.Install(node)
self._received_packet = None
def rx_callback(socket):
"""! Receive Callback
@param socket the socket to receive
@return none
"""
assert self._received_packet is None
def python_rx_callback(socket) -> None:
self._received_packet = socket.Recv(maxSize=UINT32_MAX, flags=0)
ns.cppyy.cppdef("""
Callback<void,ns3::Ptr<ns3::Socket> > make_rx_callback(void(*func)(Ptr<Socket>))
{
return MakeCallback(func);
}
""")
sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
sink.SetRecvCallback(rx_callback)
sink.Bind(ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80)))
sink.SetRecvCallback(ns.cppyy.gbl.make_rx_callback(python_rx_callback))
source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80))
source.SendTo(ns.network.Packet(19), 0, ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80)))
Simulator.Run()
ns.Simulator.Run()
self.assertTrue(self._received_packet is not None)
self.assertEqual(self._received_packet.GetSize(), 19)
@@ -194,7 +221,7 @@ class TestSimulator(unittest.TestCase):
@return none
"""
# Templated class DropTailQueue<Packet> in C++
queue = ns.network.DropTailQueue__Ns3Packet()
queue = ns.CreateObject("DropTailQueue<Packet>")
queueSizeValue = ns.network.QueueSizeValue (ns.network.QueueSize ("500p"))
queue.SetAttribute("MaxSize", queueSizeValue)
@@ -203,66 +230,69 @@ class TestSimulator(unittest.TestCase):
self.assertEqual(limit.Get(), ns.network.QueueSize ("500p"))
## -- object pointer values
mobility = ns.mobility.RandomWaypointMobilityModel()
ptr = ns.core.PointerValue()
mobility = ns.CreateObject("RandomWaypointMobilityModel")
ptr = ns.CreateObject("PointerValue")
mobility.GetAttribute("PositionAllocator", ptr)
self.assertEqual(ptr.GetObject(), None)
self.assertEqual(ptr.GetObject(), ns.core.Ptr["Object"](ns.cppyy.nullptr))
pos = ns.mobility.ListPositionAllocator()
mobility.SetAttribute("PositionAllocator", ns.core.PointerValue(pos))
ptr.SetObject(pos)
mobility.SetAttribute("PositionAllocator", ptr)
ptr = ns.core.PointerValue()
mobility.GetAttribute("PositionAllocator", ptr)
self.assertTrue(ptr.GetObject() is not None)
ptr2 = ns.CreateObject("PointerValue")
mobility.GetAttribute("PositionAllocator", ptr2)
self.assertNotEqual(ptr.GetObject(), ns.core.Ptr["Object"](ns.cppyy.nullptr))
def testIdentity(self):
"""! Test identify
@param self this object
@return none
"""
csma = ns.csma.CsmaNetDevice()
channel = ns.csma.CsmaChannel()
csma = ns.CreateObject("CsmaNetDevice")
channel = ns.CreateObject("CsmaChannel")
csma.Attach(channel)
c1 = csma.GetChannel()
c2 = csma.GetChannel()
self.assertTrue(c1 is c2)
self.assertEqual(c1, c2)
def testTypeId(self):
"""! Test type ID
@param self this object
@return none
"""
typeId1 = ns.core.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
ok, typeId1 = ns.LookupByNameFailSafe("ns3::UdpSocketFactory")
self.assertTrue(ok)
self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
self.assertRaises(KeyError, ns.core.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
ok, typeId1 = ns.LookupByNameFailSafe("ns3::__InvalidTypeName__")
self.assertFalse(ok)
def testCommandLine(self):
"""! Test command line
@param self this object
@return none
"""
cmd = ns.core.CommandLine()
cmd.AddValue("Test1", "this is a test option")
cmd.AddValue("Test2", "this is a test option")
cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
cmd.Test1 = None
cmd.Test2 = None
cmd.test_xxx = None
class Foo:
pass
foo = Foo()
foo.test_foo = None
cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
self.assertEqual(cmd.Test1, "value1")
self.assertEqual(cmd.Test2, "value2")
self.assertEqual(cmd.test_xxx, "123")
self.assertEqual(foo.test_foo, "xpto")
# def testCommandLine(self):
# """! Test command line
# @param self this object
# @return none
# """
# cmd = ns.core.CommandLine(__file__)
# cmd.AddValue("Test1", "this is a test option")
# cmd.AddValue("Test2", "this is a test option")
# cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
# cmd.Test1 = None
# cmd.Test2 = None
# cmd.test_xxx = None
# class Foo:
# pass
# foo = Foo()
# foo.test_foo = None
# cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
#
# cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
#
# self.assertEqual(cmd.Test1, "value1")
# self.assertEqual(cmd.Test2, "value2")
# self.assertEqual(cmd.test_xxx, "123")
# self.assertEqual(foo.test_foo, "xpto")
def testSubclass(self):
"""! Test subclass
@@ -271,15 +301,14 @@ class TestSimulator(unittest.TestCase):
"""
## MyNode class
class MyNode(ns.network.Node):
def __init__(self):
"""! Initializer
@param self this object
@return none
"""
super(MyNode, self).__init__()
def GetLocalTime(self) -> ns.Time:
return ns.Seconds(10)
node = MyNode()
forced_local_time = node.GetLocalTime()
self.assertEqual(forced_local_time, ns.Seconds(10))
del node
if __name__ == '__main__':
unittest.main()
unittest.main(verbosity=1, failfast=True)