From a84d5a51d5be65498a92a87200a185b16819f523 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Thu, 4 Jul 2024 18:06:35 +0900 Subject: [PATCH] [Tizen] Introduce network service discovery This patch introduces network service discovery to Tizen service. NSD will be used to set ip and port automatically. Signed-off-by: Yelin Jeong --- .../ImageClassificationOffloading/js/utils.js | 16 +-- .../src/main.c | 131 +++++++++++++++--- .../tizen-manifest.xml | 3 + 3 files changed, 124 insertions(+), 26 deletions(-) diff --git a/Tizen.web/ImageClassificationOffloading/js/utils.js b/Tizen.web/ImageClassificationOffloading/js/utils.js index 5c410e8e..647a3091 100644 --- a/Tizen.web/ImageClassificationOffloading/js/utils.js +++ b/Tizen.web/ImageClassificationOffloading/js/utils.js @@ -8,6 +8,7 @@ */ let gServiceAppId = "EQmf4iSfpX.imageclassificationoffloadingservice"; +export let gRemoteServices = new Map(); /** * Get currently used network type @@ -135,17 +136,16 @@ export function startHybridService() { export function startMessagePort() { try { - const gLocalMessagePort = + const localMessagePort = tizen.messageport.requestLocalMessagePort("MESSAGE_PORT"); - gLocalMessagePort.addMessagePortListener(function (data) { + localMessagePort.addMessagePortListener(function (data) { + let remoteService = new Object(); + let serviceName = ""; for (var i = 0; i < data.length; i++) { - var key = data[i].key; - switch (key) { - case "command": - console.log("key:" + key + " / value:" + data[i].value); - break; - } + if (data[i].key == "name") serviceName = data[i].value; + else remoteService[data[i].key] = data[i].value; } + gRemoteServices.set(serviceName, remoteService); }); } catch (e) { console.log("Failed to create local message port " + e.name); diff --git a/Tizen.web/ImageClassificationOffloadingService/src/main.c b/Tizen.web/ImageClassificationOffloadingService/src/main.c index 05bf4278..83628e57 100644 --- a/Tizen.web/ImageClassificationOffloadingService/src/main.c +++ b/Tizen.web/ImageClassificationOffloadingService/src/main.c @@ -8,14 +8,17 @@ #include "main.h" #include +#include #include #include +#include +#include #include /** * @brief Service app create callback */ -static bool _create_cb(void* user_data){ +static bool _create_cb(void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "Callback create\n"); return true; } @@ -23,40 +26,60 @@ static bool _create_cb(void* user_data){ /** * @brief Service app terminate callback */ -static void _terminate_cb(void* user_data){ +static void _terminate_cb(void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "Callback terminate\n"); } /** * @brief Service app app control callback */ -static void _app_control_cb(app_control_h app_control, void* user_data){ +static void _app_control_cb(app_control_h app_control, void *user_data) { dlog_print(DLOG_INFO, LOG_TAG, "Callback app_control\n"); } /** - * @brief Main function. + * @brief Send dnssd remote service information */ -int main(int argc, char *argv[]) { - service_app_lifecycle_callback_s event_callback = { - .create = _create_cb, - .terminate = _terminate_cb, - .app_control = _app_control_cb - }; +void _dnssd_send_found(dnssd_service_h dnssd_remote_service) { + char *service_name = NULL; + char *service_type = NULL; + char *ip_v4_address = NULL; + char *ip_v6_address = NULL; + int ret, port = 0; - dlog_print(DLOG_ERROR, LOG_TAG, - "Image classification offloading service start"); + ret = dnssd_service_get_type(dnssd_remote_service, &service_type); + if (ret != DNSSD_ERROR_NONE || service_type == NULL) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get service type (%d)", ret); + return; + } - bool found; - int ret = message_port_check_remote_port(REMOTE_APP_ID, PORT_NAME, &found); + ret = dnssd_service_get_name(dnssd_remote_service, &service_name); + if (ret != DNSSD_ERROR_NONE || service_name == NULL) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get service name (%d)", ret); + return; + } - if (ret != MESSAGE_PORT_ERROR_NONE || !found) { - dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port"); + ret = dnssd_service_get_ip(dnssd_remote_service, &ip_v4_address, + &ip_v6_address); + if (ret != DNSSD_ERROR_NONE || ip_v4_address == NULL) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get IP address (%d)", ret); + return; } - /* Todo: Send network information to ui application */ + ret = dnssd_service_get_port(dnssd_remote_service, &port); + if (ret != DNSSD_ERROR_NONE) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to get port number (%d)", ret); + return; + } + + char port_buffer[10]; + snprintf(port_buffer, 10, "%d", port); + bundle *b = bundle_create(); - bundle_add_str(b, "command", "connected"); + bundle_add_str(b, "name", service_name); + bundle_add_str(b, "ip", ip_v4_address); + bundle_add_str(b, "port", port_buffer); + ret = message_port_send_message(REMOTE_APP_ID, PORT_NAME, b); if (ret != MESSAGE_PORT_ERROR_NONE) { dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send message to %s:%s", @@ -64,5 +87,77 @@ int main(int argc, char *argv[]) { } bundle_free(b); + if (service_name) + free(service_name); + if (service_type) + free(service_type); + if (ip_v4_address) + free(ip_v4_address); + if (ip_v6_address) + free(ip_v6_address); +} + +/** + * @brief dnssd found callback + */ +void _found_cb(dnssd_service_state_e state, + dnssd_service_h dnssd_remote_service, void *user_data) { + switch (state) { + case DNSSD_SERVICE_STATE_AVAILABLE: + /* DNS-SD service found */ + _dnssd_send_found(dnssd_remote_service); + break; + case DNSSD_SERVICE_STATE_UNAVAILABLE: + /* DNS-SD service becomes unavailable */ + /* ToDo : Handle unavailable remote service */ + break; + case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED: + /* Browsing failed */ + dlog_print(DLOG_ERROR, LOG_TAG, "Browse Failure\n"); + break; + case DNSSD_SERVICE_STATE_HOST_NAME_LOOKUP_FAILED: + /* Resolving service name failed */ + dlog_print(DLOG_ERROR, LOG_TAG, "Resolve Service Name Failure\n"); + break; + case DNSSD_SERVICE_STATE_ADDRESS_LOOKUP_FAILED: + /* Resolving service address failed */ + dlog_print(DLOG_ERROR, LOG_TAG, "Resolve Service Address\n"); + break; + default: + dlog_print(DLOG_ERROR, LOG_TAG, "Unknown Browse State\n"); + break; + } +} + +/** + * @brief Main function. + */ +int main(int argc, char *argv[]) { + dnssd_browser_h browser_handle; + char *target = "_nsd_offloading._tcp"; + int ret; + bool found; + + service_app_lifecycle_callback_s event_callback = {.create = _create_cb, + .terminate = _terminate_cb, + .app_control = + _app_control_cb}; + + dlog_print(DLOG_INFO, LOG_TAG, + "Image classification offloading service start"); + + ret = dnssd_initialize(); + if (ret != DNSSD_ERROR_NONE) + dlog_print(DLOG_ERROR, LOG_TAG, "Dnssd initialize failed"); + + ret = dnssd_start_browsing_service(target, &browser_handle, _found_cb, NULL); + if (ret == DNSSD_ERROR_NONE) + dlog_print(DLOG_DEBUG, LOG_TAG, "Start browsing"); + + ret = message_port_check_remote_port(REMOTE_APP_ID, PORT_NAME, &found); + if (ret != MESSAGE_PORT_ERROR_NONE || !found) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port"); + } + return service_app_main(argc, argv, &event_callback, NULL); } diff --git a/Tizen.web/ImageClassificationOffloadingService/tizen-manifest.xml b/Tizen.web/ImageClassificationOffloadingService/tizen-manifest.xml index a95c8a21..0400d280 100644 --- a/Tizen.web/ImageClassificationOffloadingService/tizen-manifest.xml +++ b/Tizen.web/ImageClassificationOffloadingService/tizen-manifest.xml @@ -5,4 +5,7 @@ imageclassificationoffloadingservice.png + + http://tizen.org/privilege/internet +