All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
command-interest-generator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP
8 #define NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP
9 
10 #include "../interest.hpp"
11 #include "../security/key-chain.hpp"
12 #include "../util/time.hpp"
13 #include "../util/random.hpp"
14 
15 namespace ndn {
16 
23 {
24 public:
26 
28  : m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now()))
29  {
30  }
31 
32  virtual
34  {
35  }
36 
37  void
38  generate(Interest& interest, const Name& certificateName = Name());
39 
40  void
41  generateWithIdentity(Interest& interest, const Name& identity);
42 
43 private:
44  time::milliseconds m_lastTimestamp;
45  KeyChain m_keyChain;
46 };
47 
48 
49 inline void
51  const Name& certificateName /*= Name()*/)
52 {
53  time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
54  while(timestamp <= m_lastTimestamp)
55  {
56  timestamp += time::milliseconds(1);
57  }
58 
59  Name commandInterestName = interest.getName();
60  commandInterestName
61  .append(name::Component::fromNumber(timestamp.count()))
63  interest.setName(commandInterestName);
64 
65  if (certificateName.empty())
66  m_keyChain.sign(interest);
67  else
68  m_keyChain.sign(interest, certificateName);
69 
70  m_lastTimestamp = timestamp;
71 }
72 
73 inline void
75 {
76  time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
77  while(timestamp <= m_lastTimestamp)
78  {
79  timestamp += time::milliseconds(1);
80  }
81 
82  Name commandInterestName = interest.getName();
83  commandInterestName
84  .append(name::Component::fromNumber(timestamp.count()))
86  interest.setName(commandInterestName);
87 
88  m_keyChain.signByIdentity(interest, identity);
89 
90  m_lastTimestamp = timestamp;
91 }
92 
93 
94 } // namespace ndn
95 
96 #endif // NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
const Name & getName() const
Definition: interest.hpp:182
void generate(Interest &interest, const Name &certificateName=Name())
Helper class to generate CommandInterests.
void signByIdentity(T &packet, const Name &identityName)
Sign packet using the default certificate of a particular identity.
Definition: key-chain.hpp:297
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:24
void sign(T &packet)
Sign packet with default identity.
Definition: key-chain.hpp:208
Interest & setName(const Name &name)
Definition: interest.hpp:188
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
uint64_t generateWord64()
Definition: random.cpp:25
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.hpp:107
Name & append(const uint8_t *value, size_t valueLength)
Append a new component, copying from value of length valueLength.
Definition: name.hpp:142
bool empty() const
Check if name is emtpy.
Definition: name.hpp:319
void generateWithIdentity(Interest &interest, const Name &identity)