blob: 47c873ab6ddc8d60e5372481c6c6c83db023d3e4 [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
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "profile.hpp"
Yingdi Yu92e8e482013-10-17 21:13:03 -070012#include "logging.h"
Yingdi Yub4be64a2013-10-13 17:24:50 -070013
Yingdi Yueb692ac2015-02-10 18:46:18 -080014namespace chronochat {
Yingdi Yu2e3199c2013-11-06 18:42:34 -080015
Yingdi Yu0b0a7362014-08-05 16:31:30 -070016using std::vector;
17using std::string;
18using std::map;
19
20using ndn::IdentityCertificate;
21using ndn::CertificateSubjectDescription;
22
Yingdi Yu17032f82014-03-25 15:48:23 -070023const std::string Profile::OID_NAME("2.5.4.41");
24const std::string Profile::OID_ORG("2.5.4.11");
25const std::string Profile::OID_GROUP("2.5.4.1");
26const std::string Profile::OID_HOMEPAGE("2.5.4.3");
27const std::string Profile::OID_ADVISOR("2.5.4.80");
28const std::string Profile::OID_EMAIL("1.2.840.113549.1.9.1");
Yingdi Yufa4ce792014-02-06 18:09:22 -080029
30Profile::Profile(const IdentityCertificate& identityCertificate)
Yingdi Yu2e3199c2013-11-06 18:42:34 -080031{
Yingdi Yufa4ce792014-02-06 18:09:22 -080032 Name keyName = IdentityCertificate::certificateNameToPublicKeyName(identityCertificate.getName());
Yingdi Yu2e3199c2013-11-06 18:42:34 -080033
Yingdi Yu0b0a7362014-08-05 16:31:30 -070034 m_entries[string("IDENTITY")] = keyName.getPrefix(-1).toUri();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070035
Yingdi Yu0b0a7362014-08-05 16:31:30 -070036 const vector<CertificateSubjectDescription>& subList =
37 identityCertificate.getSubjectDescriptionList();
38
39 for (vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
40 it != subList.end(); it++) {
41 const string oidStr = it->getOidString();
42 string valueStr = it->getValue();
43 if (oidStr == OID_NAME)
44 m_entries["name"] = valueStr;
45 else if (oidStr == OID_ORG)
46 m_entries["institution"] = valueStr;
47 else if (oidStr == OID_GROUP)
48 m_entries["group"] = valueStr;
49 else if (oidStr == OID_HOMEPAGE)
50 m_entries["homepage"] = valueStr;
51 else if (oidStr == OID_ADVISOR)
52 m_entries["advisor"] = valueStr;
53 else if (oidStr == OID_EMAIL)
54 m_entries["email"] = valueStr;
55 else
56 m_entries[oidStr] = valueStr;
57 }
Yingdi Yu2e3199c2013-11-06 18:42:34 -080058}
59
Yingdi Yub4be64a2013-10-13 17:24:50 -070060Profile::Profile(const Name& identityName)
Yingdi Yu3b318c12013-10-15 17:54:00 -070061{
Yingdi Yufa4ce792014-02-06 18:09:22 -080062 m_entries["IDENTITY"] = identityName.toUri();
Yingdi Yu3b318c12013-10-15 17:54:00 -070063}
Yingdi Yub4be64a2013-10-13 17:24:50 -070064
65Profile::Profile(const Name& identityName,
Yingdi Yu0b0a7362014-08-05 16:31:30 -070066 const string& name,
67 const string& institution)
Yingdi Yub4be64a2013-10-13 17:24:50 -070068{
Yingdi Yufa4ce792014-02-06 18:09:22 -080069 m_entries["IDENTITY"] = identityName.toUri();
70 m_entries["name"] = name;
71 m_entries["institution"] = institution;
Yingdi Yub4be64a2013-10-13 17:24:50 -070072}
73
74Profile::Profile(const Profile& profile)
Yingdi Yufa4ce792014-02-06 18:09:22 -080075 : m_entries(profile.m_entries)
Yingdi Yu0b0a7362014-08-05 16:31:30 -070076{
77}
Yingdi Yub4be64a2013-10-13 17:24:50 -070078
79void
Yingdi Yu17032f82014-03-25 15:48:23 -070080Profile::encode(std::ostream& os) const
Yingdi Yub4be64a2013-10-13 17:24:50 -070081{
Yingdi Yueb692ac2015-02-10 18:46:18 -080082 chronochat::ProfileMsg profileMsg;
Yingdi Yufa4ce792014-02-06 18:09:22 -080083 profileMsg << (*this);
84 profileMsg.SerializeToOstream(&os);
Yingdi Yub4be64a2013-10-13 17:24:50 -070085}
86
Yingdi Yu76dd8002013-12-24 11:16:32 +080087void
Yingdi Yu17032f82014-03-25 15:48:23 -070088Profile::decode(std::istream& is)
Yingdi Yub4be64a2013-10-13 17:24:50 -070089{
Yingdi Yueb692ac2015-02-10 18:46:18 -080090 chronochat::ProfileMsg profileMsg;
Yingdi Yufa4ce792014-02-06 18:09:22 -080091 profileMsg.ParseFromIstream(&is);
92 profileMsg >> (*this);
93}
Yingdi Yub4be64a2013-10-13 17:24:50 -070094
Yingdi Yu0b0a7362014-08-05 16:31:30 -070095bool
96Profile::operator==(const Profile& profile) const
Yingdi Yufa4ce792014-02-06 18:09:22 -080097{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070098 if (m_entries.size() != profile.m_entries.size())
99 return false;
100
101 for(map<string, string>::const_iterator it = m_entries.begin(); it != m_entries.end(); it++) {
102 map<string, string>::const_iterator found = profile.m_entries.find(it->first);
103 if (found == profile.m_entries.end())
104 return false;
105 if (found->second != it->second)
106 return false;
107 }
108
109 return true;
110}
111
112bool
113Profile::operator!=(const Profile& profile) const
114{
115 return !(*this == profile);
116}
117
Yingdi Yueb692ac2015-02-10 18:46:18 -0800118chronochat::ProfileMsg&
119operator<<(chronochat::ProfileMsg& profileMsg, const Profile& profile)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700120{
121;
122 for (map<string, string>::const_iterator it = profile.begin(); it != profile.end(); it++) {
Yingdi Yueb692ac2015-02-10 18:46:18 -0800123 chronochat::ProfileMsg::ProfileEntry* profileEntry = profileMsg.add_entry();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700124 profileEntry->set_oid(it->first);
125 profileEntry->set_data(it->second);
126 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800127 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700128}
129
Yingdi Yueb692ac2015-02-10 18:46:18 -0800130chronochat::ProfileMsg&
131operator>>(chronochat::ProfileMsg& profileMsg, Profile& profile)
Yingdi Yub4be64a2013-10-13 17:24:50 -0700132{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700133 for (int i = 0; i < profileMsg.entry_size(); i++) {
Yingdi Yueb692ac2015-02-10 18:46:18 -0800134 const chronochat::ProfileMsg::ProfileEntry& profileEntry = profileMsg.entry(i);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700135 profile[profileEntry.oid()] = profileEntry.data();
136 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700137
Yingdi Yufa4ce792014-02-06 18:09:22 -0800138 return profileMsg;
Yingdi Yub4be64a2013-10-13 17:24:50 -0700139}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800140
Yingdi Yueb692ac2015-02-10 18:46:18 -0800141} // namespace chronochat