Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 8 | #ifndef NDN_KEY_LOCATOR_HPP |
| 9 | #define NDN_KEY_LOCATOR_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 11 | #include "encoding/block.hpp" |
Jeff Thompson | 2e6269c | 2013-08-22 10:36:00 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 15 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 16 | class KeyLocator { |
| 17 | public: |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 18 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 19 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 20 | enum { |
| 21 | KeyLocator_None = -1, |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 22 | KeyLocator_Name = 0, |
| 23 | |
| 24 | KeyLocator_Unknown = 255 |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 25 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame^] | 26 | |
| 27 | inline |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 28 | KeyLocator() |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 29 | : type_(KeyLocator_None) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 30 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 31 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame^] | 33 | inline |
| 34 | KeyLocator(const Name &name); |
| 35 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | inline const Block& |
| 37 | wireEncode() const; |
| 38 | |
| 39 | inline void |
| 40 | wireDecode(const Block &value); |
| 41 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 42 | inline bool |
| 43 | empty() const |
| 44 | { |
| 45 | return type_ == KeyLocator_None; |
| 46 | } |
| 47 | |
| 48 | uint32_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 49 | getType() const { return type_; } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 51 | //////////////////////////////////////////////////////// |
| 52 | // Helper methods for different types of key locators |
| 53 | // |
| 54 | // For now only Name type is actually supported |
| 55 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 56 | inline const Name& |
| 57 | getName() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 59 | inline void |
| 60 | setName(const Name &name); |
Jeff Thompson | 35a2bc1 | 2013-09-17 14:23:13 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 62 | private: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 63 | uint32_t type_; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | Name name_; |
| 65 | |
| 66 | mutable Block wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 67 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame^] | 69 | inline |
| 70 | KeyLocator::KeyLocator(const Name &name) |
| 71 | { |
| 72 | setName(name); |
| 73 | } |
| 74 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 75 | inline const Block& |
| 76 | KeyLocator::wireEncode() const |
| 77 | { |
| 78 | if (empty()) |
| 79 | throw Error("Wire encoding requested, but KeyLocator is empty"); |
| 80 | |
| 81 | if (wire_.hasWire()) |
| 82 | return wire_; |
| 83 | |
| 84 | if (type_ != KeyLocator_Name) |
| 85 | throw Error("Unsupported KeyLocator type"); |
| 86 | |
| 87 | // KeyLocator |
| 88 | wire_ = Block(Tlv::KeyLocator); |
| 89 | wire_.push_back(name_.wireEncode()); |
| 90 | wire_.encode(); |
| 91 | |
| 92 | return wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 95 | inline void |
| 96 | KeyLocator::wireDecode(const Block &value) |
| 97 | { |
| 98 | wire_ = value; |
| 99 | wire_.parse(); |
| 100 | |
| 101 | if (!wire_.getAll().empty() && wire_.getAll().front().type() == Tlv::Name) |
| 102 | { |
| 103 | type_ = KeyLocator_Name; |
| 104 | name_.wireDecode(wire_.getAll().front()); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | type_ = KeyLocator_Unknown; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | inline const Name& |
| 113 | KeyLocator::getName() const |
| 114 | { |
| 115 | if (type_ != KeyLocator_Name) |
| 116 | throw Error("Requested Name, but KeyLocator is not of the Name type"); |
| 117 | |
| 118 | return name_; |
| 119 | } |
| 120 | |
| 121 | inline void |
| 122 | KeyLocator::setName(const Name &name) |
| 123 | { |
| 124 | type_ = KeyLocator_Name; |
| 125 | name_ = name; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | } // namespace ndn |
| 130 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 131 | #endif |