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 | }; |
| 26 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 27 | KeyLocator() |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 28 | : type_(KeyLocator_None) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 29 | { |
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 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | inline const Block& |
| 33 | wireEncode() const; |
| 34 | |
| 35 | inline void |
| 36 | wireDecode(const Block &value); |
| 37 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 38 | inline bool |
| 39 | empty() const |
| 40 | { |
| 41 | return type_ == KeyLocator_None; |
| 42 | } |
| 43 | |
| 44 | uint32_t |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 45 | getType() const { return type_; } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 47 | //////////////////////////////////////////////////////// |
| 48 | // Helper methods for different types of key locators |
| 49 | // |
| 50 | // For now only Name type is actually supported |
| 51 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 52 | inline const Name& |
| 53 | getName() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 55 | inline void |
| 56 | setName(const Name &name); |
Jeff Thompson | 35a2bc1 | 2013-09-17 14:23:13 -0700 | [diff] [blame] | 57 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 58 | private: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 59 | uint32_t type_; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 60 | Name name_; |
| 61 | |
| 62 | mutable Block wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 63 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 65 | inline const Block& |
| 66 | KeyLocator::wireEncode() const |
| 67 | { |
| 68 | if (empty()) |
| 69 | throw Error("Wire encoding requested, but KeyLocator is empty"); |
| 70 | |
| 71 | if (wire_.hasWire()) |
| 72 | return wire_; |
| 73 | |
| 74 | if (type_ != KeyLocator_Name) |
| 75 | throw Error("Unsupported KeyLocator type"); |
| 76 | |
| 77 | // KeyLocator |
| 78 | wire_ = Block(Tlv::KeyLocator); |
| 79 | wire_.push_back(name_.wireEncode()); |
| 80 | wire_.encode(); |
| 81 | |
| 82 | return wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 85 | inline void |
| 86 | KeyLocator::wireDecode(const Block &value) |
| 87 | { |
| 88 | wire_ = value; |
| 89 | wire_.parse(); |
| 90 | |
| 91 | if (!wire_.getAll().empty() && wire_.getAll().front().type() == Tlv::Name) |
| 92 | { |
| 93 | type_ = KeyLocator_Name; |
| 94 | name_.wireDecode(wire_.getAll().front()); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | type_ = KeyLocator_Unknown; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | inline const Name& |
| 103 | KeyLocator::getName() const |
| 104 | { |
| 105 | if (type_ != KeyLocator_Name) |
| 106 | throw Error("Requested Name, but KeyLocator is not of the Name type"); |
| 107 | |
| 108 | return name_; |
| 109 | } |
| 110 | |
| 111 | inline void |
| 112 | KeyLocator::setName(const Name &name) |
| 113 | { |
| 114 | type_ = KeyLocator_Name; |
| 115 | name_ = name; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | } // namespace ndn |
| 120 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 121 | #endif |