examples: adding debugged implementation of a CustomApp, along with an example using it
diff --git a/examples/custom-apps/custom-app.cc b/examples/custom-apps/custom-app.cc
new file mode 100644
index 0000000..06ec139
--- /dev/null
+++ b/examples/custom-apps/custom-app.cc
@@ -0,0 +1,158 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+// custom-app.cc
+
+#include "custom-app.h"
+#include "ns3/ptr.h"
+#include "ns3/log.h"
+#include "ns3/simulator.h"
+#include "ns3/packet.h"
+
+#include "ns3/ndn-app-face.h"
+#include "ns3/ndn-interest.h"
+#include "ns3/ndn-content-object.h"
+
+#include "ns3/ndn-fib.h"
+#include "ns3/random-variable.h"
+
+
+NS_LOG_COMPONENT_DEFINE ("CustomApp");
+
+namespace ns3 {
+
+NS_OBJECT_ENSURE_REGISTERED (CustomApp);
+
+// register NS-3 type
+TypeId
+CustomApp::GetTypeId ()
+{
+ static TypeId tid = TypeId ("CustomApp")
+ .SetParent<ndn::App> ()
+ .AddConstructor<CustomApp> ()
+ ;
+ return tid;
+}
+
+// Processing upon start of the application
+void
+CustomApp::StartApplication ()
+{
+ // initialize ndn::App
+ ndn::App::StartApplication ();
+
+ // Create a name components object for name ``/prefix/sub``
+ Ptr<ndn::NameComponents> prefix = Create<ndn::NameComponents> (); // now prefix contains ``/``
+ prefix->Add ("prefix"); // now prefix contains ``/prefix``
+ prefix->Add ("sub"); // now prefix contains ``/prefix/sub``
+
+ /////////////////////////////////////////////////////////////////////////////
+ // Creating FIB entry that ensures that we will receive incoming Interests //
+ /////////////////////////////////////////////////////////////////////////////
+
+ // Get FIB object
+ Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> ();
+
+ // Add entry to FIB
+ // Note that ``m_face`` is cretaed by ndn::App
+ Ptr<ndn::fib::Entry> fibEntry = fib->Add (*prefix, m_face, 0);
+
+ Simulator::Schedule (Seconds (1.0), &CustomApp::SendInterest, this);
+}
+
+// Processing when application is stopped
+void
+CustomApp::StopApplication ()
+{
+ // cleanup ndn::App
+ ndn::App::StopApplication ();
+}
+
+void
+CustomApp::SendInterest ()
+{
+ /////////////////////////////////////
+ // Sending one Interest packet out //
+ /////////////////////////////////////
+
+ Ptr<ndn::NameComponents> prefix = Create<ndn::NameComponents> ("/prefix/sub"); // another way to create name
+
+ // Create and configure ndn::InterestHeader
+ ndn::InterestHeader interestHeader;
+ UniformVariable rand (0,std::numeric_limits<uint32_t>::max ());
+ interestHeader.SetNonce (rand.GetValue ());
+ interestHeader.SetName (prefix);
+ interestHeader.SetInterestLifetime (Seconds (1.0));
+
+ // Create packet and add ndn::InterestHeader
+ Ptr<Packet> packet = Create<Packet> ();
+ packet->AddHeader (interestHeader);
+
+ NS_LOG_DEBUG ("Sending Interest packet for " << *prefix);
+
+ // Forward packet to lower (network) layer
+ m_protocolHandler (packet);
+
+ // Call trace (for logging purposes)
+ m_transmittedInterests (&interestHeader, this, m_face);
+}
+
+// Callback that will be called when Interest arrives
+void
+CustomApp::OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> origPacket)
+{
+ NS_LOG_DEBUG ("Received Interest packet for " << interest->GetName ());
+
+ // Create and configure ndn::ContentObjectHeader and ndn::ContentObjectTail
+ // (header is added in front of the packet, tail is added at the end of the packet)
+
+ // Note that Interests send out by the app will not be sent back to the app !
+
+ ndn::ContentObjectHeader data;
+ data.SetName (Create<ndn::NameComponents> (interest->GetName ())); // data will have the same name as Interests
+
+ ndn::ContentObjectTail trailer; // doesn't require any configuration
+
+ // Create packet and add header and trailer
+ Ptr<Packet> packet = Create<Packet> (1024);
+ packet->AddHeader (data);
+ packet->AddTrailer (trailer);
+
+ NS_LOG_DEBUG ("Sending ContentObject packet for " << data.GetName ());
+
+ // Forward packet to lower (network) layer
+ m_protocolHandler (packet);
+
+ // Call trace (for logging purposes)
+ m_transmittedContentObjects (&data, packet, this, m_face);
+}
+
+// Callback that will be called when Data arrives
+void
+CustomApp::OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject,
+ Ptr<Packet> payload)
+{
+ NS_LOG_DEBUG ("Receiving ContentObject packet for " << contentObject->GetName ());
+
+ std::cout << "DATA received for name " << contentObject->GetName () << std::endl;
+}
+
+
+} // namespace ns3
diff --git a/examples/custom-apps/custom-app.h b/examples/custom-apps/custom-app.h
new file mode 100644
index 0000000..1ae1f4f
--- /dev/null
+++ b/examples/custom-apps/custom-app.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011-2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+// custom-app.h
+
+#ifndef CUSTOM_APP_H_
+#define CUSTOM_APP_H_
+
+#include "ns3/ndn-app.h"
+
+namespace ns3 {
+
+/**
+ * @brief A simple custom application
+ *
+ * This applications demonstrates how to send Interests and respond with ContentObjects to incoming interests
+ *
+ * When application starts it "sets interest filter" (install FIB entry) for /prefix/sub, as well as
+ * sends Interest for this prefix
+ *
+ * When an Interest is received, it is replied with a ContentObject with 1024-byte fake payload
+ */
+class CustomApp : public ndn::App
+{
+public:
+ // register NS-3 type "CustomApp"
+ static TypeId
+ GetTypeId ();
+
+ // (overridden from ndn::App) Processing upon start of the application
+ virtual void
+ StartApplication ();
+
+ // (overridden from ndn::App) Processing when application is stopped
+ virtual void
+ StopApplication ();
+
+ // (overridden from ndn::App) Callback that will be called when Interest arrives
+ virtual void
+ OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> origPacket);
+
+ // (overridden from ndn::App) Callback that will be called when Data arrives
+ virtual void
+ OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject,
+ Ptr<Packet> payload);
+
+private:
+ void
+ SendInterest ();
+};
+
+} // namespace ns3
+
+#endif // CUSTOM_APP_H_
diff --git a/examples/ndn-simple-with-custom-app.cc b/examples/ndn-simple-with-custom-app.cc
new file mode 100644
index 0000000..2c9d1b9
--- /dev/null
+++ b/examples/ndn-simple-with-custom-app.cc
@@ -0,0 +1,69 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+// ndn-simple-with-custom-app.cc
+
+#include "ns3/core-module.h"
+#include "ns3/network-module.h"
+#include "ns3/ndnSIM-module.h"
+
+using namespace ns3;
+
+/**
+ * This scenario simulates a one-node two-app scenario:
+ *
+ * +------+ <-----> (CustomApp1)
+ * | Node |
+ * +------+ <-----> (CustomApp2)
+ *
+ * NS_LOG=CustomApp ./waf --run=ndn-simple-with-custom-app
+ */
+
+int
+main (int argc, char *argv[])
+{
+ // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
+ CommandLine cmd;
+ cmd.Parse (argc, argv);
+
+ // Creating nodes
+ Ptr<Node> node = CreateObject<Node> ();
+
+ // Install CCNx stack on all nodes
+ ndn::StackHelper ccnxHelper;
+ ccnxHelper.InstallAll ();
+
+ // Installing applications
+
+ // Consumer
+ ndn::AppHelper consumerHelper ("CustomApp");
+ ApplicationContainer app1 = consumerHelper.Install (node);
+ ApplicationContainer app2 = consumerHelper.Install (node);
+
+ app1.Start (Seconds (1.0)); // will send out Interest, which nobody will receive (Interests generated by an app will not got back to the app)
+ app2.Start (Seconds (2.0)); // will send out an Interests, which will be received and satisfied by app1
+
+ Simulator::Stop (Seconds (3.0));
+
+ Simulator::Run ();
+ Simulator::Destroy ();
+
+ return 0;
+}
diff --git a/examples/wscript b/examples/wscript
index 9a54aba..cbd026c 100644
--- a/examples/wscript
+++ b/examples/wscript
@@ -7,6 +7,10 @@
obj = bld.create_ns3_program('ndn-grid', ['ndnSIM', 'point-to-point-layout'])
obj.source = 'ndn-grid.cc'
+ obj = bld.create_ns3_program('ndn-simple-with-custom-app', ['ndnSIM'])
+ obj.source = ['ndn-simple-with-custom-app.cc',
+ 'custom-apps/custom-app.cc']
+
if 'topology' in bld.env['NDN_plugins']:
obj = bld.create_ns3_program('ndn-grid-topo-plugin', ['ndnSIM'])
obj.source = 'ndn-grid-topo-plugin.cc'