blob: 6bb70cc15c147098da05351684d9e7d845c7cbac [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 Yu38e329b2013-10-10 22:11:35 -070013#include "exception.h"
Yingdi Yu68aced92013-10-17 21:13:03 -070014#include "logging.h"
15
Yingdi Yuad56f1c2013-10-10 17:27:54 -070016
Yingdi Yuad56f1c2013-10-10 17:27:54 -070017using namespace ndn;
Yingdi Yu5ff62102013-10-13 17:24:50 -070018using namespace std;
Yingdi Yu64206112013-12-24 11:16:32 +080019using namespace boost::posix_time;
Yingdi Yuad56f1c2013-10-10 17:27:54 -070020
Yingdi Yu68aced92013-10-17 21:13:03 -070021INIT_LOGGER("ProfileData");
22
Yingdi Yu64206112013-12-24 11:16:32 +080023ProfileData::ProfileData()
24 : Data()
25{}
26
Yingdi Yue9ea5c92013-11-06 18:42:34 -080027ProfileData::ProfileData(const Profile& profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070028 : Data()
Yingdi Yue9ea5c92013-11-06 18:42:34 -080029 , m_identity(profile.getIdentityName())
Yingdi Yu5ff62102013-10-13 17:24:50 -070030 , m_profile(profile)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070031{
Yingdi Yue9ea5c92013-11-06 18:42:34 -080032 Name dataName = m_identity;
Yingdi Yu64206112013-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 Yu5ff62102013-10-13 17:24:50 -070037 setName(dataName);
Yingdi Yu64206112013-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 Yuad56f1c2013-10-10 17:27:54 -070045}
46
Yingdi Yu5ff62102013-10-13 17:24:50 -070047ProfileData::ProfileData(const ProfileData& profileData)
Yingdi Yu64206112013-12-24 11:16:32 +080048 : Data(profileData)
Yingdi Yu5ff62102013-10-13 17:24:50 -070049 , m_identity(profileData.m_identity)
50 , m_profile(profileData.m_profile)
Yingdi Yu64206112013-12-24 11:16:32 +080051{}
Yingdi Yu5ff62102013-10-13 17:24:50 -070052
53ProfileData::ProfileData(const Data& data)
Yingdi Yu64206112013-12-24 11:16:32 +080054 : Data(data)
Yingdi Yuad56f1c2013-10-10 17:27:54 -070055{
Yingdi Yu38e329b2013-10-10 22:11:35 -070056 const Name& dataName = data.getName();
Yingdi Yu64206112013-12-24 11:16:32 +080057 Name::Component appFlag(Name::fromEscapedString("PROFILE"));
Yingdi Yu38e329b2013-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 Yu5ff62102013-10-13 17:24:50 -070069 if(profileIndex < 0)
Yingdi Yu38e329b2013-10-10 22:11:35 -070070 throw LnException("No PROFILE component in data name!");
Yingdi Yu5ff62102013-10-13 17:24:50 -070071
Yingdi Yu64206112013-12-24 11:16:32 +080072 m_identity = dataName.getPrefix(profileIndex);
Yingdi Yu5ff62102013-10-13 17:24:50 -070073
Yingdi Yu64206112013-12-24 11:16:32 +080074 string encoded((const char*)data.getContent().buf(), data.getContent().size());
75 m_profile = *Profile::decode(encoded);
Yingdi Yuad56f1c2013-10-10 17:27:54 -070076}