From ff2627e7c597e50025ca4d91ff8168eec80f9054 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 8 Sep 2009 17:05:57 +0200 Subject: make Warning, Error and Update send unicast dbus messages Signed-off-by: Denys Vlasenko --- lib/CommLayer/CommLayerInner.cpp | 71 ++++++++++++++++++++++++---------------- lib/CommLayer/CommLayerInner.h | 4 +++ lib/CommLayer/Observer.h | 4 +-- 3 files changed, 48 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/CommLayer/CommLayerInner.cpp b/lib/CommLayer/CommLayerInner.cpp index ec4dc904..5450b41c 100644 --- a/lib/CommLayer/CommLayerInner.cpp +++ b/lib/CommLayer/CommLayerInner.cpp @@ -1,50 +1,63 @@ -#include /* pthread_self() */ +#include +#include #include "abrtlib.h" #include "CommLayerInner.h" static CObserver *s_pObs; -static pthread_t s_main_id; + +typedef std::map map_uint_str_t; +static map_uint_str_t s_mapClientID; +static pthread_mutex_t s_map_mutex; +static bool s_map_mutex_inited; void init_daemon_logging(CObserver *pObs) { s_pObs = pObs; - s_main_id = pthread_self(); + if (!s_map_mutex_inited) + { + pthread_mutex_init(&s_map_mutex, NULL); + s_map_mutex_inited = true; + } +} + +void set_client_name(const char* name) +{ + uint64_t key = uint64_t(pthread_self()); + + pthread_mutex_lock(&s_map_mutex); + if (!name) + s_mapClientID.erase(key); + else + s_mapClientID[key] = name; + pthread_mutex_unlock(&s_map_mutex); } void warn_client(const std::string& pMessage) { if (!s_pObs) return; - pthread_t self = pthread_self(); - if (self != s_main_id) - { - s_pObs->Warning(pMessage,(uint64_t)self); -//log("w: '%s'", s.c_str()); - } - else - { - s_pObs->Warning(pMessage); -// debug: this should not happen - if it is, we are trying to log to a client -// but we have no job id! -log("W: '%s'", pMessage.c_str()); - } + + uint64_t key = uint64_t(pthread_self()); + + pthread_mutex_lock(&s_map_mutex); + map_uint_str_t::const_iterator ki = s_mapClientID.find(key); + const char* peer = (ki != s_mapClientID.end() ? ki->second.c_str() : NULL); + pthread_mutex_unlock(&s_map_mutex); + + s_pObs->Warning(pMessage, peer, key); } void update_client(const std::string& pMessage) { if (!s_pObs) return; - pthread_t self = pthread_self(); - if (self != s_main_id) - { - s_pObs->Status(pMessage, (uint64_t)self); -//log("u: '%s'", s.c_str()); - } - else - { - s_pObs->Status(pMessage); -// debug: this should not happen - if it is, we are trying to log to a client -// but we have no job id! -log("U: '%s'", pMessage.c_str()); - } + + uint64_t key = uint64_t(pthread_self()); + + pthread_mutex_lock(&s_map_mutex); + map_uint_str_t::const_iterator ki = s_mapClientID.find(key); + const char* peer = (ki != s_mapClientID.end() ? ki->second.c_str() : NULL); + pthread_mutex_unlock(&s_map_mutex); + + s_pObs->Status(pMessage, peer, key); } diff --git a/lib/CommLayer/CommLayerInner.h b/lib/CommLayer/CommLayerInner.h index 2b4f63a3..d161cfc7 100644 --- a/lib/CommLayer/CommLayerInner.h +++ b/lib/CommLayer/CommLayerInner.h @@ -5,6 +5,10 @@ void init_daemon_logging(CObserver *pObs); +/* + * Set client's name (dbus ID). NULL unsets it. + */ +void set_client_name(const char* name); /* Ask a client to warn the user about a non-fatal, but unexpected condition. * In GUI, it will usually be presented as a popup message. */ diff --git a/lib/CommLayer/Observer.h b/lib/CommLayer/Observer.h index 5c983949..421dc0cc 100644 --- a/lib/CommLayer/Observer.h +++ b/lib/CommLayer/Observer.h @@ -9,8 +9,8 @@ class CObserver { public: virtual ~CObserver() {} - virtual void Status(const std::string& pMessage, uint64_t pDest=0) = 0; - virtual void Warning(const std::string& pMessage, uint64_t pDest=0) = 0; + virtual void Status(const std::string& pMessage, const char* peer, uint64_t pDest) = 0; + virtual void Warning(const std::string& pMessage, const char* peer, uint64_t pDest) = 0; }; #endif -- cgit