<commit_before>var fs               = require('fs')
,   util             = require('util')
,   events           = require('events')
,   moment           = require('moment')
,   Playlist         = require('./api/Playlist')
,   Melted           = require('./api/Melted')
,   Media            = require('./api/Media')
,   ScheduledMedia   = require('./api/ScheduledMedia')
,   mvcp_server      = require('./drivers/mvcp/mvcp-driver')
,   playlists_driver = require('./drivers/playlists/playlists-driver')
,   status_driver    = require('./drivers/status/pubsub')
,   utils            = require('./utils')
,   config           = require('mbc-common').config.Mosto.General
,   _                = require('underscore')
,   hearbeats        = require('./heartbeats')
,   models           = require('./models/Mosto')
;

function mosto(customConfig) {

    /** CONFIGURATION */
    this.config = customConfig || config;
    this.server = undefined;
    this.server_started = false;
    this.pl_driver = undefined;
    this.status_driver = undefined;

    /* MODULES */
    this.heartbeats = undefined;
    this.playlists = undefined;
}

mosto.prototype.inTimeWindow = function(obj) {
    // expects obj.start and obj.end to exist and be moment()s
    return (obj.end > this.timeWindow.start && obj.start < this.timeWindow.end);
}

mosto.prototype.initDriver = function() {

    console.log("mbc-mosto: [INFO] Initializing playlists driver");

    this.pl_driver.on('create', function(playlist) {
        var now = moment();

        if(!this.inTimeWindow(playlist))
            return;

        this.playlists.addPlaylist(playlist);
    });

    self.driver.on ("update", function(playlist) {
        self.fetcher.updatePlaylist( playlist, self.fetcher);
    } );
    self.driver.on ("delete", function(playlist) {
        self.fetcher.removePlaylist( playlist, self.fetcher);
    } );

    self.pl_driver.start();
};

mosto.prototype.stopDriver = function() {

    console.log("mbc-mosto: [INFO] Stopping playlists driver");

    self.driver.stop();

    self.driver.removeAllListeners("create");
    self.driver.removeAllListeners("update");
    self.driver.removeAllListeners("delete");
};

mosto.prototype.startMvcpServer = function(callback) {
    var result = self.server.initServer();
    result.then(function() {
        console.log("mbc-mosto: [INFO] MVCP server started");
        self.server_started = true;
        if (callback !== undefined) {
            callback();
        }
    }, function(err) {
        var e = new Error("mbc-mosto: [ERROR] Error starting MVCP server: " + err + ".\nRetrying in 2 seconds...");
        console.error(e);
        setTimeout(function() {
            self.startMvcpServer(callback);
        }, 2000);
    });
};

console.log("mbc-mosto: [INFO] Starting mbc-mosto... ") ;

mosto.prototype.init = function( melted, callback) {
    console.log("mbc-mosto: [INFO] Init mbc-mosto... ") ;
    var self = this;
    /*
     * inicializar los drivers
     * inicializar los modelos backbone
     * inicializar los heartbeats
     * linkear los eventos entre pl driver y backbone driver
     * linkear heartbeat con status driver
     */
    function startall() {
        self.server        = new mvcp_server(self.config.mvcp_server);
        self.pl_driver     = new playlists_driver(self.config.playlist_server);
        self.status_driver = new status_driver();
        self.playlists     = models.Playlists;

        // melted is started, get playlist
        self.playlists.get('melted_medias').fetch();

        self.heartbeats.on('frameStatus', function(current_frame) {
            var now = moment();
            var current = self.playlists.get('melted_medias').find(function(media) {
                return media.get('start') <= now && media.get('end') >= now;
            });
            self.status_driver.setStatusClip(StatusClip(
                current.id,
                current.get('playlist_order'),
                current.playlist.id,
                current.get('fps'),
                current_frame,
                current.get('total_frames'),
            ));
        });
        self.heartbeats.on('clipStatus', function(media) {
            var melted_medias = self.playlists.get('melted_medias');
            var playlists = self.playlists.get('playlists');
            var status = {
                clip:{
                    previous: undefined,
                    current: undefined,
                    next: undefined,
                },
                show: {
                    previous: undefined,
                    current: undefined,
                    next: undefined,
                },
                position: 0,
                clips: melted_medias.toJSON(),
                status: none,
            };
            /* clips */
            var index = melted_medias.indexOf(media);
            status.clip.current = media.toJSON();
            if( index > 0 )
                status.clip.previous = melted_medias.at(index-1).toJSON();
            if( index < melted_medias.length )
                status.clip.next = melted_medias.at(index+1).toJSON();
            /* shows */
            var playlist = playlists.find(function(pl) {
                return pl.indexOf(media) >= 0;
            });
            index = playlists.indexOf(playlist);
            status.show.current = playlist.toJSON();
            if( index > 0 )
                status.show.previous = playlists.at(index-1).toJSON();
            if( index < playlists.length )
                status.show.next = playlists.at(index+1).toJSON();
            self.status_driver.setStatus(status);
        });

        self.pl_driver.on('update');
        self.pl_driver.on('delete');

        self.initDriver();
        self.heartbeats.init();

        self.startMvcpServer( function() {
            self.player.play( self.player );
            if (callback) callback();
        } );
    }<commit_msg>playlist driver update handler<commit_after>var fs               = require('fs')
,   util             = require('util')
,   events           = require('events')
,   moment           = require('moment')
,   Playlist         = require('./api/Playlist')
,   Melted           = require('./api/Melted')
,   Media            = require('./api/Media')
,   ScheduledMedia   = require('./api/ScheduledMedia')
,   mvcp_server      = require('./drivers/mvcp/mvcp-driver')
,   playlists_driver = require('./drivers/playlists/playlists-driver')
,   status_driver    = require('./drivers/status/pubsub')
,   utils            = require('./utils')
,   config           = require('mbc-common').config.Mosto.General
,   _                = require('underscore')
,   hearbeats        = require('./heartbeats')
,   models           = require('./models/Mosto')
;

function mosto(customConfig) {

    /** CONFIGURATION */
    this.config = customConfig || config;
    this.server = undefined;
    this.server_started = false;
    this.pl_driver = undefined;
    this.status_driver = undefined;

    /* MODULES */
    this.heartbeats = undefined;
    this.playlists = undefined;
}

mosto.prototype.inTimeWindow = function(obj) {
    // expects obj.start and obj.end to exist and be moment()s
    return (obj.end > this.timeWindow.start && obj.start < this.timeWindow.end);
}

mosto.prototype.initDriver = function() {

    console.log("mbc-mosto: [INFO] Initializing playlists driver");

    this.pl_driver.on('create', function(playlist) {
        var now = moment();

        if(!this.inTimeWindow(playlist))
            return;

        this.playlists.addPlaylist(playlist);
    });

    this.pl_driver.on('update', function(playlist) {
        if(!this.inTimeWindow(playlist))
            return this.playlists.removePlaylist(playlist);
        return this.playlists.addPlaylist(playlist);
    });

    self.driver.on ("delete", function(playlist) {
        self.fetcher.removePlaylist( playlist, self.fetcher);
    } );

    self.pl_driver.start();
};

mosto.prototype.stopDriver = function() {

    console.log("mbc-mosto: [INFO] Stopping playlists driver");

    self.driver.stop();

    self.driver.removeAllListeners("create");
    self.driver.removeAllListeners("update");
    self.driver.removeAllListeners("delete");
};

mosto.prototype.startMvcpServer = function(callback) {
    var result = self.server.initServer();
    result.then(function() {
        console.log("mbc-mosto: [INFO] MVCP server started");
        self.server_started = true;
        if (callback !== undefined) {
            callback();
        }
    }, function(err) {
        var e = new Error("mbc-mosto: [ERROR] Error starting MVCP server: " + err + ".\nRetrying in 2 seconds...");
        console.error(e);
        setTimeout(function() {
            self.startMvcpServer(callback);
        }, 2000);
    });
};

console.log("mbc-mosto: [INFO] Starting mbc-mosto... ") ;

mosto.prototype.init = function( melted, callback) {
    console.log("mbc-mosto: [INFO] Init mbc-mosto... ") ;
    var self = this;
    /*
     * inicializar los drivers
     * inicializar los modelos backbone
     * inicializar los heartbeats
     * linkear los eventos entre pl driver y backbone driver
     * linkear heartbeat con status driver
     */
    function startall() {
        self.server        = new mvcp_server(self.config.mvcp_server);
        self.pl_driver     = new playlists_driver(self.config.playlist_server);
        self.status_driver = new status_driver();
        self.playlists     = models.Playlists;

        // melted is started, get playlist
        self.playlists.get('melted_medias').fetch();

        self.heartbeats.on('frameStatus', function(current_frame) {
            var now = moment();
            var current = self.playlists.get('melted_medias').find(function(media) {
                return media.get('start') <= now && media.get('end') >= now;
            });
            self.status_driver.setStatusClip(StatusClip(
                current.id,
                current.get('playlist_order'),
                current.playlist.id,
                current.get('fps'),
                current_frame,
                current.get('total_frames'),
            ));
        });
        self.heartbeats.on('clipStatus', function(media) {
            var melted_medias = self.playlists.get('melted_medias');
            var playlists = self.playlists.get('playlists');
            var status = {
                clip:{
                    previous: undefined,
                    current: undefined,
                    next: undefined,
                },
                show: {
                    previous: undefined,
                    current: undefined,
                    next: undefined,
                },
                position: 0,
                clips: melted_medias.toJSON(),
                status: none,
            };
            /* clips */
            var index = melted_medias.indexOf(media);
            status.clip.current = media.toJSON();
            if( index > 0 )
                status.clip.previous = melted_medias.at(index-1).toJSON();
            if( index < melted_medias.length )
                status.clip.next = melted_medias.at(index+1).toJSON();
            /* shows */
            var playlist = playlists.find(function(pl) {
                return pl.indexOf(media) >= 0;
            });
            index = playlists.indexOf(playlist);
            status.show.current = playlist.toJSON();
            if( index > 0 )
                status.show.previous = playlists.at(index-1).toJSON();
            if( index < playlists.length )
                status.show.next = playlists.at(index+1).toJSON();
            self.status_driver.setStatus(status);
        });

        self.pl_driver.on('delete');

        self.initDriver();
        self.heartbeats.init();

        self.startMvcpServer( function() {
            self.player.play( self.player );
            if (callback) callback();
        } );
    }
{'fraction_non_alphanumeric': 0.1331103678929766, 'fraction_numerical': 0.002173913043478261, 'mean_word_length': 1.963577799801784, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2924015', 'n_tokens_mistral': 3487, 'n_tokens_neox': 3354, 'n_words': 601}