<s> import datetime <EOL> import json <EOL> import thread <EOL> import time <EOL> import uuid <EOL> from Queue import Queue <EOL> from datetime import datetime <EOL> from flask import Flask , render_template , request , session , Response , stream_with_context <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> app . debug = True <EOL> heartbeat_interval = <NUM_LIT:10> <EOL> class ExtensibleJSONEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . to_JSON ( ) <EOL> return super ( ExtensibleJSONEncoder , self ) . default ( obj ) <EOL> def jsonify ( * args , ** kwargs ) : <EOL> indent = None <EOL> status = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:200> ) <EOL> mime = kwargs . pop ( '<STR_LIT>' , '<STR_LIT:application/json>' ) <EOL> data = args [ <NUM_LIT:0> ] if args else dict ( kwargs ) <EOL> if app . config [ '<STR_LIT>' ] and not request . is_xhr : <EOL> indent = <NUM_LIT:2> <EOL> dump = json . dumps ( data , indent = indent ) <EOL> return app . response_class ( dump , status = status , mimetype = mime ) <EOL> app . json_encoder = ExtensibleJSONEncoder <EOL> class WebRTCUser ( object ) : <EOL> id = None <EOL> username = None <EOL> connected_at = None <EOL> namespace = None <EOL> def __init__ ( self , id , username = None ) : <EOL> self . id = id <EOL> self . username = username <EOL> self . connected_at = datetime . utcnow ( ) <EOL> self . rooms = [ ] <EOL> self . messages = Queue ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . id , self . username ) <EOL> def add_stream ( self , stream_id ) : <EOL> self . streams [ stream_id ] = dict ( queue = Queue ( ) ) <EOL> def emit ( self , event = None , ** data ) : <EOL> if event : <EOL> data [ '<STR_LIT>' ] = event <EOL> payload = data . pop ( '<STR_LIT>' , None ) <EOL> if not payload : <EOL> payload = json . dumps ( data ) <EOL> if payload != '<STR_LIT>' : <EOL> print '<STR_LIT>' % ( payload , self ) <EOL> self . messages . put_nowait ( payload ) <EOL> def emit_to_rooms ( self , event , ** data ) : <EOL> for room in self . rooms : <EOL> room . emit ( event , ** data ) <EOL> def to_JSON ( self ) : <EOL> return dict ( <EOL> id = self . id , <EOL> username = self . username <EOL> ) <EOL> class WebRTCRoom ( object ) : <EOL> name = '<STR_LIT>' <EOL> encryption = '<STR_LIT>' <EOL> browser = '<STR_LIT>' <EOL> browser_version = '<STR_LIT>' <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . users = [ ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> def user_join ( self , user ) : <EOL> print "<STR_LIT>" % ( user , self . name ) <EOL> if user in self . users : <EOL> return <EOL> self . users . append ( user ) <EOL> user . rooms . append ( self ) <EOL> print user . id <EOL> self . emit ( '<STR_LIT>' , _exclude = user . id , <EOL> username = user . username , <EOL> room = self . name ) <EOL> def user_leave ( self , user , disconnect = True ) : <EOL> try : <EOL> self . users . remove ( user ) <EOL> user . rooms . remove ( self ) <EOL> self . emit ( '<STR_LIT>' , _exclude = user . id , <EOL> disconnect = disconnect , <EOL> username = user . username ) <EOL> except : <EOL> pass <EOL> def emit ( self , event , ** data ) : <EOL> data [ '<STR_LIT>' ] = event <EOL> payload = json . dumps ( data ) <EOL> exclude = data . pop ( '<STR_LIT>' , None ) <EOL> print '<STR_LIT>' % ( self . name , payload , self . users , exclude ) <EOL> for user in self . users : <EOL> if exclude and user . id == exclude : <EOL> continue <EOL> user . emit ( _payload = payload ) <EOL> class WebRTC ( object ) : <EOL> _current_id_url = None <EOL> _socket_id = None <EOL> def __init__ ( self ) : <EOL> self . rooms = dict ( ) <EOL> self . users = dict ( ) <EOL> self . users_by_stream = dict ( ) <EOL> self . users_by_username = dict ( ) <EOL> def get_room ( self , room ) : <EOL> print '<STR_LIT>' , room <EOL> if room not in self . rooms : <EOL> print '<STR_LIT>' <EOL> self . rooms [ room ] = WebRTCRoom ( room ) <EOL> return self . rooms [ room ] <EOL> def get_current_user ( self ) : <EOL> if request . stream_id and self . users_by_stream [ request . stream_id ] : <EOL> return self . users_by_stream [ request . stream_id ] <EOL> return None <EOL> def disconnected ( self , stream_id ) : <EOL> user = rtc . users [ stream_id ] <EOL> for room in user . rooms : <EOL> room . user_leave ( user ) <EOL> del rtc . users [ stream_id ] <EOL> del rtc . users_by_stream [ stream_id ] <EOL> rtc = WebRTC ( ) <EOL> def event_stream ( stream_id ) : <EOL> """<STR_LIT>""" <EOL> connected = True <EOL> user = rtc . users_by_stream [ stream_id ] <EOL> try : <EOL> yield '<STR_LIT>' % json . dumps ( dict ( event = '<STR_LIT:hello>' , stream_id = stream_id ) ) <EOL> except : <EOL> print '<STR_LIT>' % stream_id <EOL> connected = False <EOL> print '<STR_LIT>' % stream_id <EOL> rtc . disconnected ( stream_id ) <EOL> return <EOL> while connected : <EOL> message = user . messages . get ( block = True , timeout = None ) <EOL> if message != '<STR_LIT>' : <EOL> print '<STR_LIT>' % ( message , stream_id ) <EOL> try : <EOL> yield '<STR_LIT>' % message <EOL> except : <EOL> print '<STR_LIT>' % stream_id <EOL> connected = False <EOL> print '<STR_LIT>' % stream_id <EOL> rtc . disconnected ( stream_id ) <EOL> return <EOL> def heartbeat ( delay ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> for user in rtc . users : <EOL> rtc . users [ user ] . emit ( '<STR_LIT>' ) <EOL> time . sleep ( delay ) <EOL> thread . start_new_thread ( heartbeat , ( heartbeat_interval , ) ) <EOL> @ app . before_request <EOL> def before_request ( ) : <EOL> stream_id = None <EOL> user = None <EOL> if '<STR_LIT>' in request . headers : <EOL> stream_id = request . headers [ '<STR_LIT>' ] <EOL> if stream_id in rtc . users : <EOL> user = rtc . users [ stream_id ] <EOL> setattr ( request , '<STR_LIT>' , stream_id ) <EOL> setattr ( request , '<STR_LIT>' , user ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def stream ( ) : <EOL> """<STR_LIT>""" <EOL> stream_id = uuid . uuid4 ( ) . get_hex ( ) <EOL> rtc . users [ stream_id ] = WebRTCUser ( stream_id ) <EOL> rtc . users_by_stream [ stream_id ] = rtc . users [ stream_id ] <EOL> return Response ( stream_with_context ( event_stream ( stream_id ) ) , <EOL> mimetype = "<STR_LIT>" ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def debug ( ) : <EOL> return render_template ( '<STR_LIT>' , rtc = rtc ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_set_name ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> if len ( request . form [ '<STR_LIT:username>' ] ) == <NUM_LIT:0> : <EOL> return jsonify ( dict ( <EOL> error = '<STR_LIT>' , <EOL> username = request . form [ '<STR_LIT:username>' ] <EOL> ) ) <EOL> for id in rtc . users : <EOL> user = rtc . users [ id ] <EOL> if user . username == request . form [ '<STR_LIT:username>' ] and user . id != request . stream_id : <EOL> return jsonify ( error = '<STR_LIT>' , _status = <NUM_LIT> ) <EOL> user = rtc . get_current_user ( ) <EOL> if not user : <EOL> return jsonify ( error = "<STR_LIT>" , <EOL> _status = <NUM_LIT> ) <EOL> old_username = user . username <EOL> if old_username : <EOL> rtc . users_by_username [ old_username ] = None <EOL> rtc . users_by_username [ request . form [ '<STR_LIT:username>' ] ] = user <EOL> user . username = request . form [ '<STR_LIT:username>' ] <EOL> for room in user . rooms : <EOL> room . emit ( '<STR_LIT>' , <EOL> old_username = old_username , <EOL> username = user . username , <EOL> exclude = user . id ) <EOL> return jsonify ( success = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_join_room ( ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' , request . form <EOL> room = rtc . get_room ( request . form [ '<STR_LIT>' ] ) <EOL> user = rtc . get_current_user ( ) <EOL> room . user_join ( user ) <EOL> data = dict ( <EOL> room = room . name , <EOL> users = [ ] , <EOL> encryption = None <EOL> ) <EOL> print room , room . users <EOL> for patron in room . users : <EOL> if patron . id != user . id : <EOL> data [ '<STR_LIT>' ] . append ( dict ( <EOL> username = patron . username , <EOL> ) ) <EOL> return jsonify ( data ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_leave_room ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> username = request . form [ '<STR_LIT:username>' ] <EOL> room = request . form [ '<STR_LIT>' ] <EOL> leave_room ( room ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_room_info ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> user = rtc . get_current_user ( ) <EOL> room = request . form [ '<STR_LIT>' ] <EOL> encryption , browser , browser_version = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> if room in encryption : <EOL> encryption = rtc . encryption [ room ] <EOL> browser = rtc . browser [ room ] <EOL> browser_version = rtc . browser_version [ room ] <EOL> user . emit ( '<STR_LIT>' , <EOL> encryption = encryption , <EOL> browser = browser , <EOL> browser_version = browser_version <EOL> ) <EOL> return jsonify ( success = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_send_ice_candidate ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> sender = rtc . get_current_user ( ) <EOL> user = rtc . users_by_username [ request . form [ '<STR_LIT:username>' ] ] <EOL> user . emit ( '<STR_LIT>' , <EOL> candidate = request . form [ '<STR_LIT>' ] , <EOL> username = sender . username <EOL> ) <EOL> return jsonify ( success = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_send_offer ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> sender = rtc . get_current_user ( ) <EOL> user = rtc . users_by_username [ request . form [ '<STR_LIT:username>' ] ] <EOL> user . emit ( '<STR_LIT>' , <EOL> sdp = request . form [ '<STR_LIT>' ] , <EOL> username = sender . username <EOL> ) <EOL> return jsonify ( success = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def on_send_answer ( ) : <EOL> print '<STR_LIT>' , request . form <EOL> sender = rtc . get_current_user ( ) <EOL> user = rtc . users_by_username [ request . form [ '<STR_LIT:username>' ] ] <EOL> user . emit ( '<STR_LIT>' , <EOL> sdp = request . form [ '<STR_LIT>' ] , <EOL> username = sender . username <EOL> ) <EOL> return jsonify ( success = True ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def get_rooms ( ) : <EOL> getRooms = [ ] <EOL> for room in rtc . rooms : <EOL> getRooms . insert ( <NUM_LIT:0> , room ) <EOL> return jsonify ( getRooms ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def get_users_in_room ( room ) : <EOL> get_users_in_room = [ ] <EOL> room = rtc . get_room ( room ) <EOL> for user in room . users : <EOL> get_users_in_room . insert ( <NUM_LIT:0> , user . username ) <EOL> return jsonify ( get_users_in_room ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def leave_rooms ( newroom , username ) : <EOL> user = rtc . users_by_username [ username ] ; <EOL> for room in rtc . rooms : <EOL> if ( room != newroom ) : <EOL> room = rtc . get_room ( room ) <EOL> room . user_leave ( user ) <EOL> return jsonify ( success = True ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( '<STR_LIT>' ) </s>
