update lib antunnel plugings

This commit is contained in:
Dany LE 2021-11-28 18:07:12 +01:00
parent 031f78c926
commit 01c9d0a10a
10 changed files with 151 additions and 39 deletions

View File

@ -4,4 +4,5 @@ This package provides also the Typescript declaration file for
application Development.
## Change logs
- v.0.1.1: Added group query support
- v.0.1.0: Antunnel API declaration and broadcast plugin

View File

@ -12,7 +12,8 @@ namespace Antunnel {
export enum BroadcastCTRLType {
SUBSCRIBE = 0x0A,
UNSUBSCRIBE = 0xB,
QUERY = 0xC
QUERY_USER = 0xC,
QUERY_GROUP = 0xD,
}
/**
@ -275,6 +276,26 @@ namespace Antunnel {
* @memberof BroadcastManager
*/
private pendings: GenericObject<BroadcastGroup>;
/**
* callback handle when a group is added to the
* manager
*
* @private
* @type (group: BroadcastGroup)=> void
* @memberof BroadcastManager
*/
ongroupadd: (group: BroadcastGroup)=> void;
/**
* callback handle when a group is removed from the
* manager
*
* @private
* @type (group: BroadcastGroup)=> void
* @memberof BroadcastManager
*/
ongroupdel: (group: BroadcastGroup)=> void;
/**
* Creates an instance of BroadcastManager.
@ -287,6 +308,8 @@ namespace Antunnel {
this.tunnel = undefined;
this.groups = {};
this.pendings = {};
this.ongroupadd = undefined;
this.ongroupdel = undefined;
}
/**
@ -344,19 +367,39 @@ namespace Antunnel {
let g_handle = this.pendings[msg.group];
if (g_handle && g_handle.user === msg.user) {
g_handle.id = msg.id;
g_handle.onready();
this.pendings[msg.group] = undefined;
delete this.pendings[msg.group];
this.groups[msg.id] = g_handle;
g_handle.state = BroadcastGroupState.SUBSCRIBED;
if(this.ongroupadd)
{
this.ongroupadd(g_handle);
}
g_handle.onready();
}
g_handle = this.groups[msg.id];
if (!g_handle) {
return;
// create the group handle
g_handle = new BroadcastGroup(msg.group);
g_handle.id = msg.id;
g_handle.state = BroadcastGroupState.SUBSCRIBED;
g_handle.mgr = this;
this.groups[msg.id] = g_handle;
if(this.ongroupadd)
{
this.ongroupadd(g_handle);
}
if(g_handle.onready)
{
g_handle.onready();
}
}
if(!g_handle.users.has(msg.user))
{
g_handle.users.add(msg.user);
if (g_handle.onuseradd)
g_handle.onuseradd(msg.user);
}
g_handle.users.add(msg.user);
if (g_handle.onuseradd)
g_handle.onuseradd(msg.user);
}
else {
let g_handle = this.groups[msg.id];
@ -370,6 +413,10 @@ namespace Antunnel {
g_handle.state = BroadcastGroupState.UNSUBSCRIBED;
if (g_handle.onclose)
g_handle.onclose();
if(this.ongroupdel)
{
this.ongroupdel(g_handle);
}
}
else {
g_handle.users.delete(msg.user);
@ -378,7 +425,7 @@ namespace Antunnel {
}
}
break;
case BroadcastCTRLType.QUERY:
case BroadcastCTRLType.QUERY_USER:
msg.id = Antunnel.Msg.int_from(d.data, 1, 4);
msg.user = new TextDecoder("utf-8").decode(d.data.slice(5));
let g_handle = this.groups[msg.id];
@ -390,6 +437,24 @@ namespace Antunnel {
g_handle.onuseradd(msg.user);
}
break;
case BroadcastCTRLType.QUERY_GROUP:
msg.id = Antunnel.Msg.int_from(d.data, 1, 4);
msg.group = new TextDecoder("utf-8").decode(d.data.slice(5));
if (this.groups[msg.id])
return;
this.groups[msg.id] = new BroadcastGroup(msg.group);
this.groups[msg.id].id = msg.id;
this.groups[msg.id].state = BroadcastGroupState.SUBSCRIBED;
this.groups[msg.id].mgr = this;
if(this.ongroupadd)
{
this.ongroupadd(this.groups[msg.id]);
}
if(this.groups[msg.id].onready)
{
this.groups[msg.id].onready();
}
break;
default:
break;
}
@ -407,11 +472,10 @@ namespace Antunnel {
* - Connect to the tunnel if the global tunnel does not exists
* - Subscribe to th e broadcast channel if not done
*
* @private
* @return {*} {Promise<any>}
* @memberof BroadcastManager
*/
private setup(): Promise<any> {
setup(): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
if (!Antunnel) {
@ -444,7 +508,6 @@ namespace Antunnel {
});
}
/**
* Remove a group handle from the manager
*
@ -467,31 +530,36 @@ namespace Antunnel {
*/
query(gid: number): void {
let arr = new Uint8Array(5);
arr[0] = BroadcastCTRLType.QUERY;
arr[0] = BroadcastCTRLType.QUERY_USER;
arr.set(Antunnel.Msg.bytes_of(gid, 4), 1);
this.sub.send(Antunnel.Msg.CTRL, arr);
}
/**
* Query all groups of the current user
*
* @memberof BroadcastManager
*/
refresh(): void
{
let arr = new Uint8Array(1);
arr[0] = BroadcastCTRLType.QUERY_GROUP;
this.sub.send(Antunnel.Msg.CTRL, arr);
}
/**
* Register a group to the manager
*
* @param {BroadcastGroup} group
* @param {string} group
* @memberof BroadcastManager
*/
subscribe(group: BroadcastGroup): void {
this.setup()
.then((_) => {
let arr = new Uint8Array(group.groupname.length + 1);
arr[0] = BroadcastCTRLType.SUBSCRIBE;
arr.set(new TextEncoder().encode(group.groupname), 1);
this.sub.send(Antunnel.Msg.CTRL, arr);
group.mgr = this;
this.pendings[group.groupname] = group;
})
.catch((e) => {
OS.announcer.oserror(__("Unable to subscribe to group {0}: {1}", group.groupname, e.toString()), e);
})
subscribe(group: string): void {
let arr = new Uint8Array(group.length + 1);
arr[0] = BroadcastCTRLType.SUBSCRIBE;
arr.set(new TextEncoder().encode(group), 1);
this.sub.send(Antunnel.Msg.CTRL, arr);
let handle = new BroadcastGroup(group);
handle.mgr = this;
this.pendings[group] = handle;
}
@ -508,6 +576,20 @@ namespace Antunnel {
this.pendings = {};
}
/**
* return the current subscriber ID
*
* @memberof BroadcastManager
* @return {number}
*/
id(): number
{
if(this.sub)
{
return this.sub.id;
}
return 0;
}
/**
* Send a message to a specific group

View File

@ -4,4 +4,5 @@ This package provides also the Typescript declaration file for
application Development.
## Change logs
- v.0.1.1: Added group query support
- v.0.1.0: Antunnel API declaration and broadcast plugin

View File

@ -249,7 +249,8 @@ declare namespace Antunnel {
enum BroadcastCTRLType {
SUBSCRIBE = 10,
UNSUBSCRIBE = 11,
QUERY = 12
QUERY_USER = 12,
QUERY_GROUP = 13
}
/**
* Group state
@ -455,6 +456,24 @@ declare namespace Antunnel {
* @memberof BroadcastManager
*/
private pendings;
/**
* callback handle when a group is added to the
* manager
*
* @private
* @type (group: BroadcastGroup)=> void
* @memberof BroadcastManager
*/
ongroupadd: (group: BroadcastGroup) => void;
/**
* callback handle when a group is removed from the
* manager
*
* @private
* @type (group: BroadcastGroup)=> void
* @memberof BroadcastManager
*/
ongroupdel: (group: BroadcastGroup) => void;
/**
* Creates an instance of BroadcastManager.
* @param {string} channel
@ -475,11 +494,10 @@ declare namespace Antunnel {
* - Connect to the tunnel if the global tunnel does not exists
* - Subscribe to th e broadcast channel if not done
*
* @private
* @return {*} {Promise<any>}
* @memberof BroadcastManager
*/
private setup;
setup(): Promise<any>;
/**
* Remove a group handle from the manager
*
@ -495,18 +513,31 @@ declare namespace Antunnel {
*/
query(gid: number): void;
/**
* Register a group to the manager
* Query all groups of the current user
*
* @param {BroadcastGroup} group
* @memberof BroadcastManager
*/
subscribe(group: BroadcastGroup): void;
refresh(): void;
/**
* Register a group to the manager
*
* @param {string} group
* @memberof BroadcastManager
*/
subscribe(group: string): void;
/**
*CLeanup the manager
*
* @memberof BroadcastManager
*/
teardown(): void;
/**
* return the current subscriber ID
*
* @memberof BroadcastManager
* @return {number}
*/
id(): number;
/**
* Send a message to a specific group
*

View File

@ -1 +1 @@
var Antunnel;!function(e){let s;!function(e){e[e.OK=0]="OK",e[e.SUBSCRIBE=2]="SUBSCRIBE",e[e.UNSUBSCRIBE=3]="UNSUBSCRIBE",e[e.ERROR=1]="ERROR",e[e.DATA=6]="DATA",e[e.CTRL=7]="CTRL",e[e.CLOSE=5]="CLOSE",e[e.PING=8]="PING"}(s=e.AntunnelMSGType||(e.AntunnelMSGType={}))}(Antunnel||(Antunnel={})),function(e){let s,t;!function(e){e[e.SUBSCRIBE=10]="SUBSCRIBE",e[e.UNSUBSCRIBE=11]="UNSUBSCRIBE",e[e.QUERY=12]="QUERY"}(s=e.BroadcastCTRLType||(e.BroadcastCTRLType={})),function(e){e[e.INIT=0]="INIT",e[e.SUBSCRIBED=1]="SUBSCRIBED",e[e.UNSUBSCRIBED=2]="UNSUBSCRIBED"}(t=e.BroadcastGroupState||(e.BroadcastGroupState={})),e.BroadcastGroup=class{constructor(e){this.groupname=e,this.users=new Set,this.onmessage=void 0,this.onready=void 0,this.onuseradd=void 0,this.onuserdel=void 0,this.onclose=void 0,this.user=OS.setting.user.name,this.mgr=void 0,this.state=t.INIT}close(){this.mgr&&this.id&&this.mgr.unsubscribe(this.id)}refresh(){this.mgr&&this.id&&this.mgr.query(this.id)}send(e){this.mgr.send(this.id,e)}},e.BroadcastManager=class{constructor(e){this.sub=void 0,this.channel=e,this.tunnel=void 0,this.groups={},this.pendings={}}connect(n){this.sub=new e.Subscriber(this.channel),this.sub.onopen=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connected to the {1} channel",this.sub.id,this.channel)),n(!0)},this.sub.onerror=e=>{let s=e;e.data&&(s=new TextDecoder("utf-8").decode(e.data)),OS.announcer.oserror(__("Subscriber {0}: Error from the {1} channel: {2}",this.sub.id,this.channel,s),void 0)},this.sub.onmessage=s=>{if(s.data){let t=e.Msg.int_from(s.data.slice(0,4),0,4),n=this.groups[t];if(!n)return;n.onmessage&&n.onmessage(s.data.slice(4))}},this.sub.onctrl=n=>{let i={user:void 0,group:void 0,type:n.data[0],id:void 0};switch(i.type){case s.SUBSCRIBE:case s.UNSUBSCRIBE:let r=n.data[1]+2;if(i.user=new TextDecoder("utf-8").decode(n.data.slice(2,r)),i.id=e.Msg.int_from(n.data,r,4),r+=4,i.group=new TextDecoder("utf-8").decode(n.data.slice(r)),i.type===s.SUBSCRIBE){let e=this.pendings[i.group];if(e&&e.user===i.user&&(e.id=i.id,e.onready(),this.pendings[i.group]=void 0,delete this.pendings[i.group],this.groups[i.id]=e,e.state=t.SUBSCRIBED),e=this.groups[i.id],!e)return;e.users.add(i.user),e.onuseradd&&e.onuseradd(i.user)}else{let e=this.groups[i.id];if(!e)return;e.user===i.user?(OS.announcer.osinfo(__("Subcriber {0}: leave group {1}",this.sub.id,i.group)),this.groups[i.id]=void 0,delete this.groups[i.id],e.state=t.UNSUBSCRIBED,e.onclose&&e.onclose()):(e.users.delete(i.user),e.onuserdel&&e.onuserdel(i.user))}break;case s.QUERY:i.id=e.Msg.int_from(n.data,1,4),i.user=new TextDecoder("utf-8").decode(n.data.slice(5));let o=this.groups[i.id];if(!o)return;o.users.has(i.user)||(o.users.add(i.user),o.onuseradd&&o.onuseradd(i.user))}},this.sub.onclose=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connection to {1} closed",this.sub.id,this.channel)),this.sub=void 0},this.tunnel.subscribe(this.sub)}setup(){return new Promise(async(s,t)=>{try{if(!e)throw new Error(__("Library not fould: %s","Antunnel").__());if(e.tunnel)this.tunnel=e.tunnel;else{await OS.GUI.pushService("Antunnel/AntunnelService");let s=OS.setting.system.tunnel_uri;if(!s)throw new Error(__("Unable to connect to: %s","Antunnel").__());await e.init(s),this.tunnel=e.tunnel}this.sub?s(!0):this.connect(s)}catch(s){e.tunnel&&e.tunnel.close(),t(__e(s))}})}unsubscribe(t){let n=new Uint8Array(5);n[0]=s.UNSUBSCRIBE,n.set(e.Msg.bytes_of(t,4),1),this.sub.send(e.Msg.CTRL,n)}query(t){let n=new Uint8Array(5);n[0]=s.QUERY,n.set(e.Msg.bytes_of(t,4),1),this.sub.send(e.Msg.CTRL,n)}subscribe(t){this.setup().then(n=>{let i=new Uint8Array(t.groupname.length+1);i[0]=s.SUBSCRIBE,i.set((new TextEncoder).encode(t.groupname),1),this.sub.send(e.Msg.CTRL,i),t.mgr=this,this.pendings[t.groupname]=t}).catch(e=>{OS.announcer.oserror(__("Unable to subscribe to group {0}: {1}",t.groupname,e.toString()),e)})}teardown(){this.sub&&this.sub.close(),this.groups={},this.pendings={}}send(s,t){let n=new Uint8Array(t.length+4);n.set(e.Msg.bytes_of(s,4),0),n.set(t,4),this.sub.send(e.Msg.DATA,n)}}}(Antunnel||(Antunnel={}));
var Antunnel;!function(s){let e;!function(s){s[s.OK=0]="OK",s[s.SUBSCRIBE=2]="SUBSCRIBE",s[s.UNSUBSCRIBE=3]="UNSUBSCRIBE",s[s.ERROR=1]="ERROR",s[s.DATA=6]="DATA",s[s.CTRL=7]="CTRL",s[s.CLOSE=5]="CLOSE",s[s.PING=8]="PING"}(e=s.AntunnelMSGType||(s.AntunnelMSGType={}))}(Antunnel||(Antunnel={})),function(s){let e,t;!function(s){s[s.SUBSCRIBE=10]="SUBSCRIBE",s[s.UNSUBSCRIBE=11]="UNSUBSCRIBE",s[s.QUERY_USER=12]="QUERY_USER",s[s.QUERY_GROUP=13]="QUERY_GROUP"}(e=s.BroadcastCTRLType||(s.BroadcastCTRLType={})),function(s){s[s.INIT=0]="INIT",s[s.SUBSCRIBED=1]="SUBSCRIBED",s[s.UNSUBSCRIBED=2]="UNSUBSCRIBED"}(t=s.BroadcastGroupState||(s.BroadcastGroupState={}));class n{constructor(s){this.groupname=s,this.users=new Set,this.onmessage=void 0,this.onready=void 0,this.onuseradd=void 0,this.onuserdel=void 0,this.onclose=void 0,this.user=OS.setting.user.name,this.mgr=void 0,this.state=t.INIT}close(){this.mgr&&this.id&&this.mgr.unsubscribe(this.id)}refresh(){this.mgr&&this.id&&this.mgr.query(this.id)}send(s){this.mgr.send(this.id,s)}}s.BroadcastGroup=n,s.BroadcastManager=class{constructor(s){this.sub=void 0,this.channel=s,this.tunnel=void 0,this.groups={},this.pendings={},this.ongroupadd=void 0,this.ongroupdel=void 0}connect(i){this.sub=new s.Subscriber(this.channel),this.sub.onopen=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connected to the {1} channel",this.sub.id,this.channel)),i(!0)},this.sub.onerror=s=>{let e=s;s.data&&(e=new TextDecoder("utf-8").decode(s.data)),OS.announcer.oserror(__("Subscriber {0}: Error from the {1} channel: {2}",this.sub.id,this.channel,e),void 0)},this.sub.onmessage=e=>{if(e.data){let t=s.Msg.int_from(e.data.slice(0,4),0,4),n=this.groups[t];if(!n)return;n.onmessage&&n.onmessage(e.data.slice(4))}},this.sub.onctrl=i=>{let r={user:void 0,group:void 0,type:i.data[0],id:void 0};switch(r.type){case e.SUBSCRIBE:case e.UNSUBSCRIBE:let o=i.data[1]+2;if(r.user=new TextDecoder("utf-8").decode(i.data.slice(2,o)),r.id=s.Msg.int_from(i.data,o,4),o+=4,r.group=new TextDecoder("utf-8").decode(i.data.slice(o)),r.type===e.SUBSCRIBE){let s=this.pendings[r.group];s&&s.user===r.user&&(s.id=r.id,this.pendings[r.group]=void 0,delete this.pendings[r.group],this.groups[r.id]=s,s.state=t.SUBSCRIBED,this.ongroupadd&&this.ongroupadd(s),s.onready()),s=this.groups[r.id],s||(s=new n(r.group),s.id=r.id,s.state=t.SUBSCRIBED,s.mgr=this,this.groups[r.id]=s,this.ongroupadd&&this.ongroupadd(s),s.onready&&s.onready()),s.users.has(r.user)||(s.users.add(r.user),s.onuseradd&&s.onuseradd(r.user))}else{let s=this.groups[r.id];if(!s)return;s.user===r.user?(OS.announcer.osinfo(__("Subcriber {0}: leave group {1}",this.sub.id,r.group)),this.groups[r.id]=void 0,delete this.groups[r.id],s.state=t.UNSUBSCRIBED,s.onclose&&s.onclose(),this.ongroupdel&&this.ongroupdel(s)):(s.users.delete(r.user),s.onuserdel&&s.onuserdel(r.user))}break;case e.QUERY_USER:r.id=s.Msg.int_from(i.data,1,4),r.user=new TextDecoder("utf-8").decode(i.data.slice(5));let u=this.groups[r.id];if(!u)return;u.users.has(r.user)||(u.users.add(r.user),u.onuseradd&&u.onuseradd(r.user));break;case e.QUERY_GROUP:if(r.id=s.Msg.int_from(i.data,1,4),r.group=new TextDecoder("utf-8").decode(i.data.slice(5)),this.groups[r.id])return;this.groups[r.id]=new n(r.group),this.groups[r.id].id=r.id,this.groups[r.id].state=t.SUBSCRIBED,this.groups[r.id].mgr=this,this.ongroupadd&&this.ongroupadd(this.groups[r.id]),this.groups[r.id].onready&&this.groups[r.id].onready()}},this.sub.onclose=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connection to {1} closed",this.sub.id,this.channel)),this.sub=void 0},this.tunnel.subscribe(this.sub)}setup(){return new Promise(async(e,t)=>{try{if(!s)throw new Error(__("Library not fould: %s","Antunnel").__());if(s.tunnel)this.tunnel=s.tunnel;else{await OS.GUI.pushService("Antunnel/AntunnelService");let e=OS.setting.system.tunnel_uri;if(!e)throw new Error(__("Unable to connect to: %s","Antunnel").__());await s.init(e),this.tunnel=s.tunnel}this.sub?e(!0):this.connect(e)}catch(e){s.tunnel&&s.tunnel.close(),t(__e(e))}})}unsubscribe(t){let n=new Uint8Array(5);n[0]=e.UNSUBSCRIBE,n.set(s.Msg.bytes_of(t,4),1),this.sub.send(s.Msg.CTRL,n)}query(t){let n=new Uint8Array(5);n[0]=e.QUERY_USER,n.set(s.Msg.bytes_of(t,4),1),this.sub.send(s.Msg.CTRL,n)}refresh(){let t=new Uint8Array(1);t[0]=e.QUERY_GROUP,this.sub.send(s.Msg.CTRL,t)}subscribe(t){let i=new Uint8Array(t.length+1);i[0]=e.SUBSCRIBE,i.set((new TextEncoder).encode(t),1),this.sub.send(s.Msg.CTRL,i);let r=new n(t);r.mgr=this,this.pendings[t]=r}teardown(){this.sub&&this.sub.close(),this.groups={},this.pendings={}}id(){return this.sub?this.sub.id:0}send(e,t){let n=new Uint8Array(t.length+4);n.set(s.Msg.bytes_of(e,4),0),n.set(t,4),this.sub.send(s.Msg.DATA,n)}}}(Antunnel||(Antunnel={}));

View File

@ -6,7 +6,7 @@
"author": "Dany LE",
"email": "mrsang@iohub.dev"
},
"version":"0.1.0-a",
"version":"0.1.1-a",
"category":"Library",
"iconclass":"fa fa-cog",
"mimes":["none"],

View File

@ -6,7 +6,7 @@
"author": "Dany LE",
"email": "mrsang@iohub.dev"
},
"version":"0.1.0-a",
"version":"0.1.1-a",
"category":"Library",
"iconclass":"fa fa-cog",
"mimes":["none"],

View File

@ -1,3 +0,0 @@
<afx-app-window apptitle="AntunnelPlugins" width="500" height="400" data-id="AntunnelPlugins">
<afx-hbox ></afx-hbox>
</afx-app-window>

View File

@ -65,7 +65,7 @@
"description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/AntunnelPlugins/README.md",
"category": "Library",
"author": "Dany LE",
"version": "0.1.0-a",
"version": "0.1.1-a",
"dependencies": ["Antunnel@0.2.0-b"],
"download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/AntunnelPlugins/build/release/AntunnelPlugins.zip"
},