Changeset 18226
- Timestamp:
- 06/18/15 12:17:48 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FACT++_scripts_refactoring/CheckStates.js
r17927 r18226 1 1 'use strict'; 2 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 */ 42 function checkStates(table, timeout, wait) 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; 3 if (!("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 = []; 66 96 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 if (!wait) 72 dim.log(table[i][0]+" in ["+states[i]+"] not as it ought to be ["+table[i][1]+"]"); 73 74 rc = false; 75 } 76 if (rc) 77 return rc; 78 79 if (!wait) 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) 80 134 return false; 81 } 82 83 if ((new Date())-time>=Math.abs(timeout)) 84 break; 85 86 v8.sleep(); 87 } 88 89 if (timeout<0) 90 return false; 91 92 // Create a list of all servers which do not have valid states yet 93 var servers = []; 94 for (var i=0; i<table.length; i++) 95 if (!states[i]) 96 servers.push(table[i][0]); 97 98 // If all servers do not yet have valid states, it is obsolete to 99 // print all their names 100 if (servers.length==table.length && servers.length>1) 101 servers = [ "servers." ]; 102 103 throw new Error("Timeout waiting for access to named states of "+servers.join(", ")); 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 }; 104 306 } 105 106 function checkSend(servers, timeout) 107 { 108 if (timeout===undefined) 109 timeout = 5000; 110 111 var states = []; 112 113 var time = new Date(); 114 while (1) 115 { 116 // Get states of all servers in question 117 states = []; 118 for (var i=0; i<servers.length; i++) 119 states[i] = dim.send(servers[i]); 120 121 // Check if they are all valid 122 if (states.indexOf(false)<0) 123 return true; 124 125 if ((new Date())-time>=Math.abs(timeout)) 126 break; 127 128 v8.sleep(); 129 } 130 131 if (timeout<0) 132 return false; 133 134 // Create a list of all servers which do not have valid states yet 135 var missing = []; 136 for (var i=0; i<servers.length; i++) 137 if (!states[i]) 138 missing.push(servers[i]); 139 140 throw new Error("Timeout waiting for send-ready of "+missing.join(", ")); 307 else{ 308 console.out("multiple include of 'CheckStates.js'"); 141 309 } 142 143 function Wait(server,states,timeout1,timeout2)144 {145 if (typeof(states)=="string")146 states = [ states ];147 148 // If timeout2 is defined and >0 wait first for the149 // server to come online. If it does not come online150 // in time, an exception is thrown.151 if (timout2>0 && CheckStates([ server ], timeout2))152 return true;153 154 var time = new Date();155 while (1)156 {157 // If a server disconnects now while checking for the158 // states, an exception will be thrown, too.159 if (CheckStates([ server, states ], 0))160 return true;161 162 if (Date()-time>=abs(timeout1))163 break;164 165 v8.sleep();166 }167 168 if (timeout1<0)169 throw new Error("Timeout waiting for Server "+server+" to be in ["+states+"]");170 171 return false;172 }173 174 // Wait 5s for the FAD_CONTROL to get to the states175 // return false if FAD_CONTROL is not online176 // return false if state is not177 // Wait("FAD_CONTROL", [ "COnnected", "Disconnected" ], 5000);178 179 function dimwait(server, state, timeout)180 {181 if (!timeout)182 timeout = 5000;183 184 var time = new Date();185 while (1)186 {187 var s = dim.state(server);188 if (s.index===state || s.name===state)189 return true;190 191 //if (s.index==undefined)192 // throw "Server "+server+" not connected waiting for "+state+".";193 194 if (Date()-time>=timeout)195 break;196 197 v8.sleep();198 }199 200 if (timeout>0)201 throw new Error("Timeout waiting for ["+states+"] of "+server+".");202 203 return false;204 205 206 /*207 if (!timeout)208 timeout = 5000;209 210 var time = new Date();211 while (timeout<0 || new Date()-time<timeout)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 v8.sleep();221 }222 223 return false;224 */225 }226 227 function Sleep(timeout)228 {229 if (!timeout)230 timeout = 5000;231 232 var time = new Date();233 while (Date()-time<timeout)234 v8.sleep();235 }236 237 function Timer()238 {239 this.date = new Date();240 241 this.reset = function() { this.date = new Date(); }242 this.print = function(id)243 {244 var diff = Date()-this.date;245 if (id)246 console.out("Time["+id+"]: "+diff+"ms");247 else248 console.out("Time: "+diff+"ms");249 }250 }251 252 function WaitStates(server, states, timeout, func)253 {254 var save = dim.onchange[server];255 256 function inner()257 {258 dim.onchange[server] = function(arg)259 {260 if (!this.states)261 this.states = states instanceof Array ? states : [ states ];262 263 var comp = this.states[0];264 if (arg.index===comp || arg.name===comp || comp=='*')265 this.states.shift();266 267 //console.out(JSON.stringify(arg), this.states, comp, arg.name, "");268 269 if (save instanceof Function)270 save();271 272 if (this.states.length==0)273 delete dim.onchange[server];274 275 }276 277 if (func instanceof Function)278 func();279 280 var time = new Date();281 while (1)282 {283 if (!dim.onchange[server])284 return true;285 286 if (new Date()-time>=timeout)287 break;288 289 v8.sleep();290 }291 292 delete dim.onchange[server];293 294 if (timeout>0)295 throw new Error("Timeout waiting for ["+states+"] of "+server+".");296 297 return false;298 }299 300 var rc = inner();301 dim.onchang302 e[server] = save;303 return rc;304 }
Note:
See TracChangeset
for help on using the changeset viewer.