25UINT32_MAX = 0xFFFFFFFF
40 """! Test schedule now
41 @param self this object
45 def callback(args: ns.cppyy.gbl.std.vector) ->
None:
46 """! Callback function
53 ns.Simulator.Destroy()
57 EventImpl* pythonMakeEvent(void (*f)(std::vector<std::string>), std::vector<std::string> l)
59 return MakeEvent(f, l);
62 event = ns.cppyy.gbl.pythonMakeEvent(callback, sys.argv)
63 ns.Simulator.ScheduleNow(event)
66 self.assertEqual(self._cb_time.GetSeconds(), 0.0)
70 @param self this object
74 def callback(args: ns.cppyy.gbl.std.vector):
75 """! Callback function
82 ns.Simulator.Destroy()
86 EventImpl* pythonMakeEvent2(void (*f)(std::vector<std::string>), std::vector<std::string> l)
88 return MakeEvent(f, l);
91 event = ns.cppyy.gbl.pythonMakeEvent2(callback, sys.argv)
92 ns.Simulator.Schedule(ns.Seconds(123), event)
95 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
98 """! Test schedule destroy
99 @param self this object
103 def callback(args: ns.cppyy.gbl.std.vector):
104 """! Callback function
111 ns.Simulator.Destroy()
114 ns.cppyy.cppdef(
"void null(){ return; }")
115 ns.Simulator.Schedule(ns.Seconds(123), ns.cppyy.gbl.null)
117 EventImpl* pythonMakeEvent3(void (*f)(std::vector<std::string>), std::vector<std::string> l)
119 return MakeEvent(f, l);
122 event = ns.cppyy.gbl.pythonMakeEvent3(callback, sys.argv)
123 ns.Simulator.ScheduleDestroy(event)
125 ns.Simulator.Destroy()
127 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
130 """! Test schedule with context
131 @param self this object
135 def callback(context, args: ns.cppyy.gbl.std.vector):
137 @param context the context
138 @param args the arguments
145 ns.Simulator.Destroy()
150 EventImpl* pythonMakeEvent4(void (*f)(uint32_t, std::vector<std::string>), uint32_t context, std::vector<std::string> l)
152 return MakeEvent(f, context, l);
155 event = ns.cppyy.gbl.pythonMakeEvent4(callback, 54321, sys.argv)
156 ns.Simulator.ScheduleWithContext(54321, ns.Seconds(123), event)
160 self.assertEqual(self._cb_time.GetSeconds(), 123.0)
163 """! Test time comparison
164 @param self this object
167 self.assertTrue(ns.Seconds(123) == ns.Seconds(123))
168 self.assertTrue(ns.Seconds(123) >= ns.Seconds(123))
169 self.assertTrue(ns.Seconds(123) <= ns.Seconds(123))
170 self.assertTrue(ns.Seconds(124) > ns.Seconds(123))
171 self.assertTrue(ns.Seconds(123) < ns.Seconds(124))
174 """! Test numeric operations
175 @param self this object
178 self.assertEqual(ns.Seconds(10) + ns.Seconds(5), ns.Seconds(15))
179 self.assertEqual(ns.Seconds(10) - ns.Seconds(5), ns.Seconds(5))
181 v1 = ns.int64x64_t(5.0) * ns.int64x64_t(10)
182 self.assertEqual(v1, ns.int64x64_t(50))
185 """! Test configuration
186 @param self this object
189 ns.Config.SetDefault("ns3::OnOffApplication::PacketSize", ns.core.UintegerValue(123))
197 nc = ns.NodeContainer(1)
199 internet = ns.CreateObject("InternetStackHelper")
200 internet.Install(node)
203 def python_rx_callback(socket) -> None:
207 Callback<void,ns3::Ptr<ns3::Socket> > make_rx_callback_test_socket(void(*func)(Ptr<Socket>))
209 return MakeCallback(func);
213 sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
214 sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80).ConvertTo())
215 sink.SetRecvCallback(ns.cppyy.gbl.make_rx_callback_test_socket(python_rx_callback))
217 source = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
218 source.SendTo(ns.network.Packet(19), 0,
219 ns.network.InetSocketAddress(ns.network.Ipv4Address(
"127.0.0.1"), 80).ConvertTo())
229 """! Test attributes function
230 @param self this object
234 queue = ns.CreateObject(
"DropTailQueue<Packet>")
235 queueSizeValue = ns.network.QueueSizeValue(ns.network.QueueSize(
"500p"))
236 queue.SetAttribute(
"MaxSize", queueSizeValue)
238 limit = ns.network.QueueSizeValue()
239 queue.GetAttribute(
"MaxSize", limit)
240 self.assertEqual(limit.Get(), ns.network.QueueSize(
"500p"))
243 mobility = ns.CreateObject(
"RandomWaypointMobilityModel")
244 ptr = ns.CreateObject(
"PointerValue")
245 mobility.GetAttribute(
"PositionAllocator", ptr)
246 self.assertEqual(ptr.GetObject(), ns.core.Ptr[
"Object"](ns.cppyy.nullptr))
248 pos = ns.mobility.ListPositionAllocator()
250 mobility.SetAttribute(
"PositionAllocator", ptr)
252 ptr2 = ns.CreateObject(
"PointerValue")
253 mobility.GetAttribute(
"PositionAllocator", ptr2)
254 self.assertNotEqual(ptr.GetObject(), ns.core.Ptr[
"Object"](ns.cppyy.nullptr))
257 del queue, mobility, ptr, ptr2
261 @param self this object
264 csma = ns.CreateObject("CsmaNetDevice")
265 channel = ns.CreateObject(
"CsmaChannel")
268 c1 = csma.GetChannel()
269 c2 = csma.GetChannel()
271 self.assertEqual(c1, c2)
278 @param self this object
281 ok, typeId1 = ns.LookupByNameFailSafe("ns3::UdpSocketFactory")
283 self.assertEqual(typeId1.GetName(),
"ns3::UdpSocketFactory")
285 ok, typeId1 = ns.LookupByNameFailSafe(
"ns3::__InvalidTypeName__")
289 """! Test command line
290 @param self this object
293 from ctypes
import c_bool, c_int, c_double, c_char_p, create_string_buffer
297 test3 = c_double(3.1415)
299 test4Buffer = create_string_buffer(b
"this is a test option", BUFFLEN)
300 test4 = c_char_p(test4Buffer.raw)
302 cmd = ns.core.CommandLine(__file__)
303 cmd.AddValue(
"Test1",
"this is a test option", test1)
304 cmd.AddValue(
"Test2",
"this is a test option", test2)
305 cmd.AddValue[
"double"](
"Test3",
"this is a test option", test3)
306 cmd.AddValue(
"Test4",
"this is a test option", test4, BUFFLEN)
308 cmd.Parse([
"python"])
309 self.assertEqual(test1.value,
True)
310 self.assertEqual(test2.value, 42)
311 self.assertEqual(test3.value, 3.1415)
312 self.assertEqual(test4.value, b
"this is a test option")
314 cmd.Parse([
"python",
"--Test1=false",
"--Test2=0",
"--Test3=0.0"])
315 self.assertEqual(test1.value,
False)
316 self.assertEqual(test2.value, 0)
317 self.assertEqual(test3.value, 0.0)
319 cmd.Parse([
"python",
"--Test4=new_string"])
320 self.assertEqual(test4.value, b
"new_string")
324 @param self this object
329 class MyNode(ns.network.Node):
330 def GetLocalTime(self) -> ns.Time:
331 return ns.Seconds(10)
334 forced_local_time = node.GetLocalTime()
335 self.assertEqual(forced_local_time, ns.Seconds(10))
339 """! Test python-based application
340 @param self this object
343 ns.Simulator.Destroy()
345 nodes = ns.network.NodeContainer()
348 pointToPoint = ns.point_to_point.PointToPointHelper()
349 pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue(
"5Mbps"))
350 pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
352 devices = pointToPoint.Install(nodes)
354 stack = ns.internet.InternetStackHelper()
357 address = ns.internet.Ipv4AddressHelper()
358 address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"),
359 ns.network.Ipv4Mask(
"255.255.255.0"))
361 interfaces = address.Assign(devices)
366 Callback<void,Ptr<Socket> > make_rx_callback(void(*func)(Ptr<Socket>))
368 return MakeCallback(func);
370 EventImpl* pythonMakeEventSend(void (*f)(Ptr<Socket>, Ptr<Packet>, Address&), Ptr<Socket> socket, Ptr<Packet> packet, Address address)
372 return MakeEvent(f, socket, packet, address);
378 class EchoServer(ns.applications.Application):
381 socketToInstanceDict = {}
383 def __init__(self, node: ns.Node, port=ECHO_PORT):
384 """! Constructor needs to call first the constructor to Application (super class)
385 @param self this object
386 @param node node where this application will be executed
387 @param port port to listen
395 self.
m_socket = ns.network.Socket.CreateSocket(node,
396 ns.core.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
397 self.
m_socket.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), self.
port).ConvertTo())
398 self.
m_socket.SetRecvCallback(ns.make_rx_callback(EchoServer._Receive))
399 EchoServer.socketToInstanceDict[self.
m_socket] = self
403 @param self this object
406 del EchoServer.socketToInstanceDict[self.m_socket]
408 def Send(self, packet: ns.Packet, address: ns.Address) ->
None:
409 """! Function to send a packet to an address
410 @param self this object
411 @param packet packet to send
412 @param address destination address
415 self.m_socket.SendTo(packet, 0, address)
416 if EchoServer.LOGGING:
417 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
418 print(
"At time +{s}s server sent {b} bytes from {ip} port {port}"
419 .format(s=ns.Simulator.Now().GetSeconds(),
420 b=packet.__deref__().GetSize(),
421 ip=inetAddress.GetIpv4(),
422 port=inetAddress.GetPort()),
427 """! Function to receive a packet from an address
428 @param self this object
431 address = ns.Address()
432 packet = self.m_socket.RecvFrom(address)
433 if EchoServer.LOGGING:
434 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
435 print(
"At time +{s}s server received {b} bytes from {ip} port {port}"
436 .format(s=ns.Simulator.Now().GetSeconds(),
437 b=packet.__deref__().GetSize(),
438 ip=inetAddress.GetIpv4(),
439 port=inetAddress.GetPort()),
442 event = ns.pythonMakeEventSend(EchoServer._Send, self.
m_socket, packet, address)
443 ns.Simulator.Schedule(ns.Seconds(1), event)
446 def _Send(socket: ns.Socket, packet: ns.Packet, address: ns.Address):
447 """! Static send function, which matches the output socket
448 to the EchoServer instance to call the instance Send function
449 @param socket socket
from the instance that should send the packet
450 @param packet packet to send
451 @param address destination address
454 instance = EchoServer.socketToInstanceDict[socket]
455 instance.Send(packet, address)
458 def _Receive(socket: ns.Socket) ->
None:
459 """! Static receive function, which matches the input socket
460 to the EchoServer instance to call the instance Receive function
461 @param socket socket
from the instance that should receive the packet
464 instance = EchoServer.socketToInstanceDict[socket]
467 echoServer = EchoServer(nodes.Get(1))
468 nodes.Get(1).AddApplication(echoServer)
470 serverApps = ns.ApplicationContainer()
471 serverApps.Add(echoServer)
472 serverApps.Start(ns.core.Seconds(1.0))
473 serverApps.Stop(ns.core.Seconds(10.0))
475 address = interfaces.GetAddress(1).ConvertTo()
476 echoClient = ns.applications.UdpEchoClientHelper(address, EchoServer.ECHO_PORT)
477 echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(10))
478 echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
479 echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(101))
481 clientApps = echoClient.Install(nodes.Get(0))
482 clientApps.Start(ns.core.Seconds(2.0))
483 clientApps.Stop(ns.core.Seconds(10.0))
486 ns.Simulator.Destroy()
489if __name__ ==
'__main__':
490 unittest.main(verbosity=1, failfast=
True)
def testScheduleDestroy(self)
Test schedule destroy.
port
Listen port for the server.
def testCommandLine(self)
Test command line.
def testTimeNumericOperations(self)
Test numeric operations.
def testEchoServerApplication(self)
Test python-based application.
__python_owns__
EchoServer application class.
def testScheduleNow(self)
Test schedule now.
def testTypeId(self)
Test type ID.
def testSubclass(self)
Test subclass.
def testSchedule(self)
Test schedule.
def testScheduleWithContext(self)
Test schedule with context.
def testAttributes(self)
Test attributes function.
def testSocket(self)
Test socket.
def testTimeComparison(self)
Test time comparison.
def testConfig(self)
Test configuration.
m_socket
Socket used by the server to listen to port.
_received_packet
received packet
def testIdentity(self)
Test identify.
static void Send(Ptr< NetDevice > dev, int level, std::string emuMode)