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 | } |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame^] | 31 | |
| 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 Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 37 | |
| 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 | |
| 46 | void |
| 47 | CommandInterestGenerator::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 Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame^] | 52 | |
| 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 Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 58 | |
| 59 | m_keyChain.signByIdentity(interest, identity); |
| 60 | |
| 61 | m_lastTimestamp = timestamp; |
| 62 | } |
| 63 | |
| 64 | }//ndn |