forked from StreamMachine/StreamMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslave_io.js
195 lines (181 loc) · 5.93 KB
/
slave_io.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
var SlaveIO, Socket,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Socket = require("socket.io-client");
module.exports = SlaveIO = (function(_super) {
__extends(SlaveIO, _super);
function SlaveIO(slave, _log, opts) {
this.slave = slave;
this._log = _log;
this.opts = opts;
this.connected = false;
this.io = null;
this.id = null;
this.attempts = 1;
this.masterIndex = -1;
this.forceDisconnect = false;
this._log.debug("Connecting to master at ", {
master: this.opts.master
});
this._start();
}
SlaveIO.prototype.once_connected = function(cb) {
if (this.connected) {
return cb(null, this.io);
} else {
return this.once("connected", (function(_this) {
return function() {
return cb(null, _this.io);
};
})(this));
}
};
SlaveIO.prototype._start = function() {
var master;
master = this.opts.master;
if (typeof master !== "string") {
if (master.length !== (this.masterIndex + 1)) {
this.masterIndex = this.masterIndex + 1;
}
if (this.attempts >= this.opts.retry) {
this.attempts = 1;
}
master = master[this.masterIndex];
}
this.disconnect();
return this._connect(master);
};
SlaveIO.prototype.disconnect = function() {
var _ref;
this.forceDisconnect = true;
return (_ref = this.io) != null ? _ref.disconnect() : void 0;
};
SlaveIO.prototype._connect = function(master) {
this._log.info("Slave trying connection to master " + master);
this.io = Socket.connect(master, {
reconnection: true,
timeout: this.opts.timeout
});
this.io.on("connect", (function(_this) {
return function() {
var pingTimeout;
_this._log.debug("Slave in _onConnect.");
pingTimeout = setTimeout(function() {
return _this._log.error("Failed to get master OK ping.");
}, 1000);
return _this.io.emit("ok", function(res) {
clearTimeout(pingTimeout);
if (res === "OK") {
_this._log.debug("Connected to master.");
_this.id = _this.io.io.engine.id;
_this.connected = true;
return _this.emit("connected");
} else {
return _this._log.error("Master OK ping response invalid: " + res);
}
});
};
})(this));
this.io.on("connect_error", (function(_this) {
return function(err) {
if (err.code = ~/ECONNREFUSED/) {
_this._log.info("Slave connection refused: " + err);
} else {
_this._log.info("Slave got connection error of " + err, {
error: err
});
console.log("got connection error of ", err);
}
_this.attempts = _this.attempts + 1;
if (_this.isNecesaryReconnect()) {
return _this._start();
}
};
})(this));
this.io.on("disconnect", (function(_this) {
return function() {
_this.connected = false;
_this._log.debug("Disconnected from master.");
return _this.emit("disconnect");
};
})(this));
this.io.on("config", (function(_this) {
return function(config) {
return _this.slave.configureStreams(config.streams);
};
})(this));
this.io.on("status", (function(_this) {
return function(cb) {
return _this.slave._streamStatus(cb);
};
})(this));
this.io.on("should_shutdown", (function(_this) {
return function(cb) {
return _this.slave._shutdown(cb);
};
})(this));
this.io.on("audio", (function(_this) {
return function(obj) {
obj.chunk.data = Buffer.from(obj.chunk.data);
obj.chunk.ts = new Date(obj.chunk.ts);
return _this.emit("audio:" + obj.stream, obj.chunk);
};
})(this));
return this.io.on("hls_snapshot", (function(_this) {
return function(obj) {
var k, s, _i, _j, _len, _len1, _ref, _ref1, _ref2;
_ref1 = ((_ref = obj.snapshot) != null ? _ref.segments : void 0) || [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
s = _ref1[_i];
_ref2 = ['ts', 'end_ts', 'ts_actual', 'end_ts_actual'];
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
k = _ref2[_j];
if (s[k]) {
s[k] = new Date(s[k]);
}
}
}
return _this.emit("hls_snapshot:" + obj.stream, obj.snapshot);
};
})(this));
};
SlaveIO.prototype.isNecesaryReconnect = function() {
var master;
master = this.opts.master;
if (typeof master !== "string") {
if (this.attempts < this.opts.retry) {
return false;
} else if (master.length !== (this.masterIndex + 1)) {
return true;
}
}
return false;
};
SlaveIO.prototype.vitals = function(key, cb) {
return this.io.emit("vitals", key, cb);
};
SlaveIO.prototype.hls_snapshot = function(key, cb) {
return this.io.emit("hls_snapshot", key, (function(_this) {
return function(err, snapshot) {
var k, s, _i, _j, _len, _len1, _ref, _ref1;
_ref = (snapshot != null ? snapshot.segments : void 0) || [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
s = _ref[_i];
_ref1 = ['ts', 'end_ts', 'ts_actual', 'end_ts_actual'];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
k = _ref1[_j];
if (s[k]) {
s[k] = new Date(s[k]);
}
}
}
return cb(err, snapshot);
};
})(this));
};
SlaveIO.prototype.log = function(obj) {
return this.io.emit("log", obj);
};
return SlaveIO;
})(require("events").EventEmitter);
//# sourceMappingURL=slave_io.js.map