Exemplo n.º 1
0
 def uploadFile(self, request, context):
     blockCnt = 0
     s = ''
     filename = ''
     for each in request:
         if each.usrName not in self.loginDict:
             return DisStorage_pb2.fileInfo(fileName='',
                                            blockNum=-1,
                                            usrName=each.usrName,
                                            mess='Login First!')
         blockCnt += 1
         s += each.block
         filename = each.fileName
     f = open(self.path + filename, 'wb')
     f.write(s)
     f.close()
     return DisStorage_pb2.fileInfo(fileName=filename,
                                    blockNum=blockCnt,
                                    usrName='done',
                                    mess='')
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
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