Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 1 | /* -*- 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 "contact-storage.h" |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 12 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 13 | #include <boost/filesystem.hpp> |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 14 | #include "logging.h" |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | using namespace ndn; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 18 | using namespace ndn::ptr_lib; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 19 | namespace fs = boost::filesystem; |
| 20 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 21 | INIT_LOGGER ("ContactStorage"); |
| 22 | |
| 23 | const string INIT_SP_TABLE = "\ |
| 24 | CREATE TABLE IF NOT EXISTS \n \ |
| 25 | SelfProfile( \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 26 | profile_identity BLOB NOT NULL, \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 27 | profile_type BLOB NOT NULL, \n \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 28 | profile_value BLOB NOT NULL, \n \ |
| 29 | \ |
| 30 | PRIMARY KEY (profile_identity, profile_type) \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 31 | ); \n \ |
| 32 | \ |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 33 | CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 34 | "; |
| 35 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 36 | const string INIT_SE_TABLE = "\ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 37 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 38 | SelfEndorse( \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 39 | identity BLOB NOT NULL UNIQUE, \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 40 | endorse_data BLOB NOT NULL, \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 41 | \ |
| 42 | PRIMARY KEY (identity) \n \ |
| 43 | ); \n \ |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 44 | CREATE INDEX se_index ON SelfEndorse(identity); \n \ |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 45 | "; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 46 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 47 | const string INIT_CONTACT_TABLE = "\ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 48 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 49 | Contact( \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 50 | contact_namespace BLOB NOT NULL, \n \ |
| 51 | contact_alias BLOB NOT NULL, \n \ |
| 52 | self_certificate BLOB NOT NULL, \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 53 | is_introducer INTEGER DEFAULT 0, \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 54 | \ |
| 55 | PRIMARY KEY (contact_namespace) \n \ |
| 56 | ); \n \ |
| 57 | \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 58 | CREATE INDEX contact_index ON Contact(contact_namespace); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 59 | "; |
| 60 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 61 | const string INIT_TS_TABLE = "\ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 62 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 63 | TrustScope( \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 64 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 65 | contact_namespace BLOB NOT NULL, \n \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 66 | trust_scope BLOB NOT NULL \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 67 | ); \n \ |
| 68 | \ |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 69 | CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \ |
| 70 | "; |
| 71 | |
| 72 | const string INIT_CP_TABLE = "\ |
| 73 | CREATE TABLE IF NOT EXISTS \n \ |
| 74 | ContactProfile( \n \ |
| 75 | profile_identity BLOB NOT NULL, \n \ |
| 76 | profile_type BLOB NOT NULL, \n \ |
| 77 | profile_value BLOB NOT NULL, \n \ |
| 78 | endorse INTEGER NOT NULL, \n \ |
| 79 | \ |
| 80 | PRIMARY KEY (profile_identity, profile_type) \n \ |
| 81 | ); \n \ |
| 82 | \ |
| 83 | CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \ |
| 84 | "; |
| 85 | |
| 86 | const string INIT_PE_TABLE = "\ |
| 87 | CREATE TABLE IF NOT EXISTS \n \ |
| 88 | ProfileEndorse( \n \ |
| 89 | identity BLOB NOT NULL UNIQUE, \n \ |
| 90 | endorse_data BLOB NOT NULL, \n \ |
| 91 | \ |
| 92 | PRIMARY KEY (identity) \n \ |
| 93 | ); \n \ |
| 94 | \ |
| 95 | CREATE INDEX pe_index ON ProfileEndorse(identity); \n \ |
| 96 | "; |
| 97 | |
| 98 | const string INIT_CE_TABLE = "\ |
| 99 | CREATE TABLE IF NOT EXISTS \n \ |
| 100 | CollectEndorse( \n \ |
| 101 | endorsee BLOB NOT NULL, \n \ |
| 102 | endorser BLOB NOT NULL, \n \ |
| 103 | endorse_name BLOB NOT NULL, \n \ |
| 104 | endorse_data BLOB NOT NULL, \n \ |
| 105 | \ |
| 106 | PRIMARY KEY (endorsee, endorser) \n \ |
| 107 | ); \n \ |
| 108 | \ |
| 109 | CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \ |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 110 | "; |
| 111 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 112 | ContactStorage::ContactStorage() |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 113 | { |
| 114 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 115 | fs::create_directories (chronosDir); |
| 116 | |
| 117 | int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db); |
| 118 | if (res != SQLITE_OK) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 119 | throw Error("Chronos DB cannot be open/created"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 120 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 121 | // Check if SelfProfile table exists |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 122 | sqlite3_stmt *stmt; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 123 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0); |
| 124 | res = sqlite3_step (stmt); |
| 125 | |
| 126 | bool spTableExist = false; |
| 127 | if (res == SQLITE_ROW) |
| 128 | spTableExist = true; |
| 129 | sqlite3_finalize (stmt); |
| 130 | |
| 131 | if(!spTableExist) |
| 132 | { |
| 133 | char *errmsg = 0; |
| 134 | res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 135 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 136 | throw Error("Init \"error\" in SelfProfile"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 139 | // Check if SelfEndorse table exists |
| 140 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfEndorse'", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 141 | res = sqlite3_step (stmt); |
| 142 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 143 | bool seTableExist = false; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 144 | if (res == SQLITE_ROW) |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 145 | seTableExist = true; |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 146 | sqlite3_finalize (stmt); |
| 147 | |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 148 | if(!seTableExist) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 149 | { |
| 150 | char *errmsg = 0; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 151 | res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 152 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 153 | throw Error("Init \"error\" in SelfEndorse"); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 157 | // Check if Contact table exists |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 158 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='Contact'", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 159 | res = sqlite3_step (stmt); |
| 160 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 161 | bool contactTableExist = false; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 162 | if (res == SQLITE_ROW) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 163 | contactTableExist = true; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 164 | sqlite3_finalize (stmt); |
| 165 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 166 | if(!contactTableExist) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 167 | { |
| 168 | char *errmsg = 0; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 169 | res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 170 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 171 | throw Error("Init \"error\" in Contact"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 172 | } |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 173 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 174 | // Check if TrustScope table exists |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 175 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustScope'", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 176 | res = sqlite3_step (stmt); |
| 177 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 178 | bool tsTableExist = false; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 179 | if (res == SQLITE_ROW) |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 180 | tsTableExist = true; |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 181 | sqlite3_finalize (stmt); |
| 182 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 183 | if(!tsTableExist) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 184 | { |
| 185 | char *errmsg = 0; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 186 | res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 187 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 188 | throw Error("Init \"error\" in TrustScope"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 189 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 190 | |
| 191 | // Check if ContactProfile table exists |
| 192 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ContactProfile'", -1, &stmt, 0); |
| 193 | res = sqlite3_step (stmt); |
| 194 | |
| 195 | bool cpTableExist = false; |
| 196 | if (res == SQLITE_ROW) |
| 197 | cpTableExist = true; |
| 198 | sqlite3_finalize (stmt); |
| 199 | |
| 200 | if(!cpTableExist) |
| 201 | { |
| 202 | char *errmsg = 0; |
| 203 | res = sqlite3_exec (m_db, INIT_CP_TABLE.c_str (), NULL, NULL, &errmsg); |
| 204 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 205 | throw Error("Init \"error\" in ContactProfile"); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Check if ProfileEndorse table exists |
| 209 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ProfileEndorse'", -1, &stmt, 0); |
| 210 | res = sqlite3_step (stmt); |
| 211 | |
| 212 | bool peTableExist = false; |
| 213 | if (res == SQLITE_ROW) |
| 214 | peTableExist = true; |
| 215 | sqlite3_finalize (stmt); |
| 216 | |
| 217 | if(!peTableExist) |
| 218 | { |
| 219 | char *errmsg = 0; |
| 220 | res = sqlite3_exec (m_db, INIT_PE_TABLE.c_str (), NULL, NULL, &errmsg); |
| 221 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 222 | throw Error("Init \"error\" in ProfileEndorse"); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Check if CollectEndorse table exists |
| 226 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='CollectEndorse'", -1, &stmt, 0); |
| 227 | res = sqlite3_step (stmt); |
| 228 | |
| 229 | bool ceTableExist = false; |
| 230 | if (res == SQLITE_ROW) |
| 231 | ceTableExist = true; |
| 232 | sqlite3_finalize (stmt); |
| 233 | |
| 234 | if(!ceTableExist) |
| 235 | { |
| 236 | char *errmsg = 0; |
| 237 | res = sqlite3_exec (m_db, INIT_CE_TABLE.c_str (), NULL, NULL, &errmsg); |
| 238 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 239 | throw Error("Init \"error\" in CollectEndorse"); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 240 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 243 | bool |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 244 | ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 245 | { |
| 246 | bool result = false; |
| 247 | |
| 248 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 249 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 250 | sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 251 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 252 | |
| 253 | int res = sqlite3_step (stmt); |
| 254 | |
| 255 | if (res == SQLITE_ROW) |
| 256 | { |
| 257 | int countAll = sqlite3_column_int (stmt, 0); |
| 258 | if (countAll > 0) |
| 259 | result = true; |
| 260 | } |
| 261 | sqlite3_finalize (stmt); |
| 262 | return result; |
| 263 | } |
| 264 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 265 | void |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 266 | ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Buffer &profileValue) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 267 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 268 | sqlite3_stmt *stmt; |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 269 | if(doesSelfEntryExist(identity, profileType)) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 270 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 271 | sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 272 | sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(profileValue.buf()), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 273 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 274 | sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 275 | } |
| 276 | else |
| 277 | { |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 278 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0); |
| 279 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 280 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 281 | sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(profileValue.buf()), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 282 | } |
| 283 | sqlite3_step (stmt); |
| 284 | sqlite3_finalize (stmt); |
| 285 | } |
| 286 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 287 | shared_ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 288 | ContactStorage::getSelfProfile(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 289 | { |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 290 | sqlite3_stmt *stmt; |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 291 | shared_ptr<Profile> profile = make_shared<Profile>(identity); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 292 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 293 | sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 294 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 295 | |
| 296 | while(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 297 | { |
| 298 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 299 | string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 300 | |
| 301 | profile->setProfileEntry(profileType, profileValue ); |
| 302 | } |
| 303 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 304 | sqlite3_finalize(stmt); |
| 305 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 306 | return profile; |
| 307 | } |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 308 | |
| 309 | void |
| 310 | ContactStorage::removeContact(const Name& contactNameSpace) |
| 311 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 312 | shared_ptr<ContactItem> contact = getContact(contactNameSpace); |
Yingdi Yu | ae8217c | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 313 | string identity = contactNameSpace.toUri(); |
| 314 | |
| 315 | if(contact == NULL) |
| 316 | return; |
| 317 | |
| 318 | sqlite3_stmt *stmt; |
| 319 | sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0); |
| 320 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 321 | int res = sqlite3_step (stmt); |
| 322 | sqlite3_finalize (stmt); |
| 323 | |
| 324 | sqlite3_prepare_v2 (m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 325 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 326 | res = sqlite3_step (stmt); |
| 327 | sqlite3_finalize (stmt); |
| 328 | |
| 329 | if(contact->isIntroducer()) |
| 330 | { |
| 331 | sqlite3_prepare_v2 (m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 332 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 333 | res = sqlite3_step (stmt); |
| 334 | sqlite3_finalize (stmt); |
| 335 | } |
| 336 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 337 | |
| 338 | void |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 339 | ContactStorage::addContact(const ContactItem& contact) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 340 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 341 | if(doesContactExist(contact.getNameSpace())) |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 342 | throw Error("Normal Contact has already existed"); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 343 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 344 | bool isIntroducer = contact.isIntroducer(); |
| 345 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 346 | sqlite3_stmt *stmt; |
| 347 | sqlite3_prepare_v2 (m_db, |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 348 | "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)", |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 349 | -1, |
| 350 | &stmt, |
| 351 | 0); |
| 352 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 353 | sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT); |
| 354 | sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 355 | const Block& selfCertificateBlock = contact.getSelfEndorseCertificate().wireEncode(); |
| 356 | sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(selfCertificateBlock.wire()), selfCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 357 | sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 358 | |
| 359 | int res = sqlite3_step (stmt); |
| 360 | sqlite3_finalize (stmt); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 361 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 362 | const Profile& profile = contact.getSelfEndorseCertificate().getProfileData().getProfile(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 363 | Profile::const_iterator it = profile.begin(); |
| 364 | string identity = contact.getNameSpace().toUri(); |
| 365 | for(; it != profile.end(); it++) |
| 366 | { |
| 367 | sqlite3_prepare_v2 (m_db, |
| 368 | "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)", |
| 369 | -1, |
| 370 | &stmt, |
| 371 | 0); |
| 372 | sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT); |
| 373 | sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 374 | sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 375 | res = sqlite3_step (stmt); |
| 376 | sqlite3_finalize (stmt); |
| 377 | } |
| 378 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 379 | if(isIntroducer) |
| 380 | { |
| 381 | const vector<Name>& scopeList = contact.getTrustScopeList(); |
| 382 | vector<Name>::const_iterator it = scopeList.begin(); |
| 383 | string nameSpace = contact.getNameSpace().toUri(); |
| 384 | |
| 385 | while(it != scopeList.end()) |
| 386 | { |
| 387 | sqlite3_prepare_v2 (m_db, |
| 388 | "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)", |
| 389 | -1, |
| 390 | &stmt, |
| 391 | 0); |
| 392 | sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT); |
| 393 | sqlite3_bind_text(stmt, 2, it->toUri().c_str(), it->toUri().size(), SQLITE_TRANSIENT); |
| 394 | res = sqlite3_step (stmt); |
| 395 | sqlite3_finalize (stmt); |
| 396 | it++; |
| 397 | } |
| 398 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 401 | void |
| 402 | ContactStorage::updateIsIntroducer(const ndn::Name& identity, bool isIntroducer) |
| 403 | { |
| 404 | sqlite3_stmt *stmt; |
| 405 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 406 | sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0)); |
| 407 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 408 | int res = sqlite3_step (stmt); |
| 409 | sqlite3_finalize (stmt); |
| 410 | return; |
| 411 | } |
| 412 | |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 413 | void |
| 414 | ContactStorage::updateAlias(const ndn::Name& identity, std::string alias) |
| 415 | { |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 416 | sqlite3_stmt *stmt; |
| 417 | sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0); |
| 418 | sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT); |
| 419 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT); |
| 420 | int res = sqlite3_step (stmt); |
| 421 | sqlite3_finalize (stmt); |
| 422 | return; |
Yingdi Yu | 2ac40fb | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 425 | bool |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 426 | ContactStorage::doesContactExist(const Name& name) |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 427 | { |
| 428 | bool result = false; |
| 429 | |
| 430 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 431 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 432 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 433 | |
| 434 | int res = sqlite3_step (stmt); |
| 435 | |
| 436 | if (res == SQLITE_ROW) |
| 437 | { |
| 438 | int countAll = sqlite3_column_int (stmt, 0); |
| 439 | if (countAll > 0) |
| 440 | result = true; |
| 441 | } |
| 442 | sqlite3_finalize (stmt); |
| 443 | return result; |
| 444 | } |
| 445 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 446 | void |
| 447 | ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 448 | { |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 449 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 450 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 451 | |
| 452 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 453 | { |
| 454 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 455 | |
| 456 | Data certData; |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 457 | certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 458 | EndorseCertificate endorseCertificate(certData); |
| 459 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 460 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 461 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 462 | contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias)); |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 463 | } |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 464 | sqlite3_finalize (stmt); |
| 465 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 466 | vector<shared_ptr<ContactItem> >::iterator it = contacts.begin(); |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 467 | for(; it != contacts.end(); it++) |
| 468 | { |
| 469 | if((*it)->isIntroducer()) |
| 470 | { |
| 471 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 472 | sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT); |
| 473 | |
| 474 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 475 | { |
| 476 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 477 | (*it)->addTrustScope(scope); |
| 478 | } |
| 479 | sqlite3_finalize (stmt); |
| 480 | } |
| 481 | } |
Yingdi Yu | d04ed1a | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 482 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 483 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 484 | shared_ptr<ContactItem> |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 485 | ContactStorage::getContact(const Name& name) |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 486 | { |
| 487 | sqlite3_stmt *stmt; |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 488 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0); |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 489 | sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 490 | |
| 491 | if( sqlite3_step (stmt) == SQLITE_ROW) |
| 492 | { |
| 493 | string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 494 | |
| 495 | Data certData; |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 496 | certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1))); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 497 | EndorseCertificate endorseCertificate(certData); |
| 498 | |
Yingdi Yu | 71c0187 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 499 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 500 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 501 | sqlite3_finalize (stmt); |
| 502 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 503 | shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 504 | |
| 505 | if(contact->isIntroducer()) |
| 506 | { |
| 507 | sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0); |
| 508 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 509 | |
| 510 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 511 | { |
| 512 | Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 513 | contact->addTrustScope(scope); |
| 514 | } |
| 515 | sqlite3_finalize (stmt); |
| 516 | } |
| 517 | |
| 518 | return contact; |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 519 | } |
Yingdi Yu | 93adb1a | 2014-01-16 10:30:26 -0800 | [diff] [blame^] | 520 | return shared_ptr<ContactItem>(); |
Yingdi Yu | 4ef8cf6 | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 523 | shared_ptr<Profile> |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 524 | ContactStorage::getSelfProfile(const Name& identity) const |
| 525 | { |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 526 | shared_ptr<Profile> profile = make_shared<Profile>(identity); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 527 | sqlite3_stmt *stmt; |
| 528 | sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
| 529 | sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 530 | |
| 531 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 532 | { |
| 533 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 534 | string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 535 | profile->setProfileEntry(profileType, profileValue); |
| 536 | } |
| 537 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 538 | sqlite3_finalize(stmt); |
| 539 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 540 | return profile; |
| 541 | } |
| 542 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 543 | Block |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 544 | ContactStorage::getSelfEndorseCertificate(const Name& identity) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 545 | { |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 546 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 547 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 548 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 54dcecc | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 549 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 550 | if(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 551 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 552 | Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 553 | sqlite3_finalize (stmt); |
| 554 | return result; |
| 555 | } |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 556 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 557 | sqlite3_finalize (stmt); |
Yingdi Yu | 92e8e48 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 558 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 559 | throw Error("ContactStorage: No self-endorse certificate found!"); |
| 560 | |
| 561 | return Block(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 564 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 565 | ContactStorage::updateSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 566 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 567 | const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 568 | |
| 569 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 570 | sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 571 | sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 572 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 573 | sqlite3_step(stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 574 | |
| 575 | sqlite3_finalize (stmt); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 579 | ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 580 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 581 | const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 582 | |
| 583 | sqlite3_stmt *stmt; |
Yingdi Yu | ec3d9a3 | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 584 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 585 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 586 | sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 587 | sqlite3_step(stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 588 | |
| 589 | sqlite3_finalize (stmt); |
| 590 | } |
| 591 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 592 | Block |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 593 | ContactStorage::getEndorseCertificate(const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 594 | { |
| 595 | sqlite3_stmt *stmt; |
| 596 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0); |
| 597 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 598 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 599 | |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 600 | if(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 601 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 602 | Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 603 | sqlite3_finalize (stmt); |
| 604 | return result; |
| 605 | } |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 606 | |
| 607 | sqlite3_finalize (stmt); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 608 | |
| 609 | throw Error("ContactStorage: No endorse certificate found!"); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 610 | |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 611 | return Block(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 615 | ContactStorage::updateEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 616 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 617 | const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 618 | |
| 619 | sqlite3_stmt *stmt; |
| 620 | sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 621 | sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 622 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 623 | sqlite3_step(stmt); |
| 624 | |
| 625 | sqlite3_finalize (stmt); |
| 626 | } |
| 627 | |
| 628 | void |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 629 | ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 630 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 631 | const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 632 | |
| 633 | sqlite3_stmt *stmt; |
| 634 | sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
| 635 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 636 | sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.value()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 637 | sqlite3_step(stmt); |
| 638 | |
| 639 | sqlite3_finalize (stmt); |
| 640 | } |
| 641 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 642 | void |
| 643 | ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 644 | { |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 645 | sqlite3_stmt *stmt; |
| 646 | sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0); |
| 647 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 648 | |
| 649 | while( sqlite3_step (stmt) == SQLITE_ROW) |
| 650 | { |
| 651 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 652 | endorseList.push_back(profileType); |
| 653 | } |
| 654 | sqlite3_finalize (stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void |
| 658 | ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate) |
| 659 | { |
| 660 | Name endorserName = endorseCertificate.getSigner(); |
| 661 | Name keyName = endorseCertificate.getPublicKeyName(); |
| 662 | Name endorseeName = keyName.getPrefix(keyName.size()-1); |
| 663 | Name getCertName = endorseCertificate.getName(); |
| 664 | Name oldCertName; |
| 665 | bool insert = true; |
| 666 | bool update = true; |
| 667 | |
| 668 | sqlite3_stmt *stmt; |
| 669 | sqlite3_prepare_v2 (m_db, "SELECT endorse_name FROM CollectEndorse WHERE endorser=? AND endorsee=?", -1, &stmt, 0); |
| 670 | sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 671 | sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 672 | |
| 673 | if(sqlite3_step (stmt) == SQLITE_ROW) |
| 674 | { |
| 675 | insert = false; |
| 676 | oldCertName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0))); |
| 677 | if(getCertName == oldCertName) |
| 678 | update = false; |
| 679 | } |
| 680 | sqlite3_finalize (stmt); |
| 681 | |
| 682 | if(insert) |
| 683 | { |
| 684 | sqlite3_prepare_v2 (m_db, "INSERT INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0); |
| 685 | sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 686 | sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 687 | sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 688 | const Block &block = endorseCertificate.wireEncode(); |
| 689 | sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 690 | int res = sqlite3_step (stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 691 | sqlite3_finalize (stmt); |
| 692 | return; |
| 693 | } |
| 694 | if(update) |
| 695 | { |
| 696 | sqlite3_prepare_v2 (m_db, "UPDATE CollectEndorse SET endorse_name=?, endorse_data=? WHERE endorser=? AND endorsee=?", -1, &stmt, 0); |
| 697 | sqlite3_bind_text(stmt, 1, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 698 | const Block &block = endorseCertificate.wireEncode(); |
| 699 | sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(block.value()), block.size(), SQLITE_TRANSIENT); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 700 | sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT); |
| 701 | sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT); |
| 702 | int res = sqlite3_step (stmt); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 703 | sqlite3_finalize (stmt); |
| 704 | return; |
| 705 | } |
| 706 | } |
| 707 | |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 708 | void |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 709 | ContactStorage::getCollectEndorseList(const Name& name, vector<Buffer>& endorseList) |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 710 | { |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 711 | sqlite3_stmt *stmt; |
| 712 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0); |
| 713 | sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT); |
| 714 | |
| 715 | while(sqlite3_step (stmt) == SQLITE_ROW) |
| 716 | { |
Yingdi Yu | f8f572d | 2014-01-13 11:19:47 -0800 | [diff] [blame] | 717 | Buffer blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 76dd800 | 2013-12-24 11:16:32 +0800 | [diff] [blame] | 718 | endorseList.push_back(blob); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Yingdi Yu | 72781e5 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 721 | sqlite3_finalize (stmt); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 722 | } |