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
|
ServerStatusWatcher {
var server;
var <>notified = false, <notify = true;
var alive = false, <aliveThread, <>aliveThreadPeriod = 0.7, statusWatcher;
var <hasBooted = false, <>serverBooting = false, <unresponsive = false;
var <numUGens=0, <numSynths=0, <numGroups=0, <numSynthDefs=0;
var <avgCPU, <peakCPU;
var <sampleRate, <actualSampleRate;
var reallyDeadCount = 0, bootNotifyFirst = true;
*new { |server|
^super.newCopyArgs(server)
}
quit { |onComplete, onFailure, watchShutDown = true|
if(watchShutDown) {
this.watchQuit(onComplete, onFailure)
} {
this.stopStatusWatcher;
onComplete.value;
};
this.stopAliveThread;
this.serverRunning = false;
hasBooted = false;
alive = false;
notified = false;
serverBooting = false;
unresponsive = false;
// server.changed(\serverRunning) should be deferred in dependants!
// just in case some don't, defer here to avoid gui updates breaking.
defer { server.changed(\serverRunning) };
bootNotifyFirst = true;
}
notify_ { |flag = true|
notify = flag;
this.sendNotifyRequest(flag);
}
// flag true requests notification, false turns it off
sendNotifyRequest { |flag = true|
this.prSendNotifyRequest(flag, false);
}
doWhenBooted { |onComplete, limit = 100, onFailure|
var mBootNotifyFirst = bootNotifyFirst, postError = true;
bootNotifyFirst = false;
^Routine {
while {
server.serverRunning.not
/*
// this is not yet implemented.
or: { serverBooting and: mBootNotifyFirst.not }
and: { (limit = limit - 1) > 0 }
and: { server.applicationRunning.not }
*/
} {
0.2.wait;
};
if(server.serverRunning.not, {
if(onFailure.notNil) {
postError = (onFailure.value(server) == false);
};
if(postError) {
"Server '%' on failed to start. You may need to kill all servers".format(server.name).error;
};
serverBooting = false;
server.changed(\serverRunning);
}, {
// make sure the server process finishes all pending tasks from Server.tree before running onComplete
server.sync;
onComplete.value;
});
}.play(AppClock)
}
watchQuit { |onComplete, onFailure|
var serverReallyQuitWatcher, serverReallyQuit = false;
statusWatcher !? {
statusWatcher.disable;
if(notified) {
serverReallyQuitWatcher = OSCFunc({ |msg|
if(msg[1] == '/quit') {
statusWatcher !? { statusWatcher.enable };
serverReallyQuit = true;
serverReallyQuitWatcher.free;
onComplete.value;
}
}, '/done', server.addr);
AppClock.sched(3.0, {
if(serverReallyQuit.not) {
if(unresponsive) {
"Server '%' remained unresponsive during quit."
} {
"Server '%' failed to quit after 3.0 seconds."
}.format(server.name).warn;
// don't accumulate quit-watchers if /done doesn't come back
serverReallyQuitWatcher.free;
statusWatcher !? { statusWatcher.disable };
onFailure.value(server)
}
})
}
}
}
addStatusWatcher {
if(statusWatcher.isNil) {
statusWatcher =
OSCFunc({ arg msg;
var cmd, one;
if(notify and: { notified.not }) { this.prSendNotifyRequest(true,true) };
alive = true;
#cmd, one, numUGens, numSynths, numGroups, numSynthDefs,
avgCPU, peakCPU, sampleRate, actualSampleRate = msg;
{
this.updateRunningState(true);
server.changed(\counts);
}.defer;
}, '/status.reply', server.addr).fix;
} {
statusWatcher.enable;
}
}
stopStatusWatcher {
statusWatcher !? { statusWatcher.disable }
}
startAliveThread { | delay = 0.0 |
this.addStatusWatcher;
^aliveThread ?? {
aliveThread = Routine {
// this thread polls the server to see if it is alive
delay.wait;
loop {
alive = false;
server.sendStatusMsg;
aliveThreadPeriod.wait;
this.updateRunningState(alive);
};
}.play(AppClock);
aliveThread
}
}
stopAliveThread {
statusWatcher.free;
statusWatcher = nil;
aliveThread.stop;
alive = false;
aliveThread = nil;
}
resumeThread {
aliveThread !? {
this.stopAliveThread;
this.startAliveThread;
}
}
serverRunning { ^hasBooted and: notified }
serverRunning_ { | running |
if(running != server.serverRunning) {
this.unresponsive = false;
hasBooted = running;
if (running.not) {
hasBooted = running;
ServerQuit.run(server);
server.disconnectSharedMemory;
if(server.isRecording) { server.stopRecording };
{ server.changed(\didQuit) }.defer;
NotificationCenter.notify(server, \didQuit); // why here "NotificationCenter" and below "changed"?
if(server.isLocal.not) {
notified = false;
};
};
}
}
updateRunningState { | running |
if(server.addr.hasBundle) {
{ server.changed(\bundling) }.defer;
} {
if(running) {
this.serverRunning = true;
this.unresponsive = false;
reallyDeadCount = server.options.pingsBeforeConsideredDead;
} {
// parrot
reallyDeadCount = reallyDeadCount - 1;
this.unresponsive = (reallyDeadCount <= 0);
}
}
}
unresponsive_ { | val |
if (val != unresponsive) {
unresponsive = val;
{ server.changed(\serverRunning) }.defer;
}
}
// final actions needed to finish booting
prFinalizeBoot {
// this needs to be forked so that ServerBoot and ServerTree will definitely run before
// notified is true.
fork({
ServerBoot.run(server);
server.sync;
server.initTree;
this.notified = true;
server.changed(\serverRunning);
}, AppClock)
}
// This method attempts to recover from a loss of client-server contact,
// which is a serious emergency in live shows. So it posts a lot of info
// on the recovered state, and possibly helpful next user actions.
prHandleLoginWhenAlreadyRegistered { |clientIDFromProcess|
"% - handling login request though already registered - \n".postf(server);
if (clientIDFromProcess.isNil) {
"% - notify response did not contain already-registered clientID from server process.\n"
"Assuming all is well.\n".postf(server);
} {
// by default, only reset clientID if changed, to leave allocators untouched:
if (clientIDFromProcess != server.clientID) {
// make sure we can set the clientID, and set it
notified = false;
server.clientID_(clientIDFromProcess);
"*** This seems to be a login after a crash, or from a new server object,\n"
"*** so you may want to release currently running synths by hand:".postln;
"%.defaultGroup.release;\n".postf(server.cs);
"*** and you may want to redo server boot finalization by hand:".postln;
"%.statusWatcher.prFinalizeBoot;\n\n".postf(server.cs);
} {
// same clientID, so leave all server resources in the state they were in!
"This seems to be a login after a loss of network contact - \n"
"- reconnected with the same clientID as before, so probably all is well.\n".postln;
};
};
// ensure that statuswatcher is in the correct state immediately.
this.notified = true;
unresponsive = false;
server.changed(\serverRunning);
}
prSendNotifyRequest { |flag = true, addingStatusWatcher|
var doneOSCFunc, failOSCFunc;
if(hasBooted.not) { ^this };
// set up oscfuncs for possible server responses, \done or \failed
doneOSCFunc = OSCFunc({ |msg|
var newClientID = msg[2], newMaxLogins = msg[3];
failOSCFunc.free;
if(newClientID.notNil) {
// notify on:
// on registering scsynth sends back a free clientID and maxLogins
// this method doesn't fork/wait so we're still in the clear
// turn notified off (if it was on) to allow setting clientID
notified = false;
server.prHandleClientLoginInfoFromServer(newClientID, newMaxLogins);
// XXX: this is a workaround because using `serverBooting` is not reliable
// when server is rebooted quickly.
if(addingStatusWatcher) {
this.prFinalizeBoot;
} {
notified = true;
}
} {
notified = false;
};
}, '/done', server.addr, argTemplate:['/notify', nil]).oneShot;
failOSCFunc = OSCFunc({|msg|
doneOSCFunc.free;
server.prHandleNotifyFailString(msg[2], msg);
}, '/fail', server.addr, argTemplate:['/notify', nil, nil]).oneShot;
server.sendMsg("/notify", flag.binaryValue, server.clientID);
if(flag){
"Requested notification messages from server '%'\n".postf(server.name)
} {
"Switched off notification messages from server '%'\n".postf(server.name);
};
}
}
|