Skip to content

Commit

Permalink
[Tizen/web] Use nsd to find service
Browse files Browse the repository at this point in the history
This patch uses nsd to find llama service provided by android device.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Oct 21, 2024
1 parent 8ace4d9 commit 605f158
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
40 changes: 36 additions & 4 deletions Tizen.web/TextGenerationLlama2/WebApplication/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
* @author Yelin Jeong <[email protected]>
*/

import { getNetworkType, getIpAddress } from "./utils.js";

import {
getNetworkType,
getIpAddress,
startHybridService,
startMessagePort,
gRemoteServices,
} from "./utils.js";

const serviceName = "llama2c";
let serviceHandle = null;
let tensorsData = null;
let tensorsInfo = null;
Expand Down Expand Up @@ -39,9 +46,26 @@ function sinkListenerCallback(sinkName, data) {
* Run a pipeline that uses other device's resources
*/
function runClient() {
/* ToDo : Use NsdService to find port and ip */
const remoteService = gRemoteServices.get(serviceName);
if (
!gRemoteServices.has(serviceName) ||
!Object.prototype.hasOwnProperty.call(remoteService, "ip") ||
!Object.prototype.hasOwnProperty.call(remoteService, "port")
) {
console.log("No remote service available");
return;
}

const filter = "tensor_query_client";
const filter =
"tensor_query_client host=" +
ip +
" port=" +
remoteService["port"] +
" dest-host=" +
remoteService["ip"] +
" dest-port=" +
remoteService["port"] +
" timeout=100000";

const pipelineDescription =
"appsrc name=srcx ! application/octet-stream ! tensor_converter ! other/tensors,format=flexible,num_tensors=1,types=uint8,framerate=0/1 ! " +
Expand All @@ -57,6 +81,11 @@ function runClient() {
* Run a pipeline that uses other device's resources
*/
function inference(input) {
if (!gRemoteServices.has(serviceName)) {
console.log("Offloading service is disappeared");
return;
}

const encoder = new TextEncoder();
const inputBuffer = encoder.encode(input);

Expand All @@ -71,6 +100,9 @@ window.onload = async function () {
const networkType = await getNetworkType();
ip = await getIpAddress(networkType);

startMessagePort();
startHybridService();

document.getElementById("start").addEventListener("click", function () {
runClient();
});
Expand Down
97 changes: 97 additions & 0 deletions Tizen.web/TextGenerationLlama2/WebApplication/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* @author Yelin Jeong <[email protected]>
*/

let gServiceAppId = "lfebC6UrMY.nsdservice";
export let gRemoteServices = new Map();

/**
* Get currently used network type
* @returns the network type
Expand Down Expand Up @@ -34,3 +37,97 @@ export async function getIpAddress(networkType) {
);
});
}

function launchServiceApp() {
function onSuccess() {
console.log("Service App launched successfully");
}

function onError(err) {
console.error("Service App launch failed", err);
}

try {
console.log("Launching [" + gServiceAppId + "] ...");
tizen.application.launch(gServiceAppId, onSuccess, onError);
} catch (exc) {
console.error("Exception while launching HybridServiceApp: " + exc.message);
}
}

function onGetAppsContextSuccess(contexts) {
let i = 0;
let appInfo = null;

for (i = 0; i < contexts.length; i = i + 1) {
try {
appInfo = tizen.application.getAppInfo(contexts[i].appId);
} catch (exc) {
console.error("Exception while getting application info " + exc.message);
}

if (appInfo.id === gServiceAppId) {
break;
}
}

if (i >= contexts.length) {
console.log("Service App not found, Trying to launch service app");
launchServiceApp();
}
}

function onGetAppsContextError(err) {
console.error("getAppsContext exc", err);
}

/**
* Starts obtaining information about applications
* that are currently running on a device.
*/
export function startHybridService() {
try {
tizen.application.getAppsContext(
onGetAppsContextSuccess,
onGetAppsContextError,
);
} catch (e) {
console.log("Get AppContext failed, " + e);
}
}

export function startMessagePort() {
try {
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++) {
if (data[i].key == "name") {
var name = data[i].value.split(" ")[0];
serviceName = name;
} else remoteService[data[i].key] = data[i].value;
}
if (gRemoteServices.has(serviceName)) {
gRemoteServices.delete(serviceName);
}
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") {
var name = data[i].value.split("(")[0];
serviceName = name;
}
}
gRemoteServices.delete(serviceName);
});
} catch (e) {
console.log("Failed to create local message port " + e.name);
}
}

0 comments on commit 605f158

Please sign in to comment.