blob: 5a85ef42f2c1417c3c4306ccdde0de4d845ac7ca [file] [log] [blame]
Qi Zhao078ac692017-03-02 16:28:51 -08001/* -*- 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 "face-status.hpp"
21#include "face-status.moc"
22
23namespace ndn {
24
25FaceStatusModel::FaceStatusModel(QObject* parent/* = 0*/)
26 : QAbstractListModel(parent)
27{
28 connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::FaceStatus>)), this,
29 SLOT(updateStatus(std::vector<ndn::nfd::FaceStatus>)),
30 Qt::QueuedConnection);
31}
32
33int
34FaceStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
35{
36 return m_items.count();
37}
38
39void
40FaceStatusModel::addItem(const FaceStatusItem& item)
41{
42 beginInsertRows(QModelIndex(), rowCount(), rowCount());
43 m_items << item;
44 endInsertRows();
45}
46
47QVariant
48FaceStatusModel::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 FaceStatusItem& item = m_items.at(index.row());
55 if (role == FaceIdRole) {
56 return static_cast<uint>(item.faceId());
57 }
58 else if (role == RemoteRole) {
59 return item.remote();
60 }
61 else if (role == LocalRole) {
62 return item.local();
63 }
64 else if (role == ScopeRole) {
65 return item.scope();
66 }
67 else if (role == PersistencyRole) {
68 return item.persistency();
69 }
70 else if (role == LinkTypeRole) {
71 return item.linkType();
72 }
73 else if (role == InInterestRole) {
74 return static_cast<uint>(item.inInterest());
75 }
76 else if (role == OutInterestRole) {
77 return static_cast<uint>(item.outInterest());
78 }
79 else if (role == InDataRole) {
80 return static_cast<uint>(item.inData());
81 }
82 else if (role == OutDataRole) {
83 return static_cast<uint>(item.outData());
84 }
85 else if (role == InByteRole) {
86 return static_cast<uint>(item.inByte());
87 }
88 else if (role == OutByteRole) {
89 return static_cast<uint>(item.outByte());
90 }
91 else if (role == InNackRole) {
92 return static_cast<uint>(item.inNack());
93 }
94 else if (role == OutNackRole) {
95 return static_cast<uint>(item.outNack());
96 }
97
98 return QVariant();
99}
100
101QHash<int, QByteArray>
102FaceStatusModel::roleNames() const
103{
104 QHash<int, QByteArray> roles;
105 roles[FaceIdRole] = "faceId";
106 roles[RemoteRole] = "remote";
107 roles[LocalRole] = "local";
108 roles[ScopeRole] = "scope";
109 roles[PersistencyRole] = "persistency";
110 roles[LinkTypeRole] = "linkType";
111 roles[InInterestRole] = "inInterest";
112 roles[OutInterestRole] = "outInterest";
113 roles[InDataRole] = "inData";
114 roles[OutDataRole] = "outData";
115 roles[InByteRole] = "inByte";
116 roles[OutByteRole] = "outByte";
117 roles[InNackRole] = "inNack";
118 roles[OutNackRole] = "outNack";
119 return roles;
120}
121
122void
123FaceStatusModel::clear()
124{
125 beginResetModel();
126 m_items.clear();
127 endResetModel();
128}
129
130void
131FaceStatusModel::updateStatus(std::vector<ndn::nfd::FaceStatus> status)
132{
133 beginResetModel();
134 m_items.clear();
135 for (auto const& faceEntry : status) {
136 QString faceScope = (faceEntry.getFaceScope() == ndn::nfd::FACE_SCOPE_LOCAL) ? QString("Local") : QString("Non-Local");
137 QString facePersistency = (faceEntry.getFacePersistency() == ndn::nfd::FACE_PERSISTENCY_PERSISTENT) ? QString("Persistent") :
138 ((faceEntry.getFacePersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) ? QString("Permanent") :
139 QString("On-Demand"));
140 QString faceLinkType = (faceEntry.getLinkType() == ndn::nfd::LINK_TYPE_MULTI_ACCESS) ? QString("Multi-Access") :
141 QString("Point-to-Point");
142 addItem(FaceStatusItem(faceEntry.getFaceId(), QString::fromStdString(faceEntry.getRemoteUri()),
143 QString::fromStdString(faceEntry.getLocalUri()), faceScope, facePersistency, faceLinkType,
144 faceEntry.getNInInterests(), faceEntry.getNOutInterests(), faceEntry.getNInDatas(),
145 faceEntry.getNOutDatas(), faceEntry.getNInBytes(), faceEntry.getNOutBytes(),
146 faceEntry.getNInNacks(), faceEntry.getNOutNacks()));
147 }
148 endResetModel();
149}
150
151void
152FaceStatusModel::onTimeout()
153{
154 std::cerr << "Request timed out (should not really happen)" << std::endl;
155}
156
157} // namespace ndn