Add RIB list display

Change-Id: I13a7fa26022e48bbc7f3e3720c1db19abe79d0b5
diff --git a/src/main.cpp b/src/main.cpp
index 67fdb63..8ea5931 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -24,12 +24,14 @@
 
 #include "forwarder-status.hpp"
 #include "fib-status.hpp"
+#include "rib-status.hpp"
 #include "tray-menu.hpp"
 
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/name.hpp>
 #include <ndn-cxx/util/scheduler.hpp>
 #include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
+#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
 
@@ -52,6 +54,7 @@
 
     context->setContextProperty("forwarderModel", &m_forwarderStatusModel);
     context->setContextProperty("fibModel", &m_fibModel);
+    context->setContextProperty("ribModel", &m_ribModel);
     context->setContextProperty("trayModel", &m_tray);
 
     m_engine.load((QUrl("qrc:/main.qml")));
@@ -101,6 +104,9 @@
     m_controller.fetch<ndn::nfd::FibDataset>(bind(&Ncc::onFibStatusRetrieved, this, _1),
                                              bind(&Ncc::onStatusTimeout, this));
 
+    m_controller.fetch<ndn::nfd::RibDataset>(bind(&Ncc::onRibStatusRetrieved, this, _1),
+                                             bind(&Ncc::onStatusTimeout, this));
+
     m_scheduler.scheduleEvent(time::seconds(6), bind(&Ncc::requestNfdStatus, this));
   }
 
@@ -118,6 +124,12 @@
   }
 
   void
+  onRibStatusRetrieved(const std::vector<nfd::RibEntry>& status)
+  {
+    emit m_ribModel.onDataReceived(status);
+  }
+
+  void
   onStatusTimeout()
   {
     emit m_tray.nfdActivityUpdate(false);
@@ -158,6 +170,7 @@
 
   ForwarderStatusModel m_forwarderStatusModel;
   FibStatusModel m_fibModel;
+  RibStatusModel m_ribModel;
   ncc::TrayMenu m_tray;
 };
 
@@ -166,6 +179,7 @@
 Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
 Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
 Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>)
+Q_DECLARE_METATYPE(std::vector<ndn::nfd::RibEntry>)
 
 int
 main(int argc, char *argv[])
@@ -173,6 +187,7 @@
   qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
   qRegisterMetaType<ndn::nfd::ForwarderStatus>();
   qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>();
+  qRegisterMetaType<std::vector<ndn::nfd::RibEntry>>();
 
   QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
   QApplication app(argc, argv);
diff --git a/src/main.qml b/src/main.qml
index b9dc890..0fcfcfb 100644
--- a/src/main.qml
+++ b/src/main.qml
@@ -154,6 +154,52 @@
             }
         }
         Tab {
+            title: "RIB"
+            TableView {
+                anchors.fill: parent
+                anchors.topMargin: 20
+                anchors.bottomMargin: 20
+                anchors.leftMargin: 20
+                anchors.rightMargin: 20
+                model: ribModel
+                TableViewColumn{
+                    role: "prefix"
+                    title: "NDN prefix"
+                    width: 200
+                }
+                TableViewColumn{
+                    role: "faceId"
+                    title: "Face ID"
+                    width: 65
+                }
+                TableViewColumn{
+                    role: "origin"
+                    title: "Origin"
+                    width: 50
+                }
+                TableViewColumn{
+                    role: "cost"
+                    title: "Cost"
+                    width: 50
+                }
+                TableViewColumn{
+                    role: "childinherit"
+                    title: "ChildInherit"
+                    width: 90
+                }
+                TableViewColumn{
+                    role: "ribcapture"
+                    title: "RibCapture"
+                    width: 90
+                }
+                TableViewColumn{
+                    role: "expiresin"
+                    title: "Expires in"
+                    width: 90
+                }
+            }
+        }
+        Tab {
             title: "Auto-config status"
             TextArea {
                 id: ndnAutoConfigTextId
diff --git a/src/rib-status.cpp b/src/rib-status.cpp
new file mode 100644
index 0000000..2b80773
--- /dev/null
+++ b/src/rib-status.cpp
@@ -0,0 +1,132 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017, Regents of the University of California,
+ *
+ * This file is part of NFD Control Center.  See AUTHORS.md for complete list of NFD
+ * authors and contributors.
+ *
+ * NFD Control Center is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD Control Center 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 NFD
+ * Control Center, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "rib-status.hpp"
+#include "rib-status.moc"
+
+namespace ndn {
+
+RibStatusModel::RibStatusModel(QObject* parent/* = 0*/)
+  : QAbstractListModel(parent)
+{
+  connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::RibEntry>)), this,
+          SLOT(updateStatus(std::vector<ndn::nfd::RibEntry>)),
+          Qt::QueuedConnection);
+}
+
+int
+RibStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
+{
+  return m_items.count();
+}
+
+void
+RibStatusModel::addItem(const RibStatusItem& item)
+{
+  beginInsertRows(QModelIndex(), rowCount(), rowCount());
+  m_items << item;
+  endInsertRows();
+}
+
+QVariant
+RibStatusModel::data(const QModelIndex& index, int role) const
+{
+  if (index.row() < 0 || index.row() >= m_items.count()) {
+    return QVariant();
+  }
+
+  const RibStatusItem& item = m_items.at(index.row());
+  if (role == PrefixRole) {
+    return item.prefix();
+  }
+  else if (role == FaceIdRole) {
+    return static_cast<uint>(item.faceId());
+  }
+  else if (role == OriginRole) {
+    return static_cast<uint>(item.origin());
+  }
+  else if (role == CostRole) {
+    return static_cast<uint>(item.cost());
+  }
+  else if (role == ChildRole) {
+    return item.child();
+  }
+  else if (role == RibCapRole) {
+    return item.ribcap();
+  }
+  else if (role == ExpRole) {
+    return item.exp();
+  }
+
+  return QVariant();
+}
+
+QHash<int, QByteArray>
+RibStatusModel::roleNames() const
+{
+  QHash<int, QByteArray> roles;
+  roles[PrefixRole] = "prefix";
+  roles[FaceIdRole] = "faceId";
+  roles[OriginRole] = "origin";
+  roles[CostRole] = "cost";
+  roles[ChildRole] = "childinherit";
+  roles[RibCapRole] = "ribcapture";
+  roles[ExpRole] = "expiresin";
+  return roles;
+}
+
+void
+RibStatusModel::clear()
+{
+  beginResetModel();
+  m_items.clear();
+  endResetModel();
+}
+
+void
+RibStatusModel::updateStatus(std::vector<ndn::nfd::RibEntry> status)
+{
+  beginResetModel();
+  m_items.clear();
+  for (auto const& ribEntry : status) {
+    bool isSamePrefix = false;
+    for (auto const& route : ribEntry.getRoutes()) {
+      QString expirationPeriod = route.hasInfiniteExpirationPeriod() ? QString("Never") :
+                                 QString::fromStdString(std::to_string(route.getExpirationPeriod().count()));
+      if (!isSamePrefix) {
+        addItem(RibStatusItem(QString::fromStdString(ribEntry.getName().toUri()), route.getFaceId(), route.getOrigin(),
+                              route.getCost(), route.isChildInherit(), route.isRibCapture(), expirationPeriod));
+        isSamePrefix = true;
+      }
+      else {
+        addItem(RibStatusItem(QString(""), route.getFaceId(), route.getOrigin(), route.getCost(), route.isChildInherit(),
+                              route.isRibCapture(), expirationPeriod));
+      }
+    }
+  }
+  endResetModel();
+}
+
+void
+RibStatusModel::onTimeout()
+{
+  std::cerr << "Request timed out (should not really happen)" << std::endl;
+}
+
+} // namespace ndn
diff --git a/src/rib-status.hpp b/src/rib-status.hpp
new file mode 100644
index 0000000..4873825
--- /dev/null
+++ b/src/rib-status.hpp
@@ -0,0 +1,148 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017, Regents of the University of California,
+ *
+ * This file is part of NFD Control Center.  See AUTHORS.md for complete list of NFD
+ * authors and contributors.
+ *
+ * NFD Control Center is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD Control Center 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 NFD
+ * Control Center, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NCC_RIB_STATUS_HPP
+#define NCC_RIB_STATUS_HPP
+
+#include <QtCore/QAbstractListModel>
+#include <QtCore/QStringList>
+
+#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
+
+namespace ndn {
+
+class RibStatusItem
+{
+public:
+  RibStatusItem(const QString& prefix, uint64_t faceId, uint64_t origin, uint64_t cost, bool child, bool ribcap,
+                const QString& exp)
+    : m_prefix(prefix)
+    , m_faceId(faceId)
+    , m_origin(origin)
+    , m_cost(cost)
+    , m_child(child)
+    , m_ribcap(ribcap)
+    , m_exp(exp)
+  {
+  }
+
+  const QString&
+  prefix() const
+  {
+    return m_prefix;
+  }
+
+  uint64_t
+  faceId() const
+  {
+    return m_faceId;
+  }
+
+  uint64_t
+  origin() const
+  {
+    return m_origin;
+  }
+
+  uint64_t
+  cost() const
+  {
+    return m_cost;
+  }
+
+  bool
+  child() const
+  {
+    return m_child;
+  }
+
+  bool
+  ribcap() const
+  {
+    return m_ribcap;
+  }
+
+  const QString&
+  exp() const
+  {
+    return m_exp;
+  }
+
+private:
+  QString m_prefix;
+  uint64_t m_faceId;
+  uint64_t m_origin;
+  uint64_t m_cost;
+  bool m_child;
+  bool m_ribcap;
+  QString m_exp;
+};
+
+class RibStatusModel : public QAbstractListModel
+{
+  Q_OBJECT
+
+signals:
+  void
+  onDataReceived(std::vector<ndn::nfd::RibEntry> status);
+
+public:
+  enum RibStatusRoles {
+    PrefixRole = Qt::UserRole + 1,
+    FaceIdRole,
+    OriginRole,
+    CostRole,
+    ChildRole,
+    RibCapRole,
+    ExpRole
+  };
+
+  explicit
+  RibStatusModel(QObject* parent = 0);
+
+  int
+  rowCount(const QModelIndex& parent = QModelIndex()) const;
+
+  void
+  addItem(const RibStatusItem& item);
+
+  QVariant
+  data(const QModelIndex& index, int role) const;
+
+  QHash<int, QByteArray>
+  roleNames() const;
+
+  void
+  clear();
+
+  void
+  onTimeout();
+
+private slots:
+
+  void
+  updateStatus(std::vector<ndn::nfd::RibEntry> status);
+
+private:
+  QList<RibStatusItem> m_items;
+};
+
+} // namespace ndn
+
+#endif // NCC_RIB_STATUS_HPP
diff --git a/wscript b/wscript
index 2a59b8e..726fee6 100644
--- a/wscript
+++ b/wscript
@@ -42,7 +42,7 @@
         features=['qt5', 'cxxprogram', 'cxx'],
         includes = ". src",
         use = "NDN_CXX BOOST QT5CORE QT5DBUS QT5QML QT5WIDGETS",
-        moc = "src/tray-menu.hpp src/key-tree-model.hpp src/key-viewer-dialog.hpp src/cert-tree-model.hpp src/fib-status.hpp src/forwarder-status.hpp",
+        moc = "src/tray-menu.hpp src/key-tree-model.hpp src/key-viewer-dialog.hpp src/cert-tree-model.hpp src/fib-status.hpp src/rib-status.hpp src/forwarder-status.hpp",
         source = bld.path.ant_glob(['src/*.cpp', 'src/**/*.qrc', 'src/**/*.ui', 'src/**/*.qrc'], excl=['src/osx-*']),
         )