nopaque/app/events.py

96 lines
3.7 KiB
Python
Raw Normal View History

from flask import current_app, request
from flask_login import current_user
2019-11-07 09:44:02 +00:00
from . import socketio
from .decorators import socketio_admin_required, socketio_login_required
2019-11-06 12:22:18 +00:00
from .models import User
import json
import jsonpatch
2019-09-02 08:59:13 +00:00
'''
2019-11-07 09:44:02 +00:00
' A list containing session ids of connected Socket.IO sessions, to keep track
' of all connected sessions, which is used to determine the runtimes of
' associated background tasks.
'''
connected_sessions = []
@socketio.on('connect')
def connect():
'''
2019-11-07 09:44:02 +00:00
' The Socket.IO module creates a session id (sid) for each request.
' On connect the sid is saved in the connected sessions list.
'''
connected_sessions.append(request.sid)
@socketio.on('disconnect')
def disconnect():
'''
2019-11-07 09:44:02 +00:00
' On disconnect the session id gets removed from the connected sessions
' list.
'''
connected_sessions.remove(request.sid)
2020-01-28 09:08:09 +00:00
@socketio.on('user_ressources_init')
@socketio_login_required
2019-11-07 09:44:02 +00:00
def subscribe_user_ressources():
2020-01-28 09:08:09 +00:00
socketio.start_background_task(user_ressource_session_handler,
2019-11-07 09:44:02 +00:00
current_app._get_current_object(),
current_user.id, request.sid)
2020-01-28 09:08:09 +00:00
@socketio.on('foreign_user_ressources_init')
@socketio_login_required
@socketio_admin_required
2019-11-07 09:44:02 +00:00
def subscribe_foreign_user_ressources(user_id):
2020-01-28 09:08:09 +00:00
socketio.start_background_task(user_ressource_session_handler,
2019-09-18 09:33:06 +00:00
current_app._get_current_object(),
2019-11-07 09:44:02 +00:00
user_id, request.sid, True)
2019-09-18 09:33:06 +00:00
2020-01-28 09:08:09 +00:00
def user_ressource_session_handler(app, user_id, session_id, foreign=False):
2019-09-18 09:33:06 +00:00
'''
' Sends initial corpus and job lists to the client. Afterwards it checks
' every 3 seconds if changes to the initial values appeared. If changes are
' detected, a RFC 6902 compliant JSON patch gets send.
'
2020-01-28 09:08:09 +00:00
' NOTE: The initial values are send as a init events.
' The JSON patches are send as update events.
2019-09-18 09:33:06 +00:00
'''
2020-03-27 09:13:42 +00:00
init_events = \
{'corpora': 'foreign_corpora_init' if foreign else 'corpora_init',
'jobs': 'foreign_jobs_init' if foreign else 'jobs_init'}
update_events = \
{'corpora': 'foreign_corpora_update' if foreign else 'corpora_update',
'jobs': 'foreign_jobs_update' if foreign else 'jobs_update'}
2019-09-18 09:33:06 +00:00
with app.app_context():
2019-11-07 09:44:02 +00:00
# Gather current values from database.
2019-11-15 12:09:12 +00:00
user = User.query.get(user_id)
corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora}
jobs = {job.id: job.to_dict() for job in user.jobs}
2019-11-07 09:44:02 +00:00
# Send initial values to the user.
socketio.emit(init_events['corpora'], json.dumps(corpora),
2019-09-18 09:33:06 +00:00
room=session_id)
2019-11-07 09:44:02 +00:00
socketio.emit(init_events['jobs'], json.dumps(jobs), room=session_id)
while session_id in connected_sessions:
2019-11-07 09:44:02 +00:00
# Get new values from the database
2020-03-27 09:13:42 +00:00
new_corpora = {corpus.id: corpus.to_dict()
for corpus in user.corpora}
new_jobs = {job.id: job.to_dict() for job in user.jobs}
2019-11-07 09:44:02 +00:00
# Compute JSON patches.
corpora_patch = jsonpatch.JsonPatch.from_diff(corpora, new_corpora)
2019-09-18 09:33:06 +00:00
jobs_patch = jsonpatch.JsonPatch.from_diff(jobs, new_jobs)
2019-11-07 09:44:02 +00:00
# In case there are patches, send them to the user.
if corpora_patch:
socketio.emit(update_events['corpora'],
corpora_patch.to_string(), room=session_id)
2019-09-18 09:33:06 +00:00
if jobs_patch:
2019-11-07 09:44:02 +00:00
socketio.emit(update_events['jobs'], jobs_patch.to_string(),
2019-09-18 09:33:06 +00:00
room=session_id)
2019-11-07 09:44:02 +00:00
# Set new values as references for the next iteration.
2019-09-18 09:33:06 +00:00
corpora = new_corpora
jobs = new_jobs
socketio.sleep(3)