blob: 3f53ef4de8deb683ce12b193902137e83bb321c9 [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 };
26
Jeff Thompson5cae5e52013-07-10 19:41:20 -070027 KeyLocator()
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080028 : type_(KeyLocator_None)
Jeff Thompson5cae5e52013-07-10 19:41:20 -070029 {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070030 }
Jeff Thompson5cae5e52013-07-10 19:41:20 -070031
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032 inline const Block&
33 wireEncode() const;
34
35 inline void
36 wireDecode(const Block &value);
37
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080038 inline bool
39 empty() const
40 {
41 return type_ == KeyLocator_None;
42 }
43
44 uint32_t
Jeff Thompson0050abe2013-09-17 12:50:25 -070045 getType() const { return type_; }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080047 ////////////////////////////////////////////////////////
48 // Helper methods for different types of key locators
49 //
50 // For now only Name type is actually supported
51
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080052 inline const Name&
53 getName() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080054
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055 inline void
56 setName(const Name &name);
Jeff Thompson35a2bc12013-09-17 14:23:13 -070057
Jeff Thompson5cae5e52013-07-10 19:41:20 -070058private:
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080059 uint32_t type_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060 Name name_;
61
62 mutable Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070063};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080064
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080065inline const Block&
66KeyLocator::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 Thompson5cae5e52013-07-10 19:41:20 -070083}
84
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080085inline void
86KeyLocator::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
102inline const Name&
103KeyLocator::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
111inline void
112KeyLocator::setName(const Name &name)
113{
114 type_ = KeyLocator_Name;
115 name_ = name;
116}
117
118
119} // namespace ndn
120
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700121#endif