blob: e5c5325185fa0d3e4843da5d2b80befe5bfeff23 [file] [log] [blame]
Yingdi Yub4be64a2013-10-13 17:24:50 -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.h"
12#include <ndn.cxx/helpers/der/der.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070013#include <ndn.cxx/helpers/der/visitor/print-visitor.h>
Yingdi Yub4be64a2013-10-13 17:24:50 -070014#include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
Yingdi Yu92e8e482013-10-17 21:13:03 -070015#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070016
17using namespace std;
18using namespace ndn;
19
Yingdi Yu92e8e482013-10-17 21:13:03 -070020INIT_LOGGER("Profile");
Yingdi Yub4be64a2013-10-13 17:24:50 -070021Profile::Profile(const Name& identityName)
22 : m_identityName(identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070023{
24 const string& nameString = identityName.toUri();
25 Blob identityBlob (nameString.c_str(), nameString.size());
26 m_entries[string("IDENTITY")] = identityBlob;
27}
Yingdi Yub4be64a2013-10-13 17:24:50 -070028
29Profile::Profile(const Name& identityName,
30 const string& name,
31 const string& institution)
32 : m_identityName(identityName)
33{
Yingdi Yu3b318c12013-10-15 17:54:00 -070034 const string& nameString = identityName.toUri();
35 Blob identityBlob (nameString.c_str(), nameString.size());
36 m_entries[string("IDENTITY")] = identityBlob;
37
Yingdi Yub4be64a2013-10-13 17:24:50 -070038 Blob nameBlob (name.c_str(), name.size());
39 Blob institutionBlob (institution.c_str(), institution.size());
40
41 m_entries[string("name")] = nameBlob;
42 m_entries[string("institution")] = institutionBlob;
43}
44
45Profile::Profile(const Profile& profile)
46 : m_identityName(profile.m_identityName)
47 , m_entries(profile.m_entries)
48{}
49
50void
51Profile::setProfileEntry(const string& profileType,
52 const Blob& profileValue)
53{ m_entries[profileType] = profileValue; }
54
55Ptr<const Blob>
Yingdi Yudbeb8e22013-10-14 09:36:31 -070056Profile::getProfileEntry(const string& profileType) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070057{
58 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yudbeb8e22013-10-14 09:36:31 -070059 return Ptr<Blob>(new Blob(m_entries.at(profileType).buf(), m_entries.at(profileType).size()));
Yingdi Yub4be64a2013-10-13 17:24:50 -070060
61 return NULL;
62}
63
64Ptr<Blob>
65Profile::toDerBlob() const
66{
67 Ptr<der::DerSequence> root = Ptr<der::DerSequence>::Create();
68
69 Ptr<der::DerPrintableString> identityName = Ptr<der::DerPrintableString>(new der::DerPrintableString(m_identityName.toUri()));
70 root->addChild(identityName);
71
72 map<string, Blob>::const_iterator it = m_entries.begin();
73 for(; it != m_entries.end(); it++)
74 {
75 Ptr<der::DerSequence> entry = Ptr<der::DerSequence>::Create();
76 Ptr<der::DerPrintableString> type = Ptr<der::DerPrintableString>(new der::DerPrintableString(it->first));
77 Ptr<der::DerOctetString> value = Ptr<der::DerOctetString>(new der::DerOctetString(it->second));
78 entry->addChild(type);
79 entry->addChild(value);
80 root->addChild(entry);
81 }
82
83 blob_stream blobStream;
84 OutputIterator & start = reinterpret_cast<OutputIterator &> (blobStream);
85 root->encode(start);
86
87 return blobStream.buf ();
88}
89
90Ptr<Profile>
91Profile::fromDerBlob(const Blob& derBlob)
92{
93 boost::iostreams::stream
94 <boost::iostreams::array_source> is (derBlob.buf(), derBlob.size());
95
96 Ptr<der::DerSequence> root = DynamicCast<der::DerSequence>(der::DerNode::parse(reinterpret_cast<InputIterator &>(is)));
Yingdi Yub4be64a2013-10-13 17:24:50 -070097 const der::DerNodePtrList & children = root->getChildren();
98 der::SimpleVisitor simpleVisitor;
99 string identityName = boost::any_cast<string>(children[0]->accept(simpleVisitor));
100 Ptr<Profile> profile = Ptr<Profile>(new Profile(identityName));
101
102 for(int i = 1; i < children.size(); i++)
103 {
104 Ptr<der::DerSequence> entry = DynamicCast<der::DerSequence>(children[i]);
Yingdi Yu92e8e482013-10-17 21:13:03 -0700105 const der::DerNodePtrList & tuple = entry->getChildren();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700106 string type = boost::any_cast<string>(tuple[0]->accept(simpleVisitor));
107 Ptr<Blob> value = boost::any_cast<Ptr<Blob> >(tuple[1]->accept(simpleVisitor));
108 profile->setProfileEntry(type, *value);
109 }
110
111 return profile;
112}