2019-09-02 08:42:28 +00:00
|
|
|
from flask import current_app, request
|
2019-08-27 08:40:29 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-11-07 09:44:02 +00:00
|
|
|
from . import socketio
|
2019-11-06 12:22:18 +00:00
|
|
|
from .decorators import admin_required
|
|
|
|
from .models import User
|
2019-08-27 08:40:29 +00:00
|
|
|
import json
|
2019-08-29 08:42:53 +00:00
|
|
|
import jsonpatch
|
2019-11-04 12:18:55 +00:00
|
|
|
import logging
|
2019-09-02 08:59:13 +00:00
|
|
|
|
2019-08-28 15:27:43 +00:00
|
|
|
|
2019-08-29 08:42:53 +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.
|
2019-08-29 08:42:53 +00:00
|
|
|
'''
|
2019-11-06 13:35:33 +00:00
|
|
|
connected_sessions = []
|
2019-08-27 08:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('connect')
|
|
|
|
@login_required
|
|
|
|
def connect():
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
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.
|
2019-09-02 07:17:00 +00:00
|
|
|
'''
|
2019-11-06 13:35:33 +00:00
|
|
|
connected_sessions.append(request.sid)
|
2019-08-28 15:27:43 +00:00
|
|
|
|
|
|
|
|
2019-11-06 13:35:33 +00:00
|
|
|
@socketio.on('disconnect')
|
|
|
|
@login_required
|
|
|
|
def disconnect():
|
|
|
|
'''
|
2019-11-07 09:44:02 +00:00
|
|
|
' On disconnect the session id gets removed from the connected sessions
|
|
|
|
' list.
|
2019-11-06 13:35:33 +00:00
|
|
|
'''
|
|
|
|
connected_sessions.remove(request.sid)
|
|
|
|
|
|
|
|
|
2019-11-07 09:44:02 +00:00
|
|
|
@socketio.on('subscribe_user_ressources')
|
|
|
|
@login_required
|
|
|
|
def subscribe_user_ressources():
|
|
|
|
socketio.start_background_task(user_ressource_subscription_handler,
|
|
|
|
current_app._get_current_object(),
|
|
|
|
current_user.id, request.sid)
|
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('subscribe_foreign_user_ressources')
|
2019-09-18 09:33:06 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-11-07 09:44:02 +00:00
|
|
|
def subscribe_foreign_user_ressources(user_id):
|
|
|
|
socketio.start_background_task(user_ressource_subscription_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
|
|
|
|
|
|
|
|
2019-11-07 09:44:02 +00:00
|
|
|
def user_ressource_subscription_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.
|
|
|
|
'
|
|
|
|
' NOTE: The initial values are send as a init-* events.
|
|
|
|
' The JSON patches are send as update-* events.
|
2019-11-07 09:44:02 +00:00
|
|
|
' > where '*' is either 'corpora' or 'jobs'
|
2019-09-18 09:33:06 +00:00
|
|
|
'''
|
2019-11-07 09:44:02 +00:00
|
|
|
init_events = {'corpora': 'init-foreign-corpora' if foreign
|
|
|
|
else 'init-corpora',
|
|
|
|
'jobs': 'init-foreign-jobs' if foreign else 'init-jobs'}
|
|
|
|
update_events = {'corpora': 'update-foreign-corpora' if foreign
|
|
|
|
else 'update-corpora',
|
|
|
|
'jobs': 'update-foreign-jobs' if foreign
|
|
|
|
else 'update-jobs'}
|
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.
|
|
|
|
user = User.query.filter_by(id=user_id).first()
|
2019-09-18 09:33:06 +00:00
|
|
|
corpora = user.corpora_as_dict()
|
|
|
|
jobs = user.jobs_as_dict()
|
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)
|
2019-11-06 13:35:33 +00:00
|
|
|
while session_id in connected_sessions:
|
2019-11-07 09:44:02 +00:00
|
|
|
# Get new values from the database
|
2019-09-18 09:33:06 +00:00
|
|
|
new_corpora = user.corpora_as_dict()
|
|
|
|
new_jobs = user.jobs_as_dict()
|
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)
|