Skip to content

Commit

Permalink
[Tizen] Introduce network service discovery
Browse files Browse the repository at this point in the history
This patch introduces network service discovery to Tizen service.
NSD will be used to set ip and port automatically.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jul 12, 2024
1 parent 7cbcf65 commit a84d5a5
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 26 deletions.
16 changes: 8 additions & 8 deletions Tizen.web/ImageClassificationOffloading/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

let gServiceAppId = "EQmf4iSfpX.imageclassificationoffloadingservice";
export let gRemoteServices = new Map();

/**
* Get currently used network type
Expand Down Expand Up @@ -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);
Expand Down
131 changes: 113 additions & 18 deletions Tizen.web/ImageClassificationOffloadingService/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,156 @@

#include "main.h"
#include <bundle.h>
#include <dns-sd.h>
#include <message_port.h>
#include <service_app.h>
#include <stdio.h>
#include <stdlib.h>
#include <tizen.h>

/**
* @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;
}

/**
* @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",
REMOTE_APP_ID, PORT_NAME);
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<icon>imageclassificationoffloadingservice.png</icon>
<label>imageclassificationoffloadingservice</label>
</service-application>
<privileges>
<privilege>http://tizen.org/privilege/internet</privilege>
</privileges>
</manifest>

0 comments on commit a84d5a5

Please sign in to comment.