Newer
Older
# run.py
import os
Mitchell Moore
committed
import time
Mitchell Moore
committed
import signal
Mitchell Moore
committed
from flask import session
from flask_socketio import SocketIO, join_room
from app import create_app
config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
Mitchell Moore
committed
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
Mitchell Moore
committed
socketio = SocketIO(app)
Mitchell Moore
committed
global username_global
Mitchell Moore
committed
Mitchell Moore
committed
def create_account(username, fullname, reason):
# Todo: Ravi's and Louis's code goes here
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
global username_global
username_global = username
socketio.emit("creating account")
signal.signal(signal.SIGALRM, account_agent)
signal.alarm(5)
Mitchell Moore
committed
Mitchell Moore
committed
def account_agent(*args):
# Todo: Code to create a consumer based on the username goes here
# Todo: Goal is to have it listening for confirmation.
global username_global
username = username_global
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit("account ready")
return True
Mitchell Moore
committed
Mitchell Moore
committed
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
username = json["user"]
Mitchell Moore
committed
Mitchell Moore
committed
room = str(session['uid'])
Mitchell Moore
committed
Mitchell Moore
committed
join_room(room)
Mitchell Moore
committed
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
Mitchell Moore
committed
print('\t\t\t|-----Room ID: ' + room)
Mitchell Moore
committed
Mitchell Moore
committed
@socketio.on('request account')
def request_account(json, methods=['GET', 'POST']):
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
Mitchell Moore
committed
Mitchell Moore
committed
try:
create_account(json['username'], json['fullname'], json['reason'])
except Exception as e:
print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
Mitchell Moore
committed
socketio.emit("Account creation failed")
if __name__ == '__main__':
Mitchell Moore
committed
# app.run()
socketio.run(app)