Beispiel #1
0
 def GetHost(self, filename):
     response = self.tracker_stub.GetHost(
         data_pb2.Filename(filename=filename))
     while response.code != 0:
         print('Servers are all down. Try again.')
         time.sleep(2)
         response = self.tracker_stub.GetHost(
             data_pb2.Filename(filename=filename))
     return response.address
Beispiel #2
0
 def DeleteFile(self, filename):
     server_address = self.GetHost(filename)
     channel = grpc.insecure_channel(server_address)
     self.server_stub = data_pb2_grpc.FileSystemStub(channel=channel)
     while not self.__LockFile(filename):
         time.sleep(2)
     response = self.server_stub.DeleteFile(
         data_pb2.Filename(filename=filename))
     self.__UnlockFile(filename)
     self.tracker_stub.DeleteFile(data_pb2.Filename(filename=filename))
     file_path = DIR + 'tmp_' + filename
     if Path(file_path).is_file():
         os.chmod(file_path, S_IWUSR | S_IREAD)
         Path(file_path).unlink()
     print(response.code, response.message)
Beispiel #3
0
 def DeleteSlave(self, filename, address):
     channel = grpc.insecure_channel(address)
     stub = data_pb2_grpc.FileSystemStub(channel=channel)
     if not self.__LockFile(stub, filename):
         raise Exception
     stub.DeleteFile(data_pb2.Filename(filename=filename))
     self.__UnlockFile(stub, filename)
Beispiel #4
0
 def __CheckCache(self, filename, fullpath):
     if Path(fullpath).is_file():
         remote_file_info = self.server_stub.GetFileInfo(
             data_pb2.Filename(filename=filename))
         if Path(fullpath).stat().st_mtime >= remote_file_info.modifiedtime:
             return True
     return False
Beispiel #5
0
 def ListFiles(self, request, context):
     data = self.__ReadData()
     for filename in data:
         addresses = data[filename]
         channel = grpc.insecure_channel(addresses[0])
         stub = data_pb2_grpc.FileSystemStub(channel=channel)
         file_info = stub.GetFileInfo(data_pb2.Filename(filename=filename))
         yield file_info
Beispiel #6
0
 def OpenFile(self, filename):
     response = self.tracker_stub.GetServer(
         data_pb2.Filename(filename=filename))
     if response.code != 0:
         response = self.tracker_stub.GetHost(
             data_pb2.Filename(filename=filename))
     channel = grpc.insecure_channel(response.address)
     self.server_stub = data_pb2_grpc.FileSystemStub(channel=channel)
     file_path = DIR + 'tmp_' + filename
     if self.__CheckCache(filename, file_path):
         print('Already in cache.')
     else:
         time.sleep(2)
         while not self.__LockFile(filename):
             time.sleep(2)
         if Path(file_path).is_file():
             os.chmod(file_path, S_IWUSR | S_IREAD)
             Path(file_path).unlink()
         result = self.__Download(filename, file_path)
         self.__UnlockFile(filename)
         os.chmod(file_path, S_IREAD | S_IRGRP | S_IROTH)
     os.system(file_path)
Beispiel #7
0
 def CreateFile(self, request, context):
     self.TryComplete()
     filename = request.filename
     file_path = _DIR + filename
     # file = Path(file_path)
     f = open(file_path, 'w')
     f.close()
     for no, response in enumerate(
             self.tracker_stub.GetServers(
                 data_pb2.Filename(filename=filename))):
         address = response.address
         if no == 0 and address != self.address:
             break
         if no == 0 and address == self.address:
             continue
         self.AddUncomplete('create', filename, address)
         self.TryComplete()
     return data_pb2.Response(code=0, message='Success.')
Beispiel #8
0
 def DeleteFile(self, request, context):
     filename = request.filename
     file_path = _DIR + filename
     file = Path(file_path)
     if not file.is_file():
         return data_pb2.Response(code=1, message='File not exists.')
     else:
         file.unlink()
         for no, response in enumerate(
                 self.tracker_stub.GetServers(
                     data_pb2.Filename(filename=filename))):
             address = response.address
             if no == 0 and address != self.address:
                 break
             if no == 0 and address == self.address:
                 continue
             self.AddUncomplete('delete', filename, address)
             self.TryComplete()
         return data_pb2.Response(code=0, message='Success.')
Beispiel #9
0
 def Upload(self, request_iterator, context):
     self.TryComplete()
     for no, request in enumerate(request_iterator):
         if no == 0:
             filename = request.filename
             file_path = _DIR + filename
             file = open(file_path, 'wb')
         else:
             file.write(request.buffer)
     file.close()
     for no, response in enumerate(
             self.tracker_stub.GetServers(
                 data_pb2.Filename(filename=filename))):
         address = response.address
         if no == 0 and address != self.address:
             break
         if no == 0 and address == self.address:
             continue
         self.AddUncomplete('update', filename, address)
         self.TryComplete()
     return data_pb2.Response(code=0, message='ok')
Beispiel #10
0
 def __Download(self, filename, fullpath):
     with open(fullpath, 'wb') as f:
         for chunk in self.server_stub.Download(
                 data_pb2.Filename(filename=filename)):
             f.write(chunk.buffer)
     return 0
Beispiel #11
0
 def __CreateFile(self, filename, address):
     channel = grpc.insecure_channel(address)
     stub = data_pb2_grpc.FileSystemStub(channel=channel)
     response = stub.CreateFile(data_pb2.Filename(filename=filename))