<commit_before>/*
 * Class Room represents a Lynckia Room. It will handle the connection, local stream publication and 
 * remote stream subscription.
 * Typical Room initialization would be:
 * var room = Room({token:'<PASSWORD>'});
 * It also handles RoomEvents and StreamEvents. For example:
 * Event 'room-connected' points out that the user has been successfully connected to the room.
 * Event 'room-disconnected' shows that the user has been already disconnected.
 * Event 'stream-added' indicates that there is a new stream available in the room.
 * Event 'stream-removed' shows that a previous available stream has been removed from the room.
 */
var Room = function (spec) {
    "use strict";
    var that = EventDispatcher(spec), connectSocket, sendMessageSocket, sendSDPSocket, removeStream, DISCONNECTED = 0, CONNECTING = 1, CONNECTED = 0; 
    that.streams = {};
    that.roomID = '';
    that.socket = {};
    that.state = DISCONNECTED;
    L.HTML.init();

    // Private functions

    that.addEventListener("room-disconnected", function(evt) {
        var index;
        that.state = DISCONNECTED;

        // Remove all streams
        for (index in that.streams) {
            if (that.streams.hasOwnProperty(index)) {
                var stream = that.streams[index];
                removeStream(stream);
                delete that.streams[index];
                var evt = StreamEvent({type: 'stream-removed', stream: stream});
                that.dispatchEvent(evt);
            }
        }
        that.streams = {};

        spec.publisher.pc.close();
        spec.publisher.pc = undefined;

        try {
            that.socket.disconnect();
        } catch (error) {
            L.Logger.debug("Socket already disconnected");
        }
        that.socket = undefined;
    });

    removeStream = function(stream) {
        if (stream.stream !== undefined) {
            // Remove HTML element
            L.HTML.removeVideo(stream.streamID);
            // Close PC stream
            stream.pc.close();
        }
    }

    connectSocket = function (token, callback, error) {
        that.socket = io.connect("hpcm.dit.upm.es:8080", {reconnect: false});
        //that.socket = io.connect(token.host, {reconnect: false});

        that.socket.on('onAddStream', function (id) {
            var stream = Stream({streamID: id})
            that.streams[id] = stream;
            var evt = StreamEvent({type: 'stream-added', stream: stream});
            that.dispatchEvent(evt);
        });

        that.socket.on('onRemoveStream', function (id) {
            var stream = that.streams[id];
            delete that.streams[id];
            removeStream(stream);
            var evt = StreamEvent({type: 'stream-removed', stream: stream});
            that.dispatchEvent(evt);
        });

        that.socket.on('disconnect', function (argument) {
            L.Logger.info("Socket disconnected");
            var disconnectEvt = RoomEvent({type: "room-disconnected"});
            that.dispatchEvent(disconnectEvt);
        });

        sendMessageSocket('token', token, callback, error);
    };

    sendMessageSocket = function (type, msg, callback, error) {
        that.socket.emit(type, msg, function (respType, msg) {
            if (respType === "success") {
                if (callback !== undefined) {
                    callback(msg);
                }
            } else {
                if (error !== undefined) {7
                    error(msg);
                }
            }
        });
    };

    sendSDPSocket = function (type, state, sdp, callback) {
        that.socket.emit(type, state, sdp, function (response, respCallback) {
            callback(response, respCallback);
        });
    };

    // Public functions

    // It stablishes a connection to the room. Once it is done it throws a RoomEvent("room-connected")
    that.connect = function () {

        if (that.state !== DISCONNECTED) {
            L.Logger.error("Room already connected");
        }

        // 1- Connect to Erizo-Controller
        var streamList = [];
        var token = L.Base64.decodeBase64(spec.token);
        that.state = CONNECTING;
        connectSocket(JSON.parse(token), function (streams) {
            var index = 0, stream, streamList = [];
            that.state = CONNECTED;

            // 2- Retrieve list of streams
            for (index in streams) {
                if (streams.hasOwnProperty(index)) {
                    stream = Stream({streamID: streams[index], stream: undefined});
                    streamList.push(stream);
                    that.streams[streams[index]] = stream;
                }
            }

            // 3 - Update RoomID
            that.roomID = '';
<commit_msg>Now it updates the room id<commit_after>/*
 * Class Room represents a Lynckia Room. It will handle the connection, local stream publication and 
 * remote stream subscription.
 * Typical Room initialization would be:
 * var room = Room({token:'<PASSWORD>'});
 * It also handles RoomEvents and StreamEvents. For example:
 * Event 'room-connected' points out that the user has been successfully connected to the room.
 * Event 'room-disconnected' shows that the user has been already disconnected.
 * Event 'stream-added' indicates that there is a new stream available in the room.
 * Event 'stream-removed' shows that a previous available stream has been removed from the room.
 */
var Room = function (spec) {
    "use strict";
    var that = EventDispatcher(spec), connectSocket, sendMessageSocket, sendSDPSocket, removeStream, DISCONNECTED = 0, CONNECTING = 1, CONNECTED = 0; 
    that.streams = {};
    that.roomID = '';
    that.socket = {};
    that.state = DISCONNECTED;
    L.HTML.init();

    // Private functions

    that.addEventListener("room-disconnected", function(evt) {
        var index;
        that.state = DISCONNECTED;

        // Remove all streams
        for (index in that.streams) {
            if (that.streams.hasOwnProperty(index)) {
                var stream = that.streams[index];
                removeStream(stream);
                delete that.streams[index];
                var evt = StreamEvent({type: 'stream-removed', stream: stream});
                that.dispatchEvent(evt);
            }
        }
        that.streams = {};

        spec.publisher.pc.close();
        spec.publisher.pc = undefined;

        try {
            that.socket.disconnect();
        } catch (error) {
            L.Logger.debug("Socket already disconnected");
        }
        that.socket = undefined;
    });

    removeStream = function(stream) {
        if (stream.stream !== undefined) {
            // Remove HTML element
            L.HTML.removeVideo(stream.streamID);
            // Close PC stream
            stream.pc.close();
        }
    }

    connectSocket = function (token, callback, error) {
        //that.socket = io.connect("hpcm.dit.upm.es:8080", {reconnect: false});
        that.socket = io.connect(token.host, {reconnect: false});

        that.socket.on('onAddStream', function (id) {
            var stream = Stream({streamID: id})
            that.streams[id] = stream;
            var evt = StreamEvent({type: 'stream-added', stream: stream});
            that.dispatchEvent(evt);
        });

        that.socket.on('onRemoveStream', function (id) {
            var stream = that.streams[id];
            delete that.streams[id];
            removeStream(stream);
            var evt = StreamEvent({type: 'stream-removed', stream: stream});
            that.dispatchEvent(evt);
        });

        that.socket.on('disconnect', function (argument) {
            L.Logger.info("Socket disconnected");
            var disconnectEvt = RoomEvent({type: "room-disconnected"});
            that.dispatchEvent(disconnectEvt);
        });

        sendMessageSocket('token', token, callback, error);
    };

    sendMessageSocket = function (type, msg, callback, error) {
        that.socket.emit(type, msg, function (respType, msg) {
            if (respType === "success") {
                if (callback !== undefined) {
                    callback(msg);
                }
            } else {
                if (error !== undefined) {7
                    error(msg);
                }
            }
        });
    };

    sendSDPSocket = function (type, state, sdp, callback) {
        that.socket.emit(type, state, sdp, function (response, respCallback) {
            callback(response, respCallback);
        });
    };

    // Public functions

    // It stablishes a connection to the room. Once it is done it throws a RoomEvent("room-connected")
    that.connect = function () {

        if (that.state !== DISCONNECTED) {
            L.Logger.error("Room already connected");
        }

        // 1- Connect to Erizo-Controller
        var streamList = [];
        var token = L.Base64.decodeBase64(spec.token);
        that.state = CONNECTING;
        connectSocket(JSON.parse(token), function (response) {
            var index = 0, stream, streamList = [], streams, roomId;
            streams = response.streams;
            roomId = response.id;
            that.state = CONNECTED;

            // 2- Retrieve list of streams
            for (index in streams) {
                if (streams.hasOwnProperty(index)) {
                    stream = Stream({streamID: streams[index], stream: undefined});
                    streamList.push(stream);
                    that.streams[streams[index]] = stream;
                }
            }

            // 3 - Update RoomID
            that.roomID = roomId;

{'fraction_non_alphanumeric': 0.11758595616495274, 'fraction_numerical': 0.003323984626571102, 'mean_word_length': 1.9497549019607843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5788912', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2502, 'n_words': 678}