Add GMainloop support in mainloop for CAPI and DBus 08/68708/5
authorSungbae Yoo <sungbae.yoo@samsung.com>
Mon, 9 May 2016 11:01:20 +0000 (20:01 +0900)
committerSungbae Yoo <sungbae.yoo@samsung.com>
Thu, 12 May 2016 11:46:37 +0000 (04:46 -0700)
Change-Id: Icd994077552c7e2a6a8be9f69dad965ea0e5704e
Signed-off-by: Sungbae Yoo <sungbae.yoo@samsung.com>
common/CMakeLists.txt
common/mainloop.cpp
common/mainloop.h
common/rmi/service.cpp
common/rmi/service.h
server/server.cpp

index 217c646..cb76978 100644 (file)
@@ -53,6 +53,7 @@ ADD_LIBRARY(${PROJECT_NAME} STATIC ${COMMON_SOURCES})
 PKG_CHECK_MODULES(COMMON_DEPS REQUIRED  libxml-2.0
                                         sqlite3
                                         libsmack
+                                        gio-2.0
 )
 
 INCLUDE_DIRECTORIES(SYSTEM  ${DPM_COMMON}
index 408e93b..5ecb608 100644 (file)
  *  limitations under the License
  */
 
-#include <sys/epoll.h>
-#include <string.h>
+#include <cstring>
+#include <iostream>
 #include <unistd.h>
 #include <assert.h>
-
-#include <iostream>
+#include <gio/gio.h>
+#include <sys/epoll.h>
 
 #include "error.h"
 #include "exception.h"
 
 namespace runtime {
 
+namespace {
+
+gboolean GIOCallback(GIOChannel* channel, GIOCondition condition, void *data)
+{
+    Mainloop* mainloop = reinterpret_cast<Mainloop*>(data);
+    mainloop->dispatch(-1);
+    return TRUE;
+}
+
+} // namespace
+
 Mainloop::Mainloop() :
     pollFd(::epoll_create1(EPOLL_CLOEXEC)),
     stopped(false)
@@ -123,7 +134,7 @@ void Mainloop::stop()
     wakeupSignal.send();
 }
 
-void Mainloop::run()
+void Mainloop::prepare()
 {
     auto wakeupMainloop = [this](int fd, Mainloop::Event event) {
         wakeupSignal.receive();
@@ -132,9 +143,29 @@ void Mainloop::run()
     };
 
     addEventSource(wakeupSignal.getFd(), EPOLLIN, wakeupMainloop);
+}
+
+void Mainloop::run(bool useGMainloop)
+{
+    prepare();
+
+    if (useGMainloop) {
+        GIOChannel* channel;
+        channel = g_io_channel_unix_new(pollFd);
+        if (channel == NULL) {
+            std::cout << "GMAINLOOP CHANNEL ALLOC FAILED" << std::endl;
+            return;
+        }
+        g_io_add_watch(channel, (GIOCondition)(G_IO_IN|G_IO_HUP), GIOCallback, this);
+        g_io_channel_unref(channel);
 
-    while (!stopped) {
-        dispatch(-1);
+        while (!stopped) {
+            g_main_iteration(TRUE);
+        }
+    } else {
+        while (!stopped) {
+            dispatch(-1);
+        }
     }
 }
 
index 7b9e752..781578b 100644 (file)
@@ -41,7 +41,7 @@ public:
     void addEventSource(const int fd, const Event events, Callback&& callback);
     void removeEventSource(const int fd);
     bool dispatch(const int timeout);
-    void run();
+    void run(bool useGMainloop = false);
     void stop();
 
 private:
@@ -52,6 +52,8 @@ private:
     int pollFd;
     std::atomic<bool> stopped;
     runtime::EventFD wakeupSignal;
+
+    void prepare();
 };
 
 } // namespace runtime
index 7170ed2..52cae3a 100644 (file)
@@ -39,7 +39,7 @@ Service::~Service()
 {
 }
 
-void Service::start()
+void Service::start(bool useGMainloop)
 {
     Socket socket(Socket::create(address));
 
@@ -50,7 +50,8 @@ void Service::start()
     mainloop.addEventSource(socket.getFd(),
                             EPOLLIN | EPOLLHUP | EPOLLRDHUP,
                             accept);
-    mainloop.run();
+
+    mainloop.run(useGMainloop);
 }
 
 void Service::stop()
index 4ebbeda..8b1163a 100644 (file)
@@ -73,7 +73,7 @@ public:
     Service(const Service&) = delete;
     Service& operator=(const Service&) = delete;
 
-    void start();
+    void start(bool useGMainloop = false);
     void stop();
 
     void setNewConnectionCallback(const ConnectionCallback& callback);
index 63a997c..61ffdd9 100644 (file)
@@ -39,7 +39,7 @@ Server::~Server()
 void Server::run()
 {
     // Prepare execution environment
-    service->start();
+    service->start(true);
 }
 
 void Server::terminate()