-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch uses nsd to find llama service provided by android device. Signed-off-by: Yelin Jeong <[email protected]>
- Loading branch information
Showing
2 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 ! " + | ||
|
@@ -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); | ||
|
||
|
@@ -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(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} |