blob: af805b0f738c3a4cdce0209c463a5531422d441e [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"
Yingdi Yu76dd8002013-12-24 11:16:32 +080012#include "null-ptrs.h"
Yingdi Yu92e8e482013-10-17 21:13:03 -070013#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070014
15using namespace std;
16using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080017using namespace ndn::ptr_lib;
Yingdi Yub4be64a2013-10-13 17:24:50 -070018
Yingdi Yu92e8e482013-10-17 21:13:03 -070019INIT_LOGGER("Profile");
Yingdi Yu2e3199c2013-11-06 18:42:34 -080020
21static string nameOid("2.5.4.41");
22static string orgOid("2.5.4.11");
23static string groupOid("2.5.4.1");
24static string homepageOid("2.5.4.3");
25static string advisor("2.5.4.80");
26static string emailOid("1.2.840.113549.1.9.1");
27
Yingdi Yu76dd8002013-12-24 11:16:32 +080028Profile::Profile(const IdentityCertificate& oldIdentityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080029{
Yingdi Yu76dd8002013-12-24 11:16:32 +080030 IdentityCertificate identityCertificate(oldIdentityCertificate);
Yingdi Yu2e3199c2013-11-06 18:42:34 -080031
32 Name keyName = identityCertificate.getPublicKeyName();
Yingdi Yu76dd8002013-12-24 11:16:32 +080033 m_identityName = keyName.getPrefix(-1);
Yingdi Yu2e3199c2013-11-06 18:42:34 -080034
35 const string& identityString = m_identityName.toUri();
Yingdi Yu76dd8002013-12-24 11:16:32 +080036 m_entries[string("IDENTITY")] = identityString;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080037
Yingdi Yu76dd8002013-12-24 11:16:32 +080038 const vector<CertificateSubjectDescription>& subList = identityCertificate.getSubjectDescriptionList();
39 vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080040 for(; it != subList.end(); it++)
41 {
Yingdi Yu76dd8002013-12-24 11:16:32 +080042 const string oidStr = it->getOidString();
43 string valueStr = it->getValue();
Yingdi Yu2e3199c2013-11-06 18:42:34 -080044 if(oidStr == nameOid)
Yingdi Yu76dd8002013-12-24 11:16:32 +080045 m_entries[string("name")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080046 else if(oidStr == orgOid)
Yingdi Yu76dd8002013-12-24 11:16:32 +080047 m_entries[string("institution")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080048 else if(oidStr == groupOid)
Yingdi Yu76dd8002013-12-24 11:16:32 +080049 m_entries[string("group")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080050 else if(oidStr == homepageOid)
Yingdi Yu76dd8002013-12-24 11:16:32 +080051 m_entries[string("homepage")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080052 else if(oidStr == advisor)
Yingdi Yu76dd8002013-12-24 11:16:32 +080053 m_entries[string("advisor")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080054 else if(oidStr == emailOid)
Yingdi Yu76dd8002013-12-24 11:16:32 +080055 m_entries[string("email")] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080056 else
Yingdi Yu76dd8002013-12-24 11:16:32 +080057 m_entries[oidStr] = valueStr;
Yingdi Yu2e3199c2013-11-06 18:42:34 -080058 }
59}
60
Yingdi Yub4be64a2013-10-13 17:24:50 -070061Profile::Profile(const Name& identityName)
62 : m_identityName(identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070063{
Yingdi Yu76dd8002013-12-24 11:16:32 +080064 const string& identityString = identityName.toUri();
65 m_entries[string("IDENTITY")] = identityString;
Yingdi Yu3b318c12013-10-15 17:54:00 -070066}
Yingdi Yub4be64a2013-10-13 17:24:50 -070067
68Profile::Profile(const Name& identityName,
69 const string& name,
70 const string& institution)
71 : m_identityName(identityName)
72{
Yingdi Yu76dd8002013-12-24 11:16:32 +080073 const string& identityString = identityName.toUri();
74 m_entries[string("IDENTITY")] = identityString;
Yingdi Yu3b318c12013-10-15 17:54:00 -070075
Yingdi Yu76dd8002013-12-24 11:16:32 +080076 m_entries[string("name")] = name;
77 m_entries[string("institution")] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070078}
79
80Profile::Profile(const Profile& profile)
81 : m_identityName(profile.m_identityName)
82 , m_entries(profile.m_entries)
83{}
84
85void
86Profile::setProfileEntry(const string& profileType,
Yingdi Yu76dd8002013-12-24 11:16:32 +080087 const string& profileValue)
Yingdi Yub4be64a2013-10-13 17:24:50 -070088{ m_entries[profileType] = profileValue; }
89
Yingdi Yu76dd8002013-12-24 11:16:32 +080090string
Yingdi Yudbeb8e22013-10-14 09:36:31 -070091Profile::getProfileEntry(const string& profileType) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070092{
93 if(m_entries.find(profileType) != m_entries.end())
Yingdi Yu76dd8002013-12-24 11:16:32 +080094 return m_entries.at(profileType);
Yingdi Yub4be64a2013-10-13 17:24:50 -070095
Yingdi Yu76dd8002013-12-24 11:16:32 +080096 return CHRONOCHAT_NULL_STR;
Yingdi Yub4be64a2013-10-13 17:24:50 -070097}
98
Yingdi Yu76dd8002013-12-24 11:16:32 +080099void
100Profile::encode(string* output) const
Yingdi Yub4be64a2013-10-13 17:24:50 -0700101{
Yingdi Yub4be64a2013-10-13 17:24:50 -0700102
Yingdi Yu76dd8002013-12-24 11:16:32 +0800103 Chronos::ProfileMsg profileMsg;
104
105 profileMsg.set_identityname(m_identityName.toUri());
106
107 map<string, string>::const_iterator it = m_entries.begin();
Yingdi Yub4be64a2013-10-13 17:24:50 -0700108 for(; it != m_entries.end(); it++)
109 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800110 Chronos::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
111 profileEntry->set_oid(it->first);
112 profileEntry->set_data(it->second);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700113 }
Yingdi Yub4be64a2013-10-13 17:24:50 -0700114
Yingdi Yu76dd8002013-12-24 11:16:32 +0800115 profileMsg.SerializeToString(output);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700116}
117
Yingdi Yu76dd8002013-12-24 11:16:32 +0800118shared_ptr<Profile>
119Profile::decode(const string& input)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700120{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800121 Chronos::ProfileMsg profileMsg;
122
123 profileMsg.ParseFromString(input);
Yingdi Yub4be64a2013-10-13 17:24:50 -0700124
Yingdi Yu76dd8002013-12-24 11:16:32 +0800125 shared_ptr<Profile> profile = make_shared<Profile>(profileMsg.identityname());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700126
Yingdi Yu76dd8002013-12-24 11:16:32 +0800127 for(int i = 0; i < profileMsg.entry_size(); i++)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700128 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800129 const Chronos::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
130 profile->setProfileEntry(profileEntry.oid(), profileEntry.data());
Yingdi Yub4be64a2013-10-13 17:24:50 -0700131 }
132
133 return profile;
134}