From 96443c057dcce8298a61d1df5b4c07fb146a961b Mon Sep 17 00:00:00 2001 From: APTX Date: Mon, 13 May 2013 21:37:16 +0200 Subject: [PATCH] Add a sequence numbering udp client instances. --- localmylist/database.cpp | 17 +++++++++++++++++ localmylist/database.h | 2 ++ localmylist/mylist.cpp | 14 ++++++++++++++ localmylist/mylist.h | 8 +++++++- localmylist/share/schema/schema.sql | 4 ++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/localmylist/database.cpp b/localmylist/database.cpp index a2f3ba6..f3c47f4 100644 --- a/localmylist/database.cpp +++ b/localmylist/database.cpp @@ -1413,6 +1413,23 @@ bool Database::log(const QString &message, int type) return exec(q); } +int Database::getNextUdpClientId() +{ + QSqlQuery &q = prepare("SELECT nextval('udp_client_session')"); + + if (!exec(q)) + return 0; + + if (!q.next()) + return 0; + + int ret = q.value(0).toInt(); + + q.finish(); + + return ret; +} + QSqlDatabase Database::connection() const { return d->db; diff --git a/localmylist/database.h b/localmylist/database.h index 056216e..cf4b5a9 100644 --- a/localmylist/database.h +++ b/localmylist/database.h @@ -125,6 +125,8 @@ public slots: bool log(const QString &message, int type = 1); + int getNextUdpClientId(); + QSqlDatabase connection() const; QSqlQuery &prepare(const char *const sql); diff --git a/localmylist/mylist.cpp b/localmylist/mylist.cpp index 9618489..fa8f37e 100644 --- a/localmylist/mylist.cpp +++ b/localmylist/mylist.cpp @@ -35,6 +35,8 @@ MyList::MyList() db = new Database("main"); connect(db, SIGNAL(connected()), this, SLOT(setupHostInfo())); m_settings = new Settings(db, this); + + m_udpClientId = 0; } MyList::~MyList() @@ -66,6 +68,11 @@ int MyList::runningTaskCount() return tasks.count(); } +int MyList::udpClientId() const +{ + return m_udpClientId; +} + QSettings *MyList::defaultLocalQSettings() const { return m_defaultLocalQSettings; @@ -157,6 +164,13 @@ void MyList::setupUdpClient() if (!db->isConnected()) return; + if (!isUdpHost()) + return; + + m_udpClientId = database()->getNextUdpClientId(); + + Q_ASSERT_X(m_udpClientId, "MyList", "Failed to get UDP Client Id"); + using namespace ::AniDBUdpClient; Client::instance()->setHost(m_settings->get("udpClientHost").toString()); Client::instance()->setHostPort(m_settings->get("udpClientHostPort").toUInt()); diff --git a/localmylist/mylist.h b/localmylist/mylist.h index ce29c8c..3489ebd 100644 --- a/localmylist/mylist.h +++ b/localmylist/mylist.h @@ -20,7 +20,8 @@ class RequestHandler; class RenameHandler; class DirectoryWatcher; -class LOCALMYLISTSHARED_EXPORT MyList : public QObject { +class LOCALMYLISTSHARED_EXPORT MyList : public QObject +{ Q_OBJECT Q_PROPERTY(LocalMyList::Database *database READ database) Q_PROPERTY(LocalMyList::Settings *settings READ settings) @@ -29,6 +30,7 @@ class LOCALMYLISTSHARED_EXPORT MyList : public QObject { Q_PROPERTY(int hostId READ hostId) Q_PROPERTY(bool isUdpHost READ isUdpHost) Q_PROPERTY(int runningTaskCount READ runningTaskCount) + Q_PROPERTY(int udpClientId READ udpClientId) public: MyList(); @@ -47,6 +49,8 @@ public: int runningTaskCount(); + int udpClientId() const; + QSettings *defaultLocalQSettings() const; public slots: @@ -99,6 +103,8 @@ private: QSettings *m_defaultLocalQSettings; + int m_udpClientId; + public: static MyList *instance(); static void destroy(); diff --git a/localmylist/share/schema/schema.sql b/localmylist/share/schema/schema.sql index 040a98f..90903af 100644 --- a/localmylist/share/schema/schema.sql +++ b/localmylist/share/schema/schema.sql @@ -369,3 +369,7 @@ CREATE OR REPLACE RULE file_location_update_notify_rule AS -- Delete rules CREATE OR REPLACE RULE file_location_delete_notify_rule AS ON DELETE TO file_location DO SELECT pg_notify('file_location_delete', old.location_id::text || ',' || old.fid::text); + + +-- Sequences +CREATE SEQUENCE udp_client_session; -- 2.52.0