async def print_message(sid, message): """ Generic message handler mostly for testing """ # When we receive a new event of type # 'message' through a socket.io connection # we print the socket ID and the message log("Socket ID: " + sid + ' message: ' + str(message))
async def index(request): """ handler for index GET requests """ log('Index requested by {client_address}'.format( client_address=request.remote)) with open(static_path + 'main.html') as f: return web.Response(text=f.read(), content_type='text/html')
async def new_connection(sid, environ): """ Sets up a User object for each session. A single computer can have multiple sessions and sockets open. """ address = environ['aiohttp.request'].remote await sio.save_session(sid, { 'user': User(sid=sid, address=address), 'address': address }) # save information about the user log("New session for host: " + address)
async def receive_schedule(sid, data): """ event handler for when a User attempts to generate a schedule """ session = await sio.get_session(sid) log('Generate schedule reqeuest from: ' + session['address']) await sio.emit( 'comfirmation', 'Data Received. Optimizing Schedule...', room=sid ) # NOTE typo on the UI for comfirmation, should be confirmation user = session['user'] user.input_data = data try: sio.start_background_task( target=generate_schedule, args=user) # this library's use of asyncio is questionable. except Exception as e: log('Something went wrong', level=LOG_ERROR, err_str=str(e))
def main(): """ prep necessary routes then start the server. """ setup_log() # default log level is debug # router global static_path static_path = '/var/www/html/src/' # base bath to all of the static files. # static_path = '/home/nate/ASAP/Frontend/' app.router.add_get('/', index) # redirect to index on initial visit. app.router.add_static( '/', static_path) # automatically add routes for everything else # app.add_routes([web.static('/', static_path)]) # this works # We kick off our server log('Starting ASAP socket server') try: web.run_app(app, host='asap.ucsd.edu', port=80) # this is blocking # web.run_app(app) # localhost for debugging. Doesn't require root to run log('Stoppping ASAP socket server') except Exception as e: log('The server messed up', level=LOG_ERROR, email=False, err_str=str(e))
async def remove_connection(sid): session = await sio.get_session(sid=sid) log('Connection ended: ' + session['address'])
async def generate_schedule(args): """ Middle man between socket server and Schedule.py. This allows for proper asynchronous calls so we don't slow down the webserver. Paremters: input_data: raw data string from the client. user: user object pertaining to who requested the schedule """ user = args input_dict = json.loads(user.input_data) log('input_dict\n' + str(input_dict)) user_courses = input_dict['course'] currentTerm = input_dict['currentTerm'] personalEvents = input_dict['personalEvent'] waitlist_okay = False if input_dict['waitlistStat'].upper() == 'TRUE': waitlist_okay = True start_time = time() must_haves, could_haves = ScheduleofClasses.get_section_pairings(user_courses=user_courses, termCode=currentTerm, \ personalEvents=personalEvents, waitlist_okay=waitlist_okay) log(message='get_section_pairings took ' + str(round(time() - start_time, 5))) log('must_haves:\n' + str(must_haves) + '\nwant_to_haves: \n' + str(could_haves) + '\nprefs: \n:' + str(input_dict['preference'])) start_time = time() user.schedule = Schedule.generateSchedule(must_haves=must_haves, want_to_haves=could_haves, \ preferences=input_dict['preference']) log(message='generateSchedule took' + str(round(time() - start_time, 5))) log('Schedule: \n' + str(user.schedule)) # save raw output of schedule new_display, new_schedule = convert_schedule(user) # test_schedule = json.dumps(test) log("Converted Schedule:\n" + str(new_schedule) + '\n' + str(new_display)) display_schedule = json.dumps({ 'display': new_display, 'schedule': new_schedule }) # json to string await sio.emit('schedule_ready', display_schedule, room=user.sid) log('Sent schedule to: ' + str(user.address))