blob: 57b5f4b6d45f8dec42aa2caeac3abd9c659ded43 [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{
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 Thompson5cae5e52013-07-10 19:41:20 -070093}
94
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095inline void
96KeyLocator::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
112inline const Name&
113KeyLocator::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
121inline void
122KeyLocator::setName(const Name &name)
123{
124 type_ = KeyLocator_Name;
125 name_ = name;
126}
127
128
129} // namespace ndn
130
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700131#endif