def observer_thread(sid=None, key=None): with app.app_context(): sys.stderr.write("Started observer thread for " + str(sid) + "\n") r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_offset) pubsub = r.pubsub() pubsub.subscribe([key, sid]) for item in pubsub.listen(): sys.stderr.write("2\n" + repr(item) + "\n") if item['type'] != 'message': continue #sys.stderr.write("observer sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data'].decode()) except: sys.stderr.write(" observer JSON parse error: " + item['data'].decode() + "\n") continue if 'message' in data and data['message'] == "KILL" and (('sid' in data and data['sid'] == sid) or 'sid' not in data): pubsub.unsubscribe() sys.stderr.write(" observer unsubscribed and finished for " + str(sid) + "\n") break elif 'message' in data: if data['message'] == "newpage": #sys.stderr.write(" Got new page for observer\n") try: obj = json.loads(r.get(data['key']).decode()) except: sys.stderr.write(" newpage JSON parse error\n") continue socketio.emit('newpage', {'obj': obj}, namespace='/observer', room=sid) elif data['message'] == "start_being_controlled": #sys.stderr.write(" got start_being_controlled message with key " + str(data['key']) + "\n") socketio.emit('start_being_controlled', {'key': data['key']}, namespace='/observer', room=sid) else: #sys.stderr.write(" Got parameters for observer\n") socketio.emit('pushchanges', {'parameters': data}, namespace='/observer', room=sid) sys.stderr.write(' exiting observer thread for sid ' + str(sid) + '\n')
def observer_thread(sid=None, key=None): with app.app_context(): sys.stderr.write("Started observer thread for " + str(sid) + "\n") r = redis.StrictRedis(host=redis_host, db=0) pubsub = r.pubsub() pubsub.subscribe([key, sid]) for item in pubsub.listen(): if item['type'] != 'message': continue #sys.stderr.write("observer sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data']) except: sys.stderr.write(" observer JSON parse error: " + str(item['data']) + "\n") continue if 'message' in data and data['message'] == "KILL" and (('sid' in data and data['sid'] == sid) or 'sid' not in data): pubsub.unsubscribe() sys.stderr.write(" observer unsubscribed and finished for " + str(sid) + "\n") break elif 'message' in data: if data['message'] == "newpage": #sys.stderr.write(" Got new page for observer\n") try: obj = json.loads(r.get(data['key'])) except: sys.stderr.write(" newpage JSON parse error\n") continue socketio.emit('newpage', {'obj': obj}, namespace='/observer', room=sid) elif data['message'] == "start_being_controlled": #sys.stderr.write(" got start_being_controlled message with key " + str(data['key']) + "\n") socketio.emit('start_being_controlled', {'key': data['key']}, namespace='/observer', room=sid) else: #sys.stderr.write(" Got parameters for observer\n") socketio.emit('pushchanges', {'parameters': data}, namespace='/observer', room=sid) sys.stderr.write(' exiting observer thread for sid ' + str(sid) + '\n')
def background_thread(sid=None, user_id=None, 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) with app.app_context(): sys.stderr.write("Started client thread for " + str(sid) + " who is " + str(user_id) + " or " + str(temp_user_id) + "\n") if user_id is None: person = None user_is_temp = True else: person = UserModel.query.filter_by(id=user_id).first() user_is_temp = False if person is not None and person.timezone is not None: the_timezone = pytz.timezone(person.timezone) else: the_timezone = pytz.timezone(get_default_timezone()) r = redis.StrictRedis(host=redis_host, db=0) partners = set() pubsub = r.pubsub() pubsub.subscribe([sid]) for item in pubsub.listen(): if item['type'] != 'message': continue #sys.stderr.write("sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data']) except: sys.stderr.write(" JSON parse error: " + str(item['data']) + "\n") continue if data.get('message', None) == "KILL" and (('sid' in data and data['sid'] == sid) or 'sid' not in data): pubsub.unsubscribe(sid) sys.stderr.write(" interview unsubscribed and finished for " + str(sid) + "\n") break else: sys.stderr.write(" Got something for sid " + str(sid) + " from " + data.get('origin', "Unknown origin") + "\n") if 'messagetype' in data: if data['messagetype'] == 'chat': #sys.stderr.write(" Emitting interview chat message: " + str(data['message']['message']) + "\n") if (user_is_temp is True and str(temp_user_id) == str(data['message'].get('temp_user_id', None))) or (user_is_temp is False and str(user_id) == str(data['message'].get('user_id', None))): data['message']['is_self'] = True else: data['message']['is_self'] = False socketio.emit('chatmessage', {'i': data['yaml_filename'], 'uid': data['uid'], 'userid': data['user_id'], 'data': data['message']}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'chatready': pubsub.subscribe(data['sid']) partners.add(data['sid']) sys.stderr.write("chatready 2") socketio.emit('chatready', {}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'departure': if data['sid'] in partners: partners.remove(data['sid']) socketio.emit('departure', {'numpartners': len(partners)}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'block': if data['sid'] in partners: partners.remove(data['sid']) socketio.emit('departure', {'numpartners': len(partners)}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'chatpartner': partners.add(data['sid']) elif data['messagetype'] == 'controllerchanges': socketio.emit('controllerchanges', {'parameters': data['parameters'], 'clicked': data['clicked']}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'controllerstart': socketio.emit('controllerstart', {}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'controllerexit': socketio.emit('controllerexit', {}, namespace='/wsinterview', room=sid) # elif data['messagetype'] == "newpage": # sys.stderr.write(" Got new page for interview\n") # try: # obj = json.loads(r.get(data['key'])) # except: # sys.stderr.write(" newpage JSON parse error\n") # continue # socketio.emit('newpage', {'obj': obj}, namespace='/wsinterview', room=sid) sys.stderr.write(' exiting interview thread for sid ' + str(sid) + '\n')
def monitor_thread(sid=None, user_id=None): with app.app_context(): sys.stderr.write("Started monitor thread for " + str(sid) + " who is " + str(user_id) + "\n") if user_id is not None: person = UserModel.query.filter_by(id=user_id).first() else: person = None if person is not None and person.timezone is not None: the_timezone = pytz.timezone(person.timezone) else: the_timezone = pytz.timezone(get_default_timezone()) r = redis.StrictRedis(host=redis_host, db=0) listening_sids = set() pubsub = r.pubsub() pubsub.subscribe(['da:monitor', sid]) for item in pubsub.listen(): if item['type'] != 'message': continue #sys.stderr.write("monitor sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data']) except: sys.stderr.write(" monitor JSON parse error: " + str(item['data']) + "\n") continue if 'message' in data and data['message'] == "KILL": if item['channel'] == str(sid): sys.stderr.write(" monitor unsubscribed from all\n") pubsub.unsubscribe() for interview_sid in listening_sids: r.publish(interview_sid, json.dumps(dict(messagetype='departure', sid=sid))) break elif item['channel'] != 'da:monitor': pubsub.unsubscribe(item['channel']) if data['sid'] in listening_sids: listening_sids.remove(data['sid']) sys.stderr.write(" monitor unsubscribed from " + str(item['channel']) + "\n") continue else: sys.stderr.write(" Got something for monitor\n") if 'messagetype' in data: #if data['messagetype'] == 'abortcontroller': # socketio.emit('abortcontroller', {'key': data['key']}, namespace='/monitor', room=sid) if data['messagetype'] == 'sessionupdate': #sys.stderr.write(" Got a session update: " + str(data['session']) + "\n") #sys.stderr.write(" Got a session update\n") socketio.emit('sessionupdate', {'key': data['key'], 'session': data['session']}, namespace='/monitor', room=sid) if data['messagetype'] == 'chatready': pubsub.subscribe(data['sid']) listening_sids.add(data['sid']) secrets[data['sid']] = data['secret'] r.hset('da:monitor:chatpartners:' + str(user_id), 'da:interviewsession:uid:' + str(data['uid']) + ':i:' + str(data['i']) + ':userid:' + str(data['userid']), 1) if str(data['userid']).startswith('t'): name = word("anonymous visitor") + ' ' + str(data['userid'])[1:] else: person = UserModel.query.filter_by(id=data['userid']).first() if person.first_name: name = str(person.first_name) + ' ' + str(person.last_name) else: name = str(person.email) sys.stderr.write("chatready 1") socketio.emit('chatready', {'uid': data['uid'], 'i': data['i'], 'userid': data['userid'], 'name': name}, namespace='/monitor', room=sid) if data['messagetype'] == 'block': pubsub.unsubscribe(item['channel']) if item['channel'] in listening_sids: listening_sids.remove(item['channel']) sys.stderr.write(" monitor unsubscribed from " + str(item['channel']) + "\n") if data['messagetype'] == 'refreshsessions': socketio.emit('refreshsessions', {}, namespace='/monitor', room=sid) if data['messagetype'] == 'chat': #sys.stderr.write(" Emitting monitor chat message: " + str(data['message']['message']) + "\n") if str(user_id) == str(data['message'].get('user_id', None)): data['message']['is_self'] = True else: data['message']['is_self'] = False socketio.emit('chatmessage', {'i': data['yaml_filename'], 'uid': data['uid'], 'userid': data['user_id'], 'data': data['message']}, namespace='/monitor', room=sid) if data['messagetype'] == 'chatstop': sys.stderr.write(" Chat termination for sid " + data['sid'] + "\n") pubsub.unsubscribe(data['sid']) if data['sid'] in secrets: del secrets[data['sid']] r.hdel('da:monitor:chatpartners:' + str(user_id), 'da:interviewsession:uid:' + str(data['uid']) + ':i:' + str(data['i']) + ':userid:' + data['userid']) socketio.emit('chatstop', {'uid': data['uid'], 'i': data['i'], 'userid': data['userid']}, namespace='/monitor', room=sid) sys.stderr.write(' exiting monitor thread for sid ' + str(sid) + '\n')
def background_thread(sid=None, user_id=None, 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) with app.app_context(): sys.stderr.write("Started client thread for " + str(sid) + " who is " + str(user_id) + " or " + str(temp_user_id) + "\n") if user_id is None: person = None user_is_temp = True else: person = UserModel.query.filter_by(id=user_id).first() user_is_temp = False if person is not None and person.timezone is not None: the_timezone = pytz.timezone(person.timezone) else: the_timezone = pytz.timezone(get_default_timezone()) r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_offset) partners = set() pubsub = r.pubsub() pubsub.subscribe([sid]) for item in pubsub.listen(): sys.stderr.write("0\n" + repr(item) + "\n") if item['type'] != 'message': continue #sys.stderr.write("sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data'].decode()) except: sys.stderr.write(" JSON parse error: " + str(item['data'].decode()) + "\n") continue if data.get('message', None) == "KILL" and (('sid' in data and data['sid'] == sid) or 'sid' not in data): pubsub.unsubscribe(sid) sys.stderr.write(" interview unsubscribed and finished for " + str(sid) + "\n") break else: sys.stderr.write(" Got something for sid " + str(sid) + " from " + data.get('origin', "Unknown origin") + "\n") if 'messagetype' in data: if data['messagetype'] == 'chat': #sys.stderr.write(" Emitting interview chat message: " + str(data['message']['message']) + "\n") if (user_is_temp is True and str(temp_user_id) == str(data['message'].get('temp_user_id', None))) or (user_is_temp is False and str(user_id) == str(data['message'].get('user_id', None))): data['message']['is_self'] = True else: data['message']['is_self'] = False socketio.emit('chatmessage', {'i': data['yaml_filename'], 'uid': data['uid'], 'userid': data['user_id'], 'data': data['message']}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'chatready': pubsub.subscribe(data['sid']) partners.add(data['sid']) sys.stderr.write("chatready 2") socketio.emit('chatready', {}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'departure': if data['sid'] in partners: partners.remove(data['sid']) socketio.emit('departure', {'numpartners': len(partners)}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'block': if data['sid'] in partners: partners.remove(data['sid']) socketio.emit('departure', {'numpartners': len(partners)}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'chatpartner': partners.add(data['sid']) elif data['messagetype'] == 'controllerchanges': socketio.emit('controllerchanges', {'parameters': data['parameters'], 'clicked': data['clicked']}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'controllerstart': socketio.emit('controllerstart', {}, namespace='/wsinterview', room=sid) elif data['messagetype'] == 'controllerexit': socketio.emit('controllerexit', {}, namespace='/wsinterview', room=sid) # elif data['messagetype'] == "newpage": # sys.stderr.write(" Got new page for interview\n") # try: # obj = json.loads(r.get(data['key'])) # except: # sys.stderr.write(" newpage JSON parse error\n") # continue # socketio.emit('newpage', {'obj': obj}, namespace='/wsinterview', room=sid) sys.stderr.write(' exiting interview thread for sid ' + str(sid) + '\n')
def monitor_thread(sid=None, user_id=None): with app.app_context(): sys.stderr.write("Started monitor thread for " + str(sid) + " who is " + str(user_id) + "\n") if user_id is not None: person = UserModel.query.filter_by(id=user_id).first() else: person = None if person is not None and person.timezone is not None: the_timezone = pytz.timezone(person.timezone) else: the_timezone = pytz.timezone(get_default_timezone()) r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_offset) listening_sids = set() pubsub = r.pubsub() pubsub.subscribe(['da:monitor', sid]) for item in pubsub.listen(): sys.stderr.write("1\n" + repr(item) + "\n") if item['type'] != 'message': continue #sys.stderr.write("monitor sid: " + str(sid) + ":\n") data = None try: data = json.loads(item['data'].decode()) except: sys.stderr.write(" monitor JSON parse error: " + item['data'].decode() + "\n") continue if 'message' in data and data['message'] == "KILL": if item['channel'].decode() == str(sid): sys.stderr.write(" monitor unsubscribed from all\n") pubsub.unsubscribe() for interview_sid in listening_sids: r.publish(interview_sid, json.dumps(dict(messagetype='departure', sid=sid))) break elif item['channel'].decode() != 'da:monitor': pubsub.unsubscribe(item['channel'].decode()) if data['sid'] in listening_sids: listening_sids.remove(data['sid']) sys.stderr.write(" monitor unsubscribed from " + item['channel'].decode() + "\n") continue else: sys.stderr.write(" Got something for monitor\n") if 'messagetype' in data: #if data['messagetype'] == 'abortcontroller': # socketio.emit('abortcontroller', {'key': data['key']}, namespace='/monitor', room=sid) if data['messagetype'] == 'sessionupdate': #sys.stderr.write(" Got a session update: " + str(data['session']) + "\n") #sys.stderr.write(" Got a session update\n") socketio.emit('sessionupdate', {'key': data['key'], 'session': data['session']}, namespace='/monitor', room=sid) if data['messagetype'] == 'chatready': pubsub.subscribe(data['sid']) listening_sids.add(data['sid']) secrets[data['sid']] = data['secret'] r.hset('da:monitor:chatpartners:' + str(user_id), 'da:interviewsession:uid:' + str(data['uid']) + ':i:' + str(data['i']) + ':userid:' + str(data['userid']), 1) if str(data['userid']).startswith('t'): name = word("anonymous visitor") + ' ' + str(data['userid'])[1:] else: person = UserModel.query.filter_by(id=data['userid']).first() if person.first_name: name = str(person.first_name) + ' ' + str(person.last_name) else: name = str(person.email) sys.stderr.write("chatready 1") socketio.emit('chatready', {'uid': data['uid'], 'i': data['i'], 'userid': data['userid'], 'name': name}, namespace='/monitor', room=sid) if data['messagetype'] == 'block': pubsub.unsubscribe(item['channel'].decode()) if item['channel'].decode() in listening_sids: listening_sids.remove(item['channel'].decode()) sys.stderr.write(" monitor unsubscribed from " + item['channel'].decode() + "\n") if data['messagetype'] == 'refreshsessions': socketio.emit('refreshsessions', {}, namespace='/monitor', room=sid) if data['messagetype'] == 'chat': #sys.stderr.write(" Emitting monitor chat message: " + str(data['message']['message']) + "\n") if str(user_id) == str(data['message'].get('user_id', None)): data['message']['is_self'] = True else: data['message']['is_self'] = False socketio.emit('chatmessage', {'i': data['yaml_filename'], 'uid': data['uid'], 'userid': data['user_id'], 'data': data['message']}, namespace='/monitor', room=sid) if data['messagetype'] == 'chatstop': sys.stderr.write(" Chat termination for sid " + data['sid'] + "\n") pubsub.unsubscribe(data['sid']) if data['sid'] in secrets: del secrets[data['sid']] r.hdel('da:monitor:chatpartners:' + str(user_id), 'da:interviewsession:uid:' + str(data['uid']) + ':i:' + str(data['i']) + ':userid:' + data['userid']) socketio.emit('chatstop', {'uid': data['uid'], 'i': data['i'], 'userid': data['userid']}, namespace='/monitor', room=sid) sys.stderr.write(' exiting monitor thread for sid ' + str(sid) + '\n')