blob: 2288f9da65f14846fb8e98c276e2c81a26701fd6 [file] [log] [blame]
Yingdi Yuad56f1c2013-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 Yu64206112013-12-24 11:16:32 +080012#include <boost/date_time/posix_time/posix_time.hpp>
Yingdi Yu68aced92013-10-17 21:13:03 -070013#include "logging.h"
14
Yingdi Yuad56f1c2013-10-10 17:27:54 -070015
Yingdi Yuad56f1c2013-10-10 17:27:54 -070016using namespace ndn;
Yingdi Yu5ff62102013-10-13 17:24:50 -070017using namespace std;
Yingdi Yu64206112013-12-24 11:16:32 +080018using namespace boost::posix_time;
Yingdi Yuad56f1c2013-10-10 17:27:54 -070019
Yingdi Yu68aced92013-10-17 21:13:03 -070020INIT_LOGGER("ProfileData");
21
Yingdi Yu64206112013-12-24 11:16:32 +080022ProfileData::ProfileData()
23 : Data()
24{}
25
Yingdi Yue9ea5c92013-11-06 18:42:34 -080026ProfileData::ProfileData(const Profile& profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070027 : Data()
Yingdi Yue9ea5c92013-11-06 18:42:34 -080028 , m_identity(profile.getIdentityName())
Yingdi Yu5ff62102013-10-13 17:24:50 -070029 , m_profile(profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070030{
Yingdi Yue9ea5c92013-11-06 18:42:34 -080031 Name dataName = m_identity;
Yingdi Yu64206112013-12-24 11:16:32 +080032
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 Yu5ff62102013-10-13 17:24:50 -070036 setName(dataName);
Yingdi Yu64206112013-12-24 11:16:32 +080037
38 string content;
39 profile.encode(&content);
40 setContent((const uint8_t *)&content[0], content.size());
41
Yingdi Yuad56f1c2013-10-10 17:27:54 -070042}
43
Yingdi Yuaccbda92013-12-27 08:44:12 +080044// ProfileData::ProfileData(const ProfileData& profileData)
45// : Data(profileData)
46// , m_identity(profileData.m_identity)
47// , m_profile(profileData.m_profile)
48// {}
Yingdi Yu5ff62102013-10-13 17:24:50 -070049
50ProfileData::ProfileData(const Data& data)
Yingdi Yu64206112013-12-24 11:16:32 +080051 : Data(data)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070052{
Yingdi Yu38e329b2013-10-10 22:11:35 -070053 const Name& dataName = data.getName();
Yingdi Yu64206112013-12-24 11:16:32 +080054 Name::Component appFlag(Name::fromEscapedString("PROFILE"));
Yingdi Yu38e329b2013-10-10 22:11:35 -070055
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 Yu5ff62102013-10-13 17:24:50 -070066 if(profileIndex < 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080067 throw Error("No PROFILE component in data name!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070068
Yingdi Yu64206112013-12-24 11:16:32 +080069 m_identity = dataName.getPrefix(profileIndex);
Yingdi Yu5ff62102013-10-13 17:24:50 -070070
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080071 string encoded(reinterpret_cast<const char*>(data.getContent().value()), data.getContent().value_size());
Yingdi Yu64206112013-12-24 11:16:32 +080072 m_profile = *Profile::decode(encoded);
Yingdi Yuad56f1c2013-10-10 17:27:54 -070073}