def downloadFile(self, request, context): if request.usrName not in self.loginDict: yield DisStorage_pb2.fileData(fileName='', block='', blockNum=-1, usrName='', mess='Login First!') else: flst = os.listdir(self.path) if request.fileName in flst: f = open(self.path + request.fileName, 'rb') blockCnt = 0 while True: s = f.readline() blockCnt += 1 if s: yield DisStorage_pb2.fileData( fileName=request.fileName, block=s, blockNum=blockCnt, usrName=request.usrName, mess='') else: break else: yield DisStorage_pb2.fileData(fileName=request.fileName, block='', blockNum=-1, usrName=request.usrName, mess='file not exist')
def downloadFile(self, request, context): self.flstAll, self.flsts, self.paths = self.updateFileList() self.sync('dataOnly') if request.usrName not in self.loginDict: yield DisStorage_pb2.fileData(fileName='', block='', blockNum=-1, usrName='', mess='Login First!') else: try: res = self.stubs[self.loginDict[request.usrName]].getState( DisStorage_pb2.usrInfo(name='__root__', pw='__root__', mess='')) for each in self.stubs[self.loginDict[ request.usrName]].downloadFile(request): yield DisStorage_pb2.fileData(fileName=each.fileName, block=each.block, blockNum=each.blockNum, usrName=each.usrName, mess=each.mess) except Exception as err: print 'download: can not connect to node %d' % self.loginDict[ request.usrName] for i in range(0, self.DisServerNum): try: res = self.stubs[i].getState( DisStorage_pb2.usrInfo(name='__root__', pw='__pw__', mess='')) print 'change to node %d' % i for each in self.stubs[i].downloadFile( DisStorage_pb2.fileInfo( fileName=request.fileName, blockNum=request.blockNum, usrName='__root__', mess=request.mess)): yield DisStorage_pb2.fileData( fileName=each.fileName, block=each.block, blockNum=each.blockNum, usrName=request.usrName, mess=each.mess) break except Exception as err: print 'download: can not connect to node %d' % i
def sync(self, param='ALL'): #info sync if param == 'ALL': self.loginDict = {} for i in range(0, self.DisServerNum): try: res = self.stubs[i].getState( DisStorage_pb2.usrInfo(name='__root__', pw='__root__', mess='')) if res.mess: print res.mess else: self.DisServerStress[i] = len(res.usrs) - 1 for each in res.usrs: self.loginDict[each] = i except Exception as err: print 'sync: can not connect to node %d' % i #data sync for i in range(0, self.DisServerNum): try: tmp = list(set(self.flstAll) - set(self.flsts[i])) if tmp: for each in tmp: for j in range(0, self.DisServerNum): if each in self.flsts[j]: fp = open(self.paths[j] + each, 'rb') blockCnt = 0 lst = [] while True: s = fp.readline() if not s: break blockCnt += 1 lst.append( DisStorage_pb2.fileData( fileName=each, block=s, blockNum=blockCnt, usrName='__root__', mess='')) res = self.stubs[i].uploadFile(lst) if res.mess: print res.mess except Exception as err: print 'sync: can not connect to node %d' % i return None
def uploadFile(self, request, context): blockCnt = 0 uname = '' fname = '' lst = [] for each in request: fname = each.fileName uname = each.usrName if each.usrName not in self.loginDict: return DisStorage_pb2.fileInfo(fileName='', blockNum=-1, usrName=each.usrName, mess='Login First') else: lst.append( DisStorage_pb2.fileData(fileName=each.fileName, block=each.block, blockNum=each.blockNum, usrName='__root__', mess=each.mess)) try: res = self.stubs[self.loginDict[uname]].getState( DisStorage_pb2.usrInfo(name='__root__', pw='__root__', mess='')) res = self.stubs[self.loginDict[uname]].uploadFile(lst) except Exception as err: print 'can not connect to node %d' % self.loginDict[uname] for i in range(0, self.DisServerNum): try: res = self.stubs[i].getState( DisStorage_pb2.usrInfo(name='__root__', pw='__root__', mess='')) res = self.stubs[i].uploadFile(lst) print 'change to node %d, upload done' % i break except Exception as err: print 'can not connect to node %d' % i if res.mess: print res.mess else: self.flstAll, self.flsts, self.paths = self.updateFileList() self.sync() return res
def run(): channel = grpc.insecure_channel("localhost:50050") stub = DisStorage_pb2.CtrServerStub(channel) usrName = '' pw = '' usrPath = r'ClientDisk/' while True: sin = raw_input("##:") if sin == 'exit': break if sin == 'reconnect': try: channel = grpc.insecure_channel("localhost:50050") stub = DisStorage_pb2.CtrServerStub(channel) print 'reconnect done' except Exception as err: print err if sin == 'login': usrName = raw_input('user name:') pw = raw_input('password:'******'')) if res.mess: print res.mess if sin == 'logout': res = stub.usrLogout( DisStorage_pb2.usrInfo(name=usrName, pw=pw, mess='')) if res.mess: print res.mess else: print "order done" if sin == 'ls': res = stub.getFileList( DisStorage_pb2.usrInfo(name=usrName, pw=pw, mess='')) if res.mess: print res.mess else: print res.nameList if sin == 'upload': fname = raw_input('file name:') f = open(usrPath + fname, 'rb') cnt = 0 lst = [] while True: s = f.readline() cnt += 1 if s: lst.append( DisStorage_pb2.fileData(fileName=fname, block=s, blockNum=cnt, usrName=usrName, mess='')) else: break res = stub.uploadFile(lst) if res.mess: print res.mess else: print('upload done') if sin == 'download': fname = raw_input('file name:') cnt = 0 downFlag = 1 f = open(usrPath + fname, 'wb') for each in stub.downloadFile( DisStorage_pb2.fileInfo(fileName=fname, blockNum=0, usrName=usrName, mess='')): if each.mess: print each.mess f.close() os.remove(usrPath + fname) downFlag = 0 break f.write(each.block) cnt += 1 f.close() if downFlag == 1: print "done" if sin == 'node_info': res = stub.getNodeInfo( DisStorage_pb2.usrInfo(name=usrName, pw=pw, mess='')) if res.mess: print res.mess