diff --git a/Tizen.web/ImageClassificationOffloading/js/main.js b/Tizen.web/ImageClassificationOffloading/js/main.js index 6c7c8b9f..c46b13f4 100644 --- a/Tizen.web/ImageClassificationOffloading/js/main.js +++ b/Tizen.web/ImageClassificationOffloading/js/main.js @@ -18,6 +18,7 @@ import { gRemoteServices, } from "./utils.js"; +const serviceName = "MobileNet"; let fHandle = null; let tensorsData = null; let tensorsInfo = null; @@ -100,9 +101,9 @@ function runLocal() { * Run a pipeline that uses other device's resources */ function runOffloading() { - const remoteService = gRemoteServices.get("MobileNet"); + const remoteService = gRemoteServices.get(serviceName); if ( - !gRemoteServices.has("MobileNet") || + !gRemoteServices.has(serviceName) || !Object.prototype.hasOwnProperty.call(remoteService, "ip") || !Object.prototype.hasOwnProperty.call(remoteService, "port") ) { @@ -137,6 +138,11 @@ let startTime; * Run a pipeline that uses other device's resources */ function inference(isLocal) { + if (!isLocal && !gRemoteServices.has(serviceName)) { + console.log("Offloading service is disappeared"); + return; + } + const img_path = GetImgPath(); let img = new Image(); img.src = img_path; diff --git a/Tizen.web/ImageClassificationOffloading/js/utils.js b/Tizen.web/ImageClassificationOffloading/js/utils.js index 647a3091..dc659815 100644 --- a/Tizen.web/ImageClassificationOffloading/js/utils.js +++ b/Tizen.web/ImageClassificationOffloading/js/utils.js @@ -136,9 +136,9 @@ export function startHybridService() { export function startMessagePort() { try { - const localMessagePort = - tizen.messageport.requestLocalMessagePort("MESSAGE_PORT"); - localMessagePort.addMessagePortListener(function (data) { + const addService = + tizen.messageport.requestLocalMessagePort("STATE_AVAILABLE"); + addService.addMessagePortListener(function (data) { let remoteService = new Object(); let serviceName = ""; for (var i = 0; i < data.length; i++) { @@ -147,6 +147,16 @@ export function startMessagePort() { } gRemoteServices.set(serviceName, remoteService); }); + + const removeService = + tizen.messageport.requestLocalMessagePort("STATE_UNAVAILABLE"); + removeService.addMessagePortListener(function (data) { + let serviceName = ""; + for (var i = 0; i < data.length; i++) { + if (data[i].key == "name") serviceName = data[i].value; + } + gRemoteServices.delete(serviceName); + }); } catch (e) { console.log("Failed to create local message port " + e.name); } diff --git a/Tizen.web/ImageClassificationOffloadingService/inc/main.h b/Tizen.web/ImageClassificationOffloadingService/inc/main.h index dd40a410..a562cfff 100644 --- a/Tizen.web/ImageClassificationOffloadingService/inc/main.h +++ b/Tizen.web/ImageClassificationOffloadingService/inc/main.h @@ -17,6 +17,5 @@ #define LOG_TAG "image_classification_offloading_service" #define REMOTE_APP_ID "EQmf4iSfpX.ImageClassificationOffloading" -#define PORT_NAME "MESSAGE_PORT" #endif /* __IMAGECLASSIFICATIONOFFLOADINGSERVICE_H__ */ diff --git a/Tizen.web/ImageClassificationOffloadingService/src/main.c b/Tizen.web/ImageClassificationOffloadingService/src/main.c index 90d9490e..06e2a581 100644 --- a/Tizen.web/ImageClassificationOffloadingService/src/main.c +++ b/Tizen.web/ImageClassificationOffloadingService/src/main.c @@ -41,7 +41,7 @@ static void _app_control_cb(app_control_h app_control, void *user_data) { /** * @brief Send dnssd remote service information */ -void _dnssd_send_found(dnssd_service_h dnssd_remote_service) { +void _dnssd_send_found(dnssd_service_h dnssd_remote_service, bool is_available) { char *service_name = NULL; char *service_type = NULL; char *ip_v4_address = NULL; @@ -81,10 +81,14 @@ void _dnssd_send_found(dnssd_service_h dnssd_remote_service) { 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 (is_available) + ret = message_port_send_message(REMOTE_APP_ID, "STATE_AVAILABLE", b); + else + ret = message_port_send_message(REMOTE_APP_ID, "STATE_UNAVAILABLE", b); + if (ret != MESSAGE_PORT_ERROR_NONE) { - dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send message to %s:%s", - REMOTE_APP_ID, PORT_NAME); + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send message to %s", + REMOTE_APP_ID); } bundle_free(b); @@ -106,11 +110,11 @@ void _found_cb(dnssd_service_state_e state, switch (state) { case DNSSD_SERVICE_STATE_AVAILABLE: /* DNS-SD service found */ - _dnssd_send_found(dnssd_remote_service); + _dnssd_send_found(dnssd_remote_service, true); break; case DNSSD_SERVICE_STATE_UNAVAILABLE: /* DNS-SD service becomes unavailable */ - /* ToDo : Handle unavailable remote service */ + _dnssd_send_found(dnssd_remote_service, false); break; case DNSSD_SERVICE_STATE_NAME_LOOKUP_FAILED: /* Browsing failed */ @@ -155,7 +159,12 @@ int main(int argc, char *argv[]) { 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); + ret = message_port_check_remote_port(REMOTE_APP_ID, "STATE_AVAILABLE", &found); + if (ret != MESSAGE_PORT_ERROR_NONE || !found) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port"); + } + + ret = message_port_check_remote_port(REMOTE_APP_ID, "STATE_UNAVAILABLE", &found); if (ret != MESSAGE_PORT_ERROR_NONE || !found) { dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port"); }