Skip to content

Commit

Permalink
v1.4.0 fix signal MBA paylod type handler
Browse files Browse the repository at this point in the history
  • Loading branch information
remocons committed Mar 27, 2024
1 parent f4f3e35 commit d1c1b14
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dist/iosignal.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/iosignal.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/iosignal.esm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/iosignal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/iosignal.min.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iosignal",
"version": "1.3.2",
"version": "1.4.0",
"description": "iosignal",
"type": "module",
"main": "./dist/iosignal.cjs",
Expand Down Expand Up @@ -37,7 +37,7 @@
"boho": "^0.8.2",
"commander": "^9.5.0",
"eventemitter3": "^5.0.1",
"meta-buffer-pack": "^1.3.1",
"meta-buffer-pack": "^1.4.0",
"ws": "^8.16.0"
},
"optionalDependencies": {
Expand All @@ -50,6 +50,11 @@
},
"homepage": "https://iosignal.net",
"keywords": [
"io","pubsub","arduino","iot","auth","signal"
"io",
"pubsub",
"arduino",
"iot",
"auth",
"signal"
]
}
2 changes: 1 addition & 1 deletion src/client/IOCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export class IOCore extends EventEmitter {
break;

case PAYLOAD_TYPE.MBA:
let mbaObject = MBP.unpack(buffer)
let mbaObject = MBP.unpack(payloadBuffer)
if (tag.indexOf('@') === 0) this.emit('@', ...mbaObject.args, tag)
if (tag !== '@') this.emit(tag, ...mbaObject.args, tag)
break;
Expand Down
8 changes: 7 additions & 1 deletion src/common/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ export function getSignalPack( tag, ...args ){
MBP.MB('#payloadType', '8', payload.type )
)
}else if( payload.type == PAYLOAD_TYPE.MBA ){
let mbaBuffer = MBP.pack( MBP.MBA(...args))
sigPack = MBP.pack(
MBP.MB('#MsgType','8', IOMsg.SIGNAL) ,
MBP.MB('#tagLen','8', tagEncoded.byteLength),
MBP.MB('#tag', tagEncoded),
MBP.MB('#payloadType', '8', payload.type ),
MBP.MBA(...args)
MBP.MB('#mbaBuffer', mbaBuffer )
)
}else {
sigPack = MBP.pack(
Expand Down Expand Up @@ -74,4 +75,9 @@ export function parsePayload( args){
}

return { type: type, buffer: pack }
}

export function getPayloadFromSignalPack( signalPack ){
let tagLen = signalPack.readUint8(1)
return signalPack.subarray(3 + tagLen)
}
7 changes: 7 additions & 0 deletions src/common/quotaTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export let quotaTable = {

// you can add your custom quota level.

// Remote service node
// High Quota, No root permission.
200: {
signalSize: 1048576 * 20,
publishCounter: 10000,
trafficRate: 1048576 * 100
},
// super admin or root user.
// to monitor, metric, sudo command, db acess
255: {
Expand Down
20 changes: 18 additions & 2 deletions src/server/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class Manager{
}



// unix wall like
serverSignal(obj) {
let sigPack = MBP.pack(
MBP.MB('#MsgType', '8', IOMsg.SERVER_SIGNAL),
Expand Down Expand Up @@ -188,6 +188,21 @@ export class Manager{
}
}

serverSignalTo( tag, ...args ){
let cid = tag.split('@')[0]
let topic = tag.split('@')[1]
let sigPack = getSignalPack( '@'+topic, ...args )
// console.log('tag split', cid )
if (cid && this.cid2remote.has(cid)) {
let targetRemote = this.cid2remote.get(cid)
console.log('target', targetRemote.state ,targetRemote.cid )
targetRemote.send_enc_mode(sigPack)
return
}else{
return "no cid"
}
}

sender(tag, remote, message) {
if(serverOption.memberOnly && !remote.boho.isAuthorized){
// console.log("## MemberOnly, reject unAuthorized remote." )
Expand All @@ -200,7 +215,7 @@ export class Manager{

let cidIndex = tag.indexOf('@');
if (cidIndex === 0) {
// ** CID_PUB is not uni-cast but multic-cast.
// ** CID_PUB multi-cast
// [cid_pub] @topic , @$retainTopic
// modify signalpack with cid_appneded tag.
tag = remote.cid + tag;
Expand All @@ -214,6 +229,7 @@ export class Manager{
if (this.cid2remote.has(targetCId)) {
//rm cid from tag.
let ommitCIdTag = '@' + tag.split('@')[1]
// console.log('uni-cast tag change from', tag, 'to' , ommitCIdTag )
message = this.getNewSignalTagMessage(message, ommitCIdTag)
// console.log(`origin tag: ${tag} omitTag: ${this.getSignalTag(message)}`)
this.cid2remote.get(targetCId).send_enc_mode(message)
Expand Down
10 changes: 5 additions & 5 deletions src/server/ServerRemoteCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ export class ServerRemoteCore {


showMessageLog(message, isBinary) {
let from = this.boho.isAuthorized ? ` id: ${this.did}(${this.cid})` : ""
let from = this.boho.isAuthorized ? `did: ${this.did} ${this.cid}@` : `${this.cid}@`
if (isBinary) {
let msgTypeName = IOMsg[message[0]]
if (!msgTypeName) msgTypeName = BohoMsg[message[0]]
msgTypeName = '#' + this.ssid + '(' + this.cid + ') [ ' + msgTypeName + ' ]';
msgTypeName =' [' + msgTypeName + ']';
if (message.byteLength > 40) {
console.log(msgTypeName + from + ' LEN:', message.length);
console.log(from + msgTypeName + ' LEN:', message.length);
} else {
console.log(msgTypeName + from, message);
console.log(from + msgTypeName , message);
}

} else {
console.log(from + '[TEXT MSG] %s', message);
console.log(from + ' [TEXT] %s', message);
}
}

Expand Down
34 changes: 34 additions & 0 deletions test-signal-payloadType/signalPack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { parsePayload, getSignalPack, getPayloadFromSignalPack, PAYLOAD_TYPE, MBP } from 'iosignal'

const type0 = []
const type1t = ['a1']
const type1n = [11]
const type2 = [new Uint8Array(5)]
const type3 = [{ name: 'type3' }]
const type4a = ['a2', 222]
const type4b = [{ name: 'a3' }, 333]
const type5 = [{ a: 1 }, Buffer.from('BBBBBB')]

// zero or one argument support: 0:EMPTY, 1:TEXT , 2:BINARY signal payload type
console.log(PAYLOAD_TYPE[parsePayload(type0).type], type0) // empty
console.log(PAYLOAD_TYPE[parsePayload(type1t).type], type1t) // text
console.log(PAYLOAD_TYPE[parsePayload(type1n).type], type1n) // number text. there is no Number type of signalPaylod.
console.log(PAYLOAD_TYPE[parsePayload(type2).type], type2) // binary.

// over two arguments support: 3:Object, 4:MJSON and 5:MBA can hold Number item.
console.log(PAYLOAD_TYPE[parsePayload(type3).type], type3) // Object
console.log(PAYLOAD_TYPE[parsePayload(type4a).type], type4a) // MJSON
console.log(PAYLOAD_TYPE[parsePayload(type4b).type], type4b) // MJSON
console.log(PAYLOAD_TYPE[parsePayload(type5).type], type5) // MBA


let signalTag = 'channel'
let signalPack = getSignalPack( signalTag, ...type5)
console.log('signalPack', signalPack)

let payloadBuffer = getPayloadFromSignalPack(signalPack)
let unpack = MBP.unpack(payloadBuffer)
console.log(unpack)



61 changes: 61 additions & 0 deletions test-signal-payloadType/signaling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { IO, Server} from 'iosignal'

let url = 'ws://localhost:5555'
let server = new Server({
port: 5555
// ,showMessage: 'message'
})
let done = false;

let tx = new IO(url)
tx.on('ready', () => {
console.log('tx ready', tx.cid)
})


let rx = new IO(url)
rx.on('ready', () => {
console.log('rx ready', rx.cid)
})

rx.listen('@', (...args) => {
console.log('uni-cast rxi', args)
})

rx.listen('channel', (...args) => {
console.log('multi-cast rxi', args)
})

function sendSignals() {
// uni-casting: tag is receiver CID@.
let tag = rx.cid + '@topic'
tx.signal(tag, ...arg1)
tx.signal(tag, ...arg2)
tx.signal(tag, ...arg3)
tx.signal(tag, ...arg4)
tx.signal(tag, ...arg5)

// multi-casting: tag is channel name.
tag = 'channel'
tx.signal(tag, ...arg1)
tx.signal(tag, ...arg2)
tx.signal(tag, ...arg3)
tx.signal(tag, ...arg4)
tx.signal(tag, ...arg5)
}


const arg1 = ['a1']
const arg2 = ['a2', 222]
const arg3 = [{ name: 'a3' }, 333]
const arg4 = [{a:1}, Buffer.from('KKKKK')]
const arg5 = [new Uint8Array(20)]


setTimeout(() => {
sendSignals();
}, 1000)

setTimeout(() => {
process.exit()
}, 2000)

0 comments on commit d1c1b14

Please sign in to comment.