From: 문선아/Tizen Platform Lab(SR)/Engineer/삼성전자 Date: Tue, 13 Apr 2021 09:28:15 +0000 (+0900) Subject: Handle a duplicated service name (#232) X-Git-Tag: submit/tizen/20210414.043047~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=565edf4d48ccae8519342a4deb5eb43222210d5f;p=platform%2Fcore%2Fapi%2Fvine.git Handle a duplicated service name (#232) * Handle a duplicated service name * Consider null character appended automatically Change-Id: If6cf46c6fe19179f7d98e09234911294fe0f0918 --- diff --git a/include/vine.h b/include/vine.h index aaf2e6b..9ee28af 100755 --- a/include/vine.h +++ b/include/vine.h @@ -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 diff --git a/plugins/dns-sd/dns-sd-plugin.cpp b/plugins/dns-sd/dns-sd-plugin.cpp index b6f744d..94ed386 100755 --- a/plugins/dns-sd/dns-sd-plugin.cpp +++ b/plugins/dns-sd/dns-sd-plugin.cpp @@ -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); diff --git a/src/include/vine-disc-plugin.h b/src/include/vine-disc-plugin.h index 5b41bfc..3dcbd20 100755 --- a/src/include/vine-disc-plugin.h +++ b/src/include/vine-disc-plugin.h @@ -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 { diff --git a/src/include/vine-dp.h b/src/include/vine-dp.h index cec5ef8..f47b354 100644 --- a/src/include/vine-dp.h +++ b/src/include/vine-dp.h @@ -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 diff --git a/src/vine-disc.cpp b/src/vine-disc.cpp index 31d3f4a..330bcaf 100755 --- a/src/vine-disc.cpp +++ b/src/vine-disc.cpp @@ -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; diff --git a/src/vine-dp.cpp b/src/vine-dp.cpp index 17007e8..7fcf898 100644 --- a/src/vine-dp.cpp +++ b/src/vine-dp.cpp @@ -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) { diff --git a/tool/tool_run.cpp b/tool/tool_run.cpp index c039d6f..1cecbb5 100644 --- a/tool/tool_run.cpp +++ b/tool/tool_run.cpp @@ -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)