update lib antunnel plugings

This commit is contained in:
Dany LE 2021-11-28 18:26:04 +01:00
parent 01c9d0a10a
commit dd3aae50c9
9 changed files with 55 additions and 6 deletions

View File

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

View File

@ -265,7 +265,7 @@ namespace Antunnel {
* @type {{[prop: number]: BroadcastGroup}} * @type {{[prop: number]: BroadcastGroup}}
* @memberof BroadcastManager * @memberof BroadcastManager
*/ */
private groups: { [prop: number]: BroadcastGroup }; groups: { [prop: number]: BroadcastGroup };
/** /**
* temporary list of group handles that wait for * temporary list of group handles that wait for
@ -604,5 +604,34 @@ namespace Antunnel {
arr.set(data, 4); arr.set(data, 4);
this.sub.send(Antunnel.Msg.DATA, arr); this.sub.send(Antunnel.Msg.DATA, arr);
} }
/**
* Get all the registered group
*
* @return {Uint8Array}
* @memberof BroadcastManager
*/
get_groups(): { [prop: number]: BroadcastGroup }
{
return this.groups;
}
/**
* Get group by name
*
* @return {Uint8Array}
* @memberof BroadcastManager
*/
get_group(name: string): BroadcastGroup
{
for(let k in this.groups)
{
if(this.groups[k].groupname === name)
{
return this.groups[k];
}
}
return undefined;
}
} }
} }

View File

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

View File

@ -446,7 +446,9 @@ declare namespace Antunnel {
* @type {{[prop: number]: BroadcastGroup}} * @type {{[prop: number]: BroadcastGroup}}
* @memberof BroadcastManager * @memberof BroadcastManager
*/ */
private groups; groups: {
[prop: number]: BroadcastGroup;
};
/** /**
* temporary list of group handles that wait for * temporary list of group handles that wait for
* an connection confirmation from the backend * an connection confirmation from the backend
@ -546,5 +548,21 @@ declare namespace Antunnel {
* @memberof BroadcastManager * @memberof BroadcastManager
*/ */
send(gid: number, data: Uint8Array): void; send(gid: number, data: Uint8Array): void;
/**
* Get all the registered group
*
* @return {Uint8Array}
* @memberof BroadcastManager
*/
get_groups(): {
[prop: number]: BroadcastGroup;
};
/**
* Get group by name
*
* @return {Uint8Array}
* @memberof BroadcastManager
*/
get_group(name: string): BroadcastGroup;
} }
} }

View File

@ -1 +1 @@
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={})); 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)}get_groups(){return this.groups}get_group(s){for(let e in this.groups)if(this.groups[e].groupname===s)return this.groups[e]}}}(Antunnel||(Antunnel={}));

View File

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

View File

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

View File

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