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

Last change on this file since 18234 was 18226, checked in by dneise, 9 years ago
turned into library
File size: 9.0 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)
109 {
110 if (timeout===undefined)
111 timeout = 5000;
112
113 var states = [];
114
115 var time = new Date();
116 while (1)
117 {
118 // Get states of all servers in question
119 states = [];
120 for (var i=0; i<servers.length; i++)
121 states[i] = dim.send(servers[i]);
122
123 // Check if they are all valid
124 if (states.indexOf(false)<0)
125 return true;
126
127 if ((new Date())-time>=Math.abs(timeout))
128 break;
129
130 v8.sleep();
131 }
132
133 if (timeout<0)
134 return false;
135
136 // Create a list of all servers which do not have valid states yet
137 var missing = [];
138 for (var i=0; i<servers.length; i++)
139 if (!states[i])
140 missing.push(servers[i]);
141
142 throw new Error("Timeout waiting for send-ready of "+missing.join(", "));
143 },
144
145 // Wait 5s for the FAD_CONTROL to get to the states
146 // return false if FAD_CONTROL is not online
147 // return false if state is not
148 // wait("FAD_CONTROL", [ "COnnected", "Disconnected" ], 5000);
149 wait : function (server,states,timeout1,timeout2)
150 {
151 if (typeof(states)=="string")
152 states = [ states ];
153
154 // If timeout2 is defined and >0 wait first for the
155 // server to come online. If it does not come online
156 // in time, an exception is thrown.
157 if (timout2>0 && CheckStates([ server ], timeout2))
158 return true;
159
160 var time = new Date();
161 while (1)
162 {
163 // If a server disconnects now while checking for the
164 // states, an exception will be thrown, too.
165 if (CheckStates([ server, states ], 0))
166 return true;
167
168 if (Date()-time>=abs(timeout1))
169 break;
170
171 v8.sleep();
172 }
173
174 if (timeout1<0)
175 throw new Error("Timeout waiting for Server "+server+" to be in ["+states+"]");
176
177 return false;
178 },
179
180 dimwait : function (server, state, timeout)
181 {
182 if (!timeout)
183 timeout = 5000;
184
185 var time = new Date();
186 while (1)
187 {
188 var s = dim.state(server);
189 if (s.index===state || s.name===state)
190 return true;
191
192 //if (s.index==undefined)
193 // throw "Server "+server+" not connected waiting for "+state+".";
194
195 if (Date()-time>=timeout)
196 break;
197
198 v8.sleep();
199 }
200
201 if (timeout>0)
202 throw new Error("Timeout waiting for ["+states+"] of "+server+".");
203
204 return false;
205
206
207 /*
208 if (!timeout)
209 timeout = 5000;
210
211 var time = new Date();
212 while (timeout<0 || new Date()-time<timeout)
213 {
214 var s = dim.state(server);
215 if (s.index===state || s.name===state)
216 return true;
217
218 if (s.index==undefined)
219 throw "Server "+server+" not connected waiting for "+state+".";
220
221 v8.sleep();
222 }
223
224 return false;
225 */
226 },
227
228 sleep: function (timeout)
229 {
230 if (!timeout)
231 timeout = 5000;
232
233 var time = new Date();
234 while (Date()-time<timeout)
235 v8.sleep();
236 },
237
238 Timer : function ()
239 {
240 this.date = new Date();
241
242 this.reset = function() { this.date = new Date(); }
243 this.print = function(id)
244 {
245 var diff = Date()-this.date;
246 if (id)
247 console.out("Time["+id+"]: "+diff+"ms");
248 else
249 console.out("Time: "+diff+"ms");
250 }
251 },
252
253 waitStates: function (server, states, timeout, func)
254 {
255 var save = dim.onchange[server];
256
257 function inner()
258 {
259 dim.onchange[server] = function(arg)
260 {
261 if (!this.states)
262 this.states = states instanceof Array ? states : [ states ];
263
264 var comp = this.states[0];
265 if (arg.index===comp || arg.name===comp || comp=='*')
266 this.states.shift();
267
268 //console.out(JSON.stringify(arg), this.states, comp, arg.name, "");
269
270 if (save instanceof Function)
271 save();
272
273 if (this.states.length==0)
274 delete dim.onchange[server];
275
276 }
277
278 if (func instanceof Function)
279 func();
280
281 var time = new Date();
282 while (1)
283 {
284 if (!dim.onchange[server])
285 return true;
286
287 if (new Date()-time>=timeout)
288 break;
289
290 v8.sleep();
291 }
292
293 delete dim.onchange[server];
294
295 if (timeout>0)
296 throw new Error("Timeout waiting for ["+states+"] of "+server+".");
297
298 return false;
299 }
300
301 var rc = inner();
302 dim.onchange[server] = save;
303 return rc;
304 },
305 };
306}
307else{
308 console.out("multiple include of 'CheckStates.js'");
309}
Note: See TracBrowser for help on using the repository browser.