Пример #1
0
 def exposed_delete_block(self, id):
     if id in self.block_id:
         self.block_id.remove(id)
         os.remove(id)
         return Reply.reply()
     else:
         return Reply.error('Block not found')
Пример #2
0
 def exposed_get_block(self, file_name):
     if file_name in self.block_id:
         read_file = open(file_name, 'rb')
         data = read_file.read()
         read_file.close()
         return Reply.reply(data)
     else:
         return Reply.error('File not found')
Пример #3
0
        def exposed_put_block(self, file_name, data, replica_node_ids):
            print('new file name', file_name)
            if file_name in self.block_id:
                return Reply.error('File name already exists')
            else:
                try:
                    with open(file_name, 'wb') as f:
                        f.write(data)
                except:
                    self.block_id.remove(id)
                    return Reply.error('Error saving block')

                self.save_block(file_name)

                # send out replicas
                replica_node_ids.pop(0)
                if len(replica_node_ids) > 0:
                    done = 1
                    tries = 0
                    print("Sending replica to ", replica_node_ids[0])
                    while done == 1:
                        c = rpyc.connect(replica_node_ids[0], 5000)
                        next_node = c.root.BlockStore()
                        reply = Reply.Load(next_node.put_block(file_name, data, replica_node_ids))
                        print(reply.status)
                        if reply.status == 0:
                            print("replica sent!")
                            done = 0
                        else:
                            print("node busy trying again")
                            # wait 5 seconds and try again
                            time.sleep(5)
                            tries += 1
                            if tries > 4:
                                # after 4 tries give up
                                print("could not send block replica")
                                break

                return Reply.reply()