blob: 0e175523aa0471e99381e16698abac88b5470a61 [file] [log] [blame]
Yingdi Yu4390ce52013-10-10 17:27:54 -07001/* -*- 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 Yu76dd8002013-12-24 11:16:32 +080012#include <boost/date_time/posix_time/posix_time.hpp>
Yingdi Yu7af8b512013-10-10 22:11:35 -070013#include "exception.h"
Yingdi Yu92e8e482013-10-17 21:13:03 -070014#include "logging.h"
15
Yingdi Yu4390ce52013-10-10 17:27:54 -070016
Yingdi Yu4390ce52013-10-10 17:27:54 -070017using namespace ndn;
Yingdi Yub4be64a2013-10-13 17:24:50 -070018using namespace std;
Yingdi Yu76dd8002013-12-24 11:16:32 +080019using namespace boost::posix_time;
Yingdi Yu4390ce52013-10-10 17:27:54 -070020
Yingdi Yu92e8e482013-10-17 21:13:03 -070021INIT_LOGGER("ProfileData");
22
Yingdi Yu76dd8002013-12-24 11:16:32 +080023ProfileData::ProfileData()
24 : Data()
25{}
26
Yingdi Yu2e3199c2013-11-06 18:42:34 -080027ProfileData::ProfileData(const Profile& profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070028 : Data()
Yingdi Yu2e3199c2013-11-06 18:42:34 -080029 , m_identity(profile.getIdentityName())
Yingdi Yub4be64a2013-10-13 17:24:50 -070030 , m_profile(profile)
Yingdi Yu4390ce52013-10-10 17:27:54 -070031{
Yingdi Yu2e3199c2013-11-06 18:42:34 -080032 Name dataName = m_identity;
Yingdi Yu76dd8002013-12-24 11:16:32 +080033
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 Yub4be64a2013-10-13 17:24:50 -070037 setName(dataName);
Yingdi Yu76dd8002013-12-24 11:16:32 +080038
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 Yu4390ce52013-10-10 17:27:54 -070045}
46
Yingdi Yu6eabbd72013-12-27 08:44:12 +080047// ProfileData::ProfileData(const ProfileData& profileData)
48// : Data(profileData)
49// , m_identity(profileData.m_identity)
50// , m_profile(profileData.m_profile)
51// {}
Yingdi Yub4be64a2013-10-13 17:24:50 -070052
53ProfileData::ProfileData(const Data& data)
Yingdi Yu76dd8002013-12-24 11:16:32 +080054 : Data(data)
Yingdi Yu4390ce52013-10-10 17:27:54 -070055{
Yingdi Yu7af8b512013-10-10 22:11:35 -070056 const Name& dataName = data.getName();
Yingdi Yu76dd8002013-12-24 11:16:32 +080057 Name::Component appFlag(Name::fromEscapedString("PROFILE"));
Yingdi Yu7af8b512013-10-10 22:11:35 -070058
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 Yub4be64a2013-10-13 17:24:50 -070069 if(profileIndex < 0)
Yingdi Yu7af8b512013-10-10 22:11:35 -070070 throw LnException("No PROFILE component in data name!");
Yingdi Yub4be64a2013-10-13 17:24:50 -070071
Yingdi Yu76dd8002013-12-24 11:16:32 +080072 m_identity = dataName.getPrefix(profileIndex);
Yingdi Yub4be64a2013-10-13 17:24:50 -070073
Yingdi Yu76dd8002013-12-24 11:16:32 +080074 string encoded((const char*)data.getContent().buf(), data.getContent().size());
75 m_profile = *Profile::decode(encoded);
Yingdi Yu4390ce52013-10-10 17:27:54 -070076}