Add vine_dp_get_id() 80/259080/7
authorSeonah Moon <seonah1.moon@samsung.com>
Tue, 1 Jun 2021 02:15:26 +0000 (11:15 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 1 Jun 2021 07:46:34 +0000 (16:46 +0900)
- Add vine_dp_get_id()
- Pass a peer Id to joined/left callback instead of IP/port

Change-Id: I2c0f9db4fdbf9e37b0746a2f30f580ae3ac13488

include/vine.h
packaging/capi-network-vine.spec
src/include/vine-dp.h
src/vine-dp.cpp
tests/unittest/vine-unittest-dp.cpp
tests/vine-test/vine-pubsub-open-close-test.cpp
tool/tool_run.cpp

index 2fbca0a..4972a6a 100755 (executable)
@@ -822,6 +822,21 @@ int vine_dp_create(vine_session_h session, vine_dp_type_e type, vine_dp_h *dp);
 int vine_dp_destroy(vine_dp_h dp);
 
 /**
+ * @brief Gets the Id for DP_TYPE_PUBSUB.
+ * @remarks This should be called after completing vine_dp_open()
+ * @since_tizen 6.5
+ * @param[in] dp The data path handle
+ * @param[out] id The PubSub Id
+ * @return 0 on success, otherwise a negative error value
+ * @retval #VINE_ERROR_NONE    Successful
+ * @retval #VINE_ERROR_INVALID_PARAMETER       Invalid parameter
+ * @retval #VINE_ERROR_INVALID_OPERATION       Invalid operation
+ * @retval #VINE_ERROR_NOT_SUPPORTED Not supported
+ * @see vine_dp_create()
+ */
+int vine_dp_get_id(vine_dp_h dp, char **id);
+
+/**
  * @brief Sets the interface name to use for datapath.
  * @remarks If the interface has multiple IP addresses, please set an IP address as @a iface_name.
  * @since_tizen 6.5
@@ -1202,13 +1217,12 @@ int vine_dp_unset_received_cb(vine_dp_h dp);
  * @brief Called when a pubsub peer is joined.
  * @since_tizen 6.5
  * @param[in] dp The datapath handle
- * @param[in] ip The peer's IP address
- * @param[in] port The peer's port
+ * @param[in] peer_id The peer's Id
  * @param[in] user_data The user data passed from the callback registration function
  * @see vine_dp_set_peer_joined_cb()
  * @see vine_dp_unset_peer_joined_cb()
  */
-typedef void(*vine_dp_peer_joined_cb)(vine_dp_h dp, const char *ip, int port, void *user_data);
+typedef void(*vine_dp_peer_joined_cb)(vine_dp_h dp, const char *peer_id, void *user_data);
 
 /**
  * @brief Registers the callback called when a pubsub peer is joined.
@@ -1242,13 +1256,12 @@ int vine_dp_unset_peer_joined_cb(vine_dp_h dp);
  * @brief Called when a pubsub peer is left.
  * @since_tizen 6.5
  * @param[in] dp The datapath handle. VINE_DP_TYPE_PUBSUB only
- * @param[in] ip The peer's IP address
- * @param[in] port The peer's port
+ * @param[in] peer_id The peer's Id
  * @param[in] user_data The user data passed from the callback registration function
  * @see vine_dp_set_peer_left_cb()
  * @see vine_dp_unset_peer_left_cb()
  */
-typedef void(*vine_dp_peer_left_cb)(vine_dp_h dp, const char *ip, int port, void *user_data);
+typedef void(*vine_dp_peer_left_cb)(vine_dp_h dp, const char *peer_id, void *user_data);
 
 /**
  * @brief Registers the callback called when a pubsub peer is left.
index 5260543..8c245c4 100755 (executable)
@@ -2,7 +2,7 @@
 %bcond_without lws_static_prebuilt
 Name:    capi-network-vine
 Summary: An service discovery framework
-Version: 1.0.2
+Version: 1.0.3
 Release: 0
 Group:   Network & Connectivity/API
 License: Apache-2.0
index f070950..2e9ab9c 100644 (file)
@@ -218,13 +218,14 @@ public:
        virtual int set_peer_left_cb(vine_dp_peer_left_cb callback, void *user_data);
        virtual int unset_peer_left_cb();
 
-       void invoke_peer_joined_cb(const char *ip, int port);
-       void invoke_peer_left_cb(const char *ip, int port);
+       void invoke_peer_joined_cb(const char *peer_id);
+       void invoke_peer_left_cb(const char *peer_id);
 
        int connect(const char *service_name, const char *ip, int port);
        int close_server_dp();
        bool is_joined_peer(const char *service_name, const char *ip);
        int get_joined_peer();
+       const char *get_joined_peer_id(vine_data_path_h datapath);
        void add_joined_peer(const char *service_name, vine_data_path_h datapath);
        void del_joined_peer(vine_data_path_h datapath);
        void clear_joined_peer();
@@ -233,7 +234,7 @@ public:
 
        int publish_service();
        int subscribe_service();
-       void set_service_name(const char *service_name) { mServiceName = service_name; }
+       void set_id(const char *id) { mId = id; }
        bool check_if_connect(const char *peer_rank,
                        vine_address_family_e ip_type, const char *peer_ip, int peer_port);
        int get_rank() { return mRank; }
@@ -244,7 +245,7 @@ private:
        std::string mTopic;
        vine_disc_h mSdSub;
        vine_disc_h mSdPub;
-       std::string mServiceName;
+       std::string mId;
 
        vine_address_family_e mAddrFamily;
        std::set<std::string> mIpList;
@@ -264,6 +265,8 @@ private:
 
        int create_rank();
        int compare_ip_priority(const char *peer_ip);
+       void create_id(char id[]);
+
 };
 
 } // namespace vine
index 014bc95..3a77b0d 100644 (file)
@@ -145,11 +145,13 @@ static void _pubsub_terminated_cb(vine_data_path_h datapath, void *user_data)
 
        VINE_LOGD("datapath[%p] is terminated by peer.", datapath);
        DPPubSub *dp =  static_cast<DPPubSub *>(user_data);
-       dp->del_joined_peer(datapath);
 
-       const char *ip = vine_data_path_get_ip(datapath);
-       int port = vine_data_path_get_port(datapath);
-       dp->invoke_peer_left_cb(ip, port);
+       char *peer_id = STRDUP(dp->get_joined_peer_id(datapath));
+       dp->del_joined_peer(datapath);
+       if (peer_id) {
+               dp->invoke_peer_left_cb(peer_id);
+               free(peer_id);
+       }
        vine_data_path_destroy(datapath);
 }
 
@@ -199,14 +201,12 @@ static void _pubsub_accepted_cb(vine_data_path_h datapath, void *user_data)
        }
 
        dp->add_joined_peer(token, datapath);
-       free(token);
 
        vine_data_path_set_received_cb(datapath, _pubsub_received_cb, user_data);
        vine_data_path_set_terminated_cb(datapath, _pubsub_terminated_cb, user_data);
 
-       const char *ip = vine_data_path_get_ip(datapath);
-       int port = vine_data_path_get_port(datapath);
-       dp->invoke_peer_joined_cb(ip, port);
+       dp->invoke_peer_joined_cb(token);
+       free(token);
 }
 
 struct pubsub_connect_data {
@@ -235,10 +235,7 @@ static void _pubsub_connected_cb(vine_data_path_h datapath, int result, void *us
        vine_data_path_set_received_cb(datapath, _pubsub_received_cb, conn_data->dp);
        vine_data_path_set_terminated_cb(datapath, _pubsub_terminated_cb, conn_data->dp);
 
-       const char *ip = vine_data_path_get_ip(datapath);
-       int port = vine_data_path_get_port(datapath);
-       dp->invoke_peer_joined_cb(ip, port);
-
+       dp->invoke_peer_joined_cb(conn_data->service_name);
        conn_data->dp = NULL;
        free(conn_data);
 }
@@ -378,7 +375,7 @@ static void _service_published_cb(vine_disc_h disc,
                return;
 
        DPPubSub *dp = static_cast<DPPubSub *>(user_data);
-       dp->set_service_name(service_name);
+       dp->set_id(service_name);
        int ret = dp->subscribe_service();
        if (ret != VINE_ERROR_NONE)
                dp->close();
@@ -829,7 +826,7 @@ DPPubSub::DPPubSub(void *event_fd)
        mTopic = "";
        mSdSub = NULL;
        mSdPub = NULL;
-       mServiceName = "";
+       mId = "";
 
        // Network Info
        mAddrFamily = VINE_ADDRESS_FAMILY_DEFAULT;
@@ -959,16 +956,16 @@ int DPPubSub::unset_peer_left_cb()
        return VINE_ERROR_NONE;
 }
 
-void DPPubSub::invoke_peer_joined_cb(const char *ip, int port)
+void DPPubSub::invoke_peer_joined_cb(const char *peer_id)
 {
        if (mPeerJoinedCb)
-               mPeerJoinedCb(static_cast<void *>(this), ip, port, mPeerJoinedCbData);
+               mPeerJoinedCb(static_cast<void *>(this), peer_id, mPeerJoinedCbData);
 }
 
-void DPPubSub::invoke_peer_left_cb(const char *ip, int port)
+void DPPubSub::invoke_peer_left_cb(const char *peer_id)
 {
        if (mPeerLeftCb)
-               mPeerLeftCb(static_cast<void *>(this), ip, port, mPeerJoinedCbData);
+               mPeerLeftCb(static_cast<void *>(this), peer_id, mPeerJoinedCbData);
 }
 
 int DPPubSub::connect(const char *service_name, const char *ip, int port)
@@ -982,7 +979,7 @@ int DPPubSub::connect(const char *service_name, const char *ip, int port)
        conn_data->service_name = STRDUP(service_name);
 
        int ret = vine_data_path_connect(mAddrFamily, ip, port, iface_name,
-                       mSecurity, mServiceName.c_str(),
+                       mSecurity, mId.c_str(),
                        _pubsub_connected_cb, (void *)conn_data,
                        &datapath, mEventFd);
        return ret;
@@ -995,6 +992,20 @@ int DPPubSub::close_server_dp()
        return ret;
 }
 
+void DPPubSub::create_id(char id[])
+{
+       const char *map = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+       const size_t map_len = strlen(map);
+
+       string rand_str;
+       generate_n(back_inserter(rand_str), 6,
+               [&](){
+                       return map[rand() % map_len];
+               });
+       snprintf(id, VINE_MAX_SERVICE_NAME_LEN, "%s-%s",
+                       VINE_DP_PUBSUB_SERVICE_NAME_PREFIX, rand_str.c_str());
+}
+
 int DPPubSub::publish_service()
 {
        vine_service_h service;
@@ -1011,9 +1022,8 @@ int DPPubSub::publish_service()
 
        mRank = create_rank();
        sprintf(rank_str, "%d", mRank);
-       snprintf(service_name, VINE_MAX_SERVICE_NAME_LEN,
-                       "%s-%d", VINE_DP_PUBSUB_SERVICE_NAME_PREFIX, mRank);
 
+       create_id(service_name);
        vine_service_set_name(service, service_name);
        vine_service_add_attribute(service, VINE_DP_PUBSUB_RANK_KEY, (const char *)rank_str);
 
@@ -1146,7 +1156,7 @@ int DPPubSub::recv(unsigned char *buf, size_t buf_len, size_t *read_len)
 
 bool DPPubSub::is_joined_peer(const char *service_name, const char *ip)
 {
-       if (mServiceName.compare(service_name) == 0) {
+       if (mId.compare(service_name) == 0) {
                VINE_LOGD("It's me!");
                mIpList.insert(ip);
                return true;
@@ -1160,6 +1170,27 @@ int DPPubSub::get_joined_peer()
        return mDataPathList.size();
 }
 
+const char *DPPubSub::get_joined_peer_id(vine_data_path_h datapath)
+{
+       if (!datapath)
+               return NULL;
+
+       DPMap::iterator found = std::find_if(mDataPathList.begin(),
+                       mDataPathList.end(),
+                       [datapath](std::pair<std::string, vine_data_path_h> const& item)
+                       {
+                               VINE_LOGD("item.second[%p]", item.second);
+                               return (item.second == datapath);
+                       });
+
+       if (found == mDataPathList.end()) {
+               VINE_LOGE("Cannot find the datapath[%p].", datapath);
+               return NULL;
+       }
+
+       return found->first.c_str();
+}
+
 void DPPubSub::add_joined_peer(const char *service_name, vine_data_path_h datapath)
 {
        if (!service_name || !datapath)
index 209bbad..c3f23a7 100755 (executable)
@@ -66,11 +66,11 @@ static void __terminated_cb(vine_dp_h dp, void *user_data)
 {
 }
 
-static void __peer_joined_cb(vine_dp_h dp, const char *ip, int port, void *user_data)
+static void __peer_joined_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
 }
 
-static void __peer_left_cb(vine_dp_h dp, const char *ip, int port, void *user_data)
+static void __peer_left_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
 }
 
index 104028d..fcc2ab0 100644 (file)
@@ -196,12 +196,12 @@ static void open_dp(vine_session_h session)
                                PRINT_LOG("dp[%p] is closed.", dp);
                        }, NULL), "vine_dp_set_terminated_cb");
        PRINT_IF_ERROR(vine_dp_set_peer_joined_cb(dp,
-                               [](vine_dp_h dp, const char *ip, int port, void *user_data) {
-                               PRINT_LOG("dp[%p]'s peer[%s:%d] is joined.", dp, ip, port);
+                               [](vine_dp_h dp, const char *peer_id, void *user_data) {
+                               PRINT_LOG("dp[%p]'s peer[%s] is joined.", dp, peer_id);
                        }, NULL), "vine_dp_set_peer_joined_cb");
        PRINT_IF_ERROR(vine_dp_set_peer_left_cb(dp,
-                               [](vine_dp_h dp, const char *ip, int port, void *user_data) {
-                               PRINT_LOG("dp[%p]'s peer[%s:%d] is left.", dp, ip, port);
+                               [](vine_dp_h dp, const char *peer_id, void *user_data) {
+                               PRINT_LOG("dp[%p]'s peer[%s] is left.", dp, peer_id);
                        }, NULL), "vine_dp_set_peer_left_cb");
        PRINT_IF_ERROR(vine_dp_open(dp,
                        [](vine_dp_h dp, vine_error_e result, void *user_data) {
index 4bb1635..d1e06fb 100644 (file)
@@ -139,15 +139,15 @@ static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data)
                send_message(accepted_dp);
 }
 
-static void __peer_joined_cb(vine_dp_h dp, const char *ip, int port, void *user_data)
+static void __peer_joined_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
-       printf("pubsub peer(%s:%d) is joined.\n", ip, port);
+       printf("pubsub peer(%s) is joined.\n", peer_id);
        send_message(dp);
 }
 
-static void __peer_left_cb(vine_dp_h dp, const char *ip, int port, void *user_data)
+static void __peer_left_cb(vine_dp_h dp, const char *peer_id, void *user_data)
 {
-       printf("pubsub peer(%s:%d) is left.\n", ip, port);
+       printf("pubsub peer(%s) is left.\n", peer_id);
 }
 
 static void __registered_cb(vine_session_h session,