Skip to content

Commit 12b82ce

Browse files
niley7464myungjoo
authored andcommitted
[Tizen/web] Use nsd to find service
This patch uses nsd to find llama service provided by android device. Signed-off-by: Yelin Jeong <[email protected]>
1 parent a673394 commit 12b82ce

File tree

2 files changed

+133
-4
lines changed

2 files changed

+133
-4
lines changed

Tizen.web/TextGenerationLlama2/WebApplication/js/main.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@
77
* @author Yelin Jeong <[email protected]>
88
*/
99

10-
import { getNetworkType, getIpAddress } from "./utils.js";
11-
10+
import {
11+
getNetworkType,
12+
getIpAddress,
13+
startHybridService,
14+
startMessagePort,
15+
gRemoteServices,
16+
} from "./utils.js";
17+
18+
const serviceName = "llama2c";
1219
let serviceHandle = null;
1320
let tensorsData = null;
1421
let tensorsInfo = null;
@@ -39,9 +46,26 @@ function sinkListenerCallback(sinkName, data) {
3946
* Run a pipeline that uses other device's resources
4047
*/
4148
function runClient() {
42-
/* ToDo : Use NsdService to find port and ip */
49+
const remoteService = gRemoteServices.get(serviceName);
50+
if (
51+
!gRemoteServices.has(serviceName) ||
52+
!Object.prototype.hasOwnProperty.call(remoteService, "ip") ||
53+
!Object.prototype.hasOwnProperty.call(remoteService, "port")
54+
) {
55+
console.log("No remote service available");
56+
return;
57+
}
4358

44-
const filter = "tensor_query_client";
59+
const filter =
60+
"tensor_query_client host=" +
61+
ip +
62+
" port=" +
63+
remoteService["port"] +
64+
" dest-host=" +
65+
remoteService["ip"] +
66+
" dest-port=" +
67+
remoteService["port"] +
68+
" timeout=100000";
4569

4670
const pipelineDescription =
4771
"appsrc name=srcx ! application/octet-stream ! tensor_converter ! other/tensors,format=flexible,num_tensors=1,types=uint8,framerate=0/1 ! " +
@@ -57,6 +81,11 @@ function runClient() {
5781
* Run a pipeline that uses other device's resources
5882
*/
5983
function inference(input) {
84+
if (!gRemoteServices.has(serviceName)) {
85+
console.log("Offloading service is disappeared");
86+
return;
87+
}
88+
6089
const encoder = new TextEncoder();
6190
const inputBuffer = encoder.encode(input);
6291

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

103+
startMessagePort();
104+
startHybridService();
105+
74106
document.getElementById("start").addEventListener("click", function () {
75107
runClient();
76108
});

Tizen.web/TextGenerationLlama2/WebApplication/js/utils.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* @author Yelin Jeong <[email protected]>
88
*/
99

10+
let gServiceAppId = "lfebC6UrMY.nsdservice";
11+
export let gRemoteServices = new Map();
12+
1013
/**
1114
* Get currently used network type
1215
* @returns the network type
@@ -34,3 +37,97 @@ export async function getIpAddress(networkType) {
3437
);
3538
});
3639
}
40+
41+
function launchServiceApp() {
42+
function onSuccess() {
43+
console.log("Service App launched successfully");
44+
}
45+
46+
function onError(err) {
47+
console.error("Service App launch failed", err);
48+
}
49+
50+
try {
51+
console.log("Launching [" + gServiceAppId + "] ...");
52+
tizen.application.launch(gServiceAppId, onSuccess, onError);
53+
} catch (exc) {
54+
console.error("Exception while launching HybridServiceApp: " + exc.message);
55+
}
56+
}
57+
58+
function onGetAppsContextSuccess(contexts) {
59+
let i = 0;
60+
let appInfo = null;
61+
62+
for (i = 0; i < contexts.length; i = i + 1) {
63+
try {
64+
appInfo = tizen.application.getAppInfo(contexts[i].appId);
65+
} catch (exc) {
66+
console.error("Exception while getting application info " + exc.message);
67+
}
68+
69+
if (appInfo.id === gServiceAppId) {
70+
break;
71+
}
72+
}
73+
74+
if (i >= contexts.length) {
75+
console.log("Service App not found, Trying to launch service app");
76+
launchServiceApp();
77+
}
78+
}
79+
80+
function onGetAppsContextError(err) {
81+
console.error("getAppsContext exc", err);
82+
}
83+
84+
/**
85+
* Starts obtaining information about applications
86+
* that are currently running on a device.
87+
*/
88+
export function startHybridService() {
89+
try {
90+
tizen.application.getAppsContext(
91+
onGetAppsContextSuccess,
92+
onGetAppsContextError,
93+
);
94+
} catch (e) {
95+
console.log("Get AppContext failed, " + e);
96+
}
97+
}
98+
99+
export function startMessagePort() {
100+
try {
101+
const addService =
102+
tizen.messageport.requestLocalMessagePort("STATE_AVAILABLE");
103+
addService.addMessagePortListener(function (data) {
104+
let remoteService = new Object();
105+
let serviceName = "";
106+
for (var i = 0; i < data.length; i++) {
107+
if (data[i].key == "name") {
108+
var name = data[i].value.split(" ")[0];
109+
serviceName = name;
110+
} else remoteService[data[i].key] = data[i].value;
111+
}
112+
if (gRemoteServices.has(serviceName)) {
113+
gRemoteServices.delete(serviceName);
114+
}
115+
gRemoteServices.set(serviceName, remoteService);
116+
});
117+
118+
const removeService =
119+
tizen.messageport.requestLocalMessagePort("STATE_UNAVAILABLE");
120+
removeService.addMessagePortListener(function (data) {
121+
let serviceName = "";
122+
for (var i = 0; i < data.length; i++) {
123+
if (data[i].key == "name") {
124+
var name = data[i].value.split("(")[0];
125+
serviceName = name;
126+
}
127+
}
128+
gRemoteServices.delete(serviceName);
129+
});
130+
} catch (e) {
131+
console.log("Failed to create local message port " + e.name);
132+
}
133+
}

0 commit comments

Comments
 (0)