blob: 40a7aee7b1e982360e27050bcd884af2af9ef26c [file] [log] [blame]
Yingdi Yu5ff62102013-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"
Yingdi Yu68aced92013-10-17 21:13:03 -070012#include "logging.h"
Yingdi Yu5ff62102013-10-13 17:24:50 -070013
14using namespace std;
15using namespace ndn;
16
Yingdi Yu68aced92013-10-17 21:13:03 -070017INIT_LOGGER("Profile");
Yingdi Yue9ea5c92013-11-06 18:42:34 -080018
Yingdi Yua1a688f2014-02-06 18:09:22 -080019namespace chronos{
Yingdi Yue9ea5c92013-11-06 18:42:34 -080020
Yingdi Yua1a688f2014-02-06 18:09:22 -080021const string Profile::OID_NAME("2.5.4.41");
22const string Profile::OID_ORG("2.5.4.11");
23const string Profile::OID_GROUP("2.5.4.1");
24const string Profile::OID_HOMEPAGE("2.5.4.3");
25const string Profile::OID_ADVISOR("2.5.4.80");
26const string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
27
28Profile::Profile(const IdentityCertificate& identityCertificate)
Yingdi Yue9ea5c92013-11-06 18:42:34 -080029{
Yingdi Yua1a688f2014-02-06 18:09:22 -080030 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName());
Yingdi Yue9ea5c92013-11-06 18:42:34 -080031
Yingdi Yua1a688f2014-02-06 18:09:22 -080032 m_entries[string("IDENTITY")] = keyName.getPrefix(-1).toUri();
Yingdi Yue9ea5c92013-11-06 18:42:34 -080033
Yingdi Yu64206112013-12-24 11:16:32 +080034 const vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList();
35 vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
Yingdi Yue9ea5c92013-11-06 18:42:34 -080036 for(; it != subList.end(); it++)
37 {
Yingdi Yu64206112013-12-24 11:16:32 +080038 const string oidStr = it->getOidString();
39 string valueStr = it->getValue();
Yingdi Yua1a688f2014-02-06 18:09:22 -080040 if(oidStr == OID_NAME)
41 m_entries["name"] = valueStr;
42 else if(oidStr == OID_ORG)
43 m_entries["institution"] = valueStr;
44 else if(oidStr == OID_GROUP)
45 m_entries["group"] = valueStr;
46 else if(oidStr == OID_HOMEPAGE)
47 m_entries["homepage"] = valueStr;
48 else if(oidStr == OID_ADVISOR)
49 m_entries["advisor"] = valueStr;
50 else if(oidStr == OID_EMAIL)
51 m_entries["email"] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080052 else
Yingdi Yu64206112013-12-24 11:16:32 +080053 m_entries[oidStr] = valueStr;
Yingdi Yue9ea5c92013-11-06 18:42:34 -080054 }
55}
56
Yingdi Yu5ff62102013-10-13 17:24:50 -070057Profile::Profile(const Name& identityName)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070058{
Yingdi Yua1a688f2014-02-06 18:09:22 -080059 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070060}
Yingdi Yu5ff62102013-10-13 17:24:50 -070061
62Profile::Profile(const Name& identityName,
63 const string& name,
64 const string& institution)
Yingdi Yu5ff62102013-10-13 17:24:50 -070065{
Yingdi Yua1a688f2014-02-06 18:09:22 -080066 m_entries["IDENTITY"] = identityName.toUri();
67 m_entries["name"] = name;
68 m_entries["institution"] = institution;
Yingdi Yu5ff62102013-10-13 17:24:50 -070069}
70
71Profile::Profile(const Profile& profile)
Yingdi Yua1a688f2014-02-06 18:09:22 -080072 : m_entries(profile.m_entries)
Yingdi Yu5ff62102013-10-13 17:24:50 -070073{}
74
75void
Yingdi Yua1a688f2014-02-06 18:09:22 -080076Profile::encode(ostream& os) const
Yingdi Yu5ff62102013-10-13 17:24:50 -070077{
Yingdi Yua1a688f2014-02-06 18:09:22 -080078 Chronos::ProfileMsg profileMsg;
79 profileMsg << (*this);
80 profileMsg.SerializeToOstream(&os);
Yingdi Yu5ff62102013-10-13 17:24:50 -070081}
82
Yingdi Yu64206112013-12-24 11:16:32 +080083void
Yingdi Yua1a688f2014-02-06 18:09:22 -080084Profile::decode(istream& is)
Yingdi Yu5ff62102013-10-13 17:24:50 -070085{
Yingdi Yua1a688f2014-02-06 18:09:22 -080086 Chronos::ProfileMsg profileMsg;
87 profileMsg.ParseFromIstream(&is);
88 profileMsg >> (*this);
89}
Yingdi Yu5ff62102013-10-13 17:24:50 -070090
Yingdi Yua1a688f2014-02-06 18:09:22 -080091Chronos::ProfileMsg&
92operator << (Chronos::ProfileMsg& profileMsg, const Profile& profile)
93{
94 map<string, string>::const_iterator it = profile.begin();
95 for(; it != profile.end(); it++)
Yingdi Yu5ff62102013-10-13 17:24:50 -070096 {
Yingdi Yu64206112013-12-24 11:16:32 +080097 Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
98 profileEntry->set_oid(it->first);
99 profileEntry->set_data(it->second);
Yingdi Yu5ff62102013-10-13 17:24:50 -0700100 }
Yingdi Yua1a688f2014-02-06 18:09:22 -0800101 return profileMsg;
Yingdi Yu5ff62102013-10-13 17:24:50 -0700102}
103
Yingdi Yua1a688f2014-02-06 18:09:22 -0800104Chronos::ProfileMsg&
105operator >> (Chronos::ProfileMsg& profileMsg, Profile& profile)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700106{
Yingdi Yu64206112013-12-24 11:16:32 +0800107 for(int i = 0; i < profileMsg.entry_size(); i++)
Yingdi Yu5ff62102013-10-13 17:24:50 -0700108 {
Yingdi Yu64206112013-12-24 11:16:32 +0800109 const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
Yingdi Yua1a688f2014-02-06 18:09:22 -0800110 profile[profileEntry.oid()] = profileEntry.data();
Yingdi Yu5ff62102013-10-13 17:24:50 -0700111 }
Yingdi Yua1a688f2014-02-06 18:09:22 -0800112
113 return profileMsg;
Yingdi Yu5ff62102013-10-13 17:24:50 -0700114}
Yingdi Yua1a688f2014-02-06 18:09:22 -0800115
116}//chronos