Пример #1
0
    def GET(self, request_str):
        req_valid, req_type, req_arg = self.parse_request(request_str)
        if req_valid == False:
            return 'Invalid request'
        else:
            # REQUEST NODE INFO
            if req_type == 'RESET':
                db_manager.remove_server()
                db_manager.remove_server_for_cache()
                db_manager.remove_all_videos()
                db_manager.remove_all_nodes()
            elif req_type == 'GET_SERVER_ADDRESS':
                res = db_manager.get_server()
                return str(res[0].ip) + ' ' + str(res[0].port)
            elif req_type == 'GET_SERVER_ADDRESS_FOR_CACHE':
                print 'get_server_address_for_cache'
                res = db_manager.get_server_for_cache()
                return str(res[0].ip) + ' ' + str(res[0].port)
            elif req_type == 'GET_CACHES_ADDRESS':
                # req = "user-hyunah-1 & 10"
                arg_user_name = req_arg.split('_')[0]
                arg_num_of_caches = req_arg.split('_')[1]
                n_of_current_caches = db_manager.get_num_of_caches()
                n_of_returned_caches = min(n_of_current_caches, int(arg_num_of_caches))
                print '[tracker.py] n_of_returned_caches', n_of_returned_caches
                caches = db_manager.get_many_caches(arg_user_name, n_of_returned_caches)
                ret_str = ''
                for cache in caches:
                    ret_str = ret_str + str(cache.ip) + ' ' + str(cache.port) + '\n'
                return ret_str
            # NODE REGISTER
            elif req_type == 'REGISTER_USER':
                # req_arg = "143.243.23.13_324"
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                arg_watching_video = req_arg.split('_')[2]
                db_manager.add_user(arg_ip, arg_port, arg_watching_video)

                print '[tracker.py] Accessing...'
                print '[tracker.py] user_pop', user_population
                user_population[str(arg_watching_video)] += 1
                log_load()

                return 'User is registered'
            elif req_type == 'REGISTER_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                db_manager.add_cache(arg_ip, arg_port)
                return 'Cache is registered'
            elif req_type == 'REGISTER_SERVER':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                # remove existing server & videos
                db_manager.add_server(arg_ip, arg_port)
                return 'Server is registered'
            elif req_type == 'REGISTER_SERVER_FOR_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                # remove existing server & videos
                db_manager.add_server_for_cache(arg_ip, arg_port)
                return 'Server is registered'
            # VIDEO REGISTER
            elif req_type == 'REGISTER_VIDEO':
                print 'add video'
                split_arg = req_arg.split('_')
                arg_vname = split_arg[0]
                arg_n_of_frames = split_arg[1]
                arg_code_param_n = split_arg[2]
                arg_code_param_k = split_arg[3]
                arg_total_size = split_arg[4]
                arg_chunk_size = split_arg[5]
                arg_last_chunk_size = split_arg[6]
                db_manager.add_video(arg_vname, arg_n_of_frames, arg_code_param_n, arg_code_param_k, arg_total_size, arg_chunk_size, arg_last_chunk_size)

                if str(arg_vname) not in user_population.keys():
                    user_population[str(arg_vname)] = 0
                    print '[tracker.py] arg_vname', str(arg_vname)
                    print '[tracker.py] user_pop', user_population

                return 'Video is registered'
            elif req_type == 'GET_ALL_VIDEOS':
                videos = db_manager.get_all_videos()
                ret_str = ''
                for video in videos:
                    ret_str = ret_str + str(video.id) + ' ' + str(video.vname) + ' ' + str(video.n_of_frames) + ' ' + str(video.code_param_n) + ' ' + str(video.code_param_k) + ' ' + str(video.total_size) + ' ' + str(video.chunk_size) + ' ' + str(video.last_chunk_size) + '\n'
                return ret_str
            elif req_type == 'REMOVE_SERVER':
                db_manager.remove_server()
                return 'Server is removed'
            elif req_type == 'REMOVE_SERVER_FOR_CACHE':
                db_manager.remove_server_for_cache()
                return 'Server for cache is removed'
            elif req_type == 'REMOVE_USER':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                arg_watching_video = req_arg.split('_')[2]
                db_manager.remove_user(arg_ip, arg_port, arg_watching_video)

                user_population[str(arg_watching_video)] -= 1
                log_load()

                return 'User is removed'
            elif req_type == 'REMOVE_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                db_manager.remove_cache(arg_ip, arg_port)
                return 'Cache is removed'
            elif req_type == 'UPDATE_CHUNKS_FOR_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                arg_vname = req_arg.split('_')[2]
                arg_chunk_str = req_arg.split('_')[3]
                db_manager.add_chunks_for_cache(arg_ip, arg_port, arg_vname, arg_chunk_str)
            elif req_type == 'UPDATE_SERVER_LOAD':
                arg_vname = req_arg.split('_')[0]
                arg_n_of_chks = req_arg.split('_')[1]
                db_manager.add_server_load(arg_vname, arg_n_of_chks)
Пример #2
0
    def GET(self, request_str):
        req_valid, req_type, req_arg = self.parse_request(request_str)
        if req_valid == False:
            return 'Invalid request'
        else:
            # REQUEST NODE INFO
            if req_type == 'RESET':
                db_manager.remove_server()
                db_manager.remove_server_for_cache()
                db_manager.remove_all_videos()
                db_manager.remove_all_nodes()
                db_manager.remove_all_caches_from_account_cache()
            elif req_type == 'GET_SERVER_ADDRESS':
                if session.get('login', False):
                    res = db_manager.get_server()
                    return str(res[0].ip) + ' ' + str(res[0].port)
                else:
                    raise web.seeother('/login')
            elif req_type == 'GET_SERVER_ADDRESS_FOR_CACHE':
                if session.get('login', False):
                    print 'get_server_address_for_cache'
                    res = db_manager.get_server_for_cache()
                    return str(res[0].ip) + ' ' + str(res[0].port)
                else:
                    raise web.seeother('/login')
            elif req_type == 'GET_CACHES_ADDRESS':
                if session.get('login', False):
                    # req = "user-hyunah-1 & 10"
                    arg_user_name = req_arg.split('_')[0]
                    arg_num_of_caches = req_arg.split('_')[1]
                    n_of_current_caches = db_manager.get_num_of_caches()
                    n_of_returned_caches = min(n_of_current_caches, int(arg_num_of_caches))
                    print '[tracker.py] n_of_returned_caches', n_of_returned_caches
                    caches = db_manager.get_many_caches(arg_user_name, n_of_returned_caches)
                    ret_str = ''
                    for cache in caches:
                        ret_str = ret_str + str(cache.ip) + ' ' + str(cache.port) + '\n'
                    return ret_str
                else:
                    raise web.seeother('/login')
            # NODE REGISTER
            elif req_type == 'REGISTER_USER':
                if session.get('login', False):
                    un = session.user_name
                    # req_arg = "143.243.23.13_324"
                    arg_ip = req_arg.split('_')[0]
                    arg_port = req_arg.split('_')[1]
                    arg_watching_video = req_arg.split('_')[2]
                    #INSERT HERE
                    
                    #Get the list of owned videos here
                    owned_videos= db_manager.get_owned_videos(un)
                    if arg_watching_video not in owned_videos:
                        #If requested video is not in the list, subtract 5 points
                        res = db_manager.update_owned_videos(un, arg_watching_video)
                        #If not enough points, return that says so
                        if res == 'Not enough points':
                            return 'Not enough points'           
                    db_manager.add_user(arg_ip, arg_port, arg_watching_video)

                    print '[tracker.py] Accessing...'
                    print '[tracker.py] user_pop', user_population
                    user_population[str(arg_watching_video)] += 1
                    log_load()
                    return 'User is registered'
                else:
                    raise web.seeother('/login')
            elif req_type == 'REGISTER_CACHE':
                if session.get('login', False):
                    ##
                    arg_ip = req_arg.split('_')[0]
                    arg_port = req_arg.split('_')[1]
                    multiplier = req_arg.split('_')[2]
                    #arg_config = req_arg.split('_')[2] TODO: cache size
                    db_manager.add_cache(arg_ip, arg_port)
                    un = session.user_name
                    db_manager.add_cache_to_account_cache(un, arg_ip, arg_port, multiplier)
                    return 'Cache is registered'
                else:
                    raise web.seeother('/login')
            elif req_type == 'REGISTER_SERVER':
                #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                # remove existing server & videos
                db_manager.add_server(arg_ip, arg_port)
                return 'Server is registered'
            elif req_type == 'REGISTER_SERVER_FOR_CACHE':
                #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                # remove existing server & videos
                db_manager.add_server_for_cache(arg_ip, arg_port)
                return 'Server is registered'
            # VIDEO REGISTER
            elif req_type == 'REGISTER_VIDEO':
                #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE
                print 'add video'
                split_arg = req_arg.split('_')
                arg_vname = split_arg[0]
                arg_n_of_frames = split_arg[1]
                arg_code_param_n = split_arg[2]
                arg_code_param_k = split_arg[3]
                arg_total_size = split_arg[4]
                arg_chunk_size = split_arg[5]
                arg_last_chunk_size = split_arg[6]
                db_manager.add_video(arg_vname, arg_n_of_frames, arg_code_param_n, arg_code_param_k, arg_total_size, arg_chunk_size, arg_last_chunk_size)

                if str(arg_vname) not in user_population.keys():
                    user_population[str(arg_vname)] = 0
                    print '[tracker.py] arg_vname', str(arg_vname)
                    print '[tracker.py] user_pop', user_population

                return 'Video is registered'
            elif req_type == 'GET_OWNED_VIDEOS':
                if session.get('login', False):
                    un = session.user_name
                    owned_videos= db_manager.get_owned_videos(un)
                    
                    ##
                    web.header('Content-Type', 'application/json')
                    return json.dumps(owned_videos)
                else:
                    raise web.seeother('/login')    
            elif req_type == 'GET_ALL_VIDEOS':
                #users can still get all videos
                videos = db_manager.get_all_videos()
                ret_str = ''
                for video in videos:
                    ret_str = ret_str + str(video.id) + ' ' + str(video.vname) + ' ' + str(video.n_of_frames) + ' ' + str(video.code_param_n) + ' ' + str(video.code_param_k) + ' ' + str(video.total_size) + ' ' + str(video.chunk_size) + ' ' + str(video.last_chunk_size) + '\n'
                return ret_str
            elif req_type == 'REMOVE_SERVER':
                db_manager.remove_server()
                return 'Server is removed'
            elif req_type == 'REMOVE_SERVER_FOR_CACHE':
                db_manager.remove_server_for_cache()
                return 'Server for cache is removed'
            elif req_type == 'REMOVE_USER':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                arg_watching_video = req_arg.split('_')[2]
                db_manager.remove_user(arg_ip, arg_port, arg_watching_video)

                user_population[str(arg_watching_video)] -= 1
                log_load()
                return 'User is removed'
            elif req_type == 'REMOVE_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                db_manager.remove_cache(arg_ip, arg_port)
                ##
                un = session.user_name
                #Right now not using username as input, because the server can remove any user it wants.
                #Need to have server logged in as admin for its extra priverlages
                db_manager.remove_cache_from_account_cache(ip=arg_ip, port=arg_port)
                return 'Cache is removed'
            elif req_type == 'UPDATE_CHUNKS_FOR_CACHE':
                arg_ip = req_arg.split('_')[0]
                arg_port = req_arg.split('_')[1]
                arg_vname = req_arg.split('_')[2]
                arg_chunk_str = req_arg.split('_')[3]
                db_manager.add_chunks_for_cache(arg_ip, arg_port, arg_vname, arg_chunk_str)
            elif req_type == 'UPDATE_SERVER_LOAD':
                arg_vname = req_arg.split('_')[0]
                arg_n_of_chks = req_arg.split('_')[1]
                db_manager.add_server_load(arg_vname, arg_n_of_chks)
            elif req_type == 'CACHE_DATA_VIS':
                return render.user_to_cache_data()
            elif req_type == 'CACHE_TO_USER_DATA':
                return render.cache_to_user_data()
            elif req_type =='GET_CACHE_DATA':
                web.header('Content-Type', 'application/json')
                user_data = dv.get_user_logs_as_json()
                return json.dumps(user_data)
                #currently reading from file. must later have post request to store data into db
            elif req_type =='GET_CACHE_DATA2':
                web.header('Content-Type', 'application/json')
                user_data = dv.get_user_logs_as_json()
                user_data = dv.rearrange_data_for_caches(user_data)
                return json.dumps(user_data)
                #currently reading from file. must later have post request to store data into db
            elif req_type == 'VERIFY_ACCOUNT':
                username = req_arg.split('_')[0]
                password = req_arg.split('_')[1]
                if len(db_manager.verify_account(username, password)) == 0:
                    return False
                else:
                    return True