def do_replicate(sock: socket, log: str, loader_ip: str, job: int, paths: list) -> bool: if prepare_replicate(loader_ip, job, paths): SendJob(sock, job) SendStr(sock, loader_ip) return LogResponse(sock, log) Logger.add(log + ' - fail') return False
def upload(sock: socket) -> ResultType: path = RecvStr(sock) if _cant_add_node(sock, path): return Result_Denied # Create Job job = Jobs.new(sock) # Select server loader = '' for ip in Storages.GetAliveServers(): if CallNSP(ip, NSP.upload, job, path): loader = ip break if not loader: SendResponse(sock, '\'%s\' cannot be uploaded' % path) Jobs.complete(job) return Result_Denied # Response client loaderfs = Storages.GetStorage(loader) SendResponse(sock, SUCCESS) SendJob(sock, job) SendStr(sock, loaderfs.pubip) # Get answer from client re = RecvResponse(sock) # Complete job Jobs.complete(job) # Check answer if re != SUCCESS: return Result_Fail # All OK, add on actual Actual.add(path, False) # Add to storage if path in loaderfs: loaderfs.remove(path) loaderfs.add(path, False) # Log Logger.addHost(*sock.getpeername(), 'has added \'%s\'' % path) # Init replication NSP.ReplicateFromOne(loader, [path]) return Result_Success
def download(sock: socket) -> ResultType: path = RecvStr(sock) Logger.addHost(*sock.getpeername(), 'attempts to download \'%s\'' % path) # Check if exists if path not in Actual: SendResponse(sock, '\'%s\' does not exist on remote' % path + Mes_UpdateLocal) return Result_Denied # Check if file if _is_dir(sock, path): return Result_Denied # Can be downloaded # Create Job job = Jobs.new(sock) # Select server loader = '' for ip in Storages.GetASWithPath(path): if CallNSP(ip, NSP.download, job, path): loader = ip break # No server has file - cannot be downloaded if not loader: SendResponse(sock, '\'%s\' cannot be downloaded' % path + Mes_UpdateLocal) Jobs.complete(job) return Result_Denied # Response client SendResponse(sock, SUCCESS) SendJob(sock, job) SendStr(sock, Storages.GetStorage(loader).pubip) # Get answer from client re = RecvResponse(sock) # Complete job Jobs.complete(job) # Check answer if re != SUCCESS: return Result_Fail # Log Logger.addHost(*sock.getpeername(), 'has downloaded \'%s\'' % path) return Result_Success
def wrapper(sock: socket, job: int, *args): SendWMI(sock, Client) SendJob(sock, job) return func(sock, *args)
def prepare_replicate(sock: socket, log: str, job: int, paths: list) -> bool: send = PathSeparator.join(paths) SendJob(sock, job) SendStr(sock, send) return LogResponse(sock, log)
def download(sock: socket, log: str, job: int, path: str) -> bool: SendJob(sock, job) SendStr(sock, path) return LogResponse(sock, log)
def wrapper(sock: socket, job: int, *args): SendWMI(sock, StorageServer) SendJob(sock, job) return func(sock, *args)