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