Exemplo n.º 1
0
def on_observe(message):
    if 'observer' not in session:
        socketio.emit('terminate', {}, namespace='/observer', room=request.sid)
        return
    if request.sid not in threads:
        key = 'da:input:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])
        #sys.stderr.write('Observing ' + key + '\n')
        threads[request.sid] = socketio.start_background_task(target=observer_thread, sid=request.sid, key=key)
Exemplo n.º 2
0
def on_observe(message):
    if 'observer' not in session:
        socketio.emit('terminate', {}, namespace='/observer', room=request.sid)
        return
    if request.sid not in threads:
        key = 'da:input:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])
        #sys.stderr.write('Observing ' + key + '\n')
        threads[request.sid] = socketio.start_background_task(target=observer_thread, sid=request.sid, key=key)
Exemplo n.º 3
0
def on_monitor_connect():
    if 'monitor' not in session:
        socketio.emit('terminate', {}, namespace='/monitor', room=request.sid)
        return
    sys.stderr.write('Client connected on monitor and will join room monitor\n')
    key = 'da:monitor:' + str(request.sid)
    pipe = rr.pipeline()
    pipe.set(key, 1)
    pipe.expire(key, 60)
    pipe.execute()
    join_room('monitor')
    join_room(request.sid)
    user_id = session.get('user_id', None)
    if request.sid not in threads:
        threads[request.sid] = socketio.start_background_task(target=monitor_thread, sid=request.sid, user_id=user_id)
Exemplo n.º 4
0
def on_monitor_connect():
    if 'monitor' not in session:
        socketio.emit('terminate', {}, namespace='/monitor', room=request.sid)
        return
    sys.stderr.write('Client connected on monitor and will join room monitor\n')
    key = 'da:monitor:' + str(request.sid)
    pipe = rr.pipeline()
    pipe.set(key, 1)
    pipe.expire(key, 60)
    pipe.execute()
    join_room('monitor')
    join_room(request.sid)
    user_id = session.get('user_id', None)
    if request.sid not in threads:
        threads[request.sid] = socketio.start_background_task(target=monitor_thread, sid=request.sid, user_id=user_id)
Exemplo n.º 5
0
def interview_connect():
    session_id = session.get('uid', None)
    if session_id is not None:
        user_dict, is_encrypted = get_dict_encrypt()
        if is_encrypted:
            secret = request.cookies.get('secret', None)
        else:
            secret = None
        if secret is not None:
            secret = str(secret)
        if user_dict is None:
            sys.stderr.write("user_dict did not exist.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        
        chat_info = user_dict['_internal']['livehelp']
        if chat_info['availability'] == 'unavailable':
            sys.stderr.write("Socket started but chat is unavailable.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        #sys.stderr.write('chat info is ' + str(chat_info) + "\n")
        if user_dict['_internal']['livehelp']['mode'] in ['peer', 'peerhelp']:
            peer_ok = True
        else:
            peer_ok = False

        yaml_filename = session.get('i', None)
        the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None)))
        
        if request.sid not in threads:
            #sys.stderr.write('Starting thread for sid ' + str(request.sid) + "\n")
            threads[request.sid] = socketio.start_background_task(target=background_thread, sid=request.sid, user_id=session.get('user_id', None), temp_user_id=session.get('tempuser', None))
        channel_up = wait_for_channel(rr, request.sid)
        if not channel_up:
            sys.stderr.write("Channel did not come up.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        lkey = 'da:ready:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id)
        #sys.stderr.write("Searching: " + lkey + "\n")
        if rr.exists(lkey):
            lkey_exists = True
        else:
            lkey_exists = False
        if lkey_exists is False and peer_ok is False:
            sys.stderr.write("Key does not exist: " + lkey + ".\n")
            #socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            #return
        failed_to_find_partner = True
        found_help = False
        if lkey_exists:
            partner_keys = rr.lrange(lkey, 0, -1)
            #sys.stderr.write("partner_keys is: " + str(type(partner_keys)) + " " + str(partner_keys) + "\n")
            if partner_keys is None and not peer_ok:
                sys.stderr.write("No partner keys: " + lkey + ".\n")
                socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
                return
            rr.delete(lkey)
            for pkey in partner_keys:
                #sys.stderr.write("Considering: " + pkey + "\n")
                partner_sid = rr.get(pkey)
                if partner_sid is not None:
                    if re.match(r'^da:monitor:available:.*', pkey):
                        is_help = True
                    else:
                        is_help = False
                    if is_help and found_help:
                        continue
                    #sys.stderr.write("Trying to pub to " + str(partner_sid) + " from " + str(pkey) + "\n")
                    listeners = rr.publish(partner_sid, json.dumps(dict(messagetype='chatready', uid=session_id, i=yaml_filename, userid=the_user_id, secret=secret, sid=request.sid)))
                    #sys.stderr.write("Listeners: " + str(listeners) + "\n")
                    if re.match(r'^da:interviewsession.*', pkey):
                        rr.publish(request.sid, json.dumps(dict(messagetype='chatready', sid=partner_sid)))
                    else:
                        rr.publish(request.sid, json.dumps(dict(messagetype='chatpartner', sid=partner_sid)))
                    if listeners > 0:
                        if is_help:
                            found_help = True
                        failed_to_find_partner = False
        if failed_to_find_partner and peer_ok is False:
            sys.stderr.write("Unable to reach any potential chat partners.\n")
            #socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            #return
        key = 'da:interviewsession:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id)
        rr.set(key, request.sid)
Exemplo n.º 6
0
def interview_connect():
    session_id = session.get('uid', None)
    if session_id is not None:
        user_dict, is_encrypted = get_dict_encrypt()
        if is_encrypted:
            secret = request.cookies.get('secret', None)
        else:
            secret = None
        if secret is not None:
            secret = str(secret)
        if user_dict is None:
            sys.stderr.write("user_dict did not exist.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        
        chat_info = user_dict['_internal']['livehelp']
        if chat_info['availability'] == 'unavailable':
            sys.stderr.write("Socket started but chat is unavailable.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        #sys.stderr.write('chat info is ' + str(chat_info) + "\n")
        if user_dict['_internal']['livehelp']['mode'] in ['peer', 'peerhelp']:
            peer_ok = True
        else:
            peer_ok = False

        yaml_filename = session.get('i', None)
        the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None)))
        
        if request.sid not in threads:
            #sys.stderr.write('Starting thread for sid ' + str(request.sid) + "\n")
            threads[request.sid] = socketio.start_background_task(target=background_thread, sid=request.sid, user_id=session.get('user_id', None), temp_user_id=session.get('tempuser', None))
        channel_up = wait_for_channel(rr, request.sid)
        if not channel_up:
            sys.stderr.write("Channel did not come up.\n")
            socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            return
        lkey = 'da:ready:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id)
        #sys.stderr.write("Searching: " + lkey + "\n")
        if rr.exists(lkey):
            lkey_exists = True
        else:
            lkey_exists = False
        if lkey_exists is False and peer_ok is False:
            sys.stderr.write("Key does not exist: " + lkey + ".\n")
            #socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            #return
        failed_to_find_partner = True
        found_help = False
        if lkey_exists:
            partner_keys = rr.lrange(lkey, 0, -1)
            #sys.stderr.write("partner_keys is: " + str(type(partner_keys)) + " " + str(partner_keys) + "\n")
            if partner_keys is None and not peer_ok:
                sys.stderr.write("No partner keys: " + lkey + ".\n")
                socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
                return
            rr.delete(lkey)
            for pkey in partner_keys:
                pkey = pkey.decode()
                #sys.stderr.write("Considering: " + pkey + "\n")
                partner_sid = rr.get(pkey)
                if partner_sid is not None:
                    partner_sid = partner_sid.decode()
                    if re.match(r'^da:monitor:available:.*', pkey):
                        is_help = True
                    else:
                        is_help = False
                    if is_help and found_help:
                        continue
                    #sys.stderr.write("Trying to pub to " + str(partner_sid) + " from " + str(pkey) + "\n")
                    listeners = rr.publish(partner_sid, json.dumps(dict(messagetype='chatready', uid=session_id, i=yaml_filename, userid=the_user_id, secret=secret, sid=request.sid)))
                    #sys.stderr.write("Listeners: " + str(listeners) + "\n")
                    if re.match(r'^da:interviewsession.*', pkey):
                        rr.publish(request.sid, json.dumps(dict(messagetype='chatready', sid=partner_sid)))
                    else:
                        rr.publish(request.sid, json.dumps(dict(messagetype='chatpartner', sid=partner_sid)))
                    if listeners > 0:
                        if is_help:
                            found_help = True
                        failed_to_find_partner = False
        if failed_to_find_partner and peer_ok is False:
            sys.stderr.write("Unable to reach any potential chat partners.\n")
            #socketio.emit('terminate', {}, namespace='/wsinterview', room=request.sid)
            #return
        key = 'da:interviewsession:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id)
        rr.set(key, request.sid)