blob: 8b4d79674db80388c70d674272b28d5aee22731e [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()
28 : m_lastTimestamp(time::now() / 1000000)
29 {
30 }
31
32 virtual
33 ~CommandInterestGenerator()
34 {
35 }
36
37 void
38 generate(Interest& interest, const Name& certificateName = Name());
39
40 void
41 generateWithIdentity(Interest& interest, const Name& identity);
42
43private:
44 int64_t m_lastTimestamp;
45 KeyChain m_keyChain;
46};
47
48
49inline void
50CommandInterestGenerator::generate(Interest& interest,
51 const Name& certificateName /*= Name()*/)
52{
53 int64_t timestamp = time::now() / 1000000;
54 while(timestamp == m_lastTimestamp)
55 {
56 usleep(1000); //Guarantee unqiueness of timestamp
57 timestamp = time::now();
58 }
59
60 Name commandInterestName = interest.getName();
61 commandInterestName
62 .append(name::Component::fromNumber(timestamp))
63 .append(name::Component::fromNumber(random::generateWord64()));
64 interest.setName(commandInterestName);
65
66 if(certificateName.empty())
67 m_keyChain.sign(interest);
68 else
69 m_keyChain.sign(interest, certificateName);
70
71 m_lastTimestamp = timestamp;
72}
73
74inline void
75CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
76{
77 int64_t timestamp = time::now() / 1000000;
78 if(timestamp <= m_lastTimestamp)
79 timestamp = m_lastTimestamp + 1;
80
81 Name commandInterestName = interest.getName();
82 commandInterestName
83 .append(name::Component::fromNumber(timestamp))
84 .append(name::Component::fromNumber(random::generateWord64()));
85 interest.setName(commandInterestName);
86
87 m_keyChain.signByIdentity(interest, identity);
88
89 m_lastTimestamp = timestamp;
90}
91
92
93} // namespace ndn
94
95#endif // NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP