add debugging html ui.
[apps/agl-service-data-persistence.git] / ll-database-binding / htdocs / persistence / AFB-websock.js
1 AFB = function(base, initialtoken){
2
3 var urlws = "ws://"+window.location.host+"/"+base;
4 var urlhttp = "http://"+window.location.host+"/"+base;
5
6 /*********************************************/
7 /****                                     ****/
8 /****             AFB_context             ****/
9 /****                                     ****/
10 /*********************************************/
11 var AFB_context;
12 {
13         var UUID = undefined;
14         var TOKEN = initialtoken;
15
16         var context = function(token, uuid) {
17                 this.token = token;
18                 this.uuid = uuid;
19         }
20
21         context.prototype = {
22                 get token() {return TOKEN;},
23                 set token(tok) {if(tok) TOKEN=tok;},
24                 get uuid() {return UUID;},
25                 set uuid(id) {if(id) UUID=id;}
26         };
27
28         AFB_context = new context();
29 }
30 /*********************************************/
31 /****                                     ****/
32 /****             AFB_websocket           ****/
33 /****                                     ****/
34 /*********************************************/
35 var AFB_websocket;
36 {
37         var CALL = 2;
38         var RETOK = 3;
39         var RETERR = 4;
40         var EVENT = 5;
41
42         var PROTO1 = "x-afb-ws-json1";
43
44         AFB_websocket = function(onopen, onabort) {
45                 var u = urlws;
46                 if (AFB_context.token) {
47                         u = u + '?x-afb-token=' + AFB_context.token;
48                         if (AFB_context.uuid)
49                                 u = u + '&x-afb-uuid=' + AFB_context.uuid;
50                 }
51                 this.ws = new WebSocket(u, [ PROTO1 ]);
52                 this.pendings = {};
53                 this.awaitens = {};
54                 this.counter = 0;
55                 this.ws.onopen = onopen.bind(this);
56                 this.ws.onerror = onerror.bind(this);
57                 this.ws.onclose = onclose.bind(this);
58                 this.ws.onmessage = onmessage.bind(this);
59                 this.onopen = onopen;
60                 this.onabort = onabort;
61                 this.onclose = onabort;
62         }
63
64         function onerror(event) {
65                 var f = this.onabort;
66                 if (f) {
67                         delete this.onopen;
68                         delete this.onabort;
69                         f && f(this);
70                 }
71                 this.onerror && this.onerror(this);
72         }
73
74         function onopen(event) {
75                 var f = this.onopen;
76                 delete this.onopen;
77                 delete this.onabort;
78                 f && f(this);
79         }
80
81         function onclose(event) {
82                 for (var id in this.pendings) {
83                         var ferr = this.pendings[id].onerror;
84                         ferr && ferr(null, this);
85                 }
86                 this.pendings = {};
87                 this.onclose && this.onclose();
88         }
89
90         function fire(awaitens, name, data) {
91                 var a = awaitens[name];
92                 if (a)
93                         a.forEach(function(handler){handler(data);});
94                 var i = name.indexOf("/");
95                 if (i >= 0) {
96                         a = awaitens[name.substring(0,i)];
97                         if (a)
98                                 a.forEach(function(handler){handler(data);});
99                 }
100                 a = awaitens["*"];
101                 if (a)
102                         a.forEach(function(handler){handler(data);});
103         }
104
105         function reply(pendings, id, ans, offset) {
106                 if (id in pendings) {
107                         var p = pendings[id];
108                         delete pendings[id];
109                         var f = p[offset];
110                         f(ans);
111                 }
112         }
113
114         function onmessage(event) {
115                 var obj = JSON.parse(event.data);
116                 var code = obj[0];
117                 var id = obj[1];
118                 var ans = obj[2];
119                 AFB_context.token = obj[3];
120                 switch (code) {
121                 case RETOK:
122                         reply(this.pendings, id, ans, 0);
123                         break;
124                 case RETERR:
125                         reply(this.pendings, id, ans, 1);
126                         break;
127                 case EVENT:
128                 default:
129                         fire(this.awaitens, id, ans);
130                         break;
131                 }
132         }
133
134         function close() {
135                 this.ws.close();
136                 this.onabort();
137         }
138
139         function call(method, request) {
140                 return new Promise((function(resolve, reject){
141                         var id, arr;
142                         do {
143                            id = String(this.counter = 4095 & (this.counter + 1));
144                         } while (id in this.pendings);
145                         this.pendings[id] = [ resolve, reject ];
146                         arr = [CALL, id, method, request ];
147                         if (AFB_context.token) arr.push(AFB_context.token);
148                         this.ws.send(JSON.stringify(arr));
149                 }).bind(this));
150         }
151
152         function onevent(name, handler) {
153                 var id = name;
154                 var list = this.awaitens[id] || (this.awaitens[id] = []);
155                 list.push(handler);
156         }
157
158         AFB_websocket.prototype = {
159                 close: close,
160                 call: call,
161                 onevent: onevent
162         };
163 }
164 /*********************************************/
165 /****                                     ****/
166 /****                                     ****/
167 /****                                     ****/
168 /*********************************************/
169 return {
170         context: AFB_context,
171         ws: AFB_websocket
172 };
173 };
174