Allow to set event loop type with api 29/260229/2
authorCheoleun Moon <chleun.moon@samsung.com>
Tue, 22 Jun 2021 07:57:56 +0000 (16:57 +0900)
committerCheoleun Moon <chleun.moon@samsung.com>
Wed, 23 Jun 2021 05:42:46 +0000 (14:42 +0900)
Change-Id: Ib33d2e130f0cc939272d496605a64a768188335c
Signed-off-by: Cheoleun Moon <chleun.moon@samsung.com>
CMakeLists.txt
include/vine.h
packaging/capi-network-vine.spec
src/include/vine-event-loop.h
src/include/vine-private.h
src/vine-event-loop-glib.cpp
src/vine-event-loop.cpp
src/vine-private.cpp
src/vine.cpp
tests/vine-test/vine-test-glib.cpp

index 964c045899abd5631e47e354098ca9936fd0bfbc..85a4fb8cd3b56b0a9a6d6d86fa47c6d223568e47 100755 (executable)
@@ -50,12 +50,11 @@ IF(TIZEN_OS)
 ENDIF(TIZEN_OS)
 
 IF(USE_EVENT_LOOP_EPOLL)
-       ADD_DEFINITIONS("-DVINE_EVENT_LOOP_EPOLL")
+       ADD_DEFINITIONS("-DUSE_VINE_EVENT_LOOP_EPOLL")
 ENDIF(USE_EVENT_LOOP_EPOLL)
 
 IF(USE_EVENT_LOOP_EXTERNAL_GLIB)
-       REMOVE_DEFINITIONS("-DVINE_EVENT_LOOP_EPOLL")
-       ADD_DEFINITIONS("-DVINE_EVENT_LOOP_EXTERNAL_GLIB")
+       ADD_DEFINITIONS("-DUSE_VINE_EVENT_LOOP_EXTERNAL_GLIB")
        STRING(CONCAT DEPS ${DEPS} " glib-2.0 gio-2.0")
 ENDIF(USE_EVENT_LOOP_EXTERNAL_GLIB)
 
index 97cfa95de100f2cf70f66b25b8089ce371465888..cdb4096f856008b8033a6bfe20ac18b1a26b7a41 100755 (executable)
@@ -290,6 +290,22 @@ typedef enum {
        VINE_ADDRESS_FAMILY_IPV6,
 } vine_address_family_e;
 
+/**
+ * @brief Enumeration for event loop
+ * @since_tizen 6.5
+ */
+typedef enum {
+       /**
+        * App uses event fd from vine
+        */
+       VINE_EVENT_LOOP_DEFAULT = 0,
+
+       /**
+        * External glib event loop
+        */
+       VINE_EVENT_LOOP_EXTERNAL_GLIB,
+} vine_event_loop_e;
+
 /**
  * @brief Initializes Vine.
  * @since_tizen 6.5
@@ -326,6 +342,18 @@ int vine_deinitialize();
  */
 int vine_get_capabilities(vine_capability_type_e type, int *capabilities);
 
+/**
+ * @brief Sets event loop.
+ * @since_tizen 6.5
+ * @param[in] loop The event loop
+  * @return 0 on success, otherwise a negative error value
+ * @retval #VINE_ERROR_NONE    Successful
+ * @retval #VINE_ERROR_NOT_INITIALIZED Not initialized
+ * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
+ * @see vine_initialize()
+ */
+int vine_set_event_loop(vine_event_loop_e loop);
+
 /**
  * @brief Gets event file discriptor for monitoring Vine events.
  * @since_tizen 6.5
index bedbdf8507c51ad438a7eff3c54b62156c5177c9..24110590e8d53d38675a8844a0f2419836f306c2 100755 (executable)
@@ -3,7 +3,7 @@
 %bcond_without use_glib_event_loop
 Name:    capi-network-vine
 Summary: An service discovery framework
-Version: 1.0.8
+Version: 1.0.9
 Release: 0
 Group:   Network & Connectivity/API
 License: Apache-2.0
@@ -90,12 +90,11 @@ export LDFLAGS+=" -lgcov"
         -DLIB_DIR:PATH=%{_libdir} \
         -DBIN_DIR:PATH=%{_bindir} \
         -DINCLUDE_DIR:PATH=%{_includedir} \
+        -DUSE_EVENT_LOOP_EPOLL=ON \
 %if %{with use_glib_event_loop}
         -DUSE_EVENT_LOOP_EXTERNAL_GLIB=ON \
-        -DUSE_EVENT_LOOP_EPOLL=OFF \
 %else
-        -DUSE_EVENT_LOOP_EXTERNAL_GLIB=OF \
-        -DUSE_EVENT_LOOP_EPOLL=ON \
+        -DUSE_EVENT_LOOP_EXTERNAL_GLIB=OFF \
 %endif
 %if %{with lws_static}
         -DUSE_LIBWEBSOCKETS_STATIC=ON \
index f2a09d9bf30cce83e63b92bfdc05bbba8fafeb4b..48ab5b647fa15c047c9ab46fd2d1bfccd4d41aca 100755 (executable)
@@ -20,6 +20,8 @@
 #include <sys/epoll.h>
 #include <pthread.h>
 
+#include "vine.h"
+
 typedef void *vine_event_queue_h;
 
 typedef void (*vine_poll_handler)(int fd, int events, void *user_data);
@@ -35,6 +37,8 @@ enum {
        VINE_POLLHUP = EPOLLHUP,
 };
 
+int vine_event_loop_set(vine_event_loop_e loop);
+
 int vine_event_loop_init();
 void vine_event_loop_deinit();
 int vine_event_loop_start();
index 53ad15cab9cc4812e3f90345ec85aa202b4327b9..a972914d09b288d7cf441ad8dbfe5f45da2c8000 100755 (executable)
@@ -20,5 +20,6 @@
 int _vine_init();
 int _vine_deinit();
 int _vine_get_capabilities(int type, int *capabilities);
+int _vine_set_event_loop(vine_event_loop_e loop);
 
 #endif
index 4f252f9b1d4ea1a2f4025686b0ac7ae733da6bdb..eafea336d5834666af881adc4cee110022cf6ec2 100755 (executable)
@@ -15,7 +15,7 @@
  *
 */
 
-#if defined VINE_EVENT_LOOP_EXTERNAL_GLIB
+#if defined USE_VINE_EVENT_LOOP_EXTERNAL_GLIB
 #include <glib.h>
 #include <unistd.h>
 #include <stdint.h>
@@ -221,4 +221,4 @@ vine_event_loop_fn vine_event_loop_glib = {
        .add_event = vine_event_loop_glib_add_event,
 };
 
-#endif // VINE_EVENT_LOOP_EXTERNAL_GLIB
\ No newline at end of file
+#endif // USE_VINE_EVENT_LOOP_EXTERNAL_GLIB
\ No newline at end of file
index e31be407fc1c68bbfc72c72bf418a51396deb029..b1dacf5c6d362b618bf60554c2a9ae0189e83c0f 100755 (executable)
 #include "vine-log.h"
 #include "vine-utils.h"
 
-static vine_event_loop_fn __event_loop;
-#if defined VINE_EVENT_LOOP_EPOLL
+#if defined USE_VINE_EVENT_LOOP_EPOLL
 extern vine_event_loop_fn vine_event_loop_epoll;
 #endif
-#if defined VINE_EVENT_LOOP_EXTERNAL_GLIB
+#if defined USE_VINE_EVENT_LOOP_EXTERNAL_GLIB
 extern vine_event_loop_fn vine_event_loop_glib;
 #endif
 
-int vine_event_loop_init()
+static vine_event_loop_fn __event_loop[] = {
+#if defined USE_VINE_EVENT_LOOP_EPOLL
+       [VINE_EVENT_LOOP_DEFAULT] = vine_event_loop_epoll,
+#endif
+#if defined USE_VINE_EVENT_LOOP_EXTERNAL_GLIB
+       [VINE_EVENT_LOOP_EXTERNAL_GLIB] = vine_event_loop_glib,
+#endif
+};
+
+static int __event_loop_type = 0;
+
+int vine_event_loop_set(vine_event_loop_e loop)
 {
-#if defined VINE_EVENT_LOOP_EPOLL
-       __event_loop = vine_event_loop_epoll;
-#elif defined VINE_EVENT_LOOP_EXTERNAL_GLIB
-       __event_loop = vine_event_loop_glib;
+       RET_VAL_IF(loop < VINE_EVENT_LOOP_DEFAULT || loop > VINE_EVENT_LOOP_EXTERNAL_GLIB,
+               VINE_ERROR_INVALID_PARAMETER, "Invalid parameter");
+
+#ifndef USE_VINE_EVENT_LOOP_EPOLL
+       RET_VAL_IF(loop == VINE_EVENT_LOOP_DEFAULT, VINE_ERROR_INVALID_PARAMETER,
+               "External glib event loop is not supported");
 #endif
+#ifndef USE_VINE_EVENT_LOOP_EXTERNAL_GLIB
+       RET_VAL_IF(loop == VINE_EVENT_LOOP_EXTERNAL_GLIB, VINE_ERROR_INVALID_PARAMETER,
+               "External glib event loop is not supported");
+#endif
+
+       __event_loop_type = (int)loop;
+       return VINE_ERROR_NONE;
+}
 
-       return __event_loop.init();
+int vine_event_loop_init()
+{
+       return __event_loop[__event_loop_type].init();
 }
 
 void vine_event_loop_deinit()
 {
-       __event_loop.deinit();
+       __event_loop[__event_loop_type].deinit();
 }
 
 int vine_event_loop_start()
 {
-       return __event_loop.start();;
+       return __event_loop[__event_loop_type].start();;
 }
 
 void vine_event_loop_stop()
 {
-       __event_loop.stop();;
+       __event_loop[__event_loop_type].stop();;
 }
 
 int vine_event_queue_create(vine_event_queue_h *event_queue)
 {
-       return __event_loop.event_queue_create(event_queue);;
+       return __event_loop[__event_loop_type].event_queue_create(event_queue);;
 }
 
 void vine_event_queue_destroy(vine_event_queue_h event_queue)
 {
-       __event_loop.event_queue_destroy(event_queue);
+       __event_loop[__event_loop_type].event_queue_destroy(event_queue);
 }
 
 void vine_event_loop_get_eventfd(vine_event_queue_h event_queue, int *eventfd)
 {
-       __event_loop.get_eventfd(event_queue, eventfd);
+       __event_loop[__event_loop_type].get_eventfd(event_queue, eventfd);
 }
 
 int vine_event_loop_add_io_handler(
        int fd, int events, vine_poll_handler handler, void *user_data)
 {
-       return __event_loop.add_io_handler(fd, events, handler, user_data);
+       return __event_loop[__event_loop_type].add_io_handler(fd, events, handler, user_data);
 }
 
 int vine_event_loop_mod_io_handler(
        int fd, int events, vine_poll_handler handler, void *user_data)
 {
-       return __event_loop.mod_io_handler(fd, events, handler, user_data);
+       return __event_loop[__event_loop_type].mod_io_handler(fd, events, handler, user_data);
 }
 
 int vine_event_loop_del_io_handler(int fd)
 {
-       return __event_loop.del_io_handler(fd);
+       return __event_loop[__event_loop_type].del_io_handler(fd);
 }
 
 // Register an event handler which is called in vine_event_loop_process()
@@ -96,10 +118,10 @@ int vine_event_loop_add_event(vine_event_queue_h event_queue, void *event_data,
        vine_event_handler handler, vine_event_free_handler free_func,
        void *user_data)
 {
-       return __event_loop.add_event(event_queue, event_data, handler, free_func, user_data);
+       return __event_loop[__event_loop_type].add_event(event_queue, event_data, handler, free_func, user_data);
 }
 
 int vine_event_loop_process(vine_event_queue_h event_queue)
 {
-       return __event_loop.process(event_queue);
+       return __event_loop[__event_loop_type].process(event_queue);
 }
index 80a322a581bb4fd4bccf80df065a9001119e6acf..c2561f4e034140072bc1585aebb0fabfce10b7a7 100755 (executable)
@@ -125,3 +125,14 @@ int _vine_get_capabilities(int type, int *capabilities)
 
        return VINE_ERROR_NONE;
 }
+
+int _vine_set_event_loop(vine_event_loop_e loop)
+{
+       VINE_LOCK(&__vine_mutex);
+       if (__vine_is_initialized()) {
+               VINE_UNLOCK(&__vine_mutex);
+               return VINE_ERROR_INVALID_OPERATION;
+       }
+       VINE_UNLOCK(&__vine_mutex);
+       return vine_event_loop_set(loop);
+}
\ No newline at end of file
index 9c7230793dcd915e3f27902fffdb571203ea9294..f5fb50a3265329e1b71b7626f50b1ab3b8a9336c 100755 (executable)
@@ -52,6 +52,14 @@ API int vine_get_capabilities(vine_capability_type_e type, int *capabilities)
        return _vine_get_capabilities(type, capabilities);
 }
 
+int vine_set_event_loop(vine_event_loop_e loop)
+{
+       __VINE_FUNC_ENTER__;
+       CHECK_FEATURE_SUPPORTED;
+
+       return _vine_set_event_loop(loop);
+}
+
 API int vine_session_get_event_fd(vine_session_h session, int *fd)
 {
        __VINE_FUNC_ENTER__;
index 36aade2ba68b9aa7c3771486e09f6114a1f1dcad..af092168dd093fc463e5fb0b3481b0c64dce947a 100755 (executable)
@@ -96,6 +96,7 @@ static void __start_event_loop()
 static void __init()
 {
        PRINT_IF_ERROR(vine_initialize(), "vine_initialize()");
+       PRINT_IF_ERROR(vine_set_event_loop(VINE_EVENT_LOOP_EXTERNAL_GLIB), "vine_set_event_loop");
 }
 
 static void __registered_cb(vine_session_h session,