Skip to content

Commit

Permalink
[Tizen] Detect when offloading services disappear
Browse files Browse the repository at this point in the history
This patch adds logic to detect when offloading services disappear.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Aug 6, 2024
1 parent 3230dfc commit 46342f7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
10 changes: 8 additions & 2 deletions Tizen.web/ImageClassificationOffloading/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
gRemoteServices,
} from "./utils.js";

const serviceName = "MobileNet";
let fHandle = null;
let tensorsData = null;
let tensorsInfo = null;
Expand Down Expand Up @@ -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")
) {
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions Tizen.web/ImageClassificationOffloading/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion Tizen.web/ImageClassificationOffloadingService/inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
23 changes: 16 additions & 7 deletions Tizen.web/ImageClassificationOffloadingService/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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 */
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 46342f7

Please sign in to comment.