Handle a duplicated service name (#232) 07/256807/1
author문선아/Tizen Platform Lab(SR)/Engineer/삼성전자 <seonah1.moon@samsung.com>
Tue, 13 Apr 2021 09:28:15 +0000 (18:28 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 13 Apr 2021 09:31:44 +0000 (18:31 +0900)
* Handle a duplicated service name

* Consider null character appended automatically

Change-Id: If6cf46c6fe19179f7d98e09234911294fe0f0918

include/vine.h
plugins/dns-sd/dns-sd-plugin.cpp
src/include/vine-disc-plugin.h
src/include/vine-dp.h
src/vine-disc.cpp
src/vine-dp.cpp
tool/tool_run.cpp

index aaf2e6b..9ee28af 100755 (executable)
@@ -109,6 +109,10 @@ typedef enum {
         */
        VINE_ERROR_NO_READ_DATA = VINE_ERROR|0x06,
        /**
+        * Service deregistered by a name conflict or similar problem
+        */
+       VINE_ERROR_SERVICE_DEREGISTERED = VINE_ERROR|0x07,
+       /**
         * Rejected by peer
         */
        //VINE_ERROR_REJECTED_BY_PEER = VINE_ERROR|0x05,
@@ -374,6 +378,9 @@ int vine_session_destroy(vine_session_h session);
 
 /**
  * @brief Called when service is registered.
+ * @remarks If @a error is VINE_ERROR_SERVICE_DEREGISTERED, It means that
+ *          @a service_name is conflicted. The callback will be invoked again
+ *          with renamed @a service_name.
  * @since_tizen 6.5
  * @param[in] session The session handle
  * @param[in] service_name The registered service name
index b6f744d..94ed386 100755 (executable)
@@ -248,10 +248,13 @@ static void __register_reply(DNSServiceRef sdRef, DNSServiceFlags flags,
                DNSServiceErrorType errorCode, const char *name,
                const char *regtype, const char *domain, void *context)
 {
-       VINE_LOGD("Service Registerd. service name[%s] error[%d]", name, errorCode);
+       VINE_LOGD("Service Registerd. service name[%s] error[%d] flags[%d]", name, errorCode, flags);
        vine_dns_sd_s *dns_sd_handle = (vine_dns_sd_s *)context;
        vine_disc_error error = __convert_dns_sd_error_to_vine_disc_error(errorCode);
 
+       if (flags == 0)
+               error = VINE_DISC_ERROR_SERVICE_DEREGISTERED;
+
        if (event_callbacks.published_cb)
                event_callbacks.published_cb(dns_sd_handle,
                        name, error, dns_sd_handle->user_data);
index 5b41bfc..3dcbd20 100755 (executable)
@@ -32,6 +32,7 @@ typedef enum {
        VINE_DISC_ERROR_OUT_OF_MEMORY,
        VINE_DISC_ERROR_NAME_CONFLICT,
        VINE_DISC_ERROR_SERVICE_NOT_RUNNING,
+       VINE_DISC_ERROR_SERVICE_DEREGISTERED,
 } vine_disc_error;
 
 typedef enum {
index cec5ef8..f47b354 100644 (file)
@@ -152,6 +152,7 @@ enum {
        VINE_DP_PUBSUB_SD_STATE_SUBSCRIBE = 1 << 2,
 };
 
+#define VINE_DP_PUBSUB_SERVICE_NAME_PREFIX "vine-pubsub"
 #define VINE_DP_PUBSUB_RANK_KEY "rank"
 #define VINE_DP_PUBSUB_RANK_MAX 53123
 #define VINE_DP_PUBSUB_RANK_LEN 6
index 31d3f4a..330bcaf 100755 (executable)
@@ -91,6 +91,8 @@ vine_error_e __convert_disc_error_to_vine_error(vine_disc_error error)
        case VINE_DISC_ERROR_NAME_CONFLICT: // DNS-SD changed the service name
                return VINE_ERROR_NAME_CONFLICT;
                // TODO: To be determined if which vine error will be returned
+       case VINE_DISC_ERROR_SERVICE_DEREGISTERED:
+               return VINE_ERROR_SERVICE_DEREGISTERED;
        case VINE_DISC_ERROR_SERVICE_NOT_RUNNING:
        default:
                return VINE_ERROR_OPERATION_FAILED;
index 17007e8..7fcf898 100644 (file)
@@ -721,6 +721,7 @@ int DPPubSub::publish_service()
        vine_service_h service;
        int ret;
        char rank_str[VINE_DP_PUBSUB_RANK_LEN] = {0 , };
+       char service_name[VINE_MAX_SERVICE_NAME_LEN + 1] = {0 , };
 
        ret = vine_service_create(&service);
        if (ret != VINE_ERROR_NONE)
@@ -731,6 +732,10 @@ 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);
+
+       vine_service_set_name(service, service_name);
        vine_service_add_attribute(service, VINE_DP_PUBSUB_RANK_KEY, (const char *)rank_str);
 
        if (mSdPub == NULL) {
index c039d6f..1cecbb5 100644 (file)
@@ -114,8 +114,13 @@ static void __accepted_cb(vine_dp_h dp, vine_dp_h accepted_dp, void *user_data)
 static void __registered_cb(vine_session_h session,
                                const char *service_name, vine_error_e error, void *user_data)
 {
-       printf("Service[%s] %s.\n", service_name,
-                       error == VINE_ERROR_NONE ? "registered" : "registration failure");
+       printf("Service[%s] ", service_name);
+       if (error == VINE_ERROR_NONE)
+               printf("registered. \n");
+       else if (error == VINE_ERROR_SERVICE_DEREGISTERED)
+               printf("deregistered by a name conflict. Service name will be renamed.\n");
+       else
+               printf("registration failure.\n");
 }
 
 static bool __print_attr(const char *key, const char *value, void *user_data)