def interview_start_being_controlled(message): #sys.stderr.write("received start_being_controlled\n") session_id = session.get('uid', None) yaml_filename = session.get('i', None) the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None))) key = 'da:input:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id) rr.publish(key, json.dumps(dict(message='start_being_controlled', key=re.sub(r'^da:input:uid:', 'da:session:uid:', key))))
def interview_start_being_controlled(message): #sys.stderr.write("received start_being_controlled\n") session_id = session.get('uid', None) yaml_filename = session.get('i', None) the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None))) key = 'da:input:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(the_user_id) rr.publish(key, json.dumps(dict(message='start_being_controlled', key=re.sub(r'^da:input:uid:', 'da:session:uid:', key))))
def observer_changes(message): sys.stderr.write('observerChanges\n') if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return sid = rr.get('da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])) if sid is None: key = 'da:session:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) sys.stderr.write('observerChanges: sid is none.\n') if rr.get(key) is None: sys.stderr.write('observerChanges: session has gone away for good. Sending stopcontrolling.\n') socketio.emit('stopcontrolling', {'key': key}, namespace='/observer', room=request.sid) else: socketio.emit('noconnection', {'key': key}, namespace='/observer', room=request.sid) else: sid = sid.decode() sys.stderr.write('observerChanges: sid exists at ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\n') rr.publish(sid, json.dumps(dict(messagetype='controllerchanges', sid=request.sid, clicked=message.get('clicked', None), parameters=message['parameters']))) # sid=request.sid, yaml_filename=str(message['i']), uid=str(message['uid']), user_id=str(message['userid']) self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) #sys.stderr.write('Controlling ' + key + '\n') pipe = rr.pipeline() pipe.set(self_key, key) pipe.expire(key, 12) pipe.set(key, request.sid) pipe.expire(key, 12) pipe.execute()
def observer_changes(message): sys.stderr.write('observerChanges\n') if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return sid = rr.get('da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])) if sid is None: key = 'da:session:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) sys.stderr.write('observerChanges: sid is none.\n') if rr.get(key) is None: sys.stderr.write('observerChanges: session has gone away for good. Sending stopcontrolling.\n') socketio.emit('stopcontrolling', {'key': key}, namespace='/observer', room=request.sid) else: socketio.emit('noconnection', {'key': key}, namespace='/observer', room=request.sid) else: sys.stderr.write('observerChanges: sid exists at ' + time.strftime("%Y-%m-%d %H:%M:%S") + '\n') rr.publish(sid, json.dumps(dict(messagetype='controllerchanges', sid=request.sid, clicked=message.get('clicked', None), parameters=message['parameters']))) # sid=request.sid, yaml_filename=str(message['i']), uid=str(message['uid']), user_id=str(message['userid']) self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) #sys.stderr.write('Controlling ' + key + '\n') pipe = rr.pipeline() pipe.set(self_key, key) pipe.expire(key, 12) pipe.set(key, request.sid) pipe.expire(key, 12) pipe.execute()
def start_control(message): if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) existing_sid = rr.get(key) if existing_sid is None or existing_sid.decode() == request.sid: #sys.stderr.write('Controlling ' + key + '\n') pipe = rr.pipeline() pipe.set(self_key, key) pipe.expire(self_key, 12) pipe.set(key, request.sid) pipe.expire(key, 12) pipe.execute() int_key = 'da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) int_sid = rr.get(int_key) if int_sid is not None: int_sid = int_sid.decode() rr.publish(int_sid, json.dumps(dict(messagetype='controllerstart'))) else: sys.stderr.write('That key ' + key + ' is already taken\n') key = 'da:session:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) #rr.publish('da:monitor', json.dumps(dict(messagetype='abortcontroller', key=key))) socketio.emit('abortcontrolling', {'key': key}, namespace='/observer', room=request.sid)
def monitor_chat_message(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) #sys.stderr.write("Key is " + str(key) + "\n") if key is None: sys.stderr.write("No key provided\n") return m = re.match(r'da:session:uid:(.*):i:(.*):userid:(.*)', key) if not m: sys.stderr.write("Invalid key provided\n") return session_id = m.group(1) yaml_filename = m.group(2) chat_user_id = m.group(3) key = 'da:interviewsession:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(chat_user_id) sid = rr.get(key) if sid is None: sys.stderr.write("No sid for monitor chat message with key " + str(key) + "\n") return sid = sid.decode() secret = secrets.get(sid, None) if secret is not None: secret = str(secret) #obtain_lock(session_id, yaml_filename) try: steps, user_dict, encrypted = fetch_user_dict(session_id, yaml_filename, secret=secret) except Exception as err: #release_lock(session_id, yaml_filename) sys.stderr.write("monitor_chat_message: could not get dictionary: " + text_type(err) + "\n") return #release_lock(session_id, yaml_filename) nowtime = datetime.datetime.utcnow() if encrypted: message = encrypt_phrase(data['data'], secret) else: message = pack_phrase(data['data']) user_id = session.get('user_id', None) if user_id is not None: user_id = int(user_id) person = UserModel.query.filter_by(id=user_id).first() chat_mode = user_dict['_internal']['livehelp']['mode'] m = re.match('t([0-9]+)', chat_user_id) if m: temp_owner_id = m.group(1) owner_id = None else: temp_owner_id = None owner_id = chat_user_id if chat_mode in ['peer', 'peerhelp']: open_to_peer = True else: open_to_peer = False record = ChatLog(filename=yaml_filename, key=session_id, message=message, encrypted=encrypted, modtime=nowtime, user_id=user_id, temp_owner_id=temp_owner_id, owner_id=owner_id, open_to_peer=open_to_peer) db.session.add(record) db.session.commit() modtime = nice_utc_date(nowtime) rr.publish(sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id=chat_user_id, message=dict(id=record.id, user_id=record.user_id, first_name=person.first_name, last_name=person.last_name, email=person.email, modtime=modtime, message=data['data'], roles=[role.name for role in person.roles], mode=chat_mode))))
def monitor_chat_message(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) #sys.stderr.write("Key is " + str(key) + "\n") if key is None: sys.stderr.write("No key provided\n") return m = re.match(r'da:session:uid:(.*):i:(.*):userid:(.*)', key) if not m: sys.stderr.write("Invalid key provided\n") return session_id = m.group(1) yaml_filename = m.group(2) chat_user_id = m.group(3) key = 'da:interviewsession:uid:' + str(session_id) + ':i:' + str(yaml_filename) + ':userid:' + str(chat_user_id) sid = rr.get(key) if sid is None: sys.stderr.write("No sid for monitor chat message with key " + str(key) + "\n") return secret = secrets.get(sid, None) if secret is not None: secret = str(secret) #obtain_lock(session_id, yaml_filename) try: steps, user_dict, encrypted = fetch_user_dict(session_id, yaml_filename, secret=secret) except Exception as err: #release_lock(session_id, yaml_filename) sys.stderr.write("monitor_chat_message: could not get dictionary: " + unicode(err) + "\n") return #release_lock(session_id, yaml_filename) nowtime = datetime.datetime.utcnow() if encrypted: message = encrypt_phrase(data['data'], secret) else: message = pack_phrase(data['data']) user_id = session.get('user_id', None) if user_id is not None: user_id = int(user_id) person = UserModel.query.filter_by(id=user_id).first() chat_mode = user_dict['_internal']['livehelp']['mode'] m = re.match('t([0-9]+)', chat_user_id) if m: temp_owner_id = m.group(1) owner_id = None else: temp_owner_id = None owner_id = chat_user_id if chat_mode in ['peer', 'peerhelp']: open_to_peer = True else: open_to_peer = False record = ChatLog(filename=yaml_filename, key=session_id, message=message, encrypted=encrypted, modtime=nowtime, user_id=user_id, temp_owner_id=temp_owner_id, owner_id=owner_id, open_to_peer=open_to_peer) db.session.add(record) db.session.commit() modtime = nice_utc_date(nowtime) rr.publish(sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id=chat_user_id, message=dict(id=record.id, user_id=record.user_id, first_name=person.first_name, last_name=person.last_name, email=person.email, modtime=modtime, message=data['data'], roles=[role.name for role in person.roles], mode=chat_mode))))
def on_interview_disconnect(): sys.stderr.write('Client disconnected from interview\n') yaml_filename = session.get('i', None) session_id = session.get('uid', None) the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None))) if request.sid in secrets: del secrets[request.sid] if session_id is not None: rr.delete('da:interviewsession:uid:' + str(session.get('uid', None)) + ':i:' + str(session.get('i', None)) + ':userid:' + str(the_user_id)) key = 'da:session:uid:' + str(session.get('uid', None)) + ':i:' + str(session.get('i', None)) + ':userid:' + str(the_user_id) rr.expire(key, 10) rr.publish(request.sid, json.dumps(dict(origin='client', message='KILL', sid=request.sid)))
def on_interview_disconnect(): sys.stderr.write('Client disconnected from interview\n') yaml_filename = session.get('i', None) session_id = session.get('uid', None) the_user_id = session.get('user_id', 't' + str(session.get('tempuser', None))) if request.sid in secrets: del secrets[request.sid] if session_id is not None: rr.delete('da:interviewsession:uid:' + str(session.get('uid', None)) + ':i:' + str(session.get('i', None)) + ':userid:' + str(the_user_id)) key = 'da:session:uid:' + str(session.get('uid', None)) + ':i:' + str(session.get('i', None)) + ':userid:' + str(the_user_id) rr.expire(key, 10) rr.publish(request.sid, json.dumps(dict(origin='client', message='KILL', sid=request.sid)))
def on_monitor_disconnect(): user_id = session.get('user_id', None) sys.stderr.write('Client disconnected from monitor\n') rr.delete('da:monitor:' + str(request.sid)) rr.expire('da:monitor:available:' + str(user_id), 5) for key in rr.keys('da:monitor:role:*:userid:' + str(user_id)): rr.expire(key, 5) for key in rr.keys('da:phonecode:monitor:' + str(user_id) + ':uid:*'): the_code = rr.get(key) if the_code is not None: rr.expire('da:callforward:' + str(the_code), 5) rr.expire(key, 5) rr.expire('da:monitor:chatpartners:' + str(user_id), 5) rr.publish(request.sid, json.dumps(dict(message='KILL', sid=request.sid)))
def on_observer_disconnect(): sys.stderr.write('Client disconnected from observer\n') self_key = 'da:control:sid:' + str(request.sid) int_key = rr.get(self_key) if int_key is not None: rr.delete(int_key) other_sid = rr.get(re.sub(r'^da:control:uid:', 'da:interviewsession:uid:', int_key)) else: other_sid = None rr.delete(self_key) if other_sid is not None: sys.stderr.write("Calling controllerexit 2"); rr.publish(other_sid, json.dumps(dict(messagetype='controllerexit', sid=request.sid))) rr.publish(request.sid, json.dumps(dict(message='KILL', sid=request.sid)))
def monitor_unblock(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) if key is None: sys.stderr.write("No key provided\n") return sys.stderr.write("Unblocking\n") rr.delete(re.sub(r'^da:session:', 'da:block:', key)) sid = rr.get(re.sub(r'^da:session:', 'da:interviewsession:', key)) if sid is not None: rr.publish(sid, json.dumps(dict(messagetype='chatpartner', sid=request.sid))) socketio.emit('unblock', {'key': key}, namespace='/monitor', room=request.sid)
def monitor_unblock(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) if key is None: sys.stderr.write("No key provided\n") return sys.stderr.write("Unblocking\n") rr.delete(re.sub(r'^da:session:', 'da:block:', key)) sid = rr.get(re.sub(r'^da:session:', 'da:interviewsession:', key)) if sid is not None: sid = sid.decode() rr.publish(sid, json.dumps(dict(messagetype='chatpartner', sid=request.sid))) socketio.emit('unblock', {'key': key}, namespace='/monitor', room=request.sid)
def monitor_block(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) if key is None: sys.stderr.write("No key provided\n") return rr.set(re.sub(r'^da:session:', 'da:block:', key), 1) sid = rr.get(re.sub(r'^da:session:', 'da:interviewsession:', key)) if sid is not None: rr.publish(sid, json.dumps(dict(messagetype='block', sid=request.sid))) sys.stderr.write("Blocking\n") else: sys.stderr.write("Could not block because could not get sid\n") socketio.emit('block', {'key': key}, namespace='/monitor', room=request.sid)
def on_observer_disconnect(): sys.stderr.write('Client disconnected from observer\n') self_key = 'da:control:sid:' + str(request.sid) int_key = rr.get(self_key) if int_key is not None: int_key = int_key.decode() rr.delete(int_key) other_sid = rr.get(re.sub(r'^da:control:uid:', 'da:interviewsession:uid:', int_key)) else: other_sid = None rr.delete(self_key) if other_sid is not None: other_sid = other_sid.decode() sys.stderr.write("Calling controllerexit 2"); rr.publish(other_sid, json.dumps(dict(messagetype='controllerexit', sid=request.sid))) rr.publish(request.sid, json.dumps(dict(message='KILL', sid=request.sid)))
def on_monitor_disconnect(): user_id = session.get('user_id', None) sys.stderr.write('Client disconnected from monitor\n') rr.delete('da:monitor:' + str(request.sid)) rr.expire('da:monitor:available:' + str(user_id), 5) for key in rr.keys('da:monitor:role:*:userid:' + str(user_id)): key = key.decode() rr.expire(key, 5) for key in rr.keys('da:phonecode:monitor:' + str(user_id) + ':uid:*'): key = key.decode() the_code = rr.get(key) if the_code is not None: the_code = the_code.decode() rr.expire('da:callforward:' + the_code, 5) rr.expire(key, 5) rr.expire('da:monitor:chatpartners:' + str(user_id), 5) rr.publish(request.sid, json.dumps(dict(message='KILL', sid=request.sid)))
def monitor_block(data): if 'monitor' not in session: socketio.emit('terminate', {}, namespace='/monitor', room=request.sid) return key = data.get('key', None) if key is None: sys.stderr.write("No key provided\n") return rr.set(re.sub(r'^da:session:', 'da:block:', key), 1) sid = rr.get(re.sub(r'^da:session:', 'da:interviewsession:', key)) if sid is not None: sid = sid.decode() rr.publish(sid, json.dumps(dict(messagetype='block', sid=request.sid))) sys.stderr.write("Blocking\n") else: sys.stderr.write("Could not block because could not get sid\n") socketio.emit('block', {'key': key}, namespace='/monitor', room=request.sid)
def wait_for_channel(rr, channel): times = 0 while times < 5 and rr.publish(channel, json.dumps(dict(messagetype='ping'))) == 0: times += 1 time.sleep(0.5) if times >= 5: return False else: return True
def wait_for_channel(rr, channel): times = 0 while times < 5 and rr.publish(channel, json.dumps(dict(messagetype='ping'))) == 0: times += 1 time.sleep(0.5) if times >= 5: return False else: return True
def stop_control(message): if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) sys.stderr.write('Stop controlling ' + key + '\n') existing_sid = rr.get(key) pipe = rr.pipeline() pipe.delete(self_key) if existing_sid is not None and existing_sid == request.sid: pipe.delete(key) pipe.execute() sid = rr.get('da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])) if sid is not None: sys.stderr.write("Calling controllerexit 1"); rr.publish(sid, json.dumps(dict(messagetype='controllerexit', sid=request.sid))) else: pipe.execute()
def stop_control(message): if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) sys.stderr.write('Stop controlling ' + key + '\n') existing_sid = rr.get(key) pipe = rr.pipeline() pipe.delete(self_key) if existing_sid is not None and existing_sid.decode() == request.sid: pipe.delete(key) pipe.execute() sid = rr.get('da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid'])) if sid is not None: sid = sid.decode() sys.stderr.write("Calling controllerexit 1"); rr.publish(sid, json.dumps(dict(messagetype='controllerexit', sid=request.sid))) else: pipe.execute()
def chat_message(data): nowtime = datetime.datetime.utcnow() session_id = session.get('uid', None) yaml_filename = session.get('i', None) encrypted = session.get('encrypted', True) secret = request.cookies.get('secret', None) if secret is not None: secret = str(secret) if encrypted: message = encrypt_phrase(data['data'], secret) else: message = pack_phrase(data['data']) user_id = session.get('user_id', None) if user_id is None: temp_user_id = session.get('tempuser', None) else: temp_user_id = None if user_id is not None: user_id = int(user_id) if temp_user_id is not None: temp_user_id = int(temp_user_id) user_dict = get_dict() chat_mode = user_dict['_internal']['livehelp']['mode'] if chat_mode in ['peer', 'peerhelp']: open_to_peer = True else: open_to_peer = False record = ChatLog(filename=yaml_filename, key=session_id, message=message, encrypted=encrypted, modtime=nowtime, temp_user_id=temp_user_id, user_id=user_id, open_to_peer=open_to_peer, temp_owner_id=temp_user_id, owner_id=user_id) db.session.add(record) db.session.commit() if user_id is not None: person = UserModel.query.filter_by(id=user_id).first() else: person = None modtime = nice_utc_date(nowtime) if person is None: rr.publish(request.sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id='t' + str(temp_user_id), message=dict(id=record.id, temp_user_id=record.temp_user_id, modtime=modtime, message=data['data'], roles=['user'], mode=chat_mode)))) else: rr.publish(request.sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id=user_id, message=dict(id=record.id, user_id=record.user_id, first_name=person.first_name, last_name=person.last_name, email=person.email, modtime=modtime, message=data['data'], roles=[role.name for role in person.roles], mode=chat_mode))))
def chat_message(data): nowtime = datetime.datetime.utcnow() session_id = session.get('uid', None) yaml_filename = session.get('i', None) encrypted = session.get('encrypted', True) secret = request.cookies.get('secret', None) if secret is not None: secret = str(secret) if encrypted: message = encrypt_phrase(data['data'], secret) else: message = pack_phrase(data['data']) user_id = session.get('user_id', None) if user_id is None: temp_user_id = session.get('tempuser', None) else: temp_user_id = None if user_id is not None: user_id = int(user_id) if temp_user_id is not None: temp_user_id = int(temp_user_id) user_dict = get_dict() chat_mode = user_dict['_internal']['livehelp']['mode'] if chat_mode in ['peer', 'peerhelp']: open_to_peer = True else: open_to_peer = False record = ChatLog(filename=yaml_filename, key=session_id, message=message, encrypted=encrypted, modtime=nowtime, temp_user_id=temp_user_id, user_id=user_id, open_to_peer=open_to_peer, temp_owner_id=temp_user_id, owner_id=user_id) db.session.add(record) db.session.commit() if user_id is not None: person = UserModel.query.filter_by(id=user_id).first() else: person = None modtime = nice_utc_date(nowtime) if person is None: rr.publish(request.sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id='t' + str(temp_user_id), message=dict(id=record.id, temp_user_id=record.temp_user_id, modtime=modtime, message=data['data'], roles=['user'], mode=chat_mode)))) else: rr.publish(request.sid, json.dumps(dict(origin='client', messagetype='chat', sid=request.sid, yaml_filename=yaml_filename, uid=session_id, user_id=user_id, message=dict(id=record.id, user_id=record.user_id, first_name=person.first_name, last_name=person.last_name, email=person.email, modtime=modtime, message=data['data'], roles=[role.name for role in person.roles], mode=chat_mode))))
def start_control(message): if 'observer' not in session: socketio.emit('terminate', {}, namespace='/observer', room=request.sid) return self_key = 'da:control:sid:' + str(request.sid) key = 'da:control:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) existing_sid = rr.get(key) if existing_sid is None or existing_sid == request.sid: #sys.stderr.write('Controlling ' + key + '\n') pipe = rr.pipeline() pipe.set(self_key, key) pipe.expire(self_key, 12) pipe.set(key, request.sid) pipe.expire(key, 12) pipe.execute() int_key = 'da:interviewsession:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) int_sid = rr.get(int_key) if int_sid is not None: rr.publish(int_sid, json.dumps(dict(messagetype='controllerstart'))) else: sys.stderr.write('That key ' + key + ' is already taken\n') key = 'da:session:uid:' + str(message['uid']) + ':i:' + str(message['i']) + ':userid:' + str(message['userid']) #rr.publish('da:monitor', json.dumps(dict(messagetype='abortcontroller', key=key))) socketio.emit('abortcontrolling', {'key': key}, namespace='/observer', room=request.sid)
def handle_message(message): #sys.stderr.write('received transmission from ' + str(session.get('uid', 'NO UID')) + ': ' + message['data'] + "\n") session_id = session.get('uid', None) if session_id is not None: rr.publish(session_id, json.dumps(dict(origin='client', room=request.sid, message=message['data'])))
def on_interview_connect(): sys.stderr.write("Client connected on interview\n") join_room(request.sid) interview_connect() rr.publish('da:monitor', json.dumps(dict(messagetype='refreshsessions')))
def on_interview_reconnect(data): sys.stderr.write("Client reconnected on interview\n") interview_connect() rr.publish('da:monitor', json.dumps(dict(messagetype='refreshsessions'))) socketio.emit('reconnected', {}, namespace='/wsinterview', room=request.sid)
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)
def handle_message(message): #sys.stderr.write('received transmission from ' + str(session.get('uid', 'NO UID')) + ': ' + message['data'] + "\n") session_id = session.get('uid', None) if session_id is not None: rr.publish(session_id, json.dumps(dict(origin='client', room=request.sid, message=message['data'])))
def on_interview_connect(): sys.stderr.write("Client connected on interview\n") join_room(request.sid) interview_connect() rr.publish('da:monitor', json.dumps(dict(messagetype='refreshsessions')))
def on_interview_reconnect(data): sys.stderr.write("Client reconnected on interview\n") interview_connect() rr.publish('da:monitor', json.dumps(dict(messagetype='refreshsessions'))) socketio.emit('reconnected', {}, namespace='/wsinterview', room=request.sid)
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)