blob: 006d440c836463db8cd64d248126a25ec6dbc10f [file] [log] [blame]
Yingdi Yu17bc3012014-02-10 17:37:12 -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#include "command-interest-generator.hpp"
8#include "../util/time.hpp"
9#include "../util/random.hpp"
10
11#include <unistd.h>
12
13namespace ndn
14{
15const Name CommandInterestGenerator::DEFAULT_CERTIFICATE_NAME = Name();
16
17CommandInterestGenerator::CommandInterestGenerator()
18 : m_lastTimestamp(time::now() / 1000000)
19{}
20
21void
22CommandInterestGenerator::generate(Interest& interest,
23 const Name& certificateName /*= DEFAULT_CERTIFICATE_NAME*/)
24{
Yingdi Yu71b67682014-02-12 17:41:07 -080025 int64_t timestamp = time::now() / 1000000;
Yingdi Yu17bc3012014-02-10 17:37:12 -080026 while(timestamp == m_lastTimestamp)
27 {
28 usleep(1000); //Guarantee unqiueness of timestamp
29 timestamp = time::now();
30 }
Alexander Afanasyevc348f832014-02-17 16:35:17 -080031
32 Name commandInterestName = interest.getName();
33 commandInterestName
34 .append(name::Component::fromNumber(timestamp))
35 .append(name::Component::fromNumber(random::generateWord64()));
36 interest.setName(commandInterestName);
Yingdi Yu17bc3012014-02-10 17:37:12 -080037
38 if(certificateName == DEFAULT_CERTIFICATE_NAME)
39 m_keyChain.sign(interest);
40 else
41 m_keyChain.sign(interest, certificateName);
42
43 m_lastTimestamp = timestamp;
44}
45
46void
47CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
48{
49 int64_t timestamp = time::now() / 1000000;
50 if(timestamp <= m_lastTimestamp)
51 timestamp = m_lastTimestamp + 1;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080052
53 Name commandInterestName = interest.getName();
54 commandInterestName
55 .append(name::Component::fromNumber(timestamp))
56 .append(name::Component::fromNumber(random::generateWord64()));
57 interest.setName(commandInterestName);
Yingdi Yu17bc3012014-02-10 17:37:12 -080058
59 m_keyChain.signByIdentity(interest, identity);
60
61 m_lastTimestamp = timestamp;
62}
63
64}//ndn