Add FIB list display

Change-Id: I175d17e095381f7bca1840da44e15ce9591fa675
diff --git a/src/fib-status.cpp b/src/fib-status.cpp
index c748439..3bfbb7d 100644
--- a/src/fib-status.cpp
+++ b/src/fib-status.cpp
@@ -20,30 +20,24 @@
 #include "fib-status.hpp"
 #include "fib-status.moc"
 
-#include <ndn-cxx/face.hpp>
-#include <ndn-cxx/name.hpp>
-#include <ndn-cxx/interest.hpp>
-#include <ndn-cxx/encoding/buffer-stream.hpp>
-#include <ndn-cxx/management/nfd-fib-entry.hpp>
-#include <ndn-cxx/management/nfd-face-status.hpp>
-#include <ndn-cxx/management/nfd-forwarder-status.hpp>
-
 namespace ndn {
 
-FibStatusModel::FibStatusModel(Face& face, QObject *parent/* = 0*/)
+FibStatusModel::FibStatusModel(QObject* parent/* = 0*/)
   : QAbstractListModel(parent)
-  // , m_face(face)
 {
+  connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::FibEntry>)), this,
+          SLOT(updateStatus(std::vector<ndn::nfd::FibEntry>)),
+          Qt::QueuedConnection);
 }
 
 int
-FibStatusModel::rowCount(const QModelIndex &parent/* = QModelIndex()*/) const
+FibStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
 {
   return m_items.count();
 }
 
 void
-FibStatusModel::addItem(const FibStatusItem &item)
+FibStatusModel::addItem(const FibStatusItem& item)
 {
   beginInsertRows(QModelIndex(), rowCount(), rowCount());
   m_items << item;
@@ -51,18 +45,20 @@
 }
 
 QVariant
-FibStatusModel::data(const QModelIndex & index, int role) const
+FibStatusModel::data(const QModelIndex& index, int role) const
 {
   if (index.row() < 0 || index.row() >= m_items.count()) {
     return QVariant();
   }
 
-  const FibStatusItem &item = m_items.at(index.row());
+  const FibStatusItem& item = m_items.at(index.row());
   if (role == PrefixRole) {
     return item.prefix();
-  } else if (role == FaceIdRole) {
+  }
+  else if (role == FaceIdRole) {
     return static_cast<uint>(item.faceId());
-  } else if (role == CostRole) {
+  }
+  else if (role == CostRole) {
     return static_cast<uint>(item.cost());
   }
 
@@ -87,80 +83,31 @@
   endResetModel();
 }
 
-Q_INVOKABLE void
-FibStatusModel::fetchFibInformation()
-{
-  // m_buffer = make_shared<OBufferStream>();
-
-  // Interest interest("/localhost/nfd/fib/list");
-  // interest.setChildSelector(1);
-  // interest.setMustBeFresh(true);
-  // m_face.expressInterest(interest,
-  //                        bind(&FibStatusModel::fetchSegments, this, _2,
-  //                             &FibStatusModel::afterFetchedFibEnumerationInformation),
-  //                        bind(&FibStatusModel::onTimeout, this));
-  // try {
-  //   m_face.processEvents();
-  // } catch (Tlv::Error e) {
-  //   std::cerr << e.what() << std::endl;
-  //   clear();
-  // }
-}
-
 void
-FibStatusModel::afterFetchedFibEnumerationInformation()
+FibStatusModel::updateStatus(std::vector<ndn::nfd::FibEntry> status)
 {
-//   beginResetModel();
-//   m_items.clear();
-//   ConstBufferPtr buf = m_buffer->buf();
-
-//   Block block;
-//   size_t offset = 0;
-//   while (offset < buf->size()) {
-//     bool ok = Block::fromBuffer(buf, offset, block);
-//     if (!ok) {
-//       std::cerr << "ERROR: cannot decode FibEntry TLV" << std::endl;
-//       break;
-//     }
-//     offset += block.size();
-
-//     nfd::FibEntry fibEntry(block);
-
-//     for (std::list<nfd::NextHopRecord>::const_iterator
-//            nextHop = fibEntry.getNextHopRecords().begin();
-//          nextHop != fibEntry.getNextHopRecords().end();
-//          ++nextHop) {
-//       addItem(FibStatusItem(QString::fromStdString(fibEntry.getPrefix().toUri()),
-//                             nextHop->getFaceId(), nextHop->getCost()));
-//     }
-//   }
-//   endResetModel();
-}
-
-
-void
-FibStatusModel::fetchSegments(const Data& data, void (FibStatusModel::*onDone)())
-{
-  // m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
-  //                 data.getContent().value_size());
-
-  // uint64_t currentSegment = data.getName().get(-1).toSegment();
-
-  // const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
-  // if (finalBlockId.empty() ||
-  //     finalBlockId.toSegment() > currentSegment) {
-  //   m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
-  //                          bind(&FibStatusModel::fetchSegments, this, _2, onDone),
-  //                          bind(&FibStatusModel::onTimeout, this));
-  // } else {
-  //   return (this->*onDone)();
-  // }
+  beginResetModel();
+  m_items.clear();
+  for (auto const& fibEntry : status) {
+    bool isSamePrefix = false;
+    for (auto const& nextHop : fibEntry.getNextHopRecords()) {
+      if (!isSamePrefix) {
+        addItem(FibStatusItem(QString::fromStdString(fibEntry.getPrefix().toUri()),
+                              nextHop.getFaceId(), nextHop.getCost()));
+        isSamePrefix = true;
+      }
+      else {
+        addItem(FibStatusItem(QString(""), nextHop.getFaceId(), nextHop.getCost()));
+      }
+    }
+  }
+  endResetModel();
 }
 
 void
 FibStatusModel::onTimeout()
 {
-  std::cerr << "Request timed out" << std::endl;
+  std::cerr << "Request timed out (should not really happen)" << std::endl;
 }
 
 } // namespace ndn
diff --git a/src/fib-status.hpp b/src/fib-status.hpp
index a5d365e..145b3bd 100644
--- a/src/fib-status.hpp
+++ b/src/fib-status.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014, Regents of the University of California,
+ * 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.
@@ -23,14 +23,14 @@
 #include <QtCore/QAbstractListModel>
 #include <QtCore/QStringList>
 
+#include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
+
 namespace ndn {
-class Face;
-class Data;
 
 class FibStatusItem
 {
 public:
-  FibStatusItem(const QString &prefix, uint64_t faceId, uint64_t cost)
+  FibStatusItem(const QString& prefix, uint64_t faceId, uint64_t cost)
     : m_prefix(prefix)
     , m_faceId(faceId)
     , m_cost(cost)
@@ -65,6 +65,10 @@
 {
   Q_OBJECT
 
+signals:
+  void
+  onDataReceived(std::vector<ndn::nfd::FibEntry> status);
+
 public:
   enum FibStatusRoles {
     PrefixRole = Qt::UserRole + 1,
@@ -73,16 +77,16 @@
   };
 
   explicit
-  FibStatusModel(Face& face, QObject *parent = 0);
+  FibStatusModel(QObject* parent = 0);
 
   int
-  rowCount(const QModelIndex &parent = QModelIndex()) const;
+  rowCount(const QModelIndex& parent = QModelIndex()) const;
 
   void
-  addItem(const FibStatusItem &item);
+  addItem(const FibStatusItem& item);
 
   QVariant
-  data(const QModelIndex & index, int role) const;
+  data(const QModelIndex& index, int role) const;
 
   QHash<int, QByteArray>
   roleNames() const;
@@ -90,22 +94,16 @@
   void
   clear();
 
-  Q_INVOKABLE void
-  fetchFibInformation();
-
-  void
-  afterFetchedFibEnumerationInformation();
-
-  void
-  fetchSegments(const Data& data, void (FibStatusModel::*onDone)());
-
   void
   onTimeout();
 
+private slots:
+
+  void
+  updateStatus(std::vector<ndn::nfd::FibEntry> status);
+
 private:
-  // Face& m_face;
   QList<FibStatusItem> m_items;
-  // shared_ptr<OBufferStream> m_buffer;
 };
 
 } // namespace ndn
diff --git a/src/main.cpp b/src/main.cpp
index 3f8d238..67fdb63 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -46,7 +46,6 @@
     , m_face(nullptr, m_keyChain)
     , m_controller(m_face, m_keyChain)
     , m_scheduler(m_face.getIoService())
-    , m_fibModel(m_face)
     , m_tray(m_engine.rootContext(), m_face)
   {
     QQmlContext* context = m_engine.rootContext();
@@ -77,7 +76,7 @@
             m_face.processEvents(time::milliseconds::zero(), true);
           }
         }
-        catch (const std::exception&e) {
+        catch (const std::exception& e) {
           emit m_tray.nfdActivityUpdate(false);
           m_face.shutdown();
 #ifdef BOOST_THREAD_USES_CHRONO
@@ -115,6 +114,7 @@
       }
     }
     emit m_tray.connectivityUpdate(isConnectedToHub);
+    emit m_fibModel.onDataReceived(status);
   }
 
   void
@@ -165,12 +165,14 @@
 
 Q_DECLARE_METATYPE(ndn::shared_ptr<const ndn::Data>)
 Q_DECLARE_METATYPE(ndn::nfd::ForwarderStatus)
+Q_DECLARE_METATYPE(std::vector<ndn::nfd::FibEntry>)
 
 int
 main(int argc, char *argv[])
 {
   qRegisterMetaType<ndn::shared_ptr<const ndn::Data>>();
   qRegisterMetaType<ndn::nfd::ForwarderStatus>();
+  qRegisterMetaType<std::vector<ndn::nfd::FibEntry>>();
 
   QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
   QApplication app(argc, argv);
diff --git a/src/main.qml b/src/main.qml
index eaa3b3a..b9dc890 100644
--- a/src/main.qml
+++ b/src/main.qml
@@ -106,32 +106,6 @@
                 }
             }
         }
-        // Tab {
-        //     title: "FIB status"
-        //     TableView {
-        //         anchors.fill: parent
-        //         anchors.topMargin: 20
-        //         anchors.bottomMargin: 20
-        //         anchors.leftMargin: 20
-        //         anchors.rightMargin: 20
-        //         TableViewColumn{
-        //             role: "prefix"
-        //             title: "NDN prefix"
-        //             width: 300
-        //         }
-        //         TableViewColumn{
-        //             role: "faceId"
-        //             title: "Face ID"
-        //             width: 50
-        //         }
-        //         TableViewColumn{
-        //             role: "cost"
-        //             title: "Cost"
-        //             width: 50
-        //         }
-        //         model: fibModel
-        //     }
-        // }
         Tab {
             title: "Forwarder status"
             TableView {
@@ -154,6 +128,32 @@
             }
         }
         Tab {
+            title: "FIB"
+            TableView {
+                anchors.fill: parent
+                anchors.topMargin: 20
+                anchors.bottomMargin: 20
+                anchors.leftMargin: 20
+                anchors.rightMargin: 20
+                model: fibModel
+                TableViewColumn{
+                    role: "prefix"
+                    title: "NDN prefix"
+                    width: 300
+                }
+                TableViewColumn{
+                    role: "faceId"
+                    title: "Face ID"
+                    width: 100
+                }
+                TableViewColumn{
+                    role: "cost"
+                    title: "Cost"
+                    width: 100
+                }
+            }
+        }
+        Tab {
             title: "Auto-config status"
             TextArea {
                 id: ndnAutoConfigTextId