Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
| 4 | * |
| 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 "rib-status.hpp" |
| 21 | #include "rib-status.moc" |
| 22 | |
| 23 | namespace ndn { |
| 24 | |
| 25 | RibStatusModel::RibStatusModel(QObject* parent/* = 0*/) |
| 26 | : QAbstractListModel(parent) |
| 27 | { |
| 28 | connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::RibEntry>)), this, |
| 29 | SLOT(updateStatus(std::vector<ndn::nfd::RibEntry>)), |
| 30 | Qt::QueuedConnection); |
| 31 | } |
| 32 | |
| 33 | int |
| 34 | RibStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const |
| 35 | { |
| 36 | return m_items.count(); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | RibStatusModel::addItem(const RibStatusItem& item) |
| 41 | { |
| 42 | beginInsertRows(QModelIndex(), rowCount(), rowCount()); |
| 43 | m_items << item; |
| 44 | endInsertRows(); |
| 45 | } |
| 46 | |
| 47 | QVariant |
| 48 | RibStatusModel::data(const QModelIndex& index, int role) const |
| 49 | { |
| 50 | if (index.row() < 0 || index.row() >= m_items.count()) { |
| 51 | return QVariant(); |
| 52 | } |
| 53 | |
| 54 | const RibStatusItem& item = m_items.at(index.row()); |
| 55 | if (role == PrefixRole) { |
| 56 | return item.prefix(); |
| 57 | } |
| 58 | else if (role == FaceIdRole) { |
| 59 | return static_cast<uint>(item.faceId()); |
| 60 | } |
| 61 | else if (role == OriginRole) { |
| 62 | return static_cast<uint>(item.origin()); |
| 63 | } |
| 64 | else if (role == CostRole) { |
| 65 | return static_cast<uint>(item.cost()); |
| 66 | } |
| 67 | else if (role == ChildRole) { |
| 68 | return item.child(); |
| 69 | } |
| 70 | else if (role == RibCapRole) { |
| 71 | return item.ribcap(); |
| 72 | } |
| 73 | else if (role == ExpRole) { |
| 74 | return item.exp(); |
| 75 | } |
| 76 | |
| 77 | return QVariant(); |
| 78 | } |
| 79 | |
| 80 | QHash<int, QByteArray> |
| 81 | RibStatusModel::roleNames() const |
| 82 | { |
| 83 | QHash<int, QByteArray> roles; |
| 84 | roles[PrefixRole] = "prefix"; |
| 85 | roles[FaceIdRole] = "faceId"; |
| 86 | roles[OriginRole] = "origin"; |
| 87 | roles[CostRole] = "cost"; |
| 88 | roles[ChildRole] = "childinherit"; |
| 89 | roles[RibCapRole] = "ribcapture"; |
| 90 | roles[ExpRole] = "expiresin"; |
| 91 | return roles; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | RibStatusModel::clear() |
| 96 | { |
| 97 | beginResetModel(); |
| 98 | m_items.clear(); |
| 99 | endResetModel(); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | RibStatusModel::updateStatus(std::vector<ndn::nfd::RibEntry> status) |
| 104 | { |
| 105 | beginResetModel(); |
| 106 | m_items.clear(); |
| 107 | for (auto const& ribEntry : status) { |
| 108 | bool isSamePrefix = false; |
| 109 | for (auto const& route : ribEntry.getRoutes()) { |
Qi Zhao | b87e4e1 | 2017-03-16 10:36:05 -0700 | [diff] [blame] | 110 | QString expirationPeriod = route.hasExpirationPeriod() ? |
| 111 | QString::fromStdString(std::to_string(route.getExpirationPeriod().count())) : QString("Never"); |
Qi Zhao | ec8ddd2 | 2017-02-24 05:16:15 -0800 | [diff] [blame] | 112 | if (!isSamePrefix) { |
| 113 | addItem(RibStatusItem(QString::fromStdString(ribEntry.getName().toUri()), route.getFaceId(), route.getOrigin(), |
| 114 | route.getCost(), route.isChildInherit(), route.isRibCapture(), expirationPeriod)); |
| 115 | isSamePrefix = true; |
| 116 | } |
| 117 | else { |
| 118 | addItem(RibStatusItem(QString(""), route.getFaceId(), route.getOrigin(), route.getCost(), route.isChildInherit(), |
| 119 | route.isRibCapture(), expirationPeriod)); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | endResetModel(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | RibStatusModel::onTimeout() |
| 128 | { |
| 129 | std::cerr << "Request timed out (should not really happen)" << std::endl; |
| 130 | } |
| 131 | |
| 132 | } // namespace ndn |