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 committed Jul 4, 2024
1 parent 15d50af commit 30ff258
Show file tree
Hide file tree
Showing 4 changed files with 42 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
18 changes: 18 additions & 0 deletions Tizen.web/ImageClassificationOffloading/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
#endif
#define LOG_TAG "imageclassificationoffloadingservice"

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

#endif /* __IMAGECLASSIFICATIONOFFLOADINGSERVICE_H__ */
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "imageclassificationoffloadingservice.h"
#include <bundle.h>
#include <message_port.h>
#include <service_app.h>
#include <tizen.h>

Expand All @@ -10,5 +12,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, ad);
}

0 comments on commit 30ff258

Please sign in to comment.