blob: 843eb83277f3019becccb5015ebad494c7505234 [file] [log] [blame]
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_NDND_ID_FETCHER_HPP
9#define NDN_NDND_ID_FETCHER_HPP
10
11#include <ndn-cpp/common.hpp>
Alexander Afanasyev94a356e2014-01-12 18:48:06 -080012#include "ndn-cpp/c/util/crypto.h"
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080013
14namespace ndn {
15
16/**
17 * An NdndIdFetcher receives the Data packet with the publisher public key digest for the connected NDN hub.
18 * This class is a function object for the callbacks. It only holds a pointer to an Info object, so it is OK to copy the pointer.
19 */
20class NdndIdFetcher {
21public:
22 typedef func_lib::function<void (void)> OnSuccess;
23 typedef func_lib::function<void (void)> OnFailure;
24
25
26 class Info;
27 NdndIdFetcher(Buffer &ndndId, const OnSuccess& onSuccess, const OnFailure& onFailure)
28 : ndndId_(ndndId)
29 , onSuccess_(onSuccess)
30 , onFailure_(onFailure)
31 {
32 }
33
34 /**
35 * We received the ndnd ID.
36 * @param interest
37 * @param data
38 */
39 inline void
40 operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData);
41
42 /**
43 * We timed out fetching the ndnd ID.
44 * @param interest
45 */
46 inline void
47 operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest);
48
49private:
50 Buffer &ndndId_;
51 OnSuccess onSuccess_;
52 OnFailure onFailure_;
53};
54
55void
56NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData)
57{
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080058 if (ndndIdData->getSignature().getType() == Signature::Sha256WithRsa)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080059 {
60 ndndId_.resize(32);
Alexander Afanasyevb24a68a2013-12-28 16:53:21 -080061 ndn_digestSha256(ndndIdData->getContent().value(), ndndIdData->getContent().value_size(), ndndId_.buf());
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062 onSuccess_();
63 }
64 else
65 onFailure_();
66}
67
68void
69NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& timedOutInterest)
70{
71 onFailure_();
72}
73
74
75} // namespace ndn
76
77#endif // NDN_NDND_ID_FETCHER_HPP