- constructor (config) {
- super();
-
- this.screens = [];
- this.renderers = [];
-
- Object.assign(this, config || {});
- }
-
- // Here's where things get started.
- run () {
- this._startServer();
- this._bindEvents();
- }
-
- // Creates a socket, server based on the configuration. Emits the
- // listening event when ready.
- _startServer () {
- const server = Net.createServer();
- this.server = server;
- server.listen(this.port, function () {
- this.emit('listening', {
- data: {
- server: server
- }
- });
- }.bind(this));
- }
-
- _bindEvents () {
- // Send the error event all the way up.
- this.server.on('error', function (err) {
- this.emit('error', {
- data: {
- error: err
- }
- });
- }.bind(this));
-
- // Send the error event all the way up.
- this.server.on('connection', function (socket) {
- this._renderScreen(socket);
- }.bind(this));
- }
-
- // Obtains viewport size, and initializes a random screen with a random
- // renderer, setting the required interval to draw.
- _renderScreen (socket) {
- let connectionData = {
- width: null,
- height: null,
- modulation: 0,
- screen: this._getScreen(),
- renderer: this._getRenderer(),
- socket: socket
- };
- let interval = null;
-
- socket.write(kNAWSRequest);
-
- socket.on('data', function (data) {
- if (data.slice(0, 6).compare(kNAWSResponse) === 0) {
- connectionData.width = Util.parse16BitBuffer(data.slice(6, 8));
- connectionData.height = Util.parse16BitBuffer(data.slice(8, 10));
-
- socket.write('\x1B[2J'); // Clear the Screen (CSI 2 J)
- interval = setInterval(this._writeMessage.bind(this, connectionData), this.frequency);
- }
-
- if (data.compare(kEscape) === 0) {
- socket.write('\n');
- clearInterval(interval);
- socket.end();
- }
- }.bind(this));
- }
-
- // Resets the cursor, gets a frame and sends it to the socket.
- _writeMessage (connectionData) {
- let payload = connectionData.screen(connectionData.modulation, connectionData.width, connectionData.height, connectionData.renderer);
- let message = `\x1B[1;1H${payload}`; // reset cursor position before payload
-
- connectionData.modulation = (connectionData.modulation + this.modulation) % 256;
- connectionData.socket.write(message);
- }
-
- _getScreen () {
- return Util.pickRandom(this.screens);
- }
-
- _getRenderer () {
- return Util.pickRandom(this.renderers);
- }
-};
+ constructor(config = {}) {
+
+ super();
+
+ this.screens = [];
+ this.renderers = [];
+
+ // Defaults.
+ this.port = 9999;
+ this.frequency = 333;
+ this.modulation = 5;
+
+ Object.assign(this, config);
+ }
+
+ /**
+ * Main entry point, initializes the server and binds events for connections
+ *
+ * @function run
+ * @instance
+ * @memberof TomatoSauce
+ */
+ run() {
+
+ this._startServer();
+ this._bindEvents();
+ }
+
+ // Creates a socket, server based on the configuration. Emits the
+ // listening event when ready.
+
+ _startServer() {
+
+ const server = Net.createServer();
+ this.server = server;
+ server.listen(this.port, () => {
+
+ this.emit('listening', {
+ data: {
+ server
+ }
+ });
+ });
+ }
+
+ // Binds the connection and error events
+
+ _bindEvents() {
+
+ // Send the error event all the way up.
+ this.server.on('error', (error) => {
+
+ this.emit('error', {
+ data: {
+ error
+ }
+ });
+ });
+
+ // Send the error event all the way up.
+ this.server.on('connection', (socket) => {