mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-26 03:14:19 +00:00
88 lines
3.0 KiB
Python
88 lines
3.0 KiB
Python
from flask import current_app, request
|
|
from flask_login import current_user
|
|
from . import db, socketio
|
|
from .decorators import socketio_admin_required, socketio_login_required
|
|
from .models import User
|
|
import json
|
|
import jsonpatch
|
|
|
|
|
|
'''
|
|
' 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():
|
|
'''
|
|
' 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():
|
|
'''
|
|
' On disconnect the session id gets removed from the connected sessions
|
|
' list.
|
|
'''
|
|
connected_sessions.remove(request.sid)
|
|
|
|
|
|
@socketio.on('user_data_stream_init')
|
|
@socketio_login_required
|
|
def user_data_stream_init():
|
|
socketio.start_background_task(user_data_stream,
|
|
current_app._get_current_object(),
|
|
current_user.id, request.sid)
|
|
|
|
|
|
@socketio.on('foreign_user_data_stream_init')
|
|
@socketio_login_required
|
|
@socketio_admin_required
|
|
def foreign_user_data_stream_init(user_id):
|
|
socketio.start_background_task(user_data_stream,
|
|
current_app._get_current_object(),
|
|
user_id, request.sid, True)
|
|
|
|
|
|
def user_data_stream(app, user_id, session_id, foreign=False):
|
|
'''
|
|
' 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.
|
|
'''
|
|
if foreign:
|
|
init_event = 'foreign_user_data_stream_init'
|
|
update_event = 'foreign_user_data_stream_update'
|
|
else:
|
|
init_event = 'user_data_stream_init'
|
|
update_event = 'user_data_stream_update'
|
|
with app.app_context():
|
|
# Gather current values from database.
|
|
user = User.query.get(user_id)
|
|
user_dict = user.to_dict()
|
|
# Send initial values to the client.
|
|
socketio.emit(init_event, json.dumps(user_dict), room=session_id)
|
|
while session_id in connected_sessions:
|
|
# Get new values from the database
|
|
db.session.refresh(user)
|
|
new_user_dict = user.to_dict()
|
|
# Compute JSON patches.
|
|
user_patch = jsonpatch.JsonPatch.from_diff(user_dict,
|
|
new_user_dict)
|
|
# In case there are patches, send them to the client.
|
|
if user_patch:
|
|
socketio.emit(update_event, user_patch.to_string(),
|
|
room=session_id)
|
|
# Set new values as references for the next iteration.
|
|
user_dict = new_user_dict
|
|
socketio.sleep(3)
|