Ejemplo n.º 1
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
Ejemplo n.º 2
0
    def linkNodes(self):
        try:
            channel = grpc.insecure_channel("localhost:50051")
            self.stubs[0] = DisStorage_pb2.DisServerStub(channel)
        except Exception as err:
            print 'linkNodes: can not connect to node 1'

        try:
            channel = grpc.insecure_channel("localhost:50052")
            self.stubs[1] = DisStorage_pb2.DisServerStub(channel)
        except Exception as err:
            print 'linkNodes: can not connect to node 2'

        try:
            channel = grpc.insecure_channel("localhost:50053")
            self.stubs[2] = DisStorage_pb2.DisServerStub(channel)
        except Exception as err:
            print 'linkNodes: can not connect to node 3'
        accessCnt = 0
        for i in range(0, self.DisServerNum):
            try:
                res = self.stubs[i].usrLogin(
                    DisStorage_pb2.usrInfo(name='__root__',
                                           pw='__root__',
                                           mess=''))
                if res.mess:
                    print res.mess
                else:
                    accessCnt += 1
            except Exception as err:
                print 'linkNode: can not login to node %d' % i
        print "link to %d DiskServer" % accessCnt
Ejemplo 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
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
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