Remove useless %lang% prefix in yaml book file
[src/app-framework-binder.git] / test / 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, p = '?';
71                 if (AFB_context.token) {
72                         u = u + '?x-afb-token=' + AFB_context.token;
73                         p = '&';
74                 }
75                 if (AFB_context.uuid)
76                         u = u + p + 'x-afb-uuid=' + AFB_context.uuid;
77                 this.ws = new WebSocket(u, [ PROTO1 ]);
78                 this.url = u;
79                 this.pendings = {};
80                 this.awaitens = {};
81                 this.counter = 0;
82                 this.ws.onopen = onopen.bind(this);
83                 this.ws.onerror = onerror.bind(this);
84                 this.ws.onclose = onclose.bind(this);
85                 this.ws.onmessage = onmessage.bind(this);
86                 this.onopen = on_open;
87                 this.onabort = on_abort;
88         }
89
90         function onerror(event) {
91                 var f = this.onabort;
92                 if (f) {
93                         delete this.onopen;
94                         delete this.onabort;
95                         f(this);
96                 }
97                 this.onerror && this.onerror(this);
98         }
99
100         function onopen(event) {
101                 var f = this.onopen;
102                 delete this.onopen;
103                 delete this.onabort;
104                 f && f(this);
105         }
106
107         function onclose(event) {
108                 var err = {
109                         jtype: 'afb-reply',
110                         request: {
111                                 status: 'disconnected',
112                                 info: 'server hung up'
113                         }
114                 };
115                 for (var id in this.pendings) {
116                         try { this.pendings[id][1](err); } catch (x) {/*NOTHING*/}
117                 }
118                 this.pendings = {};
119                 this.onclose && this.onclose();
120         }
121
122         function fire(awaitens, name, data) {
123                 var a = awaitens[name];
124                 if (a)
125                         a.forEach(function(handler){handler(data);});
126                 var i = name.indexOf("/");
127                 if (i >= 0) {
128                         a = awaitens[name.substring(0,i)];
129                         if (a)
130                                 a.forEach(function(handler){handler(data);});
131                 }
132                 a = awaitens["*"];
133                 if (a)
134                         a.forEach(function(handler){handler(data);});
135         }
136
137         function reply(pendings, id, ans, offset) {
138                 if (id in pendings) {
139                         var p = pendings[id];
140                         delete pendings[id];
141                         try { p[offset](ans); } catch (x) {/*TODO?*/}
142                 }
143         }
144
145         function onmessage(event) {
146                 var obj = JSON.parse(event.data);
147                 var code = obj[0];
148                 var id = obj[1];
149                 var ans = obj[2];
150                 AFB_context.token = obj[3];
151                 switch (code) {
152                 case RETOK:
153                         reply(this.pendings, id, ans, 0);
154                         break;
155                 case RETERR:
156                         reply(this.pendings, id, ans, 1);
157                         break;
158                 case EVENT:
159                 default:
160                         fire(this.awaitens, id, ans);
161                         break;
162                 }
163         }
164
165         function close() {
166                 this.ws.close();
167                 this.ws.onopen = 
168                 this.ws.onerror = 
169                 this.ws.onclose = 
170                 this.ws.onmessage = 
171                 this.onopen = 
172                 this.onabort = function(){};
173         }
174
175         function call(method, request, callid) {
176                 return new Promise((function(resolve, reject){
177                         var id, arr;
178                         if (callid) {
179                                 id = String(callid);
180                                 if (id in this.pendings)
181                                         throw new Error("pending callid("+id+") exists");
182                         } else {
183                                 do {
184                                         id = String(this.counter = 4095 & (this.counter + 1));
185                                 } while (id in this.pendings);
186                         }
187                         this.pendings[id] = [ resolve, reject ];
188                         arr = [CALL, id, method, request ];
189                         if (AFB_context.token) arr.push(AFB_context.token);
190                         this.ws.send(JSON.stringify(arr));
191                 }).bind(this));
192         }
193
194         function onevent(name, handler) {
195                 var id = name;
196                 var list = this.awaitens[id] || (this.awaitens[id] = []);
197                 list.push(handler);
198         }
199
200         AFB_websocket.prototype = {
201                 close: close,
202                 call: call,
203                 onevent: onevent
204         };
205 }
206 /*********************************************/
207 /****                                     ****/
208 /****                                     ****/
209 /****                                     ****/
210 /*********************************************/
211 return {
212         context: AFB_context,
213         ws: AFB_websocket
214 };
215 };
216