blob: 1dd4804db3209b428d5727d5e7aa4d2179a578d4 [file] [log] [blame]
Yingdi Yud04ed1a2013-10-14 14:07:03 -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 Yufa4ce792014-02-06 18:09:22 -080011#ifndef CHRONOS_CONTACT_STORAGE_H
12#define CHRONOS_CONTACT_STORAGE_H
Yingdi Yud04ed1a2013-10-14 14:07:03 -070013
Yingdi Yu348f5ea2014-03-01 14:47:25 -080014#include "contact.h"
15#include "endorse-collection.pb.h"
Yingdi Yufa4ce792014-02-06 18:09:22 -080016#include <sqlite3.h>
Yingdi Yud04ed1a2013-10-14 14:07:03 -070017
Yingdi Yufa4ce792014-02-06 18:09:22 -080018namespace chronos{
19
Yingdi Yud04ed1a2013-10-14 14:07:03 -070020class ContactStorage
21{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070022
Yingdi Yud04ed1a2013-10-14 14:07:03 -070023public:
Yingdi Yuf8f572d2014-01-13 11:19:47 -080024 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
25
Yingdi Yu348f5ea2014-03-01 14:47:25 -080026 ContactStorage(const ndn::Name& identity);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070027
28 ~ContactStorage()
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -070030 sqlite3_close(m_db);
Yingdi Yu348f5ea2014-03-01 14:47:25 -080031 }
Yingdi Yufa4ce792014-02-06 18:09:22 -080032
33 ndn::shared_ptr<Profile>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080034 getSelfProfile();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070035
Yingdi Yufa4ce792014-02-06 18:09:22 -080036 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080037 addSelfEndorseCertificate(const EndorseCertificate& endorseCertificate);
Yingdi Yud04ed1a2013-10-14 14:07:03 -070038
39 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080040 addEndorseCertificate(const EndorseCertificate& endorseCertificate, const ndn::Name& identity);
Yingdi Yu3b318c12013-10-15 17:54:00 -070041
Yingdi Yufa4ce792014-02-06 18:09:22 -080042 void
43 updateCollectEndorse(const EndorseCertificate& endorseCertificate);
44
45 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080046 getCollectEndorse(EndorseCollection& endorseCollection);
Yingdi Yufa4ce792014-02-06 18:09:22 -080047
48 void
49 getEndorseList(const ndn::Name& identity, std::vector<std::string>& endorseList);
50
51
Yingdi Yu3b318c12013-10-15 17:54:00 -070052
53 void
Yingdi Yuae8217c2013-11-09 00:03:26 -080054 removeContact(const ndn::Name& identity);
55
56 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080057 addContact(const Contact& contact);
Yingdi Yud04ed1a2013-10-14 14:07:03 -070058
Yingdi Yu348f5ea2014-03-01 14:47:25 -080059 ndn::shared_ptr<Contact>
60 getContact(const ndn::Name& identity) const;
Yingdi Yufa4ce792014-02-06 18:09:22 -080061
Yingdi Yub2e747d2013-11-05 23:06:43 -080062 void
63 updateIsIntroducer(const ndn::Name& identity, bool isIntroducer);
64
Yingdi Yufa0b6a02014-04-30 14:26:42 -070065 void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070066 updateAlias(const ndn::Name& identity, std::string alias);
67
Yingdi Yu76dd8002013-12-24 11:16:32 +080068 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080069 getAllContacts(std::vector<ndn::shared_ptr<Contact> >& contacts) const;
Yingdi Yu4ef8cf62013-10-23 14:05:12 -070070
Yingdi Yu348f5ea2014-03-01 14:47:25 -080071 void
72 updateDnsSelfProfileData(const ndn::Data& data)
73 {
74 updateDnsData(data.wireEncode(), "N/A", "PROFILE", data.getName().toUri());
75 }
76
77 void
78 updateDnsEndorseOthers(const ndn::Data& data, const std::string& endorsee)
79 {
80 updateDnsData(data.wireEncode(), endorsee, "ENDORSEE", data.getName().toUri());
81 }
Yingdi Yufa0b6a02014-04-30 14:26:42 -070082
Yingdi Yu348f5ea2014-03-01 14:47:25 -080083 void
84 updateDnsOthersEndorse(const ndn::Data& data)
85 {
Yingdi Yufa0b6a02014-04-30 14:26:42 -070086 updateDnsData(data.wireEncode(), "N/A", "ENDORSED", data.getName().toUri());
Yingdi Yu348f5ea2014-03-01 14:47:25 -080087 }
88
89 ndn::shared_ptr<ndn::Data>
90 getDnsData(const ndn::Name& name);
91
92 ndn::shared_ptr<ndn::Data>
93 getDnsData(const std::string& name, const std::string& type);
Yingdi Yub2e747d2013-11-05 23:06:43 -080094
Yingdi Yud04ed1a2013-10-14 14:07:03 -070095private:
Yingdi Yu348f5ea2014-03-01 14:47:25 -080096 std::string
97 getDBName();
Yingdi Yufa0b6a02014-04-30 14:26:42 -070098
Yingdi Yufa4ce792014-02-06 18:09:22 -080099 void
100 initializeTable(const std::string& tableName, const std::string& sqlCreateStmt);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700101
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700102 bool
Yingdi Yu71c01872013-11-03 16:22:05 -0800103 doesContactExist(const ndn::Name& name);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700104
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800105 void
106 updateDnsData(const ndn::Block& data, const std::string& name, const std::string& type, const std::string& dataName);
107
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700108private:
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800109 ndn::Name m_identity;
110
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700111 sqlite3 *m_db;
112};
113
Yingdi Yufa4ce792014-02-06 18:09:22 -0800114}//chronos
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700115#endif