«Clock Difference» by codepool
on 27 Apr'17 22:15 inMigration from the old SourceForge wiki.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/* In order to synchronise synthesis events sent to servers running on two different machines, it is necessary to know the difference between their two clocks and compensate for this in the time argument of sendMsg or sendBundle. Here is one method of finding this difference. These classes must be compiled in the usual way before use. Beware, the following contains overrides to other core classes, so please check the full document before using. // nonprivate@cylob.com - 5 March 2007 // HOW TO USE: // execute these one by one on both machines x = C2_ClockDifference.new; x.setup; // then enter i.p. of remote machine, within a second or so you'll get the result // ... this is important, it is not an instant process! y = "192.168.2.21"; // the i.p. to check out x.calcClockDifference(y); // the results get stored in an identity dictionary (called id) for each i.p. investigated // if nothing is received back then nothing gets added to the dictionary x.id.postln; // after the result has been returned, you can do this: (remember to turn the i.p. into a symbol to do so) x.id.at(y.asSymbol).postln; */ C2_ClockDifference { var listener, sender; var remoteAddr, masterAddr; var <id; // Listener is LocalClient // Sender is Client // function is ClientFunc calcClockDifference { arg remoteIP; if( NetAddr.testAddr(remoteIP) == true, { remoteAddr = NetAddr(remoteIP, 57120); // send to the remoteAddr sender = Client(\clockDiffSender, remoteAddr); sender.send(\sendClockDiffMsg, Process.elapsedTime, NetAddr.myIP); }, { ["clock difference - IP is not accessable"].postln; } ); } stopListener { ["C2 clock difference ... listener has been stopped"].postln; listener.stop; } setup { id = IdentityDictionary.new; listener = LocalClient.default; listener.start; // this stuff gets executed on the remote server // it's here because another computer might interrogate this one as the remote server ClientFunc( \sendClockDiffMsg, { arg inProcessTimeSync, sendToIP; var time, getAccurateTime, whenDate, whenProcess, thisProcessTimeSync, sender; ["clock difference ... instruction has been received and will be sent back to IP " ++ sendToIP.asString].postln; sender = Client(\clockDiffReturn, NetAddr(sendToIP.asString, 57120)); thisProcessTimeSync = Process.elapsedTime; getAccurateTime = Routine({ var flag = 0, start; start = Date.localtime.second; while( {flag == 0}, { if (start != Date.localtime.second, { flag = 1; whenDate = Date.localtime; whenProcess = Process.elapsedTime; sender.send(\returnClockDiffResult, inProcessTimeSync, thisProcessTimeSync, whenProcess, whenDate.hour, whenDate.minute, whenDate.second, NetAddr.myIP); } ); 0.00001.wait; } ); }); SystemClock.play(getAccurateTime); } ); // this stuff gets executed here, it is triggered by the remote server ClientFunc( \returnClockDiffResult, { arg processTimeSyncHere, processTimeSyncThere, inProcessTime, inHour, inMinute, inSecond, receivedFromIP; var time, getAccurateTime, whenDate, whenProcess, thereAll, hereAll, serverDelay; if(receivedFromIP.asString == remoteAddr.hostname.asString, { thereAll = [inHour, inMinute, inSecond].deepCopy; getAccurateTime = Routine({ var flag = 0, start; start = Date.localtime.second; while( {flag == 0}, { if (start != Date.localtime.second, { flag = 1; whenDate = Date.localtime; whenProcess = Process.elapsedTime; hereAll = [whenDate.hour, whenDate.minute, whenDate.second]; serverDelay = C2_DateMaths.workOutSchedDelay( inProcessTime, thereAll, processTimeSyncThere, whenProcess, hereAll, processTimeSyncHere ); // cut it some slack serverDelay = serverDelay - 0.001; } ); 0.00001.wait; } ); ["C2_ClockDifference: server delay has been calculated: it is " ++ serverDelay.asString].postln; id.put(receivedFromIP.asSymbol, serverDelay); }); SystemClock.play(getAccurateTime); }, { ["C2_ClockDifference: server delay has NOT been calculated ... received result from unexpected IP"].postln; } ); } ); } } C2_DateMaths { *addSeconds { arg additionValueInSeconds, hms; var temp, leftoverSeconds; var hour, minute, second; hour = hms.at(0); minute = hms.at(1); second = hms.at(2); // hours temp = additionValueInSeconds.div(3600); hour = hour + temp; if (hour < 0, {hour = hour + 24}); if (hour > 23, {hour = hour - 24}); additionValueInSeconds = additionValueInSeconds - (temp * 3600); // minutes temp = additionValueInSeconds.div(60); minute = minute + temp; if (minute < 0, { minute = minute + 60; hour = hour - 1; if (hour < 0, {hour = hour + 24}); } ); if (minute > 59, { minute = minute - 60; hour = hour + 1; if (hour > 23, {hour = hour - 24}); } ); additionValueInSeconds = additionValueInSeconds - (temp * 60); // seconds temp = additionValueInSeconds.div(1); second = second + temp; if (second < 0, { second = second + 60; minute = minute - 1; if (minute < 0, { minute = minute + 60; hour = hour - 1; if (hour < 0, {hour = hour + 24}); } ); } ); if (second > 59, { second = second - 60; minute = minute + 1; if (minute > 59, { minute = minute - 60; hour = hour + 1; if (hour > 23, {hour = hour - 24}); } ); } ); additionValueInSeconds = additionValueInSeconds - temp; leftoverSeconds = additionValueInSeconds; ^[hour, minute, second + leftoverSeconds] } *subtractHmsOutputSeconds { arg hms1, hms2; var outHours, outMinutes, outSeconds; outHours = hms1.at(0) - hms2.at(0); outMinutes = hms1.at(1) - hms2.at(1); outSeconds = hms1.at(2) - hms2.at(2); outSeconds = (outHours * 3600) + (outMinutes * 60) + outSeconds; ^outSeconds } *workOutSchedDelay { arg that_ptDated, that_date, that_ptArb, this_ptDated, this_date, this_ptArb; var that_dateArb, this_dateArb; var x, schedDelay; x = that_ptDated - that_ptArb; that_dateArb = C2_DateMaths.addSeconds(x.neg, that_date); x = this_ptDated - this_ptArb; this_dateArb = C2_DateMaths.addSeconds(x.neg, this_date); schedDelay = C2_DateMaths.subtractHmsOutputSeconds(that_dateArb, this_dateArb); ^schedDelay } } // INCLUDING OVERRIDES ALSO USEFUL FOR OTHER THINGS // bits and pieces adapted and / or stolen from things posted to sc users list + NetAddr { *new { arg hostname, port=0; var addr; addr = if (hostname.notNil, { try { hostname.gethostbyname } { "*** CMS_Overrides: NetAddr ***".postln; ("server " ++ hostname ++ " has not been found, so i.p. will be set to 0.0.0.0").postln; "******************************".postln; hostname = "0.0.0.0"; hostname.gethostbyname } }, { 0 } ); ^super.newCopyArgs(addr, port, hostname); } *testAddr { arg addr; var result = true; try { addr.gethostbyname } { result = false } ^result; } *myIP { var j, k, res; res = Pipe.findValuesForKey("ifconfig", "inet"); ^res !? { res = res.reject(_ == "127.0.0.1"); // remove loopback device ip if(res.size > 1) { warn("the first of those devices were chosen: " ++ res) }; res[0] }; } } + Pipe { *do { arg commandLine, func; var line, pipe = this.new(commandLine, "r"), i=0; { line = pipe.getLine; while { line.notNil } { func.value(line, i); i = i + 1; line = pipe.getLine; } }.protect { pipe.close }; } *findValuesForKey { arg commandLine, key, delimiter=$ ; var j, k, indices, res, keySize; key = key ++ delimiter; keySize = key.size; Pipe.do(commandLine, { |l| indices = l.findAll(key); indices !? { indices.do { |j| j = j + keySize; while { l[j] == delimiter } { j = j + 1 }; k = l.find(delimiter.asString, offset:j) ?? { l.size } - 1; res = res.add(l[j..k]) }; }; }); ^res } }
reception
comments