-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduling.js
511 lines (437 loc) · 14.7 KB
/
scheduling.js
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
var Scene = require('./models/scene').Scene;
module.exports = function(io) {
var fs = require('fs');
var PNG = require('pngjs').PNG;
var scenes = {}; // scenes being worked on. Maps sceneID -> RTScene
var TOLERANCE_TIME = 30*1000; // time we wait before reassigning work
// Job Class
// @param int id - job identifier
// @param array pixels - a pixel is a map {x, y}
var Job = function(id, startPixelIndex, endPixelIndex) {
this.id = id;
this.startPixelIndex = startPixelIndex;
this.endPixelIndex = endPixelIndex;
// this.pixels = pixels;
this.workingClients = [];
};
Job.CHUNK_SIZE = 2500;
Job.prototype.getID = function() {
return this.id;
};
Job.prototype.getWorkingClients = function() {
return this.workingClients;
};
Job.prototype.addWorkingClient = function(client) {
this.workingClients.push(client);
};
Job.prototype.clearWorkingClients = function() {
this.workingClients = [];
};
Job.prototype.getPixels = function() {
return this.pixels;
};
Job.prototype.toJSON = function() {
return {
id: this.id,
startPixelIndex: this.startPixelIndex,
endPixelIndex: this.endPixelIndex
};
// return { id: this.id, pixels: this.pixels };
};
// RTScene Class
// Scene to be ray traced and rendered into an image
var RTScene = function(id, width, height) {
if (!width || !height) {
console.log("Width or height was not specified!");
this.width = 400;
this.height = 300;
} else {
this.width = width;
this.height = height;
}
this.totalPixels = width * height;
this.id = id;
// map holding all clients connected to this image
this.connectedClients = {};
// map holding all the jobs for this image
this.allJobs = {}; // maps jobIDs to Jobs
// map holding all job results for this image
// A job result is a pixel->RGB mapping
this.jobResults = {}; // maps jobIDs to results
// Queue of jobs to be given to clients for this image
this.jobQueue = [];
// Keeps track if we've created the finished PNG image
this.createdFinishedImage = false;
};
RTScene.indexToPixel = function(i) {
return {
x: i % this.width,
y: Math.floor(i / this.width)
};
};
RTScene.pixelToIndex = function(pixel) {
return pixel.x + pixel.y*this.width;
};
// Add the client to this scene. Generate an identifier for
// this client to use with this particular scene.
// We generate a new ID instead of using the user's acid so that
// we can have the same user working on the same scene on more
// than one client (e.g. if user is logged in on multiple computers)
RTScene.prototype.addClient = function(userAcid, socket, callback) {
var i = 0;
var clientID = userAcid + "-" + i;
// keep looping until we find a clientID that is not being used
// by this userAcid on this particular scene.
// This will become the client's identifier.
while (!!this.connectedClients[clientID]) {
i++;
var clientID = userAcid + "-" + i;
}
this.connectedClients[clientID] = {
id: clientID,
// what job the client is currently working on
job: null,
// the socket holding this client's connection
socket: socket,
// when slowTimer goes off, we add this client's work back to the queue
slowTimer: null,
};
callback(this, clientID);
};
RTScene.prototype.removeClient = function(clientID) {
if (this.connectedClients[clientID]) {
this.connectedClients[clientID] = undefined;
delete this.connectedClients[clientID];
}
};
RTScene.prototype.getClientByID = function(clientID) {
return this.connectedClients[clientID];
};
// Create all the jobs for this image
// Iterate over image pixels and split it into jobs
RTScene.prototype.createJobs = function() {
var jobID = 0;
var chunk = [];
var currentChunkSize = 0;
var startPixelIndex = 0;
for (var i = 0; i < this.height * this.width; i++) {
currentChunkSize += 1;
if (currentChunkSize === Job.CHUNK_SIZE) {
this.allJobs[jobID] = new Job(
jobID,
startPixelIndex,
startPixelIndex + currentChunkSize
);
currentChunkSize = 0;
jobID += 1;
startPixelIndex = i+1;
}
}
// add the last chunk of pixels in case it didn't make it to Job.CHUNK_SIZE
if (currentChunkSize > 0) {
this.allJobs[jobID] = new Job(
jobID,
startPixelIndex,
startPixelIndex + currentChunkSize
);
}
};
RTScene.prototype.pushJob = function(job) {
this.jobQueue.push(job);
};
RTScene.prototype.pushAllJobsToQueue = function() {
for (var id in this.allJobs) {
this.pushJob(this.allJobs[id]);
}
};
// Returns true if the client was assigned a job, otherwise false.
// We give the client the first job on the queue that hasn't already
// been completed. All jobs along the way are discarded because they
// are done.
RTScene.prototype.assignJobToClient = function(clientID, socket) {
var client = this.connectedClients[clientID];
while (this.jobQueue.length !== 0) {
var job = this.jobQueue.shift();
if (!this.jobResults[job.getID()]) { // if this job isn't already complete
// give the job to the client
client.job = job;
socket.emit('jobAssignment', {
sceneID: this.id,
job: client.job.toJSON(),
});
// add the client to the job to keep track of who is working on it
job.addWorkingClient(client);
// begin the clients timer
client.slowTimer = setTimeout((function() {
// add the slow client's job back to the queue so it can
// get picked up by someone else
this.pushJob(job);
}).bind(this), TOLERANCE_TIME);
return true;
}
}
return false;
};
// Returns true if job results are stored successfully, false otherwise.
RTScene.prototype.storeJobResults = function(clientID, jobID, results) {
if (this.allJobs[jobID]) { // if we got the results for a valid job
if (this.connectedClients[clientID]) {
clearTimeout(this.connectedClients[clientID].slowTimer);
}
this.jobResults[jobID] = results;
// now loop through all of the other clients that were working on this job
// and assign them new jobs
var job = this.allJobs[jobID];
var workingClients = job.getWorkingClients();
for (var i = 0; i < workingClients.length; i++) {
var workingClient = workingClients[i];
if (workingClient.job.getID() === jobID) {
if (workingClient.id !== clientID) {
clearTimeout(workingClient.slowTimer);
this.assignJobToClient(workingClient.id, workingClient.socket);
}
}
}
return true;
}
return false;
};
RTScene.prototype.areAllJobsComplete = function() {
for (var id in this.allJobs) {
var job = this.allJobs[id];
if (!this.jobResults[job.getID()]) {
// if a job isn't complete then return false
return false;
}
}
return true;
};
RTScene.prototype.broadcastFinishedImage = function() {
// Write to DB that the image is complete
Scene
.findOne({'_id': this.id})
.exec((function(err, dbScene) {
if (err || !dbScene) {
console.log("Could not find scene to write that rendering completed.");
} else {
dbScene.finishedRendering = true;
dbScene.save((function(err) {
if (err) {
console.log("Error writing that scene finished rendering.");
} else {
for (var clientID in this.connectedClients) {
var client = this.getClientByID(clientID);
client.socket.emit('finishedImage', { sceneID: this.id, });
}
}
}).bind(this));
}
}).bind(this));
// remove this scene from being scheduled for work
scenes[this.id] = undefined;
delete scenes[this.id];
};
RTScene.prototype.createFinishedImage = function(sceneID) {
var png = new PNG({
width: this.width,
height: this.height,
filterType: -1,
});
// Iterate over all pixels and write them to the rgb buffer
for (var jobID in this.jobResults) {
var pixels = this.jobResults[jobID];
for (var i = 0; i < pixels.length; i++) {
var p = pixels[i];
var idx = (png.width * (this.height-p.y) + p.x) << 2;
png.data[idx] = p.r * 255;
png.data[idx+1] = p.g * 255;
png.data[idx+2] = p.b * 255;
png.data[idx+3] = 255;
}
}
// Create PNG image
var ws = fs.createWriteStream(
__dirname + "/static/raytraced_images/" + sceneID + ".png"
);
png.pack().pipe(ws);
this.createdFinishedImage = true;
ws.on('close', (function(fd) {
this.broadcastFinishedImage();
}).bind(this));
};
// Schedule a scene to be rendered. It'll start once clients start connecting.
var addScene = function(sceneID, width, height) {
var scene = new RTScene(sceneID, width, height);
// split the scene into jobs, push them to the queue
scene.createJobs();
scene.pushAllJobsToQueue();
scenes[sceneID] = scene;
console.log("Current number of scenes being worked on: "
+ Object.keys(scenes).length);
};
// TODO: IMPORTANT: handle slow clients
io.sockets.on('connection', function(socket) {
socket.clientSceneConnections = [];
socket.on('connectToScene', function(data) {
var userAcid = data.acid;
var rtScene = scenes[data.sceneID];
if (!rtScene) {
console.log("Scene " + data.sceneID + " is no longer scheduled for work.");
// TODO: tell user that the scene is no longer available for connection
return;
}
console.log("Starting connection for user " + userAcid);
Scene
.findOne({ '_id': rtScene.id })
.exec(function(err, dbScene) {
if (err || !dbScene) {
console.log("Could not find scene");
} else {
if (!dbScene.startedRendering) {
dbScene.startedRendering = true;
}
dbScene.numUsersConnected = dbScene.numUsersConnected + 1;
dbScene.save(function(err) {
if (!err) {
// updated scene successfully
// add the client to this scene, and tell the client what
// its identifier is for this scene
rtScene.addClient(userAcid, socket, function(scene, clientID) {
socket.clientSceneConnections.push(
{ scene: scene, clientID: clientID });
socket.emit('identifier',
{ sceneID: scene.id, clientID: clientID });
});
} else {
// TODO: emit to the client that there was an error, so
// the button doesn't change to 'Connected'
console.log(err);
}
});
}
});
});
// When the client is registered (i.e. the client has received its
// identifier for a given scene), begin assigning jobs
socket.on('registeredClient', function(data) {
var clientID = data.clientID;
var scene = scenes[data.sceneID];
if (!scene) {
console.log("Scene " + data.sceneID + " is no longer scheduled for work.");
// TODO: tell user that the scene is no longer available for connection
return;
}
console.log("Client registered (" + clientID +
") for scene [" + data.sceneID + "]");
scene.assignJobToClient(clientID, socket);
});
// received result for a given client/scene pair
socket.on('jobResult', function(data) {
var clientID = data.clientID;
var scene = scenes[data.sceneID];
var jobID = data.jobID;
var pixels = data.pixels;
if (!scene) {
console.log("Scene " + data.sceneID + " is no longer scheduled for work.");
// TODO: tell user that the scene is no longer available for connection
return;
}
// store this client's results
scene.storeJobResults(clientID, jobID, pixels);
// assign a new job to this client
var assignedJob = scene.assignJobToClient(clientID, socket);
if (!assignedJob) {
// we didn't assign a new job, so check if all jobs are done.
if (scene.areAllJobsComplete()) {
if (!scene.createdFinishedImage) {
scene.createFinishedImage(scene.id);
}
}
}
});
// client is requesting a new job
socket.on('jobRequest', function(data) {
var clientID = data.clientID;
var scene = scenes[data.sceneID];
scene.assignJobToClient(clientID, socket);
});
// disconnection because the client clicked on 'disconnect'
socket.on('disconnectFromScene', function(data) {
var clientID = data.clientID;
var scene = scenes[data.sceneID];
var disconnectedClient = scene.getClientByID(clientID);
if (!disconnectedClient) {
// client no longer exists for some reason
return;
}
// add the client's job back to the queue
if (disconnectedClient.job) {
scene.pushJob(disconnectedClient.job);
}
// release memory for this client
scene.removeClient(clientID);
// remove this clientSceneConnection from the socket
for (var i = 0; i < socket.clientSceneConnections.length; i++) {
var connection = socket.clientSceneConnections[i];
if (connection.scene.id === data.sceneID &&
connection.clientID === clientID) {
socket.clientSceneConnections.splice(i, 1);
break;
}
}
// decrease the numUsersConnected for this scene
Scene
.findOne({ '_id' : data.sceneID })
.exec(function(err, dbScene) {
if (err || !dbScene) {
console.log("Could not find scene when trying to disconnect");
} else {
dbScene.numUsersConnected = dbScene.numUsersConnected - 1;
dbScene.save(function(err) {
if (!err) {
// no error
} else {
// TODO: emit to the client that there was an error disconnecting
// so the button doesn't change to 'Disconnected'
}
});
}
});
});
// disconnection because the client quit the browser or exited the app somehow
socket.on('disconnect', function() {
var sceneIDsToDisconnect = [];
// break all client/scene connections being held by this socket
for (var i = 0; i < socket.clientSceneConnections.length; i++) {
var clientSceneConnection = socket.clientSceneConnections[i];
var clientID = clientSceneConnection.clientID;
var scene = clientSceneConnection.scene;
sceneIDsToDisconnect.push(scene.id);
var brokenClient = scene.getClientByID(clientID);
if (!brokenClient) {
// client no longer exists for some reason
return;
}
// add the client's job back to the queue
if (brokenClient.job) {
scene.pushJob(brokenClient.job);
}
// release memory for this client
scene.removeClient(clientID);
}
Scene.update(
{ _id: { $in: sceneIDsToDisconnect}},
{ $inc: { numUsersConnected: -1}},
{ multi: true },
function(err) {
if (err) {
// nothing to do really, user already disconnected
}
}
);
});
});
return {
addScene: addScene
};
};