blob: c21668073e56d8e72e893a0f42e4eceee46701c9 [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 }
31
32 interest.getName().append(name::Component::fromNumber(timestamp)).append(name::Component::fromNumber(random::generateWord64()));
33
34 if(certificateName == DEFAULT_CERTIFICATE_NAME)
35 m_keyChain.sign(interest);
36 else
37 m_keyChain.sign(interest, certificateName);
38
39 m_lastTimestamp = timestamp;
40}
41
42void
43CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity)
44{
45 int64_t timestamp = time::now() / 1000000;
46 if(timestamp <= m_lastTimestamp)
47 timestamp = m_lastTimestamp + 1;
48
49 interest.getName().append(name::Component::fromNumber(timestamp)).append(name::Component::fromNumber(random::generateWord64()));
50
51 m_keyChain.signByIdentity(interest, identity);
52
53 m_lastTimestamp = timestamp;
54}
55
56}//ndn