blob: 1a938f90dc1d5a8309467b11b84e75a3e09137a6 [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>
12#include "../c/util/crypto.h"
13
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{
58 if (ndndIdData->getSignature().getType() == Signature::SignatureSha256WithRsa)
59 {
60 ndndId_.resize(32);
61 ndn_digestSha256(ndndIdData->getSignature().getValue().value(), ndndIdData->getSignature().getValue().value_size(), ndndId_.buf());
62 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