vine-tool: timefd() bug fix
[platform/core/api/vine.git] / tool / tool_run.cpp
index 82fa950..b9e600d 100644 (file)
@@ -63,6 +63,7 @@ static struct {
 static std::map<vine_dp_h, int> timerfds;
 static int send_message(vine_dp_h dp);
 static void _stop_message_timer(vine_dp_h dp);
+static int joined_peer = 0;
 
 static vine_dp_type_e _convert_dp_type(dp_type_t type)
 {
@@ -192,12 +193,19 @@ static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data)
 static void __peer_joined_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
        printf("pubsub peer(%s) is joined.\n", peer_id);
-       send_message(dp);
+       ++joined_peer;
+       // if joined_peer is larger than 1,
+       // this means message timer has already runned.
+       if (joined_peer == 1)
+               send_message(dp);
 }
 
 static void __peer_left_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
        printf("pubsub peer(%s) is left.\n", peer_id);
+       --joined_peer;
+       if (joined_peer == 0)
+               _stop_message_timer(dp);
 }
 
 static void __registered_cb(vine_session_h session,
@@ -263,7 +271,7 @@ static void _start_message_timer(vine_dp_h dp, int sec)
                return;
 
        struct itimerspec value;
-       value.it_interval.tv_sec = 0;
+       value.it_interval.tv_sec = sec;
        value.it_interval.tv_nsec = 0;
        value.it_value.tv_sec = sec;
        value.it_value.tv_nsec = 0;
@@ -291,6 +299,11 @@ static void _stop_message_timer(vine_dp_h dp)
                return;
        }
 
+       uint64_t buf;
+       int expired = read(timerfd, &buf, sizeof(uint64_t));
+       if (expired != sizeof(uint64_t))
+               printf("expired[%d]. operation failed.\n", expired);
+
        epoll_ctl(epollfd, EPOLL_CTL_DEL, timerfd, NULL);
        close(timerfd);
        timerfds.erase(it);