Commit 3d89ed5e authored by Ravi Tripathi's avatar Ravi Tripathi Committed by Mitchell Moore
Browse files

On client connecting, emit join_room, added function to send socketio message...

On client connecting, emit join_room, added function to send socketio message to client, matched socketio message_queueu to the flask. Added join_room function to recieve new connections

- Added commented out sections to tasks for future use
- Commenting out socketio emit before calling celery task
- modify the message_queue in socketio app
- Add a new function to send socketio messages to the client, and change the socketio message_queue to match the one from flask app
- Remove username value added, as well as the parameter username passed in render_template
- On connecting, send emit fuction to join_roon
- Adding on_room fuction taht gets triggered when connection is made, and we need to join a room for the communcation to take place between client and server
- Removing unused functions and commented out lines
parent 742bda69
......@@ -21,9 +21,6 @@ def create_app(config_name):
def index():
global return_url
if 'username' not in session:
session['username'] = "name_test" + str(random.randint(0, 10000))
if 'uid' not in session:
session['uid']=str(uuid.uuid4())
......@@ -31,7 +28,7 @@ def create_app(config_name):
return_url = request.args.get("redir") or "/pun/sys/dashboard"
if name != "":
return render_template('auth/SignUp.html', user=session['username'], room_id=session['uid'])
return render_template('auth/SignUp.html', room_id=session['uid'])
# misc page error catching
@app.errorhandler(403)
......
......@@ -11,9 +11,7 @@
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on( 'connect', function() {
socket.emit( 'user connected', {
user: document.getElementById("username").innerText
} )
socket.emit( 'join_room' )
socket.on( 'creating account', function( msg ) {
document.getElementById("error").innerText = "";
......
......@@ -5,67 +5,42 @@ import time
import signal
import tasks
from flask import session
from flask_socketio import SocketIO, join_room
from app import create_app
from gevent import monkey
monkey.patch_all(subprocess=True)
config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)
# global username_global
# global room_global
socketio = SocketIO(app, message_queue='amqp://reggie:reggie@ohpc:5672/socketio')
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')
socketio.emit("creating account", room=room_global)
signal.signal(signal.SIGALRM, account_agent)
signal.alarm(5)
@socketio.on('connect')
def socket_connect():
pass
@socketio.on('join_room')
def on_room():
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.
username = session['username']
room = str(session['uid'])
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit("account ready", room=room)
return True
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
username = json["user"]
room = str(session['uid'])
join_room(room)
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' connected.')
print('\t\t\t|-----Room ID: ' + room)
@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))
print(json)
room = str(session['uid'])
socketio.emit("creating account", room=room)
sid = str(session['uid'])
print("Room: {}".format(sid))
try:
# create_account(json['username'], json['fullname'], json['reason'])
tasks.celery_create_account(json['username'], json['fullname'], json['reason'], room, socketio)
tasks.celery_create_account.delay(json['username'], json['fullname'], json['reason'], session=sid)
except Exception as e:
print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
socketio.emit("Account creation failed", room)
if __name__ == '__main__':
# app.run()
socketio.run(app, host='0.0.0.0')
......@@ -7,20 +7,20 @@ from gevent import monkey
monkey.patch_all(subprocess=True)
broker_url = 'amqp://reggie:reggie@ohpc:5672/'
#broker_url = 'amqp://'
celery = Celery('flask_user_reg', broker=broker_url)
# socketio = SocketIO(message_queue='amqp:///socketio')
socketio = SocketIO(message_queue='amqp://reggie:reggie@ohpc:5672/socketio')
def send_msg(event, room):
print("Post '{}' to room '{}'".format(event,room))
socketio.emit(event, room=room)
@celery.task
def celery_create_account(username, fullname, reason, room, socketio):
# Todo: Ravi's and Louis's code goes here
def celery_create_account(username, fullname, reason, session):
room = session
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
socketio.emit("creating account", room=room)
send_msg('creating account', room)
print(username)
subprocess.call(["/opt/rabbitmq_agents/flask_producer.py", "ohpc_account_create", username])
time.sleep(5)
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit('account ready', room=room)
send_msg('account ready', room)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment