From 19fb59dda708d65db6a2a101f632010e3725af0f Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Tue, 20 Jan 2009 18:29:06 -0800 Subject: [PATCH] make simple version of Names:Add even simpler. Use that in examples/names.cc --- examples/names.cc | 10 +++--- src/core/object-names.cc | 70 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/examples/names.cc b/examples/names.cc index 07d7b7367..e94f6eafc 100644 --- a/examples/names.cc +++ b/examples/names.cc @@ -56,8 +56,8 @@ main (int argc, char *argv[]) // as the destination, so these will go into the name system as "/Names/client" // and "/Names/server". // - Names::Add ("/Names", "client", n.Get (0)); - Names::Add ("/Names", "server", n.Get (1)); + Names::Add ("/Names/client", n.Get (0)); + Names::Add ("/Names/server", n.Get (1)); InternetStackHelper internet; internet.Install (n); @@ -72,12 +72,12 @@ main (int argc, char *argv[]) // Add some human readable names for the devices we'll be interested in. // We add the names to the name space "under" the nodes we created above. // This has the effect of making "/Names/client/eth0" and "/Names/server/eth0" - // Note that the first parameter must reference a previously named object, + // Note that the first part of the path must reference a previously named object, // and we have, in fact, already named objects "/Names/client" and // "/Names/server" // - Names::Add ("/Names/client", "eth0", d.Get (0)); - Names::Add ("/Names/server", "eth0", d.Get (1)); + Names::Add ("/Names/client/eth0", d.Get (0)); + Names::Add ("/Names/server/eth0", d.Get (1)); Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); diff --git a/src/core/object-names.cc b/src/core/object-names.cc index 57e049b4e..b27cb61c4 100644 --- a/src/core/object-names.cc +++ b/src/core/object-names.cc @@ -161,7 +161,55 @@ NamesPriv::~NamesPriv () bool NamesPriv::Add (std::string name, Ptr object) { - return Add (Ptr (0, false), name, object); + NS_LOG_FUNCTION (name << object); + // + // This is the simple, easy to use version of Add, so we want it to be flexible. + // + // If we are provided a name that doesn't begin with "/Names", we assume + // that the caller has given us a shortname that she wants added to the root + // namespace. This results in a call to the "real" Add with context set to + // zero, indicating what we want to do. + // + // If we are given a name that begins with "/Names/" we assume that this is a + // fullname to the object we want to create. We split the fullname into a + // context string and and a final segment and then call the "Real" Add. + // + std::string namespaceName = "/Names"; + std::string::size_type offset = name.find (namespaceName); + if (offset == 0) + { + // + // This must be a fully qualified longname. All fully qualified names begin + // with "/Names". We have to split off the final segment which will become + // the shortname of the object. + // + std::string::size_type i = name.rfind ("/"); + NS_ASSERT_MSG (i != std::string::npos, "NamesPriv::Add(): Internal error. Can't find '/' in name"); + + // + // The slash we found cannot be the slash at the start of the namespaceName. + // This would indicate there is no shortname in the path at all. + // + NS_ASSERT_MSG (i != 0, "NamesPriv::Add(): Can't find a shortname in the name string"); + + // + // We now know where the context string starts and ends, and where the + // shortname starts and ends. All we have to do is to call our available + // function for creating addubg a shortname under a context string. + // + return Add (name.substr (0, i), name.substr (i + 1), object); + } + else + { + // + // This must be a shortname. Shortnames can't have ANY '/' characters in + // them since they are interpreted as a final segment of a fullname. A + // shortname in this context means creating a name in the root namespace. + // We indicate this by passing a zero context to the "real" add. + // + NS_ASSERT_MSG (offset == std::string::npos, "NamesPriv::Add(): Unexpected '/' in shortname"); + return Add (Ptr (0, false), name, object); + } } bool @@ -204,7 +252,7 @@ NamesPriv::Add (std::string context, std::string name, Ptr object) { if (context == "/Names") { - return Add (name, object); + return Add (Ptr (0, false), name, object); } return Add (FindObjectFromFullName (context), name, object); } @@ -589,11 +637,11 @@ NamesTest::RunTests (void) NS_TEST_ASSERT_EQUAL (foundObject, serverEth0); // - // We also have some syntactical sugary methods, so make sure they do what + // We also have some syntactically sugary methods, so make sure they do what // they should as well. // Ptr bridge = CreateObject (); - result = Names::Add ("/Names", "Bridge", client); + result = Names::Add ("/Names", "Bridge", bridge); NS_TEST_ASSERT_EQUAL (result, true); Ptr bridgeEth0 = CreateObject (); @@ -606,6 +654,20 @@ NamesTest::RunTests (void) foundObject = Names::Find ("/Names/Bridge/eth0"); NS_TEST_ASSERT_EQUAL (foundObject, bridgeEth0); + Ptr wireless = CreateObject (); + result = Names::Add ("/Names/Wireless", wireless); + NS_TEST_ASSERT_EQUAL (result, true); + + Ptr wirelessAth0 = CreateObject (); + result = Names::Add ("/Names/Wireless/ath0", wirelessAth0); + NS_TEST_ASSERT_EQUAL (result, true); + + foundObject = Names::Find ("/Names/Wireless"); + NS_TEST_ASSERT_EQUAL (foundObject, wireless); + + foundObject = Names::Find ("/Names/Wireless/ath0"); + NS_TEST_ASSERT_EQUAL (foundObject, wirelessAth0); + // // Run the simulator and destroy it to get the Destroy method called on the // private implementation object. We depend on seeing a valgrind-clean run of