201 nc = ns.NodeContainer(1)
203 internet = ns.InternetStackHelper()
204 internet.Install(node)
207 def python_rx_callback(socket) -> None:
212 Callback<void,ns3::Ptr<ns3::Socket> > make_rx_callback_test_socket(void(*func)(Ptr<Socket>))
214 return MakeCallback(func);
219 sink = ns.Socket.CreateSocket(node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
220 sink.Bind(ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 80).ConvertTo())
221 sink.SetRecvCallback(ns.cppyy.gbl.make_rx_callback_test_socket(python_rx_callback))
223 source = ns.Socket.CreateSocket(node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory"))
227 ns.InetSocketAddress(ns.Ipv4Address(
"127.0.0.1"), 80).ConvertTo(),
238 """! Test attributes function
239 @param self this object
243 queue = ns.CreateObject[ns.DropTailQueue[ns.Packet]]()
244 queueSizeValue = ns.QueueSizeValue(ns.QueueSize(
"500p"))
245 queue.SetAttribute(
"MaxSize", queueSizeValue)
247 limit = ns.QueueSizeValue()
248 queue.GetAttribute(
"MaxSize", limit)
249 self.assertEqual(limit.Get(), ns.QueueSize(
"500p"))
252 mobility = ns.CreateObject[ns.RandomWaypointMobilityModel]()
253 ptr = ns.PointerValue()
254 mobility.GetAttribute(
"PositionAllocator", ptr)
255 self.assertEqual(ptr.GetObject(), ns.Ptr[
"Object"](ns.cppyy.nullptr))
257 pos = ns.ListPositionAllocator()
259 mobility.SetAttribute(
"PositionAllocator", ptr)
261 ptr2 = ns.PointerValue()
262 mobility.GetAttribute(
"PositionAllocator", ptr2)
263 self.assertNotEqual(ptr.GetObject(), ns.Ptr[
"Object"](ns.cppyy.nullptr))
266 del queue, mobility, ptr, ptr2
298 """! Test command line
299 @param self this object
302 from ctypes
import c_bool, c_char_p, c_double, c_int, create_string_buffer
306 test3 = c_double(3.1415)
308 test4Buffer = create_string_buffer(b
"this is a test option", BUFFLEN)
309 test4 = c_char_p(test4Buffer.raw)
311 cmd = ns.CommandLine(__file__)
312 cmd.AddValue(
"Test1",
"this is a test option", test1)
313 cmd.AddValue(
"Test2",
"this is a test option", test2)
314 cmd.AddValue[
"double"](
"Test3",
"this is a test option", test3)
315 cmd.AddValue(
"Test4",
"this is a test option", test4, BUFFLEN)
317 cmd.Parse([
"python"])
318 self.assertEqual(test1.value,
True)
319 self.assertEqual(test2.value, 42)
320 self.assertEqual(test3.value, 3.1415)
321 self.assertEqual(test4.value, b
"this is a test option")
323 cmd.Parse([
"python",
"--Test1=false",
"--Test2=0",
"--Test3=0.0"])
324 self.assertEqual(test1.value,
False)
325 self.assertEqual(test2.value, 0)
326 self.assertEqual(test3.value, 0.0)
328 cmd.Parse([
"python",
"--Test4=new_string"])
329 self.assertEqual(test4.value, b
"new_string")
348 """! Test python-based application
349 @param self this object
352 ns.Simulator.Destroy()
354 nodes = ns.NodeContainer()
357 pointToPoint = ns.PointToPointHelper()
358 pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
359 pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
361 devices = pointToPoint.Install(nodes)
363 stack = ns.InternetStackHelper()
366 address = ns.Ipv4AddressHelper()
367 address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
369 interfaces = address.Assign(devices)
375 Callback<void,Ptr<Socket> > make_rx_callback(void(*func)(Ptr<Socket>))
377 return MakeCallback(func);
379 EventImpl* pythonMakeEventSend(void (*f)(Ptr<Socket>, Ptr<Packet>, Address&), Ptr<Socket> socket, Ptr<Packet> packet, Address address)
381 return MakeEvent(f, socket, packet, address);
388 class EchoServer(ns.Application):
391 socketToInstanceDict = {}
393 def __init__(self, node: ns.Node, port=ECHO_PORT):
394 """! Constructor needs to call first the constructor to Application (super class)
395 @param self this object
396 @param node node where this application will be executed
397 @param port port to listen
407 node, ns.TypeId.LookupByName(
"ns3::UdpSocketFactory")
410 ns.InetSocketAddress(ns.Ipv4Address.GetAny(), self.
port).ConvertTo()
412 self.
m_socket.SetRecvCallback(ns.make_rx_callback(EchoServer._Receive))
413 EchoServer.socketToInstanceDict[self.
m_socket] = self
417 @param self this object
420 del EchoServer.socketToInstanceDict[self.
m_socket]
422 def Send(self, packet: ns.Packet, address: ns.Address) ->
None:
423 """! Function to send a packet to an address
424 @param self this object
425 @param packet packet to send
426 @param address destination address
429 self.
m_socket.SendTo(packet, 0, address)
430 if EchoServer.LOGGING:
431 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
433 "At time +{s}s server sent {b} bytes from {ip} port {port}".format(
434 s=ns.Simulator.Now().GetSeconds(),
435 b=packet.__deref__().GetSize(),
436 ip=inetAddress.GetIpv4(),
437 port=inetAddress.GetPort(),
444 """! Function to receive a packet from an address
445 @param self this object
448 address = ns.Address()
449 packet = self.
m_socket.RecvFrom(address)
450 if EchoServer.LOGGING:
451 inetAddress = ns.InetSocketAddress.ConvertFrom(address)
453 "At time +{s}s server received {b} bytes from {ip} port {port}".format(
454 s=ns.Simulator.Now().GetSeconds(),
455 b=packet.__deref__().GetSize(),
456 ip=inetAddress.GetIpv4(),
457 port=inetAddress.GetPort(),
462 event = ns.pythonMakeEventSend(EchoServer._Send, self.
m_socket, packet, address)
463 ns.Simulator.Schedule(ns.Seconds(1), event)
466 def _Send(socket: ns.Socket, packet: ns.Packet, address: ns.Address):
467 """! Static send function, which matches the output socket
468 to the EchoServer instance to call the instance Send function
469 @param socket socket from the instance that should send the packet
470 @param packet packet to send
471 @param address destination address
474 instance = EchoServer.socketToInstanceDict[socket]
475 instance.Send(packet, address)
478 def _Receive(socket: ns.Socket) ->
None:
479 """! Static receive function, which matches the input socket
480 to the EchoServer instance to call the instance Receive function
481 @param socket socket from the instance that should receive the packet
484 instance = EchoServer.socketToInstanceDict[socket]
487 echoServer = EchoServer(nodes.Get(1))
488 nodes.Get(1).AddApplication(echoServer)
490 serverApps = ns.ApplicationContainer()
491 serverApps.Add(echoServer)
492 serverApps.Start(ns.Seconds(1.0))
493 serverApps.Stop(ns.Seconds(10.0))
495 address = interfaces.GetAddress(1).ConvertTo()
496 echoClient = ns.UdpEchoClientHelper(address, EchoServer.ECHO_PORT)
497 echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(10))
498 echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1.0)))
499 echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(101))
501 clientApps = echoClient.Install(nodes.Get(0))
502 clientApps.Start(ns.Seconds(2.0))
503 clientApps.Stop(ns.Seconds(10.0))
506 ns.Simulator.Destroy()