taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 3766775 | 2017-02-02 13:52:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California, |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 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 "fib-status.hpp" |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 21 | #include "fib-status.moc" |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 22 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 23 | namespace ndn { |
| 24 | |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 25 | FibStatusModel::FibStatusModel(QObject* parent/* = 0*/) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 26 | : QAbstractListModel(parent) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 27 | { |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 28 | connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::FibEntry>)), this, |
| 29 | SLOT(updateStatus(std::vector<ndn::nfd::FibEntry>)), |
| 30 | Qt::QueuedConnection); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 34 | FibStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 35 | { |
| 36 | return m_items.count(); |
| 37 | } |
| 38 | |
| 39 | void |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 40 | FibStatusModel::addItem(const FibStatusItem& item) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 41 | { |
| 42 | beginInsertRows(QModelIndex(), rowCount(), rowCount()); |
| 43 | m_items << item; |
| 44 | endInsertRows(); |
| 45 | } |
| 46 | |
| 47 | QVariant |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 48 | FibStatusModel::data(const QModelIndex& index, int role) const |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 49 | { |
| 50 | if (index.row() < 0 || index.row() >= m_items.count()) { |
| 51 | return QVariant(); |
| 52 | } |
| 53 | |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 54 | const FibStatusItem& item = m_items.at(index.row()); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 55 | if (role == PrefixRole) { |
| 56 | return item.prefix(); |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 57 | } |
| 58 | else if (role == FaceIdRole) { |
susmit | 90b7182 | 2016-03-20 13:31:23 -0600 | [diff] [blame] | 59 | return static_cast<uint>(item.faceId()); |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 60 | } |
| 61 | else if (role == CostRole) { |
susmit | 90b7182 | 2016-03-20 13:31:23 -0600 | [diff] [blame] | 62 | return static_cast<uint>(item.cost()); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return QVariant(); |
| 66 | } |
| 67 | |
| 68 | QHash<int, QByteArray> |
| 69 | FibStatusModel::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 | |
| 78 | void |
| 79 | FibStatusModel::clear() |
| 80 | { |
| 81 | beginResetModel(); |
| 82 | m_items.clear(); |
| 83 | endResetModel(); |
| 84 | } |
| 85 | |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 86 | void |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 87 | FibStatusModel::updateStatus(std::vector<ndn::nfd::FibEntry> status) |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 88 | { |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 89 | 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(); |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
| 108 | FibStatusModel::onTimeout() |
| 109 | { |
Qi Zhao | d3de12b | 2017-02-21 20:11:58 -0800 | [diff] [blame] | 110 | std::cerr << "Request timed out (should not really happen)" << std::endl; |
taylorchu | c27dd48 | 2014-05-17 20:06:49 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace ndn |