blob: c7484397769d7dc196f3c5c7a619f28329719cb1 [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
23#include <ndn-cxx/face.hpp>
24#include <ndn-cxx/name.hpp>
25#include <ndn-cxx/interest.hpp>
26#include <ndn-cxx/encoding/buffer-stream.hpp>
27#include <ndn-cxx/management/nfd-fib-entry.hpp>
28#include <ndn-cxx/management/nfd-face-status.hpp>
29#include <ndn-cxx/management/nfd-forwarder-status.hpp>
30
31namespace ndn {
32
33FibStatusModel::FibStatusModel(Face& face, QObject *parent/* = 0*/)
34 : QAbstractListModel(parent)
Yingdi Yu1f824642016-03-20 17:07:22 -070035 // , m_face(face)
taylorchuc27dd482014-05-17 20:06:49 -070036{
37}
38
39int
40FibStatusModel::rowCount(const QModelIndex &parent/* = QModelIndex()*/) const
41{
42 return m_items.count();
43}
44
45void
46FibStatusModel::addItem(const FibStatusItem &item)
47{
48 beginInsertRows(QModelIndex(), rowCount(), rowCount());
49 m_items << item;
50 endInsertRows();
51}
52
53QVariant
54FibStatusModel::data(const QModelIndex & index, int role) const
55{
56 if (index.row() < 0 || index.row() >= m_items.count()) {
57 return QVariant();
58 }
59
60 const FibStatusItem &item = m_items.at(index.row());
61 if (role == PrefixRole) {
62 return item.prefix();
63 } else if (role == FaceIdRole) {
susmit90b71822016-03-20 13:31:23 -060064 return static_cast<uint>(item.faceId());
taylorchuc27dd482014-05-17 20:06:49 -070065 } else if (role == CostRole) {
susmit90b71822016-03-20 13:31:23 -060066 return static_cast<uint>(item.cost());
taylorchuc27dd482014-05-17 20:06:49 -070067 }
68
69 return QVariant();
70}
71
72QHash<int, QByteArray>
73FibStatusModel::roleNames() const
74{
75 QHash<int, QByteArray> roles;
76 roles[PrefixRole] = "prefix";
77 roles[FaceIdRole] = "faceId";
78 roles[CostRole] = "cost";
79 return roles;
80}
81
82void
83FibStatusModel::clear()
84{
85 beginResetModel();
86 m_items.clear();
87 endResetModel();
88}
89
90Q_INVOKABLE void
91FibStatusModel::fetchFibInformation()
92{
93 // m_buffer = make_shared<OBufferStream>();
94
95 // Interest interest("/localhost/nfd/fib/list");
96 // interest.setChildSelector(1);
97 // interest.setMustBeFresh(true);
98 // m_face.expressInterest(interest,
99 // bind(&FibStatusModel::fetchSegments, this, _2,
100 // &FibStatusModel::afterFetchedFibEnumerationInformation),
101 // bind(&FibStatusModel::onTimeout, this));
102 // try {
103 // m_face.processEvents();
104 // } catch (Tlv::Error e) {
105 // std::cerr << e.what() << std::endl;
106 // clear();
107 // }
108}
109
110void
111FibStatusModel::afterFetchedFibEnumerationInformation()
112{
113// beginResetModel();
114// m_items.clear();
115// ConstBufferPtr buf = m_buffer->buf();
116
117// Block block;
118// size_t offset = 0;
119// while (offset < buf->size()) {
120// bool ok = Block::fromBuffer(buf, offset, block);
121// if (!ok) {
122// std::cerr << "ERROR: cannot decode FibEntry TLV" << std::endl;
123// break;
124// }
125// offset += block.size();
126
127// nfd::FibEntry fibEntry(block);
128
129// for (std::list<nfd::NextHopRecord>::const_iterator
130// nextHop = fibEntry.getNextHopRecords().begin();
131// nextHop != fibEntry.getNextHopRecords().end();
132// ++nextHop) {
133// addItem(FibStatusItem(QString::fromStdString(fibEntry.getPrefix().toUri()),
134// nextHop->getFaceId(), nextHop->getCost()));
135// }
136// }
137// endResetModel();
138}
139
140
141void
142FibStatusModel::fetchSegments(const Data& data, void (FibStatusModel::*onDone)())
143{
144 // m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
145 // data.getContent().value_size());
146
147 // uint64_t currentSegment = data.getName().get(-1).toSegment();
148
149 // const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
150 // if (finalBlockId.empty() ||
151 // finalBlockId.toSegment() > currentSegment) {
152 // m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
153 // bind(&FibStatusModel::fetchSegments, this, _2, onDone),
154 // bind(&FibStatusModel::onTimeout, this));
155 // } else {
156 // return (this->*onDone)();
157 // }
158}
159
160void
161FibStatusModel::onTimeout()
162{
163 std::cerr << "Request timed out" << std::endl;
164}
165
166} // namespace ndn