Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "profile-data.h" |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 12 | #include <boost/date_time/posix_time/posix_time.hpp> |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 13 | #include "logging.h" |
| 14 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 15 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 16 | using namespace ndn; |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 17 | using namespace std; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 18 | using namespace boost::posix_time; |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 19 | |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 20 | INIT_LOGGER("ProfileData"); |
| 21 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 22 | ProfileData::ProfileData() |
| 23 | : Data() |
| 24 | {} |
| 25 | |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 26 | ProfileData::ProfileData(const Profile& profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 27 | : Data() |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 28 | , m_identity(profile.getIdentityName()) |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 29 | , m_profile(profile) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 30 | { |
Yingdi Yu | e9ea5c9 | 2013-11-06 18:42:34 -0800 | [diff] [blame] | 31 | Name dataName = m_identity; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 32 | |
| 33 | time_duration now = microsec_clock::universal_time () - ptime(boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
| 34 | uint64_t version = (now.total_seconds () << 12) | (0xFFF & (now.fractional_seconds () / 244)); |
| 35 | dataName.append("PROFILE").appendVersion(version); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 36 | setName(dataName); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 37 | |
| 38 | string content; |
| 39 | profile.encode(&content); |
| 40 | setContent((const uint8_t *)&content[0], content.size()); |
| 41 | |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Yingdi Yu | accbda9 | 2013-12-27 08:44:12 +0800 | [diff] [blame] | 44 | // ProfileData::ProfileData(const ProfileData& profileData) |
| 45 | // : Data(profileData) |
| 46 | // , m_identity(profileData.m_identity) |
| 47 | // , m_profile(profileData.m_profile) |
| 48 | // {} |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 49 | |
| 50 | ProfileData::ProfileData(const Data& data) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 51 | : Data(data) |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 52 | { |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 53 | const Name& dataName = data.getName(); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 54 | Name::Component appFlag(Name::fromEscapedString("PROFILE")); |
Yingdi Yu | 38e329b | 2013-10-10 22:11:35 -0700 | [diff] [blame] | 55 | |
| 56 | int profileIndex = -1; |
| 57 | for(int i = 0; i < dataName.size(); i++) |
| 58 | { |
| 59 | if(0 == dataName.get(i).compare(appFlag)) |
| 60 | { |
| 61 | profileIndex = i; |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 66 | if(profileIndex < 0) |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 67 | throw Error("No PROFILE component in data name!"); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 69 | m_identity = dataName.getPrefix(profileIndex); |
Yingdi Yu | 5ff6210 | 2013-10-13 17:24:50 -0700 | [diff] [blame] | 70 | |
Yingdi Yu | c9ffa9f | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 71 | string encoded(reinterpret_cast<const char*>(data.getContent().value()), data.getContent().value_size()); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 72 | m_profile = *Profile::decode(encoded); |
Yingdi Yu | ad56f1c | 2013-10-10 17:27:54 -0700 | [diff] [blame] | 73 | } |