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 { |
Alexander Afanasyev | 9c7ed11 | 2014-02-07 12:23:03 -0800 | [diff] [blame] | 21 | KeyLocator_None = 4294967295, |
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 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 78 | if (wire_.hasWire()) |
| 79 | return wire_; |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 81 | // KeyLocator |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 82 | |
| 83 | switch (type_) { |
| 84 | case KeyLocator_None: |
| 85 | wire_ = dataBlock(Tlv::KeyLocator, reinterpret_cast<const uint8_t*>(0), 0); |
| 86 | break; |
| 87 | case KeyLocator_Name: |
| 88 | wire_ = Block(Tlv::KeyLocator); |
| 89 | wire_.push_back(name_.wireEncode()); |
| 90 | wire_.encode(); |
| 91 | break; |
| 92 | default: |
| 93 | throw Error("Unsupported KeyLocator type"); |
| 94 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 95 | |
| 96 | return wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 99 | inline void |
| 100 | KeyLocator::wireDecode(const Block &value) |
| 101 | { |
| 102 | wire_ = value; |
| 103 | wire_.parse(); |
| 104 | |
| 105 | if (!wire_.getAll().empty() && wire_.getAll().front().type() == Tlv::Name) |
| 106 | { |
| 107 | type_ = KeyLocator_Name; |
| 108 | name_.wireDecode(wire_.getAll().front()); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | type_ = KeyLocator_Unknown; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | inline const Name& |
| 117 | KeyLocator::getName() const |
| 118 | { |
| 119 | if (type_ != KeyLocator_Name) |
| 120 | throw Error("Requested Name, but KeyLocator is not of the Name type"); |
| 121 | |
| 122 | return name_; |
| 123 | } |
| 124 | |
| 125 | inline void |
| 126 | KeyLocator::setName(const Name &name) |
| 127 | { |
| 128 | type_ = KeyLocator_Name; |
| 129 | name_ = name; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | } // namespace ndn |
| 134 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 135 | #endif |