blob: 2ff85f0e298f13aea82c9c616c58a11263cd5e48 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson71b2f872013-12-17 12:03:17 -08008#ifndef NDN_KEY_LOCATOR_HPP
9#define NDN_KEY_LOCATOR_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080011#include "encoding/block.hpp"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070012#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070013
14namespace ndn {
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080015
Jeff Thompson5cae5e52013-07-10 19:41:20 -070016class KeyLocator {
17public:
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080018 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
19
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080020 enum {
21 KeyLocator_None = -1,
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080022 KeyLocator_Name = 0,
23
24 KeyLocator_Unknown = 255
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080025 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080026
27 inline
Jeff Thompson5cae5e52013-07-10 19:41:20 -070028 KeyLocator()
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080029 : type_(KeyLocator_None)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070030 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070031 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070032
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080033 inline
34 KeyLocator(const Name &name);
35
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080036 inline const Block&
37 wireEncode() const;
38
39 inline void
40 wireDecode(const Block &value);
41
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080042 inline bool
43 empty() const
44 {
45 return type_ == KeyLocator_None;
46 }
47
48 uint32_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070049 getType() const { return type_; }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080050
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080051 ////////////////////////////////////////////////////////
52 // Helper methods for different types of key locators
53 //
54 // For now only Name type is actually supported
55
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080056 inline const Name&
57 getName() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080058
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 inline void
60 setName(const Name &name);
Jeff Thompson35a2bc12013-09-17 14:23:13 -070061
Jeff Thompson5cae5e52013-07-10 19:41:20 -070062private:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080063 uint32_t type_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064 Name name_;
65
66 mutable Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070067};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080068
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080069inline
70KeyLocator::KeyLocator(const Name &name)
71{
72 setName(name);
73}
74
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080075inline const Block&
76KeyLocator::wireEncode() const
77{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080078 if (wire_.hasWire())
79 return wire_;
Alexander Afanasyeve9a66e52014-01-17 16:07:17 -080080
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080081 // KeyLocator
Alexander Afanasyeve9a66e52014-01-17 16:07:17 -080082
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 Afanasyev2ba8f662014-01-05 22:53:18 -080095
96 return wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070097}
98
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080099inline void
100KeyLocator::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
116inline const Name&
117KeyLocator::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
125inline void
126KeyLocator::setName(const Name &name)
127{
128 type_ = KeyLocator_Name;
129 name_ = name;
130}
131
132
133} // namespace ndn
134
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700135#endif