def get(self): #user is uid user = get_current_user(self) if not user: user = self.get_argument( "user", None) # mock user since node client has no cookie if user: try: uid = int(user) except ValueError: err = json.dumps({"err": 4, "what": "uid invalid"}) self.write(err) return #TODO user_name = "TODO FIXME" # get the real user name from DB file_name = self.get_argument("file_name", None) file_hash = self.get_argument("file_hash", None) tags = self.get_argument("tags", None) try: main_type = int(self.get_argument("main_type", 0)) sub_type = int(self.get_argument("sub_type", 0)) res_grade = float(self.get_argument("res_grade", 1)) file_size = int(self.get_argument("file_size", None)) except ValueError: err = json.dumps({ "err": 3, "what": "main_type sub_type or res_grade invalid" }) self.write(err) return comment = self.get_argument("comment", None) try: is_public = int(self.get_argument("is_public", 1)) #default is public except ValueError: err = json.dumps({"err": 5, "what": "is_public invalid"}) self.write(err) return if file_name and file_hash and file_size and tags and \ res_grade and comment: tags_list = tags.split(" ") if (len(tags_list) > 0): ResourcesCache.user_upload_resource( uid, user_name, file_hash, file_name, file_size, is_public, tags_list, main_type, sub_type, res_grade, comment) ok = json.dumps({"err": 0}) self.write(ok) else: err = json.dumps({ "err": 2, "what": "tags or blocks hash invalid" }) self.write(err) else: err = json.dumps({"err": 1, "what": "argument missing error"}) self.write(err)
def get(self): #user is uid user = get_current_user(self) if not user: user = self.get_argument( "user", None) # mock user since node client has no cookie try: uid = int(user) except: err = json.dumps({"err": 4, "what": "uid invalid"}) self.write(err) return file_hash = self.get_argument("file_hash", None) #default is public if not file_hash: err = json.dumps({"err": 5, "what": "file hash err"}) self.write(err) return res_header = ResourcesCache.get_resource_header(file_hash) online_owners = DownloadMedium.get_online_file_owner(uid, file_hash) ok = json.dumps({ "err": 0, "file_info": res_header, "owners": online_owners }) self.write(ok)
def get(self): resource_list = ResourcesCache.get_resources_list() resource_data = json.dumps({ "err": 0, "resource_list": resource_list }, cls=SetEncoder) self.write(resource_data)
def get(self): resource_list = ResourcesCache.get_resources_overview() for file_hash in resource_list: resource_list[file_hash][ "online_owners_num"] = DownloadMedium.get_online_user_cnt( file_hash) resource_data = json.dumps({"err": 0, "resource_list": resource_list}) self.write(resource_data)
def get(self): #user is uid user = get_current_user(self) if not user: user = self.get_argument("user", None) # mock user since node client has no cookie try: uid = int(user) except: err = json.dumps({"err": 4, "what": "uid invalid"}) self.write(err) return file_hash = self.get_argument("file_hash", None) #default is public if not file_hash: err = json.dumps({"err": 5, "what": "file hash err"}) self.write(err) return ResourcesCache.add_owner(file_hash,uid) ResourcesCache.increase_download_num(file_hash) ok = json.dumps({"err": 0}) self.write(ok)
def get(self): #user is uid user = get_current_user(self) if not user: user = self.get_argument("user", None) # mock user since node client has no cookie try: uid = int(user) except: err = json.dumps({"err": 4, "what": "uid invalid"}) self.write(err) return resource_list = ResourcesCache.get_my_resource(uid) resource_data = json.dumps({"err": 0, "resource_list": resource_list}, cls=SetEncoder) self.write(resource_data)
def get_online_file_owner(cls, my_uid, file_hash): assert len(file_hash) > 0 assert my_uid >= 0 online_owners = filter(UserIPCache.user_online, ResourcesCache.get_resource_owners(file_hash)) # if online_owners.count(my_uid): # remove myself # online_owners.remove(my_uid) if (UserIPCache.user_online(my_uid)): my_ip = UserIPCache.get_user_ip(my_uid) is_ipv4 = IP.is_valid_ipv4_address(my_ip) if is_ipv4: same_ip_users = filter(lambda user: UserIPCache.get_user_ip(user) == my_ip, online_owners) v4_owners = [user for user in same_ip_users if IP.is_valid_ipv4_address(HttpServerInfoCache.get_user_ipv4(user))] grep_owners = [{"uid": user, "host": HttpServerInfoCache.get_user_ipv4(user), "port": 8884} for user in v4_owners] return grep_owners else: v6_owners = [user for user in online_owners if IP.is_valid_ipv6_address(HttpServerInfoCache.get_user_ipv6(user))] grep_owners = [{"uid": user, "host": HttpServerInfoCache.get_user_ipv6(user), "port": 8886} for user in v6_owners] return grep_owners else: return []
def test_download(): UserIPCache.update_my_ip(1,"1.1.1.1") UserIPCache.update_my_ip(2,"2.2.2.2") UserIPCache.update_my_ip(3,"2.2.2.2") UserIPCache.update_my_ip(4,"2.2.2.2") #2,3,4 are the same IP UserIPCache.update_my_ip(5,"2:2:2::2") UserIPCache.update_my_ip(6,"2:2:2::3") UserIPCache.update_my_ip(7,"5:2:2::3") UserIPCache.update_my_ip(8,"5:4:2::3") HttpServerInfoCache.update_ipv4_address(1,'192.168.1.101') HttpServerInfoCache.update_ipv4_address(2,'192.168.1.103') HttpServerInfoCache.update_ipv4_address(3,'192.168.1.104') HttpServerInfoCache.update_ipv4_address(4,'192.168.1.105') HttpServerInfoCache.update_ipv6_address(5,'2:2:2::2') HttpServerInfoCache.update_ipv6_address(6,'2:2:2::3') HttpServerInfoCache.update_ipv6_address(7,"5:2:2::3") #7,8 are the same IP HttpServerInfoCache.update_ipv6_address(8,"5:4:2::3") # user 2,3,4 uploaded the same file ResourcesCache.user_upload_resource(2,"user2","file_hash2","test2.txt",1023,1,"test",0,1,3,"user2 uploaded file") ResourcesCache.user_upload_resource(3,"user3","file_hash2","test2.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash2","test2.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 3,4 uploaded the same file ResourcesCache.user_upload_resource(3,"user3","file_hash3","test4.txt",1023,1,"test",0,1,3,"user3 uploaded file") ResourcesCache.user_upload_resource(4,"user4","file_hash3","test4.txt",1023,1,"test",0,1,3,"user4 uploaded file") # user 5,6,7 uploaded the same file ResourcesCache.user_upload_resource(5,"user5","file_hash5","test5.txt",10123,1,"test",0,1,3,"user5 uploaded file") ResourcesCache.user_upload_resource(6,"user6","file_hash5","test5.txt",10123,1,"test",0,1,3,"user6 uploaded file") ResourcesCache.user_upload_resource(7,"user7","file_hash5","test5.txt",10123,1,"test",0,1,3,"user7 uploaded file") assert DownloadMedium.get_online_file_owner(1,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash5") == [] assert DownloadMedium.get_online_file_owner(1,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash_not_exist") == [] assert DownloadMedium.get_online_file_owner(8,"file_hash2") == [] assert DownloadMedium.get_online_file_owner(2,"file_hash3") == [{"uid": 3, "host": '192.168.1.104', "port":8884},{"uid": 4, "host": '192.168.1.105', "port":8884}] assert DownloadMedium.get_online_file_owner(8,"file_hash5") == [{"uid": 5, "host": '2:2:2::2', "port":8886},{"uid": 6, "host": '2:2:2::3', "port":8886},{"uid": 7, "host": "5:2:2::3", "port":8886}] #if __name__ == "__main__": # test_download()
def get_online_user_cnt(cls,file_hash): return len(filter(UserIPCache.user_online, ResourcesCache.get_resource_owners(file_hash)))
def test_add_owner(): # user 2 uploaded the resource ResourcesCache.user_upload_resource(2,"user2","file_hash2","test2.txt",1023,1,"test",0,1,3,"user2 uploaded file") ResourcesCache.add_owner('file_hash2',2) #the same user assert ResourcesCache.get_resource_owners("file_hash2")==[2] ResourcesCache.add_owner('file_hash2',20) #the same user assert ResourcesCache.get_resource_owners("file_hash2")==[2,20] ResourcesCache.increase_download_num("file_hash2") assert ResourcesCache.get_resources_list()["file_hash2"]["download_num"]==0 ResourcesCache.increase_download_num("file_hash2") assert ResourcesCache.get_resources_list()["file_hash2"]["download_num"]==1 #if __name__ == "__main__": # test_add_owner()