AFB.js: fix bug (minor)
[src/app-framework-binder.git] / test / monitoring / AFB.js
1 /*
2  * Copyright (C) 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 AFB = function(base, initialtoken){
18
19 if (typeof base != "object")
20         base = { base: base, token: initialtoken };
21
22 var initial = {
23         base: base.base || "api",
24         token: base.token || initialtoken || "hello",
25         host: base.host || window.location.host,
26         url: base.url || undefined
27 };
28
29 var urlws = initial.url || "ws://"+initial.host+"/"+initial.base;
30
31 /*********************************************/
32 /****                                     ****/
33 /****             AFB_context             ****/
34 /****                                     ****/
35 /*********************************************/
36 var AFB_context;
37 {
38         var UUID = undefined;
39         var TOKEN = initial.token;
40
41         var context = function(token, uuid) {
42                 this.token = token;
43                 this.uuid = uuid;
44         }
45
46         context.prototype = {
47                 get token() {return TOKEN;},
48                 set token(tok) {if(tok) TOKEN=tok;},
49                 get uuid() {return UUID;},
50                 set uuid(id) {if(id) UUID=id;}
51         };
52
53         AFB_context = new context();
54 }
55 /*********************************************/
56 /****                                     ****/
57 /****             AFB_websocket           ****/
58 /****                                     ****/
59 /*********************************************/
60 var AFB_websocket;
61 {
62         var CALL = 2;
63         var RETOK = 3;
64         var RETERR = 4;
65         var EVENT = 5;
66
67         var PROTO1 = "x-afb-ws-json1";
68
69         AFB_websocket = function(on_open, on_abort) {
70                 var u = urlws;
71                 if (AFB_context.token) {
72                         u = u + '?x-afb-token=' + AFB_context.token;
73                         if (AFB_context.uuid)
74                                 u = u + '&x-afb-uuid=' + AFB_context.uuid;
75                 }
76                 this.ws = new WebSocket(u, [ PROTO1 ]);
77                 this.url = u;
78                 this.pendings = {};
79                 this.awaitens = {};
80                 this.counter = 0;
81                 this.ws.onopen = onopen.bind(this);
82                 this.ws.onerror = onerror.bind(this);
83                 this.ws.onclose = onclose.bind(this);
84                 this.ws.onmessage = onmessage.bind(this);
85                 this.onopen = on_open;
86                 this.onabort = on_abort;
87         }
88
89         function onerror(event) {
90                 var f = this.onabort;
91                 if (f) {
92                         delete this.onopen;
93                         delete this.onabort;
94                         f && f(this);
95                 }
96                 this.onerror && this.onerror(this);
97         }
98
99         function onopen(event) {
100                 var f = this.onopen;
101                 delete this.onopen;
102                 delete this.onabort;
103                 f && f(this);
104         }
105
106         function onclose(event) {
107                 for (var id in this.pendings) {
108                         var ferr = this.pendings[id].onerror;
109                         ferr && ferr(null, this);
110                 }
111                 this.pendings = {};
112                 this.onclose && this.onclose();
113         }
114
115         function fire(awaitens, name, data) {
116                 var a = awaitens[name];
117                 if (a)
118                         a.forEach(function(handler){handler(data);});
119                 var i = name.indexOf("/");
120                 if (i >= 0) {
121                         a = awaitens[name.substring(0,i)];
122                         if (a)
123                                 a.forEach(function(handler){handler(data);});
124                 }
125                 a = awaitens["*"];
126                 if (a)
127                         a.forEach(function(handler){handler(data);});
128         }
129
130         function reply(pendings, id, ans, offset) {
131                 if (id in pendings) {
132                         var p = pendings[id];
133                         delete pendings[id];
134                         var f = p[offset];
135                         f(ans);
136                 }
137         }
138
139         function onmessage(event) {
140                 var obj = JSON.parse(event.data);
141                 var code = obj[0];
142                 var id = obj[1];
143                 var ans = obj[2];
144                 AFB_context.token = obj[3];
145                 switch (code) {
146                 case RETOK:
147                         reply(this.pendings, id, ans, 0);
148                         break; 
149                 case RETERR:
150                         reply(this.pendings, id, ans, 1);
151                         break; 
152                 case EVENT:
153                 default:
154                         fire(this.awaitens, id, ans);
155                         break; 
156                 }
157         }
158
159         function close() {
160                 this.ws.close();
161                 this.ws.onopen = 
162                 this.ws.onerror = 
163                 this.ws.onclose = 
164                 this.ws.onmessage = 
165                 this.onopen = 
166                 this.onabort = function(){};
167         }
168
169         function call(method, request) {
170                 return new Promise((function(resolve, reject){
171                         var id, arr;
172                         do {
173                                 id = String(this.counter = 4095 & (this.counter + 1));
174                         } while (id in this.pendings);
175                         this.pendings[id] = [ resolve, reject ];
176                         arr = [CALL, id, method, request ];
177                         if (AFB_context.token) arr.push(AFB_context.token);
178                         this.ws.send(JSON.stringify(arr));
179                 }).bind(this));
180         }
181
182         function onevent(name, handler) {
183                 var id = name;
184                 var list = this.awaitens[id] || (this.awaitens[id] = []);
185                 list.push(handler);
186         }
187
188         AFB_websocket.prototype = {
189                 close: close,
190                 call: call,
191                 onevent: onevent
192         };
193 }
194 /*********************************************/
195 /****                                     ****/
196 /****                                     ****/
197 /****                                     ****/
198 /*********************************************/
199 return {
200         context: AFB_context,
201         ws: AFB_websocket
202 };
203 };
204