blob: 717521192d2b262b3631236dc7f6909ea4c1c663 [file] [log] [blame]
Chavoosh Ghasemid8f9af22019-02-28 09:47:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2016-2019, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Chavoosh Ghasemi <chghasemi@cs.arizona.edu>
24 */
25
26#include "discover-version-realtime.hpp"
27
28#include <ndn-cxx/metadata-object.hpp>
29
30namespace ndn {
31namespace chunks {
32
33DiscoverVersionRealtime::DiscoverVersionRealtime(const Name& prefix,
34 Face& face,
35 const Options& options)
36 : chunks::Options(options)
37 , DiscoverVersion(prefix, face)
38 , Options(options)
39{
40}
41
42void
43DiscoverVersionRealtime::run()
44{
45 expressInterest(MetadataObject::makeDiscoveryInterest(m_prefix)
46 .setInterestLifetime(discoveryTimeout),
47 maxRetriesOnTimeoutOrNack, maxRetriesOnTimeoutOrNack);
48}
49
50void
51DiscoverVersionRealtime::handleData(const Interest& interest, const Data& data)
52{
53 if (isVerbose)
54 std::cerr << "Data: " << data << std::endl;
55
56 // make a metadata object from received metadata packet
57 MetadataObject mobject;
58 try {
59 mobject = MetadataObject(data);
60 }
61 catch (const tlv::Error& e) {
62 this->emitSignal(onDiscoveryFailure, "Invalid metadata packet: "s + e.what());
63 return;
64 }
65
66 if (mobject.getVersionedName().empty() || !mobject.getVersionedName()[-1].isVersion()) {
67 this->emitSignal(onDiscoveryFailure, mobject.getVersionedName().toUri() +
68 " is not a valid versioned name");
69 return;
70 }
71
72 if (isVerbose) {
73 std::cerr << "Discovered Data version: " << mobject.getVersionedName()[-1].toVersion() << std::endl;
74 }
75 this->emitSignal(onDiscoverySuccess, mobject.getVersionedName());
76}
77
78} // namespace chunks
79} // namespace ndn