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