blob: 18bc960838a6c24cb2b6c4a9def5a48f40e6768f [file] [log] [blame]
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
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
15namespace ndn {
16
17/**
18 * @brief Helper class to generate CommandInterests
19 *
20 * @see http://redmine.named-data.net/projects/nfd/wiki/Command_Interests
21 */
22class CommandInterestGenerator
23{
24public:
25 static const Name DEFAULT_CERTIFICATE_NAME;
26
27 CommandInterestGenerator()
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070028 : m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080029 {
30 }
31
32 virtual
33 ~CommandInterestGenerator()
34 {
35 }
36
37 void
38 generate(Interest& interest, const Name& certificateName = Name());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080040 void
41 generateWithIdentity(Interest& interest, const Name& identity);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080043private:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 time::milliseconds m_lastTimestamp;
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080045 KeyChain m_keyChain;
46};
47
48
49inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050CommandInterestGenerator::generate(Interest& interest,
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080051 const Name& certificateName /*= Name()*/)
52{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
54 while(timestamp <= m_lastTimestamp)
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080055 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056 timestamp += time::milliseconds(1);
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080057 }
58
59 Name commandInterestName = interest.getName();
60 commandInterestName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 .append(name::Component::fromNumber(timestamp.count()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080062 .append(name::Component::fromNumber(random::generateWord64()));
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}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080073inline void
74CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
75{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
77 while(timestamp <= m_lastTimestamp)
78 {
79 timestamp += time::milliseconds(1);
80 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080081
82 Name commandInterestName = interest.getName();
83 commandInterestName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084 .append(name::Component::fromNumber(timestamp.count()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080085 .append(name::Component::fromNumber(random::generateWord64()));
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