blob: f107f0c556c11a8f37399e022a570737fe305c40 [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
11#include "contact-storage.h"
Yingdi Yud04ed1a2013-10-14 14:07:03 -070012
Yingdi Yud04ed1a2013-10-14 14:07:03 -070013#include <boost/filesystem.hpp>
Yingdi Yu3b318c12013-10-15 17:54:00 -070014#include "logging.h"
Yingdi Yud04ed1a2013-10-14 14:07:03 -070015
16using namespace std;
17using namespace ndn;
18namespace fs = boost::filesystem;
19
Yingdi Yu3b318c12013-10-15 17:54:00 -070020INIT_LOGGER ("ContactStorage");
21
Yingdi Yufa4ce792014-02-06 18:09:22 -080022namespace chronos{
23
Yingdi Yu3b318c12013-10-15 17:54:00 -070024const string INIT_SP_TABLE = "\
25CREATE TABLE IF NOT EXISTS \n \
26 SelfProfile( \n \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070027 profile_identity BLOB NOT NULL, \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070028 profile_type BLOB NOT NULL, \n \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070029 profile_value BLOB NOT NULL, \n \
30 \
31 PRIMARY KEY (profile_identity, profile_type) \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070032 ); \n \
33 \
Yingdi Yu54dcecc2013-10-17 15:07:17 -070034CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070035";
36
Yingdi Yuec3d9a32013-10-18 18:35:09 -070037const string INIT_SE_TABLE = "\
Yingdi Yu3b318c12013-10-15 17:54:00 -070038CREATE TABLE IF NOT EXISTS \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070039 SelfEndorse( \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070040 identity BLOB NOT NULL UNIQUE, \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070041 endorse_data BLOB NOT NULL, \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070042 \
43 PRIMARY KEY (identity) \n \
44 ); \n \
Yingdi Yuec3d9a32013-10-18 18:35:09 -070045CREATE INDEX se_index ON SelfEndorse(identity); \n \
Yingdi Yu3b318c12013-10-15 17:54:00 -070046";
Yingdi Yud04ed1a2013-10-14 14:07:03 -070047
Yingdi Yu71c01872013-11-03 16:22:05 -080048const string INIT_CONTACT_TABLE = "\
Yingdi Yud04ed1a2013-10-14 14:07:03 -070049CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080050 Contact( \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070051 contact_namespace BLOB NOT NULL, \n \
52 contact_alias BLOB NOT NULL, \n \
53 self_certificate BLOB NOT NULL, \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080054 is_introducer INTEGER DEFAULT 0, \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070055 \
56 PRIMARY KEY (contact_namespace) \n \
57 ); \n \
58 \
Yingdi Yub2e747d2013-11-05 23:06:43 -080059CREATE INDEX contact_index ON Contact(contact_namespace); \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070060";
61
Yingdi Yu71c01872013-11-03 16:22:05 -080062const string INIT_TS_TABLE = "\
Yingdi Yud04ed1a2013-10-14 14:07:03 -070063CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu71c01872013-11-03 16:22:05 -080064 TrustScope( \n \
Yingdi Yub2e747d2013-11-05 23:06:43 -080065 id INTEGER PRIMARY KEY AUTOINCREMENT, \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070066 contact_namespace BLOB NOT NULL, \n \
Yingdi Yub2e747d2013-11-05 23:06:43 -080067 trust_scope BLOB NOT NULL \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -070068 ); \n \
69 \
Yingdi Yub2e747d2013-11-05 23:06:43 -080070CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \
71";
72
73const string INIT_CP_TABLE = "\
74CREATE TABLE IF NOT EXISTS \n \
75 ContactProfile( \n \
76 profile_identity BLOB NOT NULL, \n \
77 profile_type BLOB NOT NULL, \n \
78 profile_value BLOB NOT NULL, \n \
79 endorse INTEGER NOT NULL, \n \
80 \
81 PRIMARY KEY (profile_identity, profile_type) \n \
82 ); \n \
83 \
84CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \
85";
86
87const string INIT_PE_TABLE = "\
88CREATE TABLE IF NOT EXISTS \n \
89 ProfileEndorse( \n \
90 identity BLOB NOT NULL UNIQUE, \n \
91 endorse_data BLOB NOT NULL, \n \
92 \
93 PRIMARY KEY (identity) \n \
94 ); \n \
95 \
96CREATE INDEX pe_index ON ProfileEndorse(identity); \n \
97";
98
99const string INIT_CE_TABLE = "\
100CREATE TABLE IF NOT EXISTS \n \
101 CollectEndorse( \n \
102 endorsee BLOB NOT NULL, \n \
103 endorser BLOB NOT NULL, \n \
104 endorse_name BLOB NOT NULL, \n \
105 endorse_data BLOB NOT NULL, \n \
106 \
107 PRIMARY KEY (endorsee, endorser) \n \
108 ); \n \
109 \
110CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700111";
112
Yingdi Yu4685b1b2013-10-18 17:05:02 -0700113ContactStorage::ContactStorage()
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700114{
115 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
116 fs::create_directories (chronosDir);
117
118 int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db);
119 if (res != SQLITE_OK)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800120 throw Error("Chronos DB cannot be open/created");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700121
Yingdi Yufa4ce792014-02-06 18:09:22 -0800122 initializeTable("SelfProfile", INIT_SP_TABLE);
123 initializeTable("SelfEndorse", INIT_SE_TABLE);
124 initializeTable("Contact", INIT_CONTACT_TABLE);
125 initializeTable("TrustScope", INIT_TS_TABLE);
126 initializeTable("ContactProfile", INIT_CP_TABLE);
127 initializeTable("ProfileEndorse", INIT_PE_TABLE);
128 initializeTable("CollectEndorse", INIT_CE_TABLE);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700129
Yingdi Yu3b318c12013-10-15 17:54:00 -0700130}
131
Yingdi Yu3b318c12013-10-15 17:54:00 -0700132void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800133ContactStorage::initializeTable(const string& tableName, const string& sqlCreateStmt)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700134{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800135 sqlite3_stmt *stmt;
136 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name=?", -1, &stmt, 0);
137 sqlite3_bind_text(stmt, 1, tableName.c_str(), tableName.size(), SQLITE_TRANSIENT);
138 int res = sqlite3_step (stmt);
139
140 bool tableExist = false;
141 if (res == SQLITE_ROW)
142 tableExist = true;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700143 sqlite3_finalize (stmt);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800144
145 if(!tableExist)
146 {
147 char *errmsg = 0;
148 res = sqlite3_exec (m_db, sqlCreateStmt.c_str (), NULL, NULL, &errmsg);
149 if (res != SQLITE_OK && errmsg != 0)
150 throw Error("Init \"error\" in " + tableName);
151 }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700152}
153
Yingdi Yu76dd8002013-12-24 11:16:32 +0800154shared_ptr<Profile>
Yingdi Yufa4ce792014-02-06 18:09:22 -0800155ContactStorage::getSelfProfile(const Name& identity) const
156{
157 shared_ptr<Profile> profile;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700158 sqlite3_stmt *stmt;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800159 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
160 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700161
Yingdi Yufa4ce792014-02-06 18:09:22 -0800162 while( sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu3b318c12013-10-15 17:54:00 -0700163 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800164 profile = make_shared<Profile>(identity);
Yingdi Yu3b318c12013-10-15 17:54:00 -0700165 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800166 string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
Yingdi Yufa4ce792014-02-06 18:09:22 -0800167 (*profile)[profileType] = profileValue;
Yingdi Yu3b318c12013-10-15 17:54:00 -0700168 }
169
Yingdi Yu72781e52013-11-06 23:00:21 -0800170 sqlite3_finalize(stmt);
171
Yingdi Yu3b318c12013-10-15 17:54:00 -0700172 return profile;
173}
Yingdi Yuae8217c2013-11-09 00:03:26 -0800174
Yingdi Yufa4ce792014-02-06 18:09:22 -0800175// Block
176// ContactStorage::getSelfEndorseCertificate(const Name& identity)
177// {
178// sqlite3_stmt *stmt;
179// sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
180// sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
181
182// if(sqlite3_step (stmt) == SQLITE_ROW)
183// {
184// Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
185// sqlite3_finalize (stmt);
186// return result;
187// }
188
189// sqlite3_finalize (stmt);
190
191// throw Error("ContactStorage: No self-endorse certificate found!");
192// }
193
194void
195ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity)
196{
197 const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode();
198
199 sqlite3_stmt *stmt;
200 sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
201 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
202 sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
203 sqlite3_step(stmt);
204
205 sqlite3_finalize (stmt);
206}
207
208// Block
209// ContactStorage::getEndorseCertificate(const Name& identity)
210// {
211// sqlite3_stmt *stmt;
212// sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0);
213// sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
214
215
216// if(sqlite3_step (stmt) == SQLITE_ROW)
217// {
218// Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
219// sqlite3_finalize (stmt);
220// return result;
221// }
222
223// sqlite3_finalize (stmt);
224
225// throw Error("ContactStorage: No endorse certificate found!");
226
227// return Block();
228// }
229
230void
231ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
232{
233 const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode();
234
235 sqlite3_stmt *stmt;
236 sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
237 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
238 sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.value()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
239 sqlite3_step(stmt);
240
241 sqlite3_finalize (stmt);
242}
243
244void
245ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate)
246{
247 Name endorserName = endorseCertificate.getSigner();
248 Name keyName = endorseCertificate.getPublicKeyName();
249 Name endorseeName = keyName.getPrefix(keyName.size()-1);
250 Name getCertName = endorseCertificate.getName();
251
252 sqlite3_stmt *stmt;
253 sqlite3_prepare_v2 (m_db, "INSERT OR REPLACE INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
254 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
255 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
256 sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
257 const Block &block = endorseCertificate.wireEncode();
258 sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT);
259 int res = sqlite3_step (stmt);
260 sqlite3_finalize (stmt);
261 return;
262}
263
264void
265ContactStorage::getCollectEndorseList(const Name& name, vector<Buffer>& endorseList)
266{
267 sqlite3_stmt *stmt;
268 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0);
269 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
270
271 while(sqlite3_step (stmt) == SQLITE_ROW)
272 {
273 Buffer blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
274 endorseList.push_back(blob);
275 }
276
277 sqlite3_finalize (stmt);
278}
279
280void
281ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList)
282{
283 sqlite3_stmt *stmt;
284 sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0);
285 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
286
287 while( sqlite3_step (stmt) == SQLITE_ROW)
288 {
289 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
290 endorseList.push_back(profileType);
291 }
292 sqlite3_finalize (stmt);
293}
294
295
Yingdi Yuae8217c2013-11-09 00:03:26 -0800296void
297ContactStorage::removeContact(const Name& contactNameSpace)
298{
Yingdi Yuae8217c2013-11-09 00:03:26 -0800299 string identity = contactNameSpace.toUri();
300
Yingdi Yuae8217c2013-11-09 00:03:26 -0800301 sqlite3_stmt *stmt;
302 sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
303 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
304 int res = sqlite3_step (stmt);
305 sqlite3_finalize (stmt);
306
307 sqlite3_prepare_v2 (m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, 0);
308 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
309 res = sqlite3_step (stmt);
310 sqlite3_finalize (stmt);
311
Yingdi Yufa4ce792014-02-06 18:09:22 -0800312 sqlite3_prepare_v2 (m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
313 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
314 res = sqlite3_step (stmt);
315 sqlite3_finalize (stmt);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800316}
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700317
318void
Yingdi Yu71c01872013-11-03 16:22:05 -0800319ContactStorage::addContact(const ContactItem& contact)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700320{
Yingdi Yu71c01872013-11-03 16:22:05 -0800321 if(doesContactExist(contact.getNameSpace()))
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800322 throw Error("Normal Contact has already existed");
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700323
Yingdi Yu71c01872013-11-03 16:22:05 -0800324 bool isIntroducer = contact.isIntroducer();
325
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700326 sqlite3_stmt *stmt;
327 sqlite3_prepare_v2 (m_db,
Yingdi Yu71c01872013-11-03 16:22:05 -0800328 "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)",
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700329 -1,
330 &stmt,
331 0);
332
Yingdi Yu71c01872013-11-03 16:22:05 -0800333 sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
334 sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800335 const Block& selfCertificateBlock = contact.getSelfEndorseCertificate().wireEncode();
336 sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(selfCertificateBlock.wire()), selfCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yu71c01872013-11-03 16:22:05 -0800337 sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700338
339 int res = sqlite3_step (stmt);
340 sqlite3_finalize (stmt);
Yingdi Yu71c01872013-11-03 16:22:05 -0800341
Yingdi Yufa4ce792014-02-06 18:09:22 -0800342 const Profile& profile = contact.getSelfEndorseCertificate().getProfile();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800343 Profile::const_iterator it = profile.begin();
344 string identity = contact.getNameSpace().toUri();
345 for(; it != profile.end(); it++)
346 {
347 sqlite3_prepare_v2 (m_db,
348 "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)",
349 -1,
350 &stmt,
351 0);
352 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
353 sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800354 sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800355 res = sqlite3_step (stmt);
356 sqlite3_finalize (stmt);
357 }
358
Yingdi Yu71c01872013-11-03 16:22:05 -0800359 if(isIntroducer)
360 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800361 ContactItem::const_iterator it = contact.trustScopeBegin();
Yingdi Yu71c01872013-11-03 16:22:05 -0800362 string nameSpace = contact.getNameSpace().toUri();
363
Yingdi Yufa4ce792014-02-06 18:09:22 -0800364 while(it != contact.trustScopeEnd())
Yingdi Yu71c01872013-11-03 16:22:05 -0800365 {
366 sqlite3_prepare_v2 (m_db,
367 "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)",
368 -1,
369 &stmt,
370 0);
371 sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800372 sqlite3_bind_text(stmt, 2, it->first.toUri().c_str(), it->first.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu71c01872013-11-03 16:22:05 -0800373 res = sqlite3_step (stmt);
374 sqlite3_finalize (stmt);
375 it++;
376 }
377 }
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700378}
379
Yingdi Yufa4ce792014-02-06 18:09:22 -0800380
381shared_ptr<ContactItem>
382ContactStorage::getContact(const Name& name)
383{
384 sqlite3_stmt *stmt;
385 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0);
386 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
387
388 if( sqlite3_step (stmt) == SQLITE_ROW)
389 {
390 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
391
392 Data certData;
393 certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
394 EndorseCertificate endorseCertificate(certData);
395
396 int isIntroducer = sqlite3_column_int (stmt, 2);
397
398 sqlite3_finalize (stmt);
399
400 shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias);
401
402 if(contact->isIntroducer())
403 {
404 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
405 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
406
407 while( sqlite3_step (stmt) == SQLITE_ROW)
408 {
409 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
410 contact->addTrustScope(scope);
411 }
412 sqlite3_finalize (stmt);
413 }
414
415 return contact;
416 }
417 return shared_ptr<ContactItem>();
418}
419
420
Yingdi Yub2e747d2013-11-05 23:06:43 -0800421void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800422ContactStorage::updateIsIntroducer(const Name& identity, bool isIntroducer)
Yingdi Yub2e747d2013-11-05 23:06:43 -0800423{
424 sqlite3_stmt *stmt;
425 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0);
426 sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0));
427 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
428 int res = sqlite3_step (stmt);
429 sqlite3_finalize (stmt);
430 return;
431}
432
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700433void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800434ContactStorage::updateAlias(const Name& identity, string alias)
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700435{
Yingdi Yu71c01872013-11-03 16:22:05 -0800436 sqlite3_stmt *stmt;
437 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
438 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
439 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
440 int res = sqlite3_step (stmt);
441 sqlite3_finalize (stmt);
442 return;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700443}
444
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700445bool
Yingdi Yu71c01872013-11-03 16:22:05 -0800446ContactStorage::doesContactExist(const Name& name)
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700447{
448 bool result = false;
449
450 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800451 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700452 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
453
454 int res = sqlite3_step (stmt);
455
456 if (res == SQLITE_ROW)
457 {
458 int countAll = sqlite3_column_int (stmt, 0);
459 if (countAll > 0)
460 result = true;
461 }
462 sqlite3_finalize (stmt);
463 return result;
464}
465
Yingdi Yu76dd8002013-12-24 11:16:32 +0800466void
467ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700468{
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700469 sqlite3_stmt *stmt;
Yingdi Yu71c01872013-11-03 16:22:05 -0800470 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700471
472 while( sqlite3_step (stmt) == SQLITE_ROW)
473 {
474 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800475
476 Data certData;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800477 certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800478 EndorseCertificate endorseCertificate(certData);
479
Yingdi Yu71c01872013-11-03 16:22:05 -0800480 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700481
Yingdi Yu76dd8002013-12-24 11:16:32 +0800482 contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias));
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700483 }
Yingdi Yu71c01872013-11-03 16:22:05 -0800484 sqlite3_finalize (stmt);
485
Yingdi Yu76dd8002013-12-24 11:16:32 +0800486 vector<shared_ptr<ContactItem> >::iterator it = contacts.begin();
Yingdi Yu71c01872013-11-03 16:22:05 -0800487 for(; it != contacts.end(); it++)
488 {
489 if((*it)->isIntroducer())
490 {
491 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
492 sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT);
493
494 while( sqlite3_step (stmt) == SQLITE_ROW)
495 {
496 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
497 (*it)->addTrustScope(scope);
498 }
499 sqlite3_finalize (stmt);
500 }
501 }
Yingdi Yud04ed1a2013-10-14 14:07:03 -0700502}
Yingdi Yu3b318c12013-10-15 17:54:00 -0700503
Yingdi Yufa4ce792014-02-06 18:09:22 -0800504}//chronos