source: branches/FACT++_scripts_refactoring/CheckStates.js@ 19133

Last change on this file since 19133 was 18236, checked in by dneise, 9 years ago
CheckStates.checkSend has new parameter 'verbose'
File size: 9.7 KB
Line 
1'use strict';
2
3if (!("CheckStates" in this)){
4 var CheckStates = {
5 /**
6 *
7 * Waits for the timeout (in ms) for the given servers to be online
8 * and in one of the provided states.
9 *
10 * Returns true if MAGIC_WEATHER is online, FTM_CONTROL is in Idle
11 * and LID_CONTROL is either in Open or Closed.
12 *
13 * Returns false if all given servers are online, but at least one is
14 * not in the expected state.
15 *
16 * Throws an exception if not all provided servers are online after
17 * the given timeout.
18 *
19 * If you want to wait infinitely: CheckStates(table, null)
20 * If the timeout is negative (e.g. -500 means 500ms) or zero,
21 * no exception is thrown but false is returned.
22 *
23 * @param table
24 *
25 * @param {Integer} [timeout=5000]
26 * timeout in milliseconds
27 *
28 * @returns
29 *
30 * @throws
31 *
32 * @example
33 * var table =
34 * [
35 * [ "MAGIC_WEATHER" ],
36 * [ "FTM_CONTROL", [ "Idle" ] ],
37 * [ "LID_CONTROL", [ "Open", "Closed" ] ],
38 * ];
39 *
40 * checkStates(table);
41 *
42 *
43 */
44 checkStates : function (table, timeout, wait)
45 {
46 if (timeout===undefined)
47 timeout = 5000;
48
49 var states = [];
50
51 var time = new Date();
52 while (1)
53 {
54 // Get states of all servers in question
55 states = [];
56 for (var i=0; i<table.length; i++)
57 {
58 var state = dim.state(table[i][0]);
59 states[i] = state ? state.name : undefined;
60 }
61
62 // Check if they are all valid
63 if (states.indexOf(undefined)<0)
64 {
65 // If they are all valid, check them against the
66 // state lists provided for each server
67 var rc = true;
68 for (var i=0; i<table.length; i++)
69 {
70 if (!table[i][1] || table[i][1].indexOf(states[i])>=0)
71 continue;
72
73 if (!wait)
74 dim.log(table[i][0]+" in ["+states[i]+"] not as it ought to be ["+table[i][1]+"]");
75
76 rc = false;
77 }
78 if (rc)
79 return rc;
80
81 if (!wait)
82 return false;
83 }
84
85 if ((new Date())-time>=Math.abs(timeout))
86 break;
87
88 v8.sleep();
89 }
90
91 if (timeout<0)
92 return false;
93
94 // Create a list of all servers which do not have valid states yet
95 var servers = [];
96 for (var i=0; i<table.length; i++)
97 if (!states[i])
98 servers.push(table[i][0]);
99
100 // If all servers do not yet have valid states, it is obsolete to
101 // print all their names
102 if (servers.length==table.length && servers.length>1)
103 servers = [ "servers." ];
104
105 throw new Error("Timeout waiting for access to named states of "+servers.join(", "));
106 },
107
108 checkSend : function (servers, timeout, verbose){
109 if (verbose)
110 console.out("checkSend: [timeout="+timeout+"]");
111 if (verbose > 1)
112 {
113 for (var i=0; i<servers.length; i++){
114 console.out(" "+servers[i]);
115 }
116 }
117 try
118 {
119 this._checkSend(servers, timeout);
120 }
121 catch(error)
122 {
123 // we do nothing and let the exception propagate upwards.
124 }
125 finally
126 {
127 if (verbose)
128 console.out("Checking send: done");
129 }
130 }
131
132 _checkSend : function (servers, timeout)
133 {
134
135 if (timeout===undefined)
136 timeout = 5000;
137
138 var states = [];
139
140 var time = new Date();
141 while (1)
142 {
143 // Get states of all servers in question
144 states = [];
145 for (var i=0; i<servers.length; i++)
146 states[i] = dim.send(servers[i]);
147
148 // Check if they are all valid
149 if (states.indexOf(false)<0)
150 return true;
151
152 if ((new Date())-time>=Math.abs(timeout))
153 break;
154
155 v8.sleep();
156 }
157
158 if (timeout<0)
159 return false;
160
161 // Create a list of all servers which do not have valid states yet
162 var missing = [];
163 for (var i=0; i<servers.length; i++)
164 if (!states[i])
165 missing.push(servers[i]);
166
167 throw new Error("Timeout waiting for send-ready of "+missing.join(", "));
168 },
169
170 // Wait 5s for the FAD_CONTROL to get to the states
171 // return false if FAD_CONTROL is not online
172 // return false if state is not
173 // wait("FAD_CONTROL", [ "COnnected", "Disconnected" ], 5000);
174 wait : function (server,states,timeout1,timeout2)
175 {
176 if (typeof(states)=="string")
177 states = [ states ];
178
179 // If timeout2 is defined and >0 wait first for the
180 // server to come online. If it does not come online
181 // in time, an exception is thrown.
182 if (timout2>0 && CheckStates([ server ], timeout2))
183 return true;
184
185 var time = new Date();
186 while (1)
187 {
188 // If a server disconnects now while checking for the
189 // states, an exception will be thrown, too.
190 if (CheckStates([ server, states ], 0))
191 return true;
192
193 if (Date()-time>=abs(timeout1))
194 break;
195
196 v8.sleep();
197 }
198
199 if (timeout1<0)
200 throw new Error("Timeout waiting for Server "+server+" to be in ["+states+"]");
201
202 return false;
203 },
204
205 dimwait : function (server, state, timeout)
206 {
207 if (!timeout)
208 timeout = 5000;
209
210 var time = new Date();
211 while (1)
212 {
213 var s = dim.state(server);
214 if (s.index===state || s.name===state)
215 return true;
216
217 //if (s.index==undefined)
218 // throw "Server "+server+" not connected waiting for "+state+".";
219
220 if (Date()-time>=timeout)
221 break;
222
223 v8.sleep();
224 }
225
226 if (timeout>0)
227 throw new Error("Timeout waiting for ["+states+"] of "+server+".");
228
229 return false;
230
231
232 /*
233 if (!timeout)
234 timeout = 5000;
235
236 var time = new Date();
237 while (timeout<0 || new Date()-time<timeout)
238 {
239 var s = dim.state(server);
240 if (s.index===state || s.name===state)
241 return true;
242
243 if (s.index==undefined)
244 throw "Server "+server+" not connected waiting for "+state+".";
245
246 v8.sleep();
247 }
248
249 return false;
250 */
251 },
252
253 sleep: function (timeout)
254 {
255 if (!timeout)
256 timeout = 5000;
257
258 var time = new Date();
259 while (Date()-time<timeout)
260 v8.sleep();
261 },
262
263 Timer : function ()
264 {
265 this.date = new Date();
266
267 this.reset = function() { this.date = new Date(); }
268 this.print = function(id)
269 {
270 var diff = Date()-this.date;
271 if (id)
272 console.out("Time["+id+"]: "+diff+"ms");
273 else
274 console.out("Time: "+diff+"ms");
275 }
276 },
277
278 waitStates: function (server, states, timeout, func)
279 {
280 var save = dim.onchange[server];
281
282 function inner()
283 {
284 dim.onchange[server] = function(arg)
285 {
286 if (!this.states)
287 this.states = states instanceof Array ? states : [ states ];
288
289 var comp = this.states[0];
290 if (arg.index===comp || arg.name===comp || comp=='*')
291 this.states.shift();
292
293 //console.out(JSON.stringify(arg), this.states, comp, arg.name, "");
294
295 if (save instanceof Function)
296 save();
297
298 if (this.states.length==0)
299 delete dim.onchange[server];
300
301 }
302
303 if (func instanceof Function)
304 func();
305
306 var time = new Date();
307 while (1)
308 {
309 if (!dim.onchange[server])
310 return true;
311
312 if (new Date()-time>=timeout)
313 break;
314
315 v8.sleep();
316 }
317
318 delete dim.onchange[server];
319
320 if (timeout>0)
321 throw new Error("Timeout waiting for ["+states+"] of "+server+".");
322
323 return false;
324 }
325
326 var rc = inner();
327 dim.onchange[server] = save;
328 return rc;
329 },
330 };
331}
332else{
333 console.out("multiple include of 'CheckStates.js'");
334}
Note: See TracBrowser for help on using the repository browser.