blob: 3bfbb7d37b93ec4e45798e86f34f0b004b6ce343 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev37667752017-02-02 13:52:12 -08003 * Copyright (c) 2013-2017, Regents of the University of California,
taylorchuc27dd482014-05-17 20:06:49 -07004 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "fib-status.hpp"
taylorchuc27dd482014-05-17 20:06:49 -070021#include "fib-status.moc"
taylorchuc27dd482014-05-17 20:06:49 -070022
taylorchuc27dd482014-05-17 20:06:49 -070023namespace ndn {
24
Qi Zhaod3de12b2017-02-21 20:11:58 -080025FibStatusModel::FibStatusModel(QObject* parent/* = 0*/)
taylorchuc27dd482014-05-17 20:06:49 -070026 : QAbstractListModel(parent)
taylorchuc27dd482014-05-17 20:06:49 -070027{
Qi Zhaod3de12b2017-02-21 20:11:58 -080028 connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::FibEntry>)), this,
29 SLOT(updateStatus(std::vector<ndn::nfd::FibEntry>)),
30 Qt::QueuedConnection);
taylorchuc27dd482014-05-17 20:06:49 -070031}
32
33int
Qi Zhaod3de12b2017-02-21 20:11:58 -080034FibStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
taylorchuc27dd482014-05-17 20:06:49 -070035{
36 return m_items.count();
37}
38
39void
Qi Zhaod3de12b2017-02-21 20:11:58 -080040FibStatusModel::addItem(const FibStatusItem& item)
taylorchuc27dd482014-05-17 20:06:49 -070041{
42 beginInsertRows(QModelIndex(), rowCount(), rowCount());
43 m_items << item;
44 endInsertRows();
45}
46
47QVariant
Qi Zhaod3de12b2017-02-21 20:11:58 -080048FibStatusModel::data(const QModelIndex& index, int role) const
taylorchuc27dd482014-05-17 20:06:49 -070049{
50 if (index.row() < 0 || index.row() >= m_items.count()) {
51 return QVariant();
52 }
53
Qi Zhaod3de12b2017-02-21 20:11:58 -080054 const FibStatusItem& item = m_items.at(index.row());
taylorchuc27dd482014-05-17 20:06:49 -070055 if (role == PrefixRole) {
56 return item.prefix();
Qi Zhaod3de12b2017-02-21 20:11:58 -080057 }
58 else if (role == FaceIdRole) {
susmit90b71822016-03-20 13:31:23 -060059 return static_cast<uint>(item.faceId());
Qi Zhaod3de12b2017-02-21 20:11:58 -080060 }
61 else if (role == CostRole) {
susmit90b71822016-03-20 13:31:23 -060062 return static_cast<uint>(item.cost());
taylorchuc27dd482014-05-17 20:06:49 -070063 }
64
65 return QVariant();
66}
67
68QHash<int, QByteArray>
69FibStatusModel::roleNames() const
70{
71 QHash<int, QByteArray> roles;
72 roles[PrefixRole] = "prefix";
73 roles[FaceIdRole] = "faceId";
74 roles[CostRole] = "cost";
75 return roles;
76}
77
78void
79FibStatusModel::clear()
80{
81 beginResetModel();
82 m_items.clear();
83 endResetModel();
84}
85
taylorchuc27dd482014-05-17 20:06:49 -070086void
Qi Zhaod3de12b2017-02-21 20:11:58 -080087FibStatusModel::updateStatus(std::vector<ndn::nfd::FibEntry> status)
taylorchuc27dd482014-05-17 20:06:49 -070088{
Qi Zhaod3de12b2017-02-21 20:11:58 -080089 beginResetModel();
90 m_items.clear();
91 for (auto const& fibEntry : status) {
92 bool isSamePrefix = false;
93 for (auto const& nextHop : fibEntry.getNextHopRecords()) {
94 if (!isSamePrefix) {
95 addItem(FibStatusItem(QString::fromStdString(fibEntry.getPrefix().toUri()),
96 nextHop.getFaceId(), nextHop.getCost()));
97 isSamePrefix = true;
98 }
99 else {
100 addItem(FibStatusItem(QString(""), nextHop.getFaceId(), nextHop.getCost()));
101 }
102 }
103 }
104 endResetModel();
taylorchuc27dd482014-05-17 20:06:49 -0700105}
106
107void
108FibStatusModel::onTimeout()
109{
Qi Zhaod3de12b2017-02-21 20:11:58 -0800110 std::cerr << "Request timed out (should not really happen)" << std::endl;
taylorchuc27dd482014-05-17 20:06:49 -0700111}
112
113} // namespace ndn