Skip to content

Commit

Permalink
adding TSN nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyangXYZ committed Dec 27, 2023
1 parent 921055a commit 988e8f6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/hooks/useStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/networks/TSCH/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 15 additions & 11 deletions src/networks/TSN/network.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(<Message>{
// type: MSG_TYPES.INIT,
// payload: <INIT_MSG_PAYLOAD>{
// id: n.id,
// pos: toRaw(n.pos),
// sch_config: toRaw(this.SchConfig.value)
// }
// })
n.w!.postMessage(<Message>{
type: MSG_TYPES.INIT,
payload: <INIT_MSG_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) {
Expand Down
43 changes: 40 additions & 3 deletions src/networks/TSN/node.ts
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions src/networks/TSN/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 1 addition & 5 deletions src/networks/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Network {
this.Type = -1
this.TopoConfig = ref<TopologyConfig>({
seed: 1,
num_nodes: 10,
num_nodes: 5,
num_es: 4,
grid_size: 80,
tx_range: 25
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 988e8f6

Please sign in to comment.