def file_split(filename): file_list = [] filesize = os.path.getsize(filename) part_filename = hashlib.sha256(filename.encode('utf-8')).hexdigest() # broadcast msg to give size to all nodes udp = UDPHandler() udp.get_disk_space(json.dumps({})) # get all node's free space ips = [] context = zmq.Context() zsocket = context.socket(zmq.REP) zsocket.bind("tcp://127.0.0.1:%s" % settings.STORAGE_ZMQ_PORT) zpoll = zmq.Poller() zpoll.register(zsocket) start_timestamp = time.time() while time.time() - start_timestamp < 8: events = dict(zpoll.poll(1)) for key in events: strecv = json.loads(key.recv_string()) key.send_string("recieved your free space") if strecv["data"] > filesize: # add ip to array ips.append(strecv["ip_addr"]) zsocket.send_string("got someone to store") zpoll.unregister(zsocket) zsocket.close() context.destroy() # size of ips n = len(ips) SPLIT_SIZE = math.ceil(filesize / n) with open(filename, "rb") as f: i = 0 while True: bytes_read = f.read(SPLIT_SIZE) if not bytes_read: break # hash = hashlib.sha256(json.dumps(bytes_read).encode("utf-8")).hexdigest() file_list.append(bytes_read) i = i + 1 for i in range(0, n): file_n = settings.TEMP_STORAGE_PATH + part_filename + str(i) with open(file_n, "wb") as f: f.write(file_list[i]) os.remove(filename) return ([n, ips])
def test_disk_space(self): udp = UDPHandler() udp.get_disk_space()