blob: 300c29e0ebc33ae1f3a9f0947274d2e7e7217130 [file] [log] [blame]
taylorchuc27dd482014-05-17 20:06:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014, 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 "forwarder-status.hpp"
21
22#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/name.hpp>
24#include <ndn-cxx/interest.hpp>
25#include <ndn-cxx/management/nfd-fib-entry.hpp>
26#include <ndn-cxx/management/nfd-face-status.hpp>
27#include <ndn-cxx/management/nfd-forwarder-status.hpp>
28
29#ifdef WAF
30#include "forwarder-status.moc"
31// #include "forwarder-status.cpp.moc"
32#endif
33
34namespace ndn {
35
36ForwarderStatusModel::ForwarderStatusModel(Face& face, QObject* parent/* = 0*/)
37 : QAbstractListModel(parent)
38 , m_face(face)
39{
40 connect(this, SIGNAL(onDataReceived(ndn::shared_ptr<const ndn::Data>)), this,
41 SLOT(updateStatus(ndn::shared_ptr<const ndn::Data>)));
42}
43
44int
45ForwarderStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
46{
47 return m_items.count();
48}
49
50void
51ForwarderStatusModel::addItem(const ForwarderStatusItem& item)
52{
53 beginInsertRows(QModelIndex(), rowCount(), rowCount());
54 m_items << item;
55 endInsertRows();
56}
57
58QVariant
59ForwarderStatusModel::data(const QModelIndex& index, int role) const
60{
61 if (index.row() < 0 || index.row() >= m_items.count()) {
62 return QVariant();
63 }
64
65 const ForwarderStatusItem& item = m_items.at(index.row());
66 if (role == TypeRole) {
67 return item.type();
68 }
69 else if (role == ValueRole) {
70 return item.value();
71 }
72 return QVariant();
73}
74
75QHash<int, QByteArray>
76ForwarderStatusModel::roleNames() const
77{
78 QHash<int, QByteArray> roles;
79 roles[TypeRole] = "type";
80 roles[ValueRole] = "value";
81 return roles;
82}
83
84Q_INVOKABLE void
85ForwarderStatusModel::fetchVersionInformation()
86{
87 Interest interest("/localhost/nfd/status");
88 interest.setMustBeFresh(true);
89 m_face.expressInterest(interest,
90 bind(&ForwarderStatusModel::afterFetchedVersionInformation, this, _2),
91 bind(&ForwarderStatusModel::onTimeout, this, _1));
92}
93
94void
95ForwarderStatusModel::clear()
96{
97 beginResetModel();
98 m_items.clear();
99 endResetModel ();
100}
101
102void
103ForwarderStatusModel::afterFetchedVersionInformation(const Data& data)
104{
105 emit onDataReceived(data.shared_from_this());
106}
107
108void
109ForwarderStatusModel::updateStatus(shared_ptr<const Data> data)
110{
111 beginResetModel();
112 m_items.clear();
113 nfd::ForwarderStatus status(data->getContent());
114 addItem(ForwarderStatusItem("version", QString::number(status.getNfdVersion())));
115 addItem(ForwarderStatusItem("startTime", QString::fromStdString(time::toIsoString(status.getStartTimestamp()))));
116 addItem(ForwarderStatusItem("currentTime", QString::fromStdString(time::toIsoString(status.getCurrentTimestamp()))));
117 addItem(ForwarderStatusItem("nNameTreeEntries", QString::number(status.getNNameTreeEntries())));
118 addItem(ForwarderStatusItem("nFibEntries", QString::number(status.getNFibEntries())));
119 addItem(ForwarderStatusItem("nPitEntries", QString::number(status.getNPitEntries())));
120 addItem(ForwarderStatusItem("nMeasurementsEntries", QString::number(status.getNMeasurementsEntries())));
121 addItem(ForwarderStatusItem("nCsEntries", QString::number(status.getNCsEntries())));
122 addItem(ForwarderStatusItem("nInInterests", QString::number(status.getNInInterests())));
123 addItem(ForwarderStatusItem("nOutInterests", QString::number(status.getNOutInterests())));
124 addItem(ForwarderStatusItem("nInDatas", QString::number(status.getNInDatas())));
125 addItem(ForwarderStatusItem("nOutDatas", QString::number(status.getNOutDatas())));
126 endResetModel();
127}
128
129void
130ForwarderStatusModel::onTimeout(const Interest& interest)
131{
132 std::cerr << "Request timed out (should not really happen)" << std::endl;
133}
134
135} // namespace ndn