-
Notifications
You must be signed in to change notification settings - Fork 0
/
LectureRuntime.js
151 lines (135 loc) · 3.58 KB
/
LectureRuntime.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
// Lecture Runtime
// Driver for running a lecture on the http.ServerResponse
// Author: Michael Timbrook <mpt2360@rit.edu>
// Create a new runtime
function LectureRuntime(Lname, desc, lecture) {
this.name = Lname;
this.description = desc;
this.sittingClients = {};
this.numberOfClients = 0;
this.lecture = lecture;
this.stream = null;
this.screenData = null;
console.log("New Runtime: " + this.name);
}
LectureRuntime.prototype.addClient = function(client_id, socket) {
if (!(client_id in this.sittingClients)) {
this.sittingClients[client_id] = socket;
this.numberOfClients++;
}
var data = this.lecture.data;
console.log("Sending "+this.lecture.data.length+" diffs to client");
socket.emit('update-info', {
info : 'start',
count : data.length
});
for (var i = 0; i < data.length; i++) {
update = {
index : i,
info : 'update',
action : data[i].action,
x : data[i].x,
y : data[i].y
}
socket.emit('update-info', update);
}
socket.emit('update-info', {
info : 'end'
});
};
LectureRuntime.prototype.removeClient = function(client_id) {
if (this.sittingClients && client_id in this.sittingClients) {
delete this.sittingClients[client_id];
this.numberOfClients--;
}
};
LectureRuntime.prototype.getClients = function() {
var clients = [];
var i = 0;
for(key in this.sittingClients) {
clients[i] = this.sittingClients[key];
i++;
}
return clients;
}
LectureRuntime.prototype.setStream = function(socket) {
var that = this;
this.stream = socket;
this.stream.on('disconnect', function(){
delete that.stream;
});
this.stream.emit('status', {status : "ready"});
this.stream.emit('get-size');
this.stream.emit('set-cord', function(data){
that.screenData = data;
that.updateScreenSize();
});
this.stream.on('image', function(data){
that.sendToClients(data, 'image');
});
this.stream.on('clear-screen', function(data){
that.sendToClients(null, "clear-screen");
});
this.lecture.data = [];
this.lecture.save();
this.sendToClients(null, "clear-screen");
this.start();
};
LectureRuntime.prototype.imageTransfer = function(data) {
for (_id in this.sittingClients) {
var socket = this.sittingClients[_id];
socket.emit('image', data);
}
}
LectureRuntime.prototype.updateScreenSize = function() {
var that = this;
for (_id in this.sittingClients) {
var socket = this.sittingClients[_id];
socket.emit('set-size', that.screenData );
}
}
LectureRuntime.prototype.getStreamInfo = function() {
if (this.stream) {
return this.stream.name;
} else {
return null;
}
}
LectureRuntime.prototype.start = function() {
var that = this;
// Push updated from stream
this.stream.on('update', function(data) {
// Save data to lecture
that.lecture.data.push(data);
that.lecture.save();
that.sendToClients(data, 'update');
});
};
LectureRuntime.prototype.stop = function() {
console.log("Stopping " + this.name + "...");
// Save and clean up lecture data, disconnect clients
for (_id in this.sittingClients) {
var socket = this.sittingClients[_id];
socket.emit('termination', {
message: 'Stream is ending',
status: 'clean'
});
}
// Remove referances
delete this.sittingClients;
delete this.numberOfClients;
delete this.lecture;
delete this.name;
console.log("Stopped");
}
// Running loop
LectureRuntime.prototype.sendToClients = function(data, action) {
// if (this.numberOfClients > 0)
// console.log(this.name + " pushing updates to " + this.numberOfClients + " clients");
for (_id in this.sittingClients) {
var socket = this.sittingClients[_id];
socket.emit(action, data);
}
}
// 'Return' the module
module.exports = LectureRuntime;