source: trunk/FACT++/scripts/CheckStates.js@ 16991

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