network: make addresses ConvertTo public and simplify python scripts
This commit is contained in:
@@ -396,17 +396,6 @@ def load_modules():
|
||||
|
||||
cppyy.gbl.ns3.Node.__del__ = Node_del
|
||||
|
||||
# Define ns.cppyy.gbl.addressFromIpv4Address and others
|
||||
cppyy.cppdef("""using namespace ns3;
|
||||
Address addressFromIpv4Address(Ipv4Address ip){ return Address(ip); };
|
||||
Address addressFromInetSocketAddress(InetSocketAddress addr){ return Address(addr); };
|
||||
Address addressFromPacketSocketAddress(PacketSocketAddress addr){ return Address(addr); };
|
||||
""")
|
||||
# Expose addressFromIpv4Address as a member of the ns3 namespace (equivalent to ns)
|
||||
setattr(cppyy.gbl.ns3, "addressFromIpv4Address", cppyy.gbl.addressFromIpv4Address)
|
||||
setattr(cppyy.gbl.ns3, "addressFromInetSocketAddress", cppyy.gbl.addressFromInetSocketAddress)
|
||||
setattr(cppyy.gbl.ns3, "addressFromPacketSocketAddress", cppyy.gbl.addressFromPacketSocketAddress)
|
||||
|
||||
cppyy.cppdef("""
|
||||
using namespace ns3;
|
||||
std::tuple<bool, TypeId> LookupByNameFailSafe(std::string name)
|
||||
|
||||
@@ -83,7 +83,7 @@ Here is some example code that is written in Python and that runs |ns3|, which i
|
||||
serverApps.Start(ns.core.Seconds(1.0))
|
||||
serverApps.Stop(ns.core.Seconds(10.0))
|
||||
|
||||
address = ns.addressFromIpv4Address(interfaces.GetAddress(1))
|
||||
address = interfaces.GetAddress(1).ConvertTo()
|
||||
echoClient = ns.applications.UdpEchoClientHelper(address, 9)
|
||||
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
|
||||
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
|
||||
@@ -324,20 +324,11 @@ module (`ns-3-dev/bindings/python/ns__init__.py`).
|
||||
A different operator used by |ns3| is `operator Address()`, used to
|
||||
convert different types of Addresses into the generic type Address.
|
||||
This is not supported by Cppyy and requires explicit conversion.
|
||||
Some helpers have been added to handle the common cases.
|
||||
|
||||
.. sourcecode:: python
|
||||
|
||||
# Define ns.cppyy.gbl.addressFromIpv4Address and others
|
||||
cppyy.cppdef("""using namespace ns3;
|
||||
Address addressFromIpv4Address(Ipv4Address ip){ return Address(ip); };
|
||||
Address addressFromInetSocketAddress(InetSocketAddress addr){ return Address(addr); };
|
||||
Address addressFromPacketSocketAddress(PacketSocketAddress addr){ return Address(addr); };
|
||||
""")
|
||||
# Expose addressFromIpv4Address as a member of the ns3 namespace (equivalent to ns)
|
||||
setattr(cppyy.gbl.ns3, "addressFromIpv4Address", cppyy.gbl.addressFromIpv4Address)
|
||||
setattr(cppyy.gbl.ns3, "addressFromInetSocketAddress", cppyy.gbl.addressFromInetSocketAddress)
|
||||
setattr(cppyy.gbl.ns3, "addressFromPacketSocketAddress", cppyy.gbl.addressFromPacketSocketAddress)
|
||||
# Explicitly convert the InetSocketAddress to Address using InetSocketAddress.ConvertTo()
|
||||
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80).ConvertTo())
|
||||
|
||||
Most of the missing APIs can be wrapped, given enough time, patience, and expertise, and will likely be wrapped if bug reports are submitted.
|
||||
However, don't file a bug report saying "bindings are incomplete", because the project does not have maintainers to maintain every API.
|
||||
|
||||
@@ -85,7 +85,7 @@ def main(argv):
|
||||
packetSize = 1024
|
||||
maxPacketCount = 500
|
||||
interPacketInterval = ns.core.Seconds(0.01)
|
||||
client = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(i.GetAddress (1)), port)
|
||||
client = ns.applications.UdpEchoClientHelper(i.GetAddress(1).ConvertTo(), port)
|
||||
client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
|
||||
client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
|
||||
client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
|
||||
|
||||
@@ -49,7 +49,7 @@ serverApps = echoServer.Install(nodes.Get(1))
|
||||
serverApps.Start(ns.core.Seconds(1.0))
|
||||
serverApps.Stop(ns.core.Seconds(10.0))
|
||||
|
||||
address = ns.addressFromIpv4Address(interfaces.GetAddress(1))
|
||||
address = interfaces.GetAddress(1).ConvertTo()
|
||||
echoClient = ns.applications.UdpEchoClientHelper(address, 9)
|
||||
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
|
||||
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
|
||||
|
||||
@@ -76,7 +76,7 @@ serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
|
||||
serverApps.Start(ns.core.Seconds(1.0))
|
||||
serverApps.Stop(ns.core.Seconds(10.0))
|
||||
|
||||
echoClient = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(csmaInterfaces.GetAddress(nCsma.value)), 9)
|
||||
echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 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))
|
||||
|
||||
@@ -126,7 +126,7 @@ serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
|
||||
serverApps.Start(ns.core.Seconds(1.0))
|
||||
serverApps.Stop(ns.core.Seconds(10.0))
|
||||
|
||||
echoClient = ns.applications.UdpEchoClientHelper(ns.addressFromIpv4Address(csmaInterfaces.GetAddress(nCsma.value)), 9)
|
||||
echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 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))
|
||||
|
||||
@@ -320,15 +320,14 @@ def main(argv):
|
||||
# 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)
|
||||
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory", socketAddr.ConvertTo())
|
||||
apps = onoff.Install(ns.network.NodeContainer(appSource))
|
||||
apps.Start(ns.core.Seconds(3))
|
||||
apps.Stop(ns.core.Seconds(stopTime.value - 1))
|
||||
|
||||
# Create a packet sink to receive these packets
|
||||
sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
|
||||
ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)))
|
||||
ns.network.InetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)).ConvertTo())
|
||||
sinkContainer = ns.network.NodeContainer(appSink)
|
||||
apps = sink.Install(sinkContainer)
|
||||
apps.Start(ns.core.Seconds(3))
|
||||
|
||||
@@ -128,8 +128,7 @@ def main(argv):
|
||||
socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
|
||||
socket.SetProtocol(1)
|
||||
|
||||
genericAddress = ns.addressFromPacketSocketAddress(socket)
|
||||
onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", genericAddress)
|
||||
onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", socket.ConvertTo())
|
||||
onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
|
||||
|
||||
apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
|
||||
|
||||
@@ -96,8 +96,7 @@ def main(argv):
|
||||
port = 9 # Discard port(RFC 863)
|
||||
|
||||
inet_sock_address = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.2"), port)
|
||||
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
|
||||
ns.network.Address(ns.addressFromInetSocketAddress(inet_sock_address)))
|
||||
onoff = ns.applications.OnOffHelper("ns3::UdpSocketFactory", inet_sock_address.ConvertTo())
|
||||
onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
|
||||
|
||||
app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
|
||||
@@ -107,8 +106,7 @@ def main(argv):
|
||||
|
||||
# Create an optional packet sink to receive these packets
|
||||
inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)
|
||||
sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory",
|
||||
ns.network.Address(ns.addressFromInetSocketAddress(inet_address)))
|
||||
sink = ns.applications.PacketSinkHelper("ns3::UdpSocketFactory", inet_address.ConvertTo())
|
||||
app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
|
||||
app.Start(ns.core.Seconds(0.0))
|
||||
|
||||
@@ -117,7 +115,7 @@ def main(argv):
|
||||
#
|
||||
inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.1.1.1"), port)
|
||||
onoff.SetAttribute("Remote",
|
||||
ns.network.AddressValue(ns.addressFromInetSocketAddress(inet_address)))
|
||||
ns.network.AddressValue(inet_address.ConvertTo()))
|
||||
app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
|
||||
app.Start(ns.core.Seconds(1.1))
|
||||
app.Stop(ns.core.Seconds(10.0))
|
||||
|
||||
@@ -62,8 +62,7 @@ def main(argv):
|
||||
|
||||
port = 9 # Discard port(RFC 863)
|
||||
inetAddress = ns.network.InetSocketAddress(ns.network.Ipv4Address("10.0.0.1"), port)
|
||||
onOffHelper = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
|
||||
ns.network.Address(ns.addressFromInetSocketAddress(inetAddress)))
|
||||
onOffHelper = ns.applications.OnOffHelper("ns3::UdpSocketFactory", inetAddress.ConvertTo())
|
||||
onOffHelper.SetAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate("100kbps")))
|
||||
onOffHelper.SetAttribute("OnTime", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=1]"))
|
||||
onOffHelper.SetAttribute("OffTime", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=0]"))
|
||||
@@ -97,8 +96,7 @@ def main(argv):
|
||||
for i, node in [(i, nodes.Get(i)) for i in range(nodes.GetN())]:
|
||||
destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
|
||||
#print (i, destaddr)
|
||||
genericAddress = ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(destaddr, port))
|
||||
onOffHelper.SetAttribute("Remote", ns.network.AddressValue(genericAddress))
|
||||
onOffHelper.SetAttribute("Remote", ns.network.AddressValue(ns.network.InetSocketAddress(destaddr, port).ConvertTo()))
|
||||
container = ns.network.NodeContainer(node)
|
||||
app = onOffHelper.Install(container)
|
||||
urv = ns.CreateObject("UniformRandomVariable")#ns.cppyy.gbl.get_rng()
|
||||
|
||||
@@ -86,6 +86,14 @@ namespace ns3
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* To convert a specific Address T (e.g., Ipv6Address) to and from an Address type,
|
||||
* a class must implement three public functions:
|
||||
* \code
|
||||
* static T ConvertFrom(const Address& address);
|
||||
* Address ConvertTo() const;
|
||||
* operator Address() const;
|
||||
* \endcode
|
||||
*
|
||||
* \see attribute_Address
|
||||
*/
|
||||
class Address
|
||||
|
||||
@@ -116,13 +116,13 @@ class InetSocketAddress
|
||||
*/
|
||||
static InetSocketAddress ConvertFrom(const Address& address);
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief Convert to an Address type
|
||||
* \return the Address corresponding to this object.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief Get the underlying address type (automatically assigned).
|
||||
*
|
||||
|
||||
@@ -113,13 +113,13 @@ class Inet6SocketAddress
|
||||
*/
|
||||
static Inet6SocketAddress ConvertFrom(const Address& addr);
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief Convert to Address.
|
||||
* \return Address instance
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief Get the type.
|
||||
* \return the type of Inet6SocketAddress
|
||||
|
||||
@@ -182,6 +182,12 @@ class Ipv4Address
|
||||
* Ipv4Address.
|
||||
*/
|
||||
static Ipv4Address ConvertFrom(const Address& address);
|
||||
/**
|
||||
* \brief Convert to an Address type
|
||||
* \return the Address corresponding to this object.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \return the 0.0.0.0 address
|
||||
*/
|
||||
@@ -200,12 +206,6 @@ class Ipv4Address
|
||||
static Ipv4Address GetLoopback();
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief Convert to an Address type
|
||||
* \return the Address corresponding to this object.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \brief Get the underlying address type (automatically assigned).
|
||||
*
|
||||
|
||||
@@ -345,6 +345,12 @@ class Ipv6Address
|
||||
*/
|
||||
static Ipv6Address ConvertFrom(const Address& address);
|
||||
|
||||
/**
|
||||
* \brief convert the IPv6Address object to an Address object.
|
||||
* \return the Address object corresponding to this object.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \return true if address is initialized (i.e., set to something), false otherwise
|
||||
*/
|
||||
@@ -399,12 +405,6 @@ class Ipv6Address
|
||||
void GetBytes(uint8_t buf[16]) const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \brief convert the IPv6Address object to an Address object.
|
||||
* \return the Address object corresponding to this object.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \brief Return the Type of address.
|
||||
* \return type of address
|
||||
|
||||
@@ -77,6 +77,13 @@ class Mac16Address
|
||||
* Mac16Address.
|
||||
*/
|
||||
static Mac16Address ConvertFrom(const Address& address);
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \param address address to test
|
||||
* \returns true if the address matches, false otherwise.
|
||||
@@ -150,13 +157,6 @@ class Mac16Address
|
||||
bool IsMulticast() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \brief Return the Type of address.
|
||||
* \return type of address
|
||||
|
||||
@@ -81,6 +81,13 @@ class Mac48Address
|
||||
* Mac48Address.
|
||||
*/
|
||||
static Mac48Address ConvertFrom(const Address& address);
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \param address address to test
|
||||
* \returns true if the address matches, false otherwise.
|
||||
@@ -154,13 +161,6 @@ class Mac48Address
|
||||
typedef void (*TracedCallback)(Mac48Address value);
|
||||
|
||||
private:
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \brief Return the Type of address.
|
||||
* \return type of address
|
||||
|
||||
@@ -80,6 +80,13 @@ class Mac64Address
|
||||
* Mac64Address.
|
||||
*/
|
||||
static Mac64Address ConvertFrom(const Address& address);
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \param address address to test
|
||||
* \returns true if the address matches, false otherwise.
|
||||
@@ -107,13 +114,6 @@ class Mac64Address
|
||||
static void ResetAllocationIndex();
|
||||
|
||||
private:
|
||||
/**
|
||||
* \returns a new Address instance
|
||||
*
|
||||
* Convert an instance of this class to a polymorphic Address instance.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \brief Return the Type of address.
|
||||
* \return type of address
|
||||
|
||||
@@ -62,6 +62,13 @@ class Mac8Address
|
||||
*/
|
||||
static Mac8Address ConvertFrom(const Address& address);
|
||||
|
||||
/**
|
||||
* Convert to a generic Address.
|
||||
*
|
||||
* \return The Address value.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* Check that a generic Address is compatible with Mac8Address.
|
||||
*
|
||||
@@ -133,12 +140,6 @@ class Mac8Address
|
||||
* \return The type value.
|
||||
*/
|
||||
static uint8_t GetType();
|
||||
/**
|
||||
* Convert to a generic Address.
|
||||
*
|
||||
* \return The Address value.
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
friend bool operator<(const Mac8Address& a, const Mac8Address& b);
|
||||
friend bool operator==(const Mac8Address& a, const Mac8Address& b);
|
||||
|
||||
@@ -103,6 +103,12 @@ class PacketSocketAddress
|
||||
*/
|
||||
static PacketSocketAddress ConvertFrom(const Address& address);
|
||||
|
||||
/**
|
||||
* \brief Convert an instance of this class to a polymorphic Address instance.
|
||||
* \returns a new Address instance
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
/**
|
||||
* \param address address to test
|
||||
* \returns true if the address matches, false otherwise.
|
||||
@@ -116,12 +122,6 @@ class PacketSocketAddress
|
||||
*/
|
||||
static uint8_t GetType();
|
||||
|
||||
/**
|
||||
* \brief Convert an instance of this class to a polymorphic Address instance.
|
||||
* \returns a new Address instance
|
||||
*/
|
||||
Address ConvertTo() const;
|
||||
|
||||
uint16_t m_protocol; //!< Protocol
|
||||
bool m_isSingleDevice; //!< True if directed to a specific outgoing NetDevice
|
||||
uint32_t m_device; //!< Outgoing NetDevice index
|
||||
|
||||
@@ -204,11 +204,11 @@ class TestSimulator(unittest.TestCase):
|
||||
""")
|
||||
|
||||
sink = ns.network.Socket.CreateSocket(node, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
|
||||
sink.Bind(ns.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80)))
|
||||
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80).ConvertTo())
|
||||
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.addressFromInetSocketAddress(ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80)))
|
||||
source.SendTo(ns.network.Packet(19), 0, ns.network.InetSocketAddress(ns.network.Ipv4Address("127.0.0.1"), 80).ConvertTo())
|
||||
|
||||
ns.Simulator.Run()
|
||||
self.assertTrue(self._received_packet is not None)
|
||||
|
||||
Reference in New Issue
Block a user