From 9b66012ea3972cd70ab6853d7da350f43c2ad38d Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Sat, 2 May 2009 14:09:03 +0200 Subject: [PATCH] remove threads once again --- configure.in | 2 - src/callback.cpp | 13 +--- src/misc.cpp | 7 --- src/scrobby.cpp | 179 ++++++++++++++++++++++++------------------------------- src/scrobby.h | 6 +- src/song.cpp | 22 ++----- src/song.h | 6 -- 7 files changed, 85 insertions(+), 150 deletions(-) diff --git a/configure.in b/configure.in index 860a085..cd40b08 100644 --- a/configure.in +++ b/configure.in @@ -31,8 +31,6 @@ AC_CHECK_HEADERS([openssl/evp.h], , AC_MSG_ERROR([missing openssl/evp.h header]) dnl ================================= dnl = checking for curl and pthread = dnl ================================= -AC_CHECK_LIB(pthread, pthread_create, LDFLAGS="$LDFLAGS -lpthread", AC_MSG_ERROR([pthread library is required])) -AC_CHECK_HEADERS([pthread.h], , AC_MSG_ERROR([missing pthread.h header])) AC_PATH_PROG(CURL_CONFIG, curl-config) if test "$CURL_CONFIG" != "" ; then CPPFLAGS="$CPPFLAGS `$CURL_CONFIG --cflags`" diff --git a/src/callback.cpp b/src/callback.cpp index 323a2c5..7b265de 100644 --- a/src/callback.cpp +++ b/src/callback.cpp @@ -75,7 +75,7 @@ void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void if (current_state == MPD::psPlay || current_state == MPD::psPause) { s.SetData(Mpd->CurrentSong()); - MPD::Song::NowPlayingNotify = s.Data && !s.isStream(); + MPD::Song::NowPlayingNotify = s.Queue.size()+s.SubmitQueue.size() != 1 && s.Data && !s.isStream(); } } @@ -94,10 +94,8 @@ void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void } else if (s.Data->artist && s.Data->title) { - myHandshake.Lock(); if (!myHandshake.OK()) { - myHandshake.Unlock(); MPD::Song::NowPlayingNotify = 1; return; } @@ -106,7 +104,7 @@ void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void Log(llInfo, "Sending now playing notification..."); std::ostringstream postdata; - string url, result, postdata_str; + string result, postdata_str; CURLcode code; char *c_artist = curl_easy_escape(0, s.Data->artist, 0); @@ -141,11 +139,8 @@ void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void Log(llVerbose, "URL: %s", myHandshake.NowPlayingURL.c_str()); Log(llVerbose, "Post data: %s", postdata_str.c_str()); - url = myHandshake.NowPlayingURL; - myHandshake.Unlock(); - CURL *np_notification = curl_easy_init(); - curl_easy_setopt(np_notification, CURLOPT_URL, url.c_str()); + curl_easy_setopt(np_notification, CURLOPT_URL, myHandshake.NowPlayingURL.c_str()); curl_easy_setopt(np_notification, CURLOPT_POST, 1); curl_easy_setopt(np_notification, CURLOPT_POSTFIELDS, postdata_str.c_str()); curl_easy_setopt(np_notification, CURLOPT_WRITEFUNCTION, write_data); @@ -174,9 +169,7 @@ void ScrobbyStatusChanged(MPD::Connection *Mpd, MPD::StatusChanges changed, void { Log(llInfo, "Audioscrobbler returned status %s", result.c_str()); // it can return only OK or BADSESSION, so if we are here, BADSESSION was returned. - myHandshake.Lock(); myHandshake.Clear(); - myHandshake.Unlock(); Log(llVerbose, "Handshake reset"); MPD::Song::NowPlayingNotify = 1; } diff --git a/src/misc.cpp b/src/misc.cpp index ba4b2b7..c17bc14 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -36,13 +36,6 @@ size_t write_data(char *buffer, size_t size, size_t nmemb, void *data) return result; } -size_t queue_write_data(char *buffer, size_t size, size_t nmemb, void *data) -{ - size_t result = size * nmemb; - static_cast(data)->append(buffer, result); - return result; -} - void ChangeToUser() { if (Config.dedicated_user.empty() || getuid() != 0) diff --git a/src/scrobby.cpp b/src/scrobby.cpp index fbabe11..94e0cb9 100644 --- a/src/scrobby.cpp +++ b/src/scrobby.cpp @@ -35,18 +35,23 @@ using std::string; Handshake myHandshake; MPD::Song s; -pthread_mutex_t Handshake::itsLock = PTHREAD_MUTEX_INITIALIZER; - namespace { time_t now = 0; - void do_at_exit(); - void signal_handler(int); - - void *queue_handler(void *); + void do_at_exit() + { + s.Submit(); + s.ExtractQueue(); + Log(llInfo, "Shutting down..."); + if (remove(Config.file_pid.c_str()) != 0) + Log(llInfo, "Couldn't remove pid file!"); + } - bool handshake_sent_properly(); + void signal_handler(int) + { + exit(0); + } } int main(int argc, char **argv) @@ -113,26 +118,22 @@ int main(int argc, char **argv) atexit(do_at_exit); - curl_global_init(CURL_GLOBAL_ALL); - - pthread_t queue_thread; - pthread_create(&queue_thread, 0, queue_handler, 0); - pthread_detach(queue_thread); - int handshake_delay = 0; + int queue_delay = 0; int mpd_delay = 0; time_t handshake_ts = 0; + time_t queue_ts = 0; time_t mpd_ts = 0; while (!sleep(1)) { time(&now); - myHandshake.Lock(); + if (now > handshake_ts && !myHandshake.OK()) { myHandshake.Clear(); - if (handshake_sent_properly() && !myHandshake.Status.empty()) + if (myHandshake.Send() && !myHandshake.Status.empty()) { Log(llInfo, "Handshake returned %s", myHandshake.Status.c_str()); } @@ -148,7 +149,7 @@ int main(int argc, char **argv) handshake_ts = time(0)+handshake_delay; } } - myHandshake.Unlock(); + if (Mpd->Connected()) { Mpd->UpdateStatus(); @@ -169,105 +170,79 @@ int main(int argc, char **argv) mpd_ts = time(0)+mpd_delay; } } + + if (now > queue_ts && (!MPD::Song::SubmitQueue.empty() || !MPD::Song::Queue.empty())) + { + if (!MPD::Song::SendQueue()) + { + queue_delay += 30; + Log(llInfo, "Submission failed, retrieving in %d seconds...", queue_delay); + queue_ts = time(0)+queue_delay; + } + else + queue_delay = 0; + } } return 0; } -namespace +bool Handshake::Send() { - void do_at_exit() - { - s.Submit(); - s.ExtractQueue(); - Log(llInfo, "Shutting down..."); - if (remove(Config.file_pid.c_str()) != 0) - Log(llInfo, "Couldn't remove pid file!"); - } - - void signal_handler(int) + CURLcode code; + string handshake_url; + string result; + string timestamp = IntoStr(time(NULL)); + + handshake_url = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=mpc&v="VERSION"&u="; + handshake_url += Config.lastfm_user; + handshake_url += "&t="; + handshake_url += timestamp; + handshake_url += "&a="; + handshake_url += md5sum((Config.lastfm_md5_password.empty() ? md5sum(Config.lastfm_password) : Config.lastfm_md5_password) + timestamp); + + CURL *hs = curl_easy_init(); + curl_easy_setopt(hs, CURLOPT_URL, handshake_url.c_str()); + curl_easy_setopt(hs, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(hs, CURLOPT_WRITEDATA, &result); + curl_easy_setopt(hs, CURLOPT_CONNECTTIMEOUT, curl_connecttimeout); + curl_easy_setopt(hs, CURLOPT_TIMEOUT, curl_timeout); + curl_easy_setopt(hs, CURLOPT_DNS_CACHE_TIMEOUT, 0); + curl_easy_setopt(hs, CURLOPT_NOPROGRESS, 1); + curl_easy_setopt(hs, CURLOPT_NOSIGNAL, 1); + code = curl_easy_perform(hs); + curl_easy_cleanup(hs); + + if (code != CURLE_OK) { - exit(0); + Log(llInfo, "Error while sending handshake: %s", curl_easy_strerror(code)); + return false; } - void *queue_handler(void *) + size_t i = result.find("\n"); + Status = result.substr(0, i); + if (Status != "OK") { - int queue_delay = 0; - time_t queue_ts = 0; - while (!sleep(1)) + if (Status == "BANNED") { - if (now > queue_ts && (!MPD::Song::SubmitQueue.empty() || !MPD::Song::Queue.empty())) - { - if (!MPD::Song::SendQueue()) - { - queue_delay += 30; - Log(llInfo, "Submission failed, retrieving in %d seconds...", queue_delay); - queue_ts = time(0)+queue_delay; - } - else - queue_delay = 0; - } + Log(llInfo, "Ops, this version of scrobby is banned. Please update to the newest one or if it's the newest, inform me about it (electricityispower@gmail.com)"); } - pthread_exit(0); - } - - bool handshake_sent_properly() - { - CURLcode code; - string handshake_url; - string result; - string timestamp = IntoStr(time(NULL)); - - handshake_url = "http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=mpc&v="VERSION"&u="; - handshake_url += Config.lastfm_user; - handshake_url += "&t="; - handshake_url += timestamp; - handshake_url += "&a="; - handshake_url += md5sum((Config.lastfm_md5_password.empty() ? md5sum(Config.lastfm_password) : Config.lastfm_md5_password) + timestamp); - - CURL *hs = curl_easy_init(); - curl_easy_setopt(hs, CURLOPT_URL, handshake_url.c_str()); - curl_easy_setopt(hs, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(hs, CURLOPT_WRITEDATA, &result); - curl_easy_setopt(hs, CURLOPT_CONNECTTIMEOUT, curl_connecttimeout); - curl_easy_setopt(hs, CURLOPT_TIMEOUT, curl_timeout); - curl_easy_setopt(hs, CURLOPT_DNS_CACHE_TIMEOUT, 0); - curl_easy_setopt(hs, CURLOPT_NOPROGRESS, 1); - curl_easy_setopt(hs, CURLOPT_NOSIGNAL, 1); - code = curl_easy_perform(hs); - curl_easy_cleanup(hs); - - if (code != CURLE_OK) + else if (Status == "BADAUTH") { - Log(llInfo, "Error while sending handshake: %s", curl_easy_strerror(code)); - return false; + Log(llInfo, "User authentication failed. Please recheck your username/password settings."); } - - size_t i = result.find("\n"); - myHandshake.Status = result.substr(0, i); - if (myHandshake.Status != "OK") - { - if (myHandshake.Status == "BANNED") - { - Log(llInfo, "Ops, this version of scrobby is banned. Please update to the newest one or if it's the newest, inform me about it (electricityispower@gmail.com)"); - } - else if (myHandshake.Status == "BADAUTH") - { - Log(llInfo, "User authentication failed. Please recheck your username/password settings."); - } - else - return false; - exit(1); - } - result = result.substr(i+1); - i = result.find("\n"); - myHandshake.SessionID = result.substr(0, i); - result = result.substr(i+1); - i = result.find("\n"); - myHandshake.NowPlayingURL = result.substr(0, i); - result = result.substr(i+1); - IgnoreNewlines(result); - myHandshake.SubmissionURL = result; - return true; + else + return false; + exit(1); } + result = result.substr(i+1); + i = result.find("\n"); + SessionID = result.substr(0, i); + result = result.substr(i+1); + i = result.find("\n"); + NowPlayingURL = result.substr(0, i); + result = result.substr(i+1); + IgnoreNewlines(result); + SubmissionURL = result; + return true; } diff --git a/src/scrobby.h b/src/scrobby.h index a2b735c..5c36213 100644 --- a/src/scrobby.h +++ b/src/scrobby.h @@ -43,16 +43,12 @@ struct Handshake bool OK() { return Status == "OK"; } - void Lock() { pthread_mutex_lock(&itsLock); } - void Unlock() { pthread_mutex_unlock(&itsLock); } + bool Send(); std::string Status; std::string SessionID; std::string NowPlayingURL; std::string SubmissionURL; - - private: - static pthread_mutex_t itsLock; }; #endif diff --git a/src/song.cpp b/src/song.cpp index 79a40c2..15868dd 100644 --- a/src/song.cpp +++ b/src/song.cpp @@ -38,8 +38,6 @@ bool MPD::Song::NowPlayingNotify = 0; std::deque MPD::Song::SubmitQueue; std::queue MPD::Song::Queue; -pthread_mutex_t MPD::Song::itsQueueMutex = PTHREAD_MUTEX_INITIALIZER; - MPD::Song::Song() : Data(0), StartTime(0), Playback(0), @@ -83,10 +81,8 @@ void MPD::Song::Submit() if (canBeSubmitted()) { - LockQueue(); Queue.push(*this); Data = 0; - UnlockQueue(); Log(llInfo, "Song queued for submission."); } Clear(); @@ -185,19 +181,14 @@ void MPD::Song::ExtractQueue() bool MPD::Song::SendQueue() { - LockQueue(); ExtractQueue(); - UnlockQueue(); - myHandshake.Lock(); if (!myHandshake.OK()) - { - myHandshake.Unlock(); return false; - } + Log(llInfo, "Submitting songs..."); - string url, result, postdata; + string result, postdata; CURLcode code; postdata = "s="; @@ -209,14 +200,11 @@ bool MPD::Song::SendQueue() Log(llVerbose, "URL: %s", myHandshake.SubmissionURL.c_str()); Log(llVerbose, "Post data: %s", postdata.c_str()); - url = myHandshake.SubmissionURL; - myHandshake.Unlock(); - CURL *submission = curl_easy_init(); - curl_easy_setopt(submission, CURLOPT_URL, url.c_str()); + curl_easy_setopt(submission, CURLOPT_URL, myHandshake.SubmissionURL.c_str()); curl_easy_setopt(submission, CURLOPT_POST, 1); curl_easy_setopt(submission, CURLOPT_POSTFIELDS, postdata.c_str()); - curl_easy_setopt(submission, CURLOPT_WRITEFUNCTION, queue_write_data); + curl_easy_setopt(submission, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(submission, CURLOPT_WRITEDATA, &result); curl_easy_setopt(submission, CURLOPT_CONNECTTIMEOUT, curl_queue_connecttimeout); curl_easy_setopt(submission, CURLOPT_TIMEOUT, curl_queue_timeout); @@ -247,9 +235,7 @@ bool MPD::Song::SendQueue() { Log(llInfo, "Audioscrobbler returned status %s", result.c_str()); // BADSESSION or FAILED was returned, handshake needs resetting. - myHandshake.Lock(); myHandshake.Clear(); - myHandshake.Unlock(); Log(llVerbose, "Handshake reset"); } return false; diff --git a/src/song.h b/src/song.h index ffb5ab8..ea4abce 100644 --- a/src/song.h +++ b/src/song.h @@ -42,9 +42,6 @@ namespace MPD time_t StartTime; int Playback; - static void LockQueue() { pthread_mutex_lock(&itsQueueMutex); } - static void UnlockQueue() { pthread_mutex_unlock(&itsQueueMutex); } - static void GetCached(); static void ExtractQueue(); @@ -58,9 +55,6 @@ namespace MPD private: void Clear(); - static pthread_mutex_t itsSubmitQueueMutex; - static pthread_mutex_t itsQueueMutex; - bool canBeSubmitted(); bool itsIsStream; }; -- 2.11.4.GIT