Skip to content

Commit

Permalink
[Tizen] Use message port to communicate
Browse files Browse the repository at this point in the history
This patch introduces message port in hybrid application.
Using message port, Tizen native and web application can communicate.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 authored and anyj0527 committed Jul 9, 2024
1 parent 98e95d9 commit 7cbcf65
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Tizen.web/ImageClassificationOffloading/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GetImgPath,
loadLabelInfo,
startHybridService,
startMessagePort,
} from "./utils.js";

let fHandle = null;
Expand Down Expand Up @@ -161,6 +162,7 @@ window.onload = async function () {
const networkType = await getNetworkType();
ip = await getIpAddress(networkType);
labels = loadLabelInfo();
startMessagePort();
startHybridService();
document.getElementById("start_local").addEventListener("click", function () {
runLocal();
Expand Down
19 changes: 19 additions & 0 deletions Tizen.web/ImageClassificationOffloading/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,22 @@ export function startHybridService() {
console.log("Get AppContext failed, " + e);
}
}

export function startMessagePort() {
try {
const gLocalMessagePort =
tizen.messageport.requestLocalMessagePort("MESSAGE_PORT");
gLocalMessagePort.addMessagePortListener(function (data) {
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;
}
}
});
} catch (e) {
console.log("Failed to create local message port " + e.name);
}
}
2 changes: 2 additions & 0 deletions Tizen.web/ImageClassificationOffloadingService/inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
#endif
#define LOG_TAG "image_classification_offloading_service"

#define REMOTE_APP_ID "EQmf4iSfpX.ImageClassificationOffloading"
#define PORT_NAME "MESSAGE_PORT"

#endif /* __IMAGECLASSIFICATIONOFFLOADINGSERVICE_H__ */
20 changes: 20 additions & 0 deletions Tizen.web/ImageClassificationOffloadingService/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

#include "main.h"
#include <bundle.h>
#include <message_port.h>
#include <service_app.h>
#include <tizen.h>

Expand Down Expand Up @@ -44,5 +46,23 @@ int main(int argc, char *argv[]) {

dlog_print(DLOG_ERROR, LOG_TAG,
"Image classification offloading service start");

bool found;
int 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");
}

/* Todo: Send network information to ui application */
bundle *b = bundle_create();
bundle_add_str(b, "command", "connected");
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",
REMOTE_APP_ID, PORT_NAME);
}
bundle_free(b);

return service_app_main(argc, argv, &event_callback, NULL);
}

0 comments on commit 7cbcf65

Please sign in to comment.