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