8fccdb04e8fd863f27f704a6fe324e6ff615a56b
[src/app-framework-binder.git] / test / monitoring / AFB.js
1 /*
2  * Copyright (C) 2017, 2018 "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                         try { this.pendings[id][1](); } catch (x) {/*TODO?*/}
109                 }
110                 this.pendings = {};
111                 this.onclose && this.onclose();
112         }
113
114         function fire(awaitens, name, data) {
115                 var a = awaitens[name];
116                 if (a)
117                         a.forEach(function(handler){handler(data);});
118                 var i = name.indexOf("/");
119                 if (i >= 0) {
120                         a = awaitens[name.substring(0,i)];
121                         if (a)
122                                 a.forEach(function(handler){handler(data);});
123                 }
124                 a = awaitens["*"];
125                 if (a)
126                         a.forEach(function(handler){handler(data);});
127         }
128
129         function reply(pendings, id, ans, offset) {
130                 if (id in pendings) {
131                         var p = pendings[id];
132                         delete pendings[id];
133                         try { p[offset](ans); } catch (x) {/*TODO?*/}
134                 }
135         }
136
137         function onmessage(event) {
138                 var obj = JSON.parse(event.data);
139                 var code = obj[0];
140                 var id = obj[1];
141                 var ans = obj[2];
142                 AFB_context.token = obj[3];
143                 switch (code) {
144                 case RETOK:
145                         reply(this.pendings, id, ans, 0);
146                         break; 
147                 case RETERR:
148                         reply(this.pendings, id, ans, 1);
149                         break; 
150                 case EVENT:
151                 default:
152                         fire(this.awaitens, id, ans);
153                         break; 
154                 }
155         }
156
157         function close() {
158                 this.ws.close();
159                 this.ws.onopen = 
160                 this.ws.onerror = 
161                 this.ws.onclose = 
162                 this.ws.onmessage = 
163                 this.onopen = 
164                 this.onabort = function(){};
165         }
166
167         function call(method, request, callid) {
168                 return new Promise((function(resolve, reject){
169                         var id, arr;
170                         if (callid) {
171                                 id = String(callid);
172                                 if (id in this.pendings)
173                                         throw new Error("pending callid("+id+") exists");
174                         } else {
175                                 do {
176                                         id = String(this.counter = 4095 & (this.counter + 1));
177                                 } while (id in this.pendings);
178                         }
179                         this.pendings[id] = [ resolve, reject ];
180                         arr = [CALL, id, method, request ];
181                         if (AFB_context.token) arr.push(AFB_context.token);
182                         this.ws.send(JSON.stringify(arr));
183                 }).bind(this));
184         }
185
186         function onevent(name, handler) {
187                 var id = name;
188                 var list = this.awaitens[id] || (this.awaitens[id] = []);
189                 list.push(handler);
190         }
191
192         AFB_websocket.prototype = {
193                 close: close,
194                 call: call,
195                 onevent: onevent
196         };
197 }
198 /*********************************************/
199 /****                                     ****/
200 /****                                     ****/
201 /****                                     ****/
202 /*********************************************/
203 return {
204         context: AFB_context,
205         ws: AFB_websocket
206 };
207 };
208