Modified (and simplified) NS-3 example
diff --git a/examples/sync-example.cc b/examples/sync-example.cc
index 54f715f..14485d5 100644
--- a/examples/sync-example.cc
+++ b/examples/sync-example.cc
@@ -6,20 +6,21 @@
#include <ns3/point-to-point-module.h>
#include "sync-logic.h"
+#include "sync-logic-helper.h"
using namespace ns3;
using namespace Sync;
NS_LOG_COMPONENT_DEFINE ("SyncExample");
-void OnUpdate (Ptr<Node> node, const std::string &prefix, const SeqNo &newSeq, const SeqNo &/*oldSeq*/)
+void OnUpdate (const std::string &prefix, const SeqNo &newSeq, const SeqNo &/*oldSeq*/)
{
- NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << node->GetId () << ", prefix: " << prefix << ", seqNo: " << newSeq);
+ NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << Simulator::GetContext () << ", prefix: " << prefix << ", seqNo: " << newSeq);
}
-void OnRemove (Ptr<Node> node, const std::string &prefix)
+void OnRemove (const std::string &prefix)
{
- NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << node->GetId () << ", prefix: "<< prefix);
+ NS_LOG_LOGIC (Simulator::Now ().ToDouble (Time::S) <<"s\tNode: " << Simulator::GetContext () << ", prefix: "<< prefix);
}
int
@@ -64,28 +65,20 @@
ccnxHelper.AddRoute ("1", "/sync", 1, 0);
ccnxHelper.AddRoute ("2", "/sync", 0, 0);
- ApplicationContainer apps;
- Ptr<Application> app;
- app = Create<SyncLogic> ("/sync",
- boost::bind (OnUpdate, nodes.Get (0), _1, _2, _3),
- boost::bind (OnRemove, nodes.Get (0), _1));
-
- nodes.Get (0)->AddApplication (app);
- apps.Add (app);
-
- app = Create<SyncLogic> ("/sync",
- boost::bind (OnUpdate, nodes.Get (1), _1, _2, _3),
- boost::bind (OnRemove, nodes.Get (1), _1));
-
- nodes.Get (1)->AddApplication (app);
- apps.Add (app);
-
+ SyncLogicHelper logicHelper;
+ logicHelper.SetPrefix ("/sync");
+ logicHelper.SetCallbacks (OnUpdate, OnRemove);
+ ApplicationContainer apps = logicHelper.Install (NodeContainer (nodes.Get (0), nodes.Get (1)));
+
// one data
Simulator::ScheduleWithContext (0, Seconds (0.5), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (0)), "/0", 1, 1);
// two producers at the same time
Simulator::ScheduleWithContext (0, Seconds (1.001), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (0)), "/0", 1, 2);
Simulator::ScheduleWithContext (1, Seconds (1.001), &SyncLogic::addLocalNames, DynamicCast<SyncLogic> (apps.Get (1)), "/1", 1, 2);
+
+ logicHelper.Install (nodes.Get (2)).
+ Start (Seconds (2.001));
Simulator::Stop (finishTime);
Simulator::Run ();
diff --git a/ns3/sync-logic-helper.cc b/ns3/sync-logic-helper.cc
new file mode 100644
index 0000000..2425ec3
--- /dev/null
+++ b/ns3/sync-logic-helper.cc
@@ -0,0 +1,94 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 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>
+ */
+
+#include "sync-logic-helper.h"
+#include "sync-logic.h"
+
+#include "ns3/log.h"
+#include "ns3/string.h"
+#include "ns3/names.h"
+
+NS_LOG_COMPONENT_DEFINE ("SyncLogicHelper");
+
+using namespace ns3;
+
+namespace Sync
+{
+
+SyncLogicHelper::SyncLogicHelper ()
+{
+ // m_factory.SetTypeId ("Sync::SyncLogic");
+}
+
+void
+SyncLogicHelper::SetPrefix (const std::string &prefix)
+{
+ m_prefix = prefix;
+}
+
+
+void
+SyncLogicHelper::SetCallbacks (LogicUpdateCallback onUpdate, LogicRemoveCallback onRemove)
+{
+ m_onUpdate = onUpdate;
+ m_onRemove = onRemove;
+}
+
+// void
+// CcnxAppHelper::SetAttribute (std::string name, const AttributeValue &value)
+// {
+// m_factory.Set (name, value);
+// }
+
+ApplicationContainer
+SyncLogicHelper::Install (Ptr<Node> node)
+{
+ return ApplicationContainer (InstallPriv (node));
+}
+
+ApplicationContainer
+SyncLogicHelper::Install (std::string nodeName)
+{
+ Ptr<Node> node = Names::Find<Node> (nodeName);
+ return ApplicationContainer (InstallPriv (node));
+}
+
+ApplicationContainer
+SyncLogicHelper::Install (NodeContainer c)
+{
+ ApplicationContainer apps;
+ for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
+ {
+ apps.Add (InstallPriv (*i));
+ }
+
+ return apps;
+}
+
+Ptr<Application>
+SyncLogicHelper::InstallPriv (Ptr<Node> node)
+{
+ Ptr<SyncLogic> app = CreateObject<SyncLogic> ("/sync", m_onUpdate, m_onRemove);
+ node->AddApplication (app);
+
+ return app;
+}
+
+}
diff --git a/ns3/sync-logic-helper.h b/ns3/sync-logic-helper.h
new file mode 100644
index 0000000..20ad746
--- /dev/null
+++ b/ns3/sync-logic-helper.h
@@ -0,0 +1,115 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2011 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>
+ */
+
+#ifndef SYNC_LOGIC_HELPER_H
+#define SYNC_LOGIC_HELPER_H
+
+#include "ns3/object-factory.h"
+#include "ns3/attribute.h"
+#include "ns3/node-container.h"
+#include "ns3/application-container.h"
+#include "ns3/ptr.h"
+
+#include <boost/function.hpp>
+
+namespace Sync
+{
+
+class SeqNo;
+
+/**
+ * \brief A helper to make it easier to instantiate an ns3::CcnxConsumer Application
+ * on a set of nodes.
+ */
+class SyncLogicHelper
+{
+public:
+ typedef boost::function< void ( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) > LogicUpdateCallback;
+ typedef boost::function< void ( const std::string &/*prefix*/ ) > LogicRemoveCallback;
+
+ /**
+ * \brief Create an CcnxAppHelper to make it easier to work with CCNx apps
+ *
+ * \param app Class of the application
+ */
+ SyncLogicHelper ();
+
+ /**
+ * @brief Set the sync prefix
+ */
+ void
+ SetPrefix (const std::string &prefix);
+
+ /**
+ * @brief Set onUpdate and onRemove callbacks
+ */
+ void
+ SetCallbacks (LogicUpdateCallback onUpdate, LogicRemoveCallback onRemove);
+
+ /**
+ * Install an ns3::CcnxConsumer on each node of the input container
+ * configured with all the attributes set with SetAttribute.
+ *
+ * \param c NodeContainer of the set of nodes on which an CcnxConsumer
+ * will be installed.
+ * \returns Container of Ptr to the applications installed.
+ */
+ ns3::ApplicationContainer
+ Install (ns3::NodeContainer c);
+
+ /**
+ * Install an ns3::CcnxConsumer on the node configured with all the
+ * attributes set with SetAttribute.
+ *
+ * \param node The node on which an CcnxConsumer will be installed.
+ * \returns Container of Ptr to the applications installed.
+ */
+ ns3::ApplicationContainer
+ Install (ns3::Ptr<ns3::Node> node);
+
+ /**
+ * Install an ns3::CcnxConsumer on the node configured with all the
+ * attributes set with SetAttribute.
+ *
+ * \param nodeName The node on which an CcnxConsumer will be installed.
+ * \returns Container of Ptr to the applications installed.
+ */
+ ns3::ApplicationContainer
+ Install (std::string nodeName);
+
+private:
+ /**
+ * \internal
+ * Install an ns3::CcnxConsumer on the node configured with all the
+ * attributes set with SetAttribute.
+ *
+ * \param node The node on which an CcnxConsumer will be installed.
+ * \returns Ptr to the application installed.
+ */
+ ns3::Ptr<ns3::Application> InstallPriv (ns3::Ptr<ns3::Node> node);
+ std::string m_prefix; // sync prefix
+ LogicUpdateCallback m_onUpdate;
+ LogicRemoveCallback m_onRemove;
+};
+
+} // namespace Sync
+
+#endif // SYNC_LOGIC_HELPER_H
+
diff --git a/wscript b/wscript
index 2c870f9..aa7c28c 100644
--- a/wscript
+++ b/wscript
@@ -69,6 +69,7 @@
'ns3/sync-ccnx-wrapper.cc',
'ns3/sync-ns3-name-info.cc',
'ns3/sync-scheduler.cc',
+ 'ns3/sync-logic-helper.cc',
# 'model/sync-app-data-fetch.cc',
# 'model/sync-app-data-publish.cc',