From bc32805af0b5290321e303e433060acd97798e48 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 8 Nov 2017 14:08:13 +0100 Subject: [PATCH] feat(MainDb): provide a way to check stored events --- src/CMakeLists.txt | 3 ++ src/db/main-db-event-key-p.h | 38 ++++++++++++++++++++ src/db/main-db-event-key.cpp | 69 ++++++++++++++++++++++++++++++++++++ src/db/main-db-event-key.h | 53 +++++++++++++++++++++++++++ src/db/main-db.cpp | 49 ++++++++++++++++--------- src/db/main-db.h | 4 +-- src/event-log/event-log-p.h | 3 +- src/event-log/event-log.cpp | 1 + src/event-log/event-log.h | 1 - src/logger/logger.h | 3 +- 10 files changed, 203 insertions(+), 21 deletions(-) create mode 100644 src/db/main-db-event-key-p.h create mode 100644 src/db/main-db-event-key.cpp create mode 100644 src/db/main-db-event-key.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e668511fb..10c0ed793 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -91,6 +91,8 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES core/platform-helpers/platform-helpers.h db/abstract/abstract-db-p.h db/abstract/abstract-db.h + db/main-db-event-key-p.h + db/main-db-event-key.h db/main-db-p.h db/main-db.h db/session/db-session-p.h @@ -188,6 +190,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES core/paths/paths.cpp core/platform-helpers/platform-helpers.cpp db/abstract/abstract-db.cpp + db/main-db-event-key.cpp db/main-db.cpp db/session/db-session-provider.cpp db/session/db-session.cpp diff --git a/src/db/main-db-event-key-p.h b/src/db/main-db-event-key-p.h new file mode 100644 index 000000000..62e4f6eaa --- /dev/null +++ b/src/db/main-db-event-key-p.h @@ -0,0 +1,38 @@ +/* + * main-db-event-key-p.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _MAIN_DB_EVENT_KEY_P_H_ +#define _MAIN_DB_EVENT_KEY_P_H_ + +#include "main-db-event-key.h" +#include "object/clonable-object-p.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class MainDbEventKeyPrivate : public ClonableObjectPrivate { +public: + std::weak_ptr core; + long long storageId = -1; +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _MAIN_DB_EVENT_KEY_P_H_ diff --git a/src/db/main-db-event-key.cpp b/src/db/main-db-event-key.cpp new file mode 100644 index 000000000..a363ba3c4 --- /dev/null +++ b/src/db/main-db-event-key.cpp @@ -0,0 +1,69 @@ +/* + * main-db-event-key.cpp + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "main-db-event-key-p.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +// ----------------------------------------------------------------------------- + +MainDbEventKey::MainDbEventKey () : ClonableObject(*new MainDbEventKeyPrivate) {} + +MainDbEventKey::MainDbEventKey (const shared_ptr &core, long long storageId) : MainDbEventKey() { + L_D(); + d->core = core; + d->storageId = storageId; +} + +MainDbEventKey::MainDbEventKey (const MainDbEventKey &src) : MainDbEventKey() { + L_D(); + const MainDbEventKeyPrivate *dSrc = src.getPrivate(); + + d->core = dSrc->core; + d->storageId = dSrc->storageId; +} + +MainDbEventKey::~MainDbEventKey () { + if (isValid()) { + // TODO: Remove key from main db references if necessary. + } +} + +MainDbEventKey &MainDbEventKey::operator= (const MainDbEventKey &src) { + L_D(); + + if (this != &src) { + const MainDbEventKeyPrivate *dSrc = src.getPrivate(); + d->core = dSrc->core; + d->storageId = dSrc->storageId; + } + + return *this; +} + +bool MainDbEventKey::isValid () const { + L_D(); + return !d->core.expired() && d->storageId >= 0; +} + +LINPHONE_END_NAMESPACE diff --git a/src/db/main-db-event-key.h b/src/db/main-db-event-key.h new file mode 100644 index 000000000..37ea05c35 --- /dev/null +++ b/src/db/main-db-event-key.h @@ -0,0 +1,53 @@ +/* + * main-db-event-key.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _MAIN_DB_EVENT_KEY_H_ +#define _MAIN_DB_EVENT_KEY_H_ + +#include + +#include "object/clonable-object.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class Core; +class MainDbEventKeyPrivate; + +class MainDbEventKey : public ClonableObject { + friend class MainDb; + +public: + MainDbEventKey (); + MainDbEventKey (const std::shared_ptr &core, long long storageId); + MainDbEventKey (const MainDbEventKey &src); + ~MainDbEventKey (); + + MainDbEventKey &operator= (const MainDbEventKey &src); + + bool isValid () const; + +private: + L_DECLARE_PRIVATE(MainDbEventKey); +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _MAIN_DB_EVENT_KEY_H_ diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index cc7c18b6e..84d413e1f 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -36,6 +36,7 @@ #include "event-log/event-log-p.h" #include "event-log/events.h" #include "logger/logger.h" +#include "main-db-event-key-p.h" #include "main-db-p.h" // ============================================================================= @@ -778,7 +779,14 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return false; } + const EventLogPrivate *dEventLog = eventLog->getPrivate(); + if (dEventLog->dbKey.isValid()) { + lWarning() << "Unable to add an event twice!!!"; + return false; + } + bool soFarSoGood = false; + long long storageId; L_BEGIN_LOG_EXCEPTION @@ -790,32 +798,32 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), case EventLog::Type::ConferenceCreated: case EventLog::Type::ConferenceDestroyed: - d->insertConferenceEvent(eventLog); + storageId = d->insertConferenceEvent(eventLog); break; case EventLog::Type::ConferenceCallStart: case EventLog::Type::ConferenceCallEnd: - d->insertConferenceCallEvent(eventLog); + storageId = d->insertConferenceCallEvent(eventLog); break; case EventLog::Type::ConferenceChatMessage: - d->insertConferenceChatMessageEvent(eventLog); + storageId = d->insertConferenceChatMessageEvent(eventLog); break; case EventLog::Type::ConferenceParticipantAdded: case EventLog::Type::ConferenceParticipantRemoved: case EventLog::Type::ConferenceParticipantSetAdmin: case EventLog::Type::ConferenceParticipantUnsetAdmin: - d->insertConferenceParticipantEvent(eventLog); + storageId = d->insertConferenceParticipantEvent(eventLog); break; case EventLog::Type::ConferenceParticipantDeviceAdded: case EventLog::Type::ConferenceParticipantDeviceRemoved: - d->insertConferenceParticipantDeviceEvent(eventLog); + storageId = d->insertConferenceParticipantDeviceEvent(eventLog); break; case EventLog::Type::ConferenceSubjectChanged: - d->insertConferenceSubjectEvent(eventLog); + storageId = d->insertConferenceSubjectEvent(eventLog); break; } @@ -825,30 +833,39 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), L_END_LOG_EXCEPTION + if (soFarSoGood) + dEventLog->dbKey = MainDbEventKey(getCore(), storageId); + return soFarSoGood; } bool MainDb::deleteEvent (const shared_ptr &eventLog) { - L_D(); + EventLogPrivate *dEventLog = eventLog->getPrivate(); + if (!dEventLog->dbKey.isValid()) { + lWarning() << "Unable to delete invalid event."; + return false; + } - if (!isConnected()) { + MainDbEventKeyPrivate *dEventKey = dEventLog->dbKey.getPrivate(); + shared_ptr core = dEventKey->core.lock(); + L_ASSERT(core); + + MainDb &mainDb = *core->getPrivate()->mainDb.get(); + if (!mainDb.isConnected()) { lWarning() << "Unable to delete event. Not connected."; return false; } - long long &storageId = eventLog->getPrivate()->storageId; - if (storageId < 0) - return false; - L_BEGIN_LOG_EXCEPTION - soci::session *session = d->dbSession.getBackendSession(); - *session << "DELETE FROM event WHERE id = :id", soci::use(storageId); - storageId = -1; + soci::session *session = mainDb.getPrivate()->dbSession.getBackendSession(); + *session << "DELETE FROM event WHERE id = :id", soci::use(dEventKey->storageId); L_END_LOG_EXCEPTION - return storageId == -1; + dEventLog->dbKey = MainDbEventKey(); + + return true; } int MainDb::getEventsCount (FilterMask mask) const { diff --git a/src/db/main-db.h b/src/db/main-db.h index 3f96b1dde..acfae01c7 100644 --- a/src/db/main-db.h +++ b/src/db/main-db.h @@ -35,7 +35,7 @@ class Core; class EventLog; class MainDbPrivate; -class LINPHONE_PUBLIC MainDb : public AbstractDb, public CoreAccessor { +class MainDb : public AbstractDb, public CoreAccessor { public: enum Filter { NoFilter = 0x0, @@ -53,7 +53,7 @@ public: // --------------------------------------------------------------------------- bool addEvent (const std::shared_ptr &eventLog); - bool deleteEvent (const std::shared_ptr &eventLog); + static bool deleteEvent (const std::shared_ptr &eventLog); int getEventsCount (FilterMask mask = NoFilter) const; // --------------------------------------------------------------------------- diff --git a/src/event-log/event-log-p.h b/src/event-log/event-log-p.h index 06701bac7..4c4085883 100644 --- a/src/event-log/event-log-p.h +++ b/src/event-log/event-log-p.h @@ -20,6 +20,7 @@ #ifndef _EVENT_LOG_P_H_ #define _EVENT_LOG_P_H_ +#include "db/main-db-event-key.h" #include "object/base-object-p.h" #include "event-log.h" @@ -30,7 +31,7 @@ LINPHONE_BEGIN_NAMESPACE class EventLogPrivate : public BaseObjectPrivate { public: - long long storageId = -1; + mutable MainDbEventKey dbKey; private: EventLog::Type type = EventLog::Type::None; diff --git a/src/event-log/event-log.cpp b/src/event-log/event-log.cpp index 087ee6797..9ccfa8afc 100644 --- a/src/event-log/event-log.cpp +++ b/src/event-log/event-log.cpp @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "core/core-p.h" #include "event-log-p.h" // ============================================================================= diff --git a/src/event-log/event-log.h b/src/event-log/event-log.h index 98acbe238..3134421e8 100644 --- a/src/event-log/event-log.h +++ b/src/event-log/event-log.h @@ -21,7 +21,6 @@ #define _EVENT_LOG_H_ #include -#include #include "linphone/enums/event-log-enums.h" #include "linphone/utils/enum-generator.h" diff --git a/src/logger/logger.h b/src/logger/logger.h index 5c8a9ea19..e968e5794 100644 --- a/src/logger/logger.h +++ b/src/logger/logger.h @@ -59,7 +59,8 @@ LINPHONE_END_NAMESPACE #define lFatal() LinphonePrivate::Logger(LinphonePrivate::Logger::Fatal).getOutput() #define L_BEGIN_LOG_EXCEPTION try { - #define L_END_LOG_EXCEPTION \ + +#define L_END_LOG_EXCEPTION \ } catch (const exception &e) { \ lWarning() << "Error: " << e.what(); \ }