Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 94a356e | 2014-01-12 18:48:06 -0800 | [diff] [blame] | 12 | #include "ndn-cpp/c/util/crypto.h" |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 13 | |
| 14 | namespace 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 | */ |
| 20 | class NdndIdFetcher { |
| 21 | public: |
| 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 | |
| 49 | private: |
| 50 | Buffer &ndndId_; |
| 51 | OnSuccess onSuccess_; |
| 52 | OnFailure onFailure_; |
| 53 | }; |
| 54 | |
| 55 | void |
| 56 | NdndIdFetcher::operator()(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& ndndIdData) |
| 57 | { |
Alexander Afanasyev | bf1a67a | 2014-01-05 23:36:13 -0800 | [diff] [blame] | 58 | if (ndndIdData->getSignature().getType() == Signature::Sha256WithRsa) |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 59 | { |
| 60 | ndndId_.resize(32); |
Alexander Afanasyev | b24a68a | 2013-12-28 16:53:21 -0800 | [diff] [blame] | 61 | ndn_digestSha256(ndndIdData->getContent().value(), ndndIdData->getContent().value_size(), ndndId_.buf()); |
Alexander Afanasyev | e2e0d75 | 2014-01-03 13:30:30 -0800 | [diff] [blame] | 62 | onSuccess_(); |
| 63 | } |
| 64 | else |
| 65 | onFailure_(); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | NdndIdFetcher::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 |