def sendServerDownloadInfoToClient(self, dicData, client): fileName = dicData['fileName'] fileExist = False fileServerInfor = [] for fileInfo in self.fileInfo: if fileInfo["fileName"] == fileName: fileExist = True blockSize = fileInfo["blockSize"] #返回两个server供下载 for serverPosition in fileInfo["serverPosition"]: if serverPosition["blockEnd"] < blockSize: fileServerInfor.append(dict(serverPosition)) break for serverPosition in fileInfo["serverPosition"]: if serverPosition["blockEnd"] == blockSize: fileServerInfor.append(dict(serverPosition)) break else: logging.warning("file information is not right!!") if not fileExist: logging.warning("No such file exists!") if len(fileServerInfor) < 2: logging.warning("alive server is not enough!") packetData = pickle.dumps(fileServerInfor) client.send(packetData) client.close() logging.info("send download file information to client: '{0}' to monitor".format(pickle.unpack(packetData)))
def read_crypt2(filename): with open(filename) as fin: pickled = struct.unpack('<H', fin.read(struct.calcsize('<H')))[0] metadata = pickle.unpack(pickled) if metadata.version is not formatVersion: raise VersionError('Version {} does not match {}'.format( formatVersion, metadata.version)) file = struct.unpack('<Q', fin.read(struct.calcsize('<Q')))[0] aesKey = struct.unpack('<L', fin.read(struct.calcsize('<L')))[0] if metadata.rsa: rsaKey = struct.unpack('<L', fin.read(struct.calcsize('<L')))[0]
def sendServerUploadInfoToClient(self, dicData, client): if 'blockSize' in dicData and 'fileName' in dicData: aliveLength = len(self.aliveServerData) #只考虑为四台或三台 else: logging.warning("can not get necessary information(blocksize) form client") return -1 #文件名不能已经存在 for fileInfo in self.fileInfo: if fileInfo["fileName"] == dicData['fileName']: logging.warning("上传文件已存在,请检查文件名") return -1 # 两台服务器直接接受客户端文件 and 一台或两台通过服务器备份 firstServerFileEndSize = math.ceil(dicData['blockSize']/2) self.aliveServerData[0]['blockStart'] = 1 self.aliveServerData[0]['blockEnd'] = firstServerFileEndSize self.aliveServerData[1]['blockStart'] = firstServerFileEndSize + 1 self.aliveServerData[1]['blockEnd'] = dicData['blockSize'] if aliveLength == 3: #上传时就已经损坏一台 self.aliveServerData[2]['blockStart'] = 1 self.aliveServerData[2]['blockEnd'] = dicData['blockSize'] elif aliveLength == 4: self.aliveServerData[2]['blockStart'] = 1 self.aliveServerData[2]['blockEnd'] = firstServerFileEndSize self.aliveServerData[3]['blockStart'] = firstServerFileEndSize + 1 self.aliveServerData[3]['blockEnd'] = dicData['blockSize'] else: logging.warning("the server number is not right") fileInfo = dict()#保存文件信息 fileInfo['fileName'] = dicData['fileName'] fileInfo['blockSize'] = dicData['blockSize'] fileInfo["serverPosition"] = [] for server in self.aliveServerData: tempPostionInfo = dict() tempPostionInfo["ip"] = server["ip"] tempPostionInfo["port"] = server["port"] tempPostionInfo["blockStart"] = server["blockStart"] tempPostionInfo["blockEnd"] = server["blockEnd"] fileInfo["serverPosition"].append(tempPostionInfo) self.fileInfo.append(fileInfo) packetData = pickle.dumps(self.aliveServerData) client.send(packetData) client.close() logging.info("send upload file information to client: '{0}' to monitor".format(pickle.unpack(packetData)))
def update_event(self, inp=-1): self.set_output_val(0, pickle.unpack(self.input(0), self.input(1)))