blob: 369818720ec70f26c982c0ed942244846642afeb [file] [log] [blame]
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080011 */
12
13#ifndef NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP
14#define NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP
15
16#include "../interest.hpp"
17#include "../security/key-chain.hpp"
18#include "../util/time.hpp"
19#include "../util/random.hpp"
20
21namespace ndn {
22
23/**
24 * @brief Helper class to generate CommandInterests
25 *
26 * @see http://redmine.named-data.net/projects/nfd/wiki/Command_Interests
27 */
28class CommandInterestGenerator
29{
30public:
31 static const Name DEFAULT_CERTIFICATE_NAME;
32
33 CommandInterestGenerator()
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070034 : m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080035 {
36 }
37
38 virtual
39 ~CommandInterestGenerator()
40 {
41 }
42
43 void
44 generate(Interest& interest, const Name& certificateName = Name());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080046 void
47 generateWithIdentity(Interest& interest, const Name& identity);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080049private:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050 time::milliseconds m_lastTimestamp;
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080051 KeyChain m_keyChain;
52};
53
54
55inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070056CommandInterestGenerator::generate(Interest& interest,
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080057 const Name& certificateName /*= Name()*/)
58{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
60 while(timestamp <= m_lastTimestamp)
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080061 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070062 timestamp += time::milliseconds(1);
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080063 }
64
65 Name commandInterestName = interest.getName();
66 commandInterestName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070067 .append(name::Component::fromNumber(timestamp.count()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080068 .append(name::Component::fromNumber(random::generateWord64()));
69 interest.setName(commandInterestName);
70
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 if (certificateName.empty())
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080072 m_keyChain.sign(interest);
73 else
74 m_keyChain.sign(interest, certificateName);
75
76 m_lastTimestamp = timestamp;
77}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070078
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080079inline void
80CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
81{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
83 while(timestamp <= m_lastTimestamp)
84 {
85 timestamp += time::milliseconds(1);
86 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080087
88 Name commandInterestName = interest.getName();
89 commandInterestName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090 .append(name::Component::fromNumber(timestamp.count()))
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080091 .append(name::Component::fromNumber(random::generateWord64()));
92 interest.setName(commandInterestName);
93
94 m_keyChain.signByIdentity(interest, identity);
95
96 m_lastTimestamp = timestamp;
97}
98
99
100} // namespace ndn
101
102#endif // NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP