Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 13 | namespace ndn |
| 14 | { |
| 15 | const Name CommandInterestGenerator::DEFAULT_CERTIFICATE_NAME = Name(); |
| 16 | |
| 17 | CommandInterestGenerator::CommandInterestGenerator() |
| 18 | : m_lastTimestamp(time::now() / 1000000) |
| 19 | {} |
| 20 | |
| 21 | void |
| 22 | CommandInterestGenerator::generate(Interest& interest, |
| 23 | const Name& certificateName /*= DEFAULT_CERTIFICATE_NAME*/) |
| 24 | { |
Yingdi Yu | 71b6768 | 2014-02-12 17:41:07 -0800 | [diff] [blame] | 25 | int64_t timestamp = time::now() / 1000000; |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 26 | 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 | |
| 42 | void |
| 43 | CommandInterestGenerator::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 |