From 988e8f6420783199b7063d27d7b12f49ccedc1f4 Mon Sep 17 00:00:00 2001 From: AmyangXYZ Date: Wed, 27 Dec 2023 11:53:46 -0500 Subject: [PATCH] adding TSN nodes --- src/hooks/useStates.ts | 8 +++---- src/networks/TSCH/node.ts | 2 +- src/networks/TSN/network.ts | 26 +++++++++++++--------- src/networks/TSN/node.ts | 43 +++++++++++++++++++++++++++++++++--- src/networks/TSN/typedefs.ts | 20 +++++++++++++++++ src/networks/common.ts | 6 +---- 6 files changed, 81 insertions(+), 24 deletions(-) diff --git a/src/hooks/useStates.ts b/src/hooks/useStates.ts index e2969c8..63fd629 100644 --- a/src/hooks/useStates.ts +++ b/src/hooks/useStates.ts @@ -2,11 +2,11 @@ import { ref } from 'vue' -import { TSCHNetwork } from '@/networks/TSCH/network' -export const Network = new TSCHNetwork() +// import { TSCHNetwork } from '@/networks/TSCH/network' +// export const Network = new TSCHNetwork() -// import { TSNNetwork } from '@/networks/TSN/network' -// export const Network = new TSNNetwork() +import { TSNNetwork } from '@/networks/TSN/network' +export const Network = new TSNNetwork() // import { FiveGNetwork } from '@/networks/5G/network' // export const Network = new FiveGNetwork() diff --git a/src/networks/TSCH/node.ts b/src/networks/TSCH/node.ts index c90aaff..9f85407 100644 --- a/src/networks/TSCH/node.ts +++ b/src/networks/TSCH/node.ts @@ -51,7 +51,7 @@ class TSCHNode { run() { onmessage = (e: any) => { - if ('ch' in e.data == false) { + if ('uid' in e.data == false) { const msg: Message = e.data if (this.msgHandlers[msg.type] != undefined) { this.msgHandlers[msg.type](msg) diff --git a/src/networks/TSN/network.ts b/src/networks/TSN/network.ts index 97cbdb0..e8cda90 100644 --- a/src/networks/TSN/network.ts +++ b/src/networks/TSN/network.ts @@ -1,9 +1,12 @@ -import { ref } from 'vue' -import { Network, NetworkType, NODE_TYPE } from '../common' -import type { ScheduleConfig, TSNNodeMeta } from './typedefs' +import { ref, toRaw } from 'vue' +import { Network, NetworkType, NODE_TYPE, type Message } from '../common' +import { MSG_TYPES, type INIT_MSG_PAYLOAD, type ScheduleConfig, type TSNNodeMeta } from './typedefs' import { SeededRandom } from '@/hooks/useSeed' export class TSNNetwork extends Network { + InPorts: any + OutPorts: any + constructor() { super() this.Type = NetworkType.TSN @@ -54,14 +57,15 @@ export class TSNNetwork extends Network { w: new Worker(new URL('@/networks/TSN/node.ts', import.meta.url), { type: 'module' }) } // send init msg - // n.w!.postMessage({ - // type: MSG_TYPES.INIT, - // payload: { - // id: n.id, - // pos: toRaw(n.pos), - // sch_config: toRaw(this.SchConfig.value) - // } - // }) + n.w!.postMessage({ + type: MSG_TYPES.INIT, + payload: { + id: n.id, + pos: toRaw(n.pos), + neighbors: [], + sch_config: toRaw(this.SchConfig.value) + } + }) // handle msg/pkt from nodes // n.w!.onmessage = (e: any) => { // if ('ch' in e.data == false) { diff --git a/src/networks/TSN/node.ts b/src/networks/TSN/node.ts index b28caa2..8fc400a 100644 --- a/src/networks/TSN/node.ts +++ b/src/networks/TSN/node.ts @@ -1,11 +1,48 @@ +import type { INIT_MSG_PAYLOAD } from '../TSCH/typedefs' +import type { Packet, Message, MsgHandler, PktHandler } from '../common' + class TSNNode { id: number = 0 ports: { [p: number]: number } = {} + msgHandlers: { [type: number]: MsgHandler } = {} + pktHandlers: { [type: number]: PktHandler } = {} constructor() {} + registerMsgHandler(type: number, handler: MsgHandler) { + this.msgHandlers[type] = handler + } + registerPktHandler(type: number, handler: PktHandler) { + this.pktHandlers[type] = handler + } + + run() { + onmessage = (e: any) => { + if ('uid' in e.data == false) { + const msg: Message = e.data + if (this.msgHandlers[msg.type] != undefined) { + this.msgHandlers[msg.type](msg) + } else { + console.log('!! undefined message type:', msg.type) + } + } else { + const pkt: Packet = e.data - Run() { - // console.log(this.id) + if (this.pktHandlers[pkt.type] != undefined) { + this.pktHandlers[pkt.type](pkt) + } else { + console.log('!! undefined packet type:', pkt.type) + } + } + } + } + + initMsgHandler = (msg: Message) => { + const payload: INIT_MSG_PAYLOAD = msg.payload + this.id = payload.id + console.log(this.id) + } + dataPktHandler = (pkt: Packet) => { + console.log(pkt) } } -new TSNNode().Run() +new TSNNode().run() diff --git a/src/networks/TSN/typedefs.ts b/src/networks/TSN/typedefs.ts index 31683f7..6d28894 100644 --- a/src/networks/TSN/typedefs.ts +++ b/src/networks/TSN/typedefs.ts @@ -5,3 +5,23 @@ export interface ScheduleConfig { } export interface TSNNodeMeta extends NodeMeta {} + + +export enum MSG_TYPES { + INIT, + ASN, + DONE, // finished all activities of the current slot + STAT, +} + +export interface INIT_MSG_PAYLOAD { + id: number + pos: number[] + neighbors:[], + sch_config: ScheduleConfig +} + + +export enum PKT_TYPES { + DATA +} \ No newline at end of file diff --git a/src/networks/common.ts b/src/networks/common.ts index f8c46f1..411817a 100644 --- a/src/networks/common.ts +++ b/src/networks/common.ts @@ -24,7 +24,7 @@ export class Network { this.Type = -1 this.TopoConfig = ref({ seed: 1, - num_nodes: 10, + num_nodes: 5, num_es: 4, grid_size: 80, tx_range: 25 @@ -113,10 +113,6 @@ export interface Packet { len: number payload: any - // callback function when the packet is successfully tranmistted - // (received ack) - callback: () => void | undefined - // for display on packet sniffer id: number children: any