193 nc = ns.NodeContainer(1)
195 internet = ns.InternetStackHelper()
196 internet.Install(node)
199 def python_rx_callback(socket) -> None:
203 Callback<void,ns3::Ptr<ns3::Socket> > make_rx_callback_test_socket(void(*func)(Ptr<Socket>))
205 return MakeCallback(func);
209 sink = ns.Socket.CreateSocket(node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
210 sink.Bind(ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 80).ConvertTo())
211 sink.SetRecvCallback(ns.cppyy.gbl.make_rx_callback_test_socket(python_rx_callback))
213 source = ns.Socket.CreateSocket(node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
217 ns.InetSocketAddress(ns.Ipv4Address(
"127.0.0.1"), 80).ConvertTo(),
228 """! Test attributes function
229 @param self this object
233 queue = ns.CreateObject[ns.DropTailQueue[ns.Packet]]()
234 queueSizeValue = ns.QueueSizeValue(ns.QueueSize(
"500p"))
235 queue.SetAttribute(
"MaxSize", queueSizeValue)
237 limit = ns.QueueSizeValue()
238 queue.GetAttribute(
"MaxSize", limit)
239 self.assertEqual(limit.Get(), ns.QueueSize(
"500p"))
242 mobility = ns.CreateObject[ns.RandomWaypointMobilityModel]()
243 ptr = ns.PointerValue()
244 mobility.GetAttribute(
"PositionAllocator", ptr)
245 self.assertEqual(ptr.GetObject(), ns.Ptr[
"Object"](ns.cppyy.nullptr))
247 pos = ns.ListPositionAllocator()
249 mobility.SetAttribute(
"PositionAllocator", ptr)
251 ptr2 = ns.PointerValue()
252 mobility.GetAttribute(
"PositionAllocator", ptr2)
253 self.assertNotEqual(ptr.GetObject(), ns.Ptr[
"Object"](ns.cppyy.nullptr))
256 del queue, mobility, ptr, ptr2
288 """! Test command line
289 @param self this object
292 from ctypes
import c_bool, c_char_p, c_double, c_int, create_string_buffer
296 test3 = c_double(3.1415)
298 test4Buffer = create_string_buffer(b
"this is a test option", BUFFLEN)
299 test4 = c_char_p(test4Buffer.raw)
301 cmd = ns.CommandLine(__file__)
302 cmd.AddValue(
"Test1",
"this is a test option", test1)
303 cmd.AddValue(
"Test2",
"this is a test option", test2)
304 cmd.AddValue[
"double"](
"Test3",
"this is a test option", test3)
305 cmd.AddValue(
"Test4",
"this is a test option", test4, BUFFLEN)
307 cmd.Parse([
"python"])
308 self.assertEqual(test1.value,
True)
309 self.assertEqual(test2.value, 42)
310 self.assertEqual(test3.value, 3.1415)
311 self.assertEqual(test4.value, b
"this is a test option")
313 cmd.Parse([
"python",
"--Test1=false",
"--Test2=0",
"--Test3=0.0"])
314 self.assertEqual(test1.value,
False)
315 self.assertEqual(test2.value, 0)
316 self.assertEqual(test3.value, 0.0)
318 cmd.Parse([
"python",
"--Test4=new_string"])
319 self.assertEqual(test4.value, b
"new_string")
338 """! Test python-based application
339 @param self this object
342 ns.Simulator.Destroy()
344 nodes = ns.NodeContainer()
347 pointToPoint = ns.PointToPointHelper()
348 pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
349 pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
351 devices = pointToPoint.Install(nodes)
353 stack = ns.InternetStackHelper()
356 address = ns.Ipv4AddressHelper()
357 address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
359 interfaces = address.Assign(devices)
364 Callback<void,Ptr<Socket> > make_rx_callback(void(*func)(Ptr<Socket>))
366 return MakeCallback(func);
368 EventImpl* pythonMakeEventSend(void (*f)(Ptr<Socket>, Ptr<Packet>, Address&), Ptr<Socket> socket, Ptr<Packet> packet, Address address)
370 return MakeEvent(f, socket, packet, address);
376 class EchoServer(ns.Application):
379 socketToInstanceDict = {}
381 def __init__(self, node: ns.Node, port=ECHO_PORT):
382 """! Constructor needs to call first the constructor to Application (super class)
383 @param self this object
384 @param node node where this application will be executed
385 @param port port to listen
395 node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory")
398 ns.InetSocketAddress(ns.Ipv4Address.GetAny(), self.
port).ConvertTo()
400 self.
m_socket.SetRecvCallback(ns.make_rx_callback(EchoServer._Receive))
401 EchoServer.socketToInstanceDict[self.
m_socket] = self
405 @param self this object
408 del EchoServer.socketToInstanceDict[self.
m_socket]
410 def Send(self, packet: ns.Packet, address: ns.Address) ->
None:
411 """! Function to send a packet to an address
412 @param self this object
413 @param packet packet to send
414 @param address destination address
417 self.
m_socket.SendTo(packet, 0, address)
418 if EchoServer.LOGGING:
419 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
421 "At time +{s}s server sent {b} bytes from {ip} port {port}".format(
422 s=ns.Simulator.Now().GetSeconds(),
423 b=packet.__deref__().GetSize(),
424 ip=inetAddress.GetIpv4(),
425 port=inetAddress.GetPort(),
432 """! Function to receive a packet from an address
433 @param self this object
436 address = ns.Address()
437 packet = self.
m_socket.RecvFrom(address)
438 if EchoServer.LOGGING:
439 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
441 "At time +{s}s server received {b} bytes from {ip} port {port}".format(
442 s=ns.Simulator.Now().GetSeconds(),
443 b=packet.__deref__().GetSize(),
444 ip=inetAddress.GetIpv4(),
445 port=inetAddress.GetPort(),
450 event = ns.pythonMakeEventSend(EchoServer._Send, self.
m_socket, packet, address)
451 ns.Simulator.Schedule(ns.Seconds(1), event)
454 def _Send(socket: ns.Socket, packet: ns.Packet, address: ns.Address):
455 """! Static send function, which matches the output socket
456 to the EchoServer instance to call the instance Send function
457 @param socket socket from the instance that should send the packet
458 @param packet packet to send
459 @param address destination address
462 instance = EchoServer.socketToInstanceDict[socket]
463 instance.Send(packet, address)
466 def _Receive(socket: ns.Socket) ->
None:
467 """! Static receive function, which matches the input socket
468 to the EchoServer instance to call the instance Receive function
469 @param socket socket from the instance that should receive the packet
472 instance = EchoServer.socketToInstanceDict[socket]
475 echoServer = EchoServer(nodes.Get(1))
476 nodes.Get(1).AddApplication(echoServer)
478 serverApps = ns.ApplicationContainer()
479 serverApps.Add(echoServer)
480 serverApps.Start(ns.Seconds(1))
481 serverApps.Stop(ns.Seconds(10))
483 address = interfaces.GetAddress(1).ConvertTo()
484 echoClient = ns.UdpEchoClientHelper(address, EchoServer.ECHO_PORT)
485 echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(10))
486 echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1)))
487 echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(101))
489 clientApps = echoClient.Install(nodes.Get(0))
490 clientApps.Start(ns.Seconds(2))
491 clientApps.Stop(ns.Seconds(10))
494 ns.Simulator.Destroy()