<commit_before>###

Copyright (c) 2011-2013  Voicious

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU Affero General Public License as published by the Free Software Foundation, either version
3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this
program. If not, see <http://www.gnu.org/licenses/>.

###

class Ws
    constructor : (@uid, @rid, @emitter) ->

    dance : (ws) =>
        @ws           = new WebSocket "ws://#{ws.host}:#{ws.port}"
        @ws.onopen    = @onOpen
        @ws.onmessage = @onMessage

    send : (data) =>
        @ws.send JSON.stringify data

    forward : (to, data) =>
        @send { type : 'forward' , params : { to : to , data : data } }

    onOpen : () =>
        auth =
            type   : 'authenticate'
            params :
                rid : @rid
                uid : @uid
        @send auth

    onMessage : (message) =>
        message = JSON.parse message.data
        @emitter.trigger message.type, message.params

    close : () =>
        do @ws.close

class PeerJs
    constructor     : (@uid, @rid, @emitter) ->

    dance           : (pjs) =>
        console.log pjs
        @pjs    = new Peer @uid, { host: pjs.host, port: pjs.port }
        @pjs.on 'connection', @onConnection

    onConnection    : (conn) =>
        peer = new PC conn, @emitter, @uid

    connect         : (peerUid) =>
        conn = @pjs.connect peerUid
        peer = new PC conn, @emitter, @uid

class PC
    constructor : (@conn, @emitter, from) ->
        @conn.on 'open', () =>
            @onOpen from
        @conn.on 'data', @onData

    onOpen      : (from) =>
        @send 'peer.uid', { uid: from }

    onData      : (data) =>
        if data.type is 'peer.uid'
            data.peer = @
        @emitter.trigger data.type, data

    send        : (type, data) =>
        @conn.send { type: type, data: data }

    close       : () =>
        do @conn.close

class Connections
    constructor : (@emitter, @uid, @rid, @wsPortal, @pjsPortal) ->
        @peers       = { }
        @ws          = new Ws @uid, @rid, @emitter
        @pjs         = new PeerJs @uid, @rid, @emitter
        @localStream = undefined
        @userMedia   =
            video : no
            audio : no
        @emitter.on 'message.sendtoall', @sendToAll
        @emitter.on 'message.sendToOneName', @sendToOneName
        @emitter.on 'message.sendToOneId', @sendToOneId
        @emitter.on 'authenticated', @initUser
        window.onClose = () =>
            do @ws.close
            for peer in @peers
                do peer.close

    modifyStream : () =>
        if @localStream isnt undefined
            do @localStream.stop
            for id, peer of @peers
                peer.rmLocalStream @localStream
                do peer.offerHandshake
                if @userMedia['video'] is no or @userMedia['audio'] is no
                    @sendStreamState id
        @localStream = undefined # erase old stream
        if @userMedia['video'] is yes or @userMedia['audio'] is yes
            do @enableCamera

    initUser : (event, data) =>
        window.Voicious.currentUser = data
        @emitter.trigger 'module.initialize', { }
        
    removePeer   : (peerId) =>
        do @peers[peerId].close
        @peers[peerId] = null
        delete @peers[peerId]

    toggleCamera : () =>
        @userMedia['video'] = !@userMedia['video']

    toggleMicro : () =>
        @userMedia['audio'] = !@userMedia['audio']

    dance : () =>
        @ws.dance @wsPortal
        @pjs.dance @pjsPortal
        @emitter.on 'peer.list', (event, data) =>
            for peer in data.peers
                @pjs.connect peer.id
                # @sendStreamState peer.id
        @emitter.on 'peer.uid', (event, data) =>
            @peers[data.id] = data.peer
            # @sendStreamState data.id
        @emitter.on 'ping', (event, data) =>
            @ws.send { type : 'pong' , params : { token : data.token } }
        @emitter.on 'peer.remove', (event, data) =>
            if @peers[data.id]?
                @removePeer data.id
                @emitter.trigger 'stream.remove', data

    sendToAll : (event, data) =>
        message
        if data.type?
            message = data
        else
            message = { type : 'chat.message' , params : { message : data } }
        for id of @peers
            @ws.forward id, message
    
    sendToOneName : (event, msg) =>
        message = { type : msg.type, params : msg.params }
        userId = @getIdFromUsername msg.to
        if userId is undefined
            @emitter.trigger 'chat.error', { text : 'kick: ' + msg.to + ' isn\'t in this room.' }
        else    
            @ws.forward userId, message

    sendToOneId : (event, msg) =>
        message = { type : msg.type, params : msg.params }<commit_msg>Delete useless code<commit_after>###

Copyright (c) 2011-2013  Voicious

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU Affero General Public License as published by the Free Software Foundation, either version
3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this
program. If not, see <http://www.gnu.org/licenses/>.

###

class Ws
    constructor : (@uid, @rid, @emitter) ->

    dance : (ws) =>
        @ws           = new WebSocket "ws://#{ws.host}:#{ws.port}"
        @ws.onopen    = @onOpen
        @ws.onmessage = @onMessage

    send : (data) =>
        @ws.send JSON.stringify data

    forward : (to, data) =>
        @send { type : 'forward' , params : { to : to , data : data } }

    onOpen : () =>
        auth =
            type   : 'authenticate'
            params :
                rid : @rid
                uid : @uid
        @send auth

    onMessage : (message) =>
        message = JSON.parse message.data
        @emitter.trigger message.type, message.params

    close : () =>
        do @ws.close

class PeerJs
    constructor     : (@uid, @rid, @emitter) ->

    dance           : (pjs) =>
        @pjs    = new Peer @uid, { host: pjs.host, port: pjs.port }
        @pjs.on 'connection', @onConnection

    onConnection    : (conn) =>
        peer = new PC conn, @emitter

    connect         : (peerUid) =>
        conn = @pjs.connect peerUid
        peer = new PC conn, @emitter

class PC
    constructor : (@conn, @emitter) ->
        @conn.on 'open', @onOpen
        @conn.on 'data', @onData

    onOpen      : () =>
        @send 'peer.uid', {}

    onData      : (data) =>
        if data.type is 'peer.uid'
            data.peer = @
        @emitter.trigger data.type, data

    send        : (type, data) =>
        @conn.send { type: type, data: data }

    close       : () =>
        do @conn.close

class Connections
    constructor : (@emitter, @uid, @rid, @wsPortal, @pjsPortal) ->
        @peers       = { }
        @ws          = new Ws @uid, @rid, @emitter
        @pjs         = new PeerJs @uid, @rid, @emitter
        @localStream = undefined
        @userMedia   =
            video : no
            audio : no
        @emitter.on 'message.sendtoall', @sendToAll
        @emitter.on 'message.sendToOneName', @sendToOneName
        @emitter.on 'message.sendToOneId', @sendToOneId
        @emitter.on 'authenticated', @initUser
        window.onClose = () =>
            do @ws.close
            for peer in @peers
                do peer.close

    modifyStream : () =>
        if @localStream isnt undefined
            do @localStream.stop
            for id, peer of @peers

                # peer.rmLocalStream @localStream
                # do peer.offerHandshake
                if @userMedia['video'] is no or @userMedia['audio'] is no
                    @sendStreamState id
        @localStream = undefined # erase old stream
        if @userMedia['video'] is yes or @userMedia['audio'] is yes
            do @enableCamera

    initUser : (event, data) =>
        window.Voicious.currentUser = data
        @emitter.trigger 'module.initialize', { }
        
    removePeer   : (peerId) =>
        do @peers[peerId].close
        @peers[peerId] = null
        delete @peers[peerId]

    toggleCamera : () =>
        @userMedia['video'] = !@userMedia['video']

    toggleMicro : () =>
        @userMedia['audio'] = !@userMedia['audio']

    dance : () =>
        @ws.dance @wsPortal
        @pjs.dance @pjsPortal
        @emitter.on 'peer.list', (event, data) =>
            for peer in data.peers
                @pjs.connect peer.id
        @emitter.on 'peer.uid', (event, data) =>
            @peers[data.peer.peer] = data.peer
            @sendStreamState data.peer.peer
        @emitter.on 'peer.remove', (event, data) =>
            if @peers[data.id]?
                @removePeer data.id
                @emitter.trigger 'stream.remove', data
        @emitter.on 'ping', (event, data) =>
            @ws.send { type : 'pong' , params : { token : data.token } }

    sendToAll : (event, data) =>
        message
        if data.type?
            message = data
        else
            message = { type : 'chat.message' , params : { message : data } }
        for id of @peers
            @ws.forward id, message
    
    sendToOneName : (event, msg) =>
        message = { type : msg.type, params : msg.params }
        userId = @getIdFromUsername msg.to
        if userId is undefined
            @emitter.trigger 'chat.error', { text : 'kick: ' + msg.to + ' isn\'t in this room.' }
        else    
            @ws.forward userId, message

    sendToOneId : (event, msg) =>
        message = { type : msg.type, params : msg.params }
{'fraction_non_alphanumeric': 0.11980892961590954, 'fraction_numerical': 0.0017547280171573405, 'mean_word_length': 1.8037715222738453, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4424189', 'n_tokens_mistral': 3098, 'n_tokens_neox': 2977, 'n_words': 903}