diff options
Diffstat (limited to 'lib/DBus/DBusManager.cpp')
-rw-r--r-- | lib/DBus/DBusManager.cpp | 179 |
1 files changed, 21 insertions, 158 deletions
diff --git a/lib/DBus/DBusManager.cpp b/lib/DBus/DBusManager.cpp index 0b2cb97f..456c1786 100644 --- a/lib/DBus/DBusManager.cpp +++ b/lib/DBus/DBusManager.cpp @@ -19,185 +19,48 @@ #include "DBusManager.h" #include <iostream> -#include <marshal.h> - -// only for testing - used by LoopSend() -static gboolean send_message(DBusGConnection *con) -{ - DBusMessage *message; - /* Create a new signal "Crash" on the "com.redhat.CrashCatcher" interface, - * from the object "/com/redhat/CrashCatcher". */ - message = dbus_message_new_signal("/com/redhat/CrashCatcher/Crash", - "com.redhat.CrashCatcher", "Crash"); - if(!message){ - fprintf(stderr,"Message creating error"); - } - char *progname = "Foo"; - /* Append some info to the signal */ - dbus_message_append_args(message,DBUS_TYPE_STRING, &progname, DBUS_TYPE_INVALID); - /* get the DBusConnection */ - DBusConnection *dbus_con = dbus_g_connection_get_connection(con); - /* Send the signal via low level dbus function coz glib doesn't seem to work */ - if(!dbus_connection_send(dbus_con, message, NULL)){ - printf("Error while sending message\n"); - } - printf("flushing bus %p\n", dbus_con); - dbus_connection_flush(dbus_con); - /* Free the signal */ - dbus_message_unref(message); - /* Tell the user we send a signal */ - g_print("Message sent!\n"); - /* Return TRUE to tell the event loop we want to be called again */ - return TRUE; -} - +#include "marshal.h" CDBusManager::CDBusManager() { - GError *error = NULL; - g_type_init(); - /* first we need to connect to dbus */ - m_nBus = dbus_g_bus_get(DBUS_BUS, &error); - if(!m_nBus) - throw std::string("Couldn't connect to dbus") + error->message; + DBus::default_dispatcher = new DBus::Glib::BusDispatcher(); + m_pConn = new DBus::Connection(DBus::Connection::SessionBus()); + } CDBusManager::~CDBusManager() { + //delete DBus::default_dispatcher + // delete m_pConn } /* register name com.redhat.CrashCatcher on dbus */ void CDBusManager::RegisterService() { - GError *error = NULL; - guint request_name_result; - g_type_init(); - // then we need a proxy to talk to dbus - m_nBus_proxy = dbus_g_proxy_new_for_name(m_nBus, DBUS_SERVICE_DBUS, - DBUS_PATH_DBUS, - DBUS_INTERFACE_DBUS); - if(!m_nBus_proxy){ - std::cerr << "Error while creating dbus proxy!" << error->message << std::endl; - } - /* and register our name */ - if (!dbus_g_proxy_call(m_nBus_proxy, "RequestName", &error, - G_TYPE_STRING, CC_DBUS_NAME, - G_TYPE_UINT, 0, - G_TYPE_INVALID, - G_TYPE_UINT, &request_name_result, - G_TYPE_INVALID)) + /* this can lead to race condition - rewrite with better check */ + if(m_pConn->has_name(CC_DBUS_NAME)) { - throw std::string("Failed to acquire com.redhat.CrashCatcher:") + error->message ; + /* shouldn't happen, but ... */ + throw std::string("Name already taken: ") + CC_DBUS_NAME; } + /* register our name */ + m_pConn->request_name(CC_DBUS_NAME,DBUS_NAME_FLAG_DO_NOT_QUEUE); #ifdef DEBUG std::cout << "Service running" << std::endl; #endif } -void CDBusManager::ConnectToDaemon() -{ - GError *error = NULL; - guint request_name_result; - g_type_init(); - /* create a proxy object to talk and listen to CC daemon */ - m_nCCBus_proxy = dbus_g_proxy_new_for_name_owner(m_nBus, CC_DBUS_NAME, - CC_DBUS_PATH_NOTIFIER, - CC_DBUS_IFACE, &error); - if(!m_nCCBus_proxy){ -#ifdef DEBUG - std::cerr << "Couldn't connect to daemon via dbus: " << error->message << std::endl; -#endif - throw std::string(error->message); - } -#ifdef DEBUG - std::cout << "Connected! Waiting for signals\n" << std::endl; -#endif -} - -void CDBusManager::RegisterToMessage(const std::string& pMessage, GCallback handler, void * data, GClosureNotify free_data_func) -{ -#ifdef DEBUG - std::cout << "Trying to register" << std::endl; -#endif /*DEBUG*/ - /* Register dbus signal marshaller */ - dbus_g_object_register_marshaller(marshal_VOID__STRING, - G_TYPE_NONE, G_TYPE_STRING, - G_TYPE_INVALID); - dbus_g_proxy_add_signal(m_nCCBus_proxy,"Crash",G_TYPE_STRING, G_TYPE_INVALID); - dbus_g_proxy_connect_signal(m_nCCBus_proxy,"Crash",handler,NULL, NULL); -#ifdef DEBUG - std::cout << "Register done" << std::endl; -#endif /*DEBUG*/ -} - -bool CDBusManager::GSendMessage(const std::string& pMessage, const std::string& pMessParam) -{ - DBusMessage *message; - /* Create a new signal "Crash" on the "com.redhat.CrashCatcher" interface, - * from the object "/com/redhat/CrashCatcher/Crash". */ - message = dbus_message_new_signal ("/com/redhat/CrashCatcher/Crash", - "com.redhat.CrashCatcher", pMessage.c_str()); - if(!message){ - std::cerr << "Message creating error" << std::endl; - } -#ifdef DEBUG - std::cerr << message << std::endl; -#endif - const char *progname = pMessParam.c_str(); - /* Append some info to the signal */ - dbus_message_append_args(message,DBUS_TYPE_STRING, &progname, DBUS_TYPE_INVALID); - /* Send the signal */ - dbus_g_proxy_send(m_nCCBus_proxy, message, NULL); - /* Free the signal */ - dbus_message_unref(message); -#ifdef DEBUG - g_print("Message sent!\n"); -#endif - /* Return TRUE to tell the event loop we want to be called again */ - return TRUE; -} - bool CDBusManager::SendMessage(const std::string& pMessage, const std::string& pMessParam) { - DBusMessage *message; const char *progname = pMessParam.c_str(); - /* Create a new signal "Crash" on the "com.redhat.CrashCatcher" interface, - * from the object "/com/redhat/CrashCatcher/Crash". */ - message = dbus_message_new_signal ("/com/redhat/CrashCatcher/Crash", - "com.redhat.CrashCatcher", pMessage.c_str()); - if(!message){ - std::cerr << "Message creating error" << std::endl; - } -#ifdef DEBUG - std::cerr << message << std::endl; -#endif - /* Add program name as the message argument */ - dbus_message_append_args(message,DBUS_TYPE_STRING, &progname, DBUS_TYPE_INVALID); - /* Send the signal */ - DBusConnection *dbus_con = dbus_g_connection_get_connection(m_nBus); - /* Send the signal via low level dbus functio coz glib sucks */ - if(!dbus_connection_send(dbus_con, message, NULL)){ - throw "Error while sending message"; - } - /* flush the connection, otherwise, it won't show on dbus? */ - dbus_connection_flush(dbus_con); - /* Free the signal */ - dbus_message_unref(message); -#ifdef DEBUG - g_print("Message sent!\n"); -#endif - /* Return TRUE to tell the event loop we want to be called again */ + DBus::SignalMessage mess(CC_DBUS_PATH, CC_DBUS_NAME, pMessage.c_str()); + /* append some info to the signal */ + mess.append(DBUS_TYPE_STRING,&progname,DBUS_TYPE_INVALID); + /* send the message */ + m_pConn->send(mess); + /* flush - to make sure it's not stuck in queue */ + // probably not needed + //m_pConn->flush(); + std::cerr << "Message sent!" << std::endl; return TRUE; } - - -// just for testing purposes -void CDBusManager::LoopSend() -{ - g_timeout_add(1000, (GSourceFunc)send_message, m_nBus); -} - -void CDBusManager::Unregister() -{ - std::cerr << "Unregister" << std::endl; -} |