Commit 88aa6f05 authored by NzSN's avatar NzSN

backup

parent db721063
let ident = undefined;
let encoder = null; let encoder = null;
let isInited = false; let isInited = false;
let isBridged = false; let isBridged = false;
...@@ -28,11 +29,10 @@ createParaEncoder().then(m => { ...@@ -28,11 +29,10 @@ createParaEncoder().then(m => {
// Init // Init
onmessage = async e => { onmessage = async e => {
await main(e.data); await eventloop(e.data);
} }
async function main(msg) { async function eventloop(msg) {
if (!isEncGrpMsg(msg)) return; if (!isEncGrpMsg(msg)) return;
switch (typeOfMsg(msg)) { switch (typeOfMsg(msg)) {
...@@ -48,7 +48,7 @@ async function main(msg) { ...@@ -48,7 +48,7 @@ async function main(msg) {
if (isBridged) await deBridging(); if (isBridged) await deBridging();
break; break;
case MESSAGE_TYPE.DATA: case MESSAGE_TYPE.DATA:
if (isInited && isBridged) { if (isBridged) {
await steps(); await steps();
} }
break; break;
...@@ -71,6 +71,7 @@ async function bridging(msg) { ...@@ -71,6 +71,7 @@ async function bridging(msg) {
} }
bridge = new Channel(info.size, SharedArrayBuffer, info.shm); bridge = new Channel(info.size, SharedArrayBuffer, info.shm);
postMessage(makeMsg(MESSAGE_TYPE.BRIDGE, {}));
isBridged = true; isBridged = true;
} }
...@@ -82,6 +83,7 @@ async function deBridging() { ...@@ -82,6 +83,7 @@ async function deBridging() {
async function init(msg) { async function init(msg) {
let info = getInfoFromMsg(msg), ret; let info = getInfoFromMsg(msg), ret;
src = new Channel(info.size, SharedArrayBuffer, info.shm); src = new Channel(info.size, SharedArrayBuffer, info.shm);
ident = info.ident;
// Wait encoder init // Wait encoder init
while (encoder == null) while (encoder == null)
...@@ -131,10 +133,13 @@ async function steps() { ...@@ -131,10 +133,13 @@ async function steps() {
if (await step() === false) { if (await step() === false) {
await sleep(SLEEP_INTERVAL); await sleep(SLEEP_INTERVAL);
timeout -= SLEEP_INTERVAL; timeout -= SLEEP_INTERVAL;
} else {
timeout = SPIN_TIMEOUT;
} }
} }
src.unsetPriv(PRIV_FLAGS.EXECUTING); src.unsetPriv(PRIV_FLAGS.EXECUTING);
postMessage(makeMsg(MESSAGE_TYPE.DATA_REQUIRE, {ident:ident}))
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
// Status // Status
let ident = undefined;
let isInited = false; let isInited = false;
let isBridged = false; let isBridged = false;
let bridge = null; let bridge = null;
...@@ -118,4 +119,6 @@ async function steps() { ...@@ -118,4 +119,6 @@ async function steps() {
} }
src.unsetPriv(MSG.PRIV_FLAGS.EXECUTING); src.unsetPriv(MSG.PRIV_FLAGS.EXECUTING);
postMessage(makeMsg(MESSAGE_TYPE.DATA_REQUIRE, {}))
} }
import { MESSAGE_TYPE, typeOfMsg } from './encGrooupMsg';
import { Observable, filter } from './rxjs'; import { Observable, filter } from './rxjs';
...@@ -9,6 +10,13 @@ export class WWInitError extends Error { ...@@ -9,6 +10,13 @@ export class WWInitError extends Error {
} }
export const WW_STATE = Object.freeze({
UNINITIALIZED : 0,
READY : 1,
RUNNING : 2,
});
export class WW extends Observable { export class WW extends Observable {
#ident = undefined; #ident = undefined;
...@@ -18,7 +26,19 @@ export class WW extends Observable { ...@@ -18,7 +26,19 @@ export class WW extends Observable {
#ww = undefined; #ww = undefined;
#connected = {}; #connected = {};
#remoteInited = false;
#remoteBridged = false;
#state = WW_STATE.UNINITIALIZED;
constructor(name, path) { constructor(name, path) {
if (typeof(name) != "string" ||
typeof(path) != "string") {
throw new TypeError("Mismatch arguments", "WW.js");
}
super(subscriber => { super(subscriber => {
this.#ww.onmessage = this.#ww.onmessage =
e => { subscriber.next(e.data); }; e => { subscriber.next(e.data); };
...@@ -26,6 +46,8 @@ export class WW extends Observable { ...@@ -26,6 +46,8 @@ export class WW extends Observable {
this.#ident = name; this.#ident = name;
this.#ww = new Worker(path); this.#ww = new Worker(path);
this.#monitor();
} }
async start(initWork) { async start(initWork) {
...@@ -85,9 +107,29 @@ export class WW extends Observable { ...@@ -85,9 +107,29 @@ export class WW extends Observable {
return this.#ww.terminate(); return this.#ww.terminate();
} }
monitor() { getState() {
return this.#state;
}
// Enable status tracking for WW,
// status of WW will map to local variables.
#monitor() {
this.subscribe(msg => { this.subscribe(msg => {
switch (typeOfMsg(msg)) {
case MESSAGE_TYPE.INIT:
this.#remoteInited = true;
break;
case MESSAGE_TYPE.BRIDGE:
this.#remoteBridged = true;
break;
}
// Update State
if (this.#remoteInited && this.#remoteBridged) {
this.#state = WW_STATE.READY;
}
}); });
} }
} }
...@@ -42,8 +42,8 @@ export function makeMsg(type, info_) { ...@@ -42,8 +42,8 @@ export function makeMsg(type, info_) {
return { type: type, info: info_ }; return { type: type, info: info_ };
} }
export function makeInitMsg(shm, size) { export function makeInitMsg(shm, size, ident) {
return makeMsg(MESSAGE_TYPE.INIT, {shm: shm, size:size}) return makeMsg(MESSAGE_TYPE.INIT, {shm: shm, size:size, ident: ident})
} }
export function typeOfMsg(msg) { export function typeOfMsg(msg) {
......
...@@ -182,7 +182,7 @@ async function muxInit(chnls, ww) { ...@@ -182,7 +182,7 @@ async function muxInit(chnls, ww) {
let channel = new Channel(Math.pow(2, 20)); let channel = new Channel(Math.pow(2, 20));
ww.postMessage( ww.postMessage(
makeInitMsg(channel.getShMem(), Math.pow(2, 20))); makeInitMsg(channel.getShMem(), Math.pow(2, 20), ww.ident()));
chnls[ww.ident()] = channel; chnls[ww.ident()] = channel;
return true; return true;
......
import { WW } from '../src/WW.js'; import { WW, WW_STATE } from '../src/WW.js';
import { Channel } from '../src/channel.js'; import { Channel } from '../src/channel.js';
import { makeMsg, MESSAGE_TYPE } from '../src/encGrooupMsg.js'; import { makeMsg, MESSAGE_TYPE } from '../src/encGrooupMsg.js';
import { sleep, assert } from '../src/utils.js'; import { sleep, assert } from '../src/utils.js';
...@@ -162,9 +162,32 @@ describe("EncWW Specifications", () => { ...@@ -162,9 +162,32 @@ describe("EncWW Specifications", () => {
}, 1000); }, 1000);
}); });
expect(ch2.dataSize()).toBe(26597); expect(ch2.dataSize()).toBe(26597);
expect(ch2_.dataSize()).toBe(26597); expect(ch2_.dataSize()).toBe(26597);
}, 100000); }, 100000);
})
it("Enc WW Spec", async () => {
let enc = new WW("ENC", "../resources/workers/encWW.js");
let mem = new SharedArrayBuffer(1024);
enc.postMessage(makeMsg(MESSAGE_TYPE.INIT, {shm: mem, size: 1024, ident: "ENC" }));
let memB = new SharedArrayBuffer(1024);
enc.postMessage(makeMsg(MESSAGE_TYPE.BRIDGE, { shm: memB, size: 1024 }));
await sleep(500);
expect(enc.getState() == WW_STATE.READY).toBe(true);
await sleep(1000);
});
fit("Mux WW Spec", async () => {
let mux = new WW("MUX", "../resources/workers/muxWW.js");
let mem = new SharedArrayBuffer(1024);
mux.postMessage(makeMsg(MESSAGE_TYPE.INIT, {shm: mem, size: 1024 }));
await sleep(1000);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment