blob: 5662ebb48e7eaed9fb3ad5435efb28737ccdcf1b [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 Yu4685b1b2013-10-18 17:05:02 -070018
Yingdi Yufa4ce792014-02-06 18:09:22 -080019namespace chronos{
20
Yingdi Yud04ed1a2013-10-14 14:07:03 -070021class ContactStorage
22{
Yingdi Yu4685b1b2013-10-18 17:05:02 -070023
Yingdi Yud04ed1a2013-10-14 14:07:03 -070024public:
Yingdi Yuf8f572d2014-01-13 11:19:47 -080025 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
26
Yingdi Yu348f5ea2014-03-01 14:47:25 -080027 ContactStorage(const ndn::Name& identity);
Yingdi Yud04ed1a2013-10-14 14:07:03 -070028
Yingdi Yu4685b1b2013-10-18 17:05:02 -070029 ~ContactStorage()
Yingdi Yu348f5ea2014-03-01 14:47:25 -080030 {
31 sqlite3_close(m_db);
32 }
Yingdi Yufa4ce792014-02-06 18:09:22 -080033
34 ndn::shared_ptr<Profile>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080035 getSelfProfile();
Yingdi Yufa4ce792014-02-06 18:09:22 -080036
37 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080038 addSelfEndorseCertificate(const EndorseCertificate& endorseCertificate);
Yingdi Yud04ed1a2013-10-14 14:07:03 -070039
40 void
Yingdi Yufa4ce792014-02-06 18:09:22 -080041 addEndorseCertificate(const EndorseCertificate& endorseCertificate, const ndn::Name& identity);
Yingdi Yu3b318c12013-10-15 17:54:00 -070042
Yingdi Yufa4ce792014-02-06 18:09:22 -080043 void
44 updateCollectEndorse(const EndorseCertificate& endorseCertificate);
45
46 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080047 getCollectEndorse(EndorseCollection& endorseCollection);
Yingdi Yufa4ce792014-02-06 18:09:22 -080048
49 void
50 getEndorseList(const ndn::Name& identity, std::vector<std::string>& endorseList);
51
52
Yingdi Yu3b318c12013-10-15 17:54:00 -070053
54 void
Yingdi Yuae8217c2013-11-09 00:03:26 -080055 removeContact(const ndn::Name& identity);
56
57 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080058 addContact(const Contact& contact);
Yingdi Yud04ed1a2013-10-14 14:07:03 -070059
Yingdi Yu348f5ea2014-03-01 14:47:25 -080060 ndn::shared_ptr<Contact>
61 getContact(const ndn::Name& identity) const;
Yingdi Yufa4ce792014-02-06 18:09:22 -080062
Yingdi Yub2e747d2013-11-05 23:06:43 -080063 void
64 updateIsIntroducer(const ndn::Name& identity, bool isIntroducer);
65
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070066 void
67 updateAlias(const ndn::Name& identity, std::string alias);
68
Yingdi Yu76dd8002013-12-24 11:16:32 +080069 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080070 getAllContacts(std::vector<ndn::shared_ptr<Contact> >& contacts) const;
Yingdi Yu4ef8cf62013-10-23 14:05:12 -070071
Yingdi Yu348f5ea2014-03-01 14:47:25 -080072 void
73 updateDnsSelfProfileData(const ndn::Data& data)
74 {
75 updateDnsData(data.wireEncode(), "N/A", "PROFILE", data.getName().toUri());
76 }
77
78 void
79 updateDnsEndorseOthers(const ndn::Data& data, const std::string& endorsee)
80 {
81 updateDnsData(data.wireEncode(), endorsee, "ENDORSEE", data.getName().toUri());
82 }
Yingdi Yub2e747d2013-11-05 23:06:43 -080083
Yingdi Yu348f5ea2014-03-01 14:47:25 -080084 void
85 updateDnsOthersEndorse(const ndn::Data& data)
86 {
87 updateDnsData(data.wireEncode(), "N/A", "ENDORSED", data.getName().toUri());
88 }
89
90 ndn::shared_ptr<ndn::Data>
91 getDnsData(const ndn::Name& name);
92
93 ndn::shared_ptr<ndn::Data>
94 getDnsData(const std::string& name, const std::string& type);
Yingdi Yub2e747d2013-11-05 23:06:43 -080095
Yingdi Yud04ed1a2013-10-14 14:07:03 -070096private:
Yingdi Yu348f5ea2014-03-01 14:47:25 -080097 std::string
98 getDBName();
99
Yingdi Yufa4ce792014-02-06 18:09:22 -0800100 void
101 initializeTable(const std::string& tableName, const std::string& sqlCreateStmt);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700102
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700103 bool
Yingdi Yu71c01872013-11-03 16:22:05 -0800104 doesContactExist(const ndn::Name& name);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700105
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800106 void
107 updateDnsData(const ndn::Block& data, const std::string& name, const std::string& type, const std::string& dataName);
108
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700109private:
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800110 ndn::Name m_identity;
111
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700112 sqlite3 *m_db;
113};
114
Yingdi Yufa4ce792014-02-06 18:09:22 -0800115}//chronos
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700116#endif