Reticulum/RNS/Bundle.py

227 lines
7.2 KiB
Python
Raw Normal View History

2020-06-14 18:18:46 +00:00
import RNS
2020-08-11 18:15:23 +00:00
import time
import os
import os.path
from .vendor import umsgpack as umsgpack
2020-06-14 18:18:46 +00:00
class Bundle:
2020-08-12 12:06:29 +00:00
APP_NAME = "rnsbundle"
2020-08-11 18:15:23 +00:00
NO_CUSTODY = 0x00;
TAKING_CUSTODY = 0x01;
FULL_CUSTODY = 0x02;
2020-08-12 12:06:29 +00:00
REMOVED = 0xFF;
2020-08-11 18:15:23 +00:00
CHUNK_SIZE = RNS.Resource.MAX_EFFICIENT_SIZE / 4
2020-08-12 12:06:29 +00:00
def __init__(self, destination_hash = None, data = None, filepath = None, advertisement_data = None):
self.destination_hash = None
self.is_originator = False
self.state = None
self.data_file = None
self.meta_file = None
self.data_hash = None
self.id = None
self.storagepath = None
self.size = None
self.chunks = 0
self.created = time.time()
self.heartbeat = created
self.transferring = False
self.chunk_request_destination = None
2020-08-11 18:15:23 +00:00
try:
if data != None or filepath != None:
2020-08-12 12:06:29 +00:00
self.destination_hash = destination_hash
self.is_originator = True
self.id = RNS.Identity.getRandomHash()
2020-08-11 18:15:23 +00:00
if filepath == None and data != None:
try:
2020-08-12 12:06:29 +00:00
self.data_hash = RNS.Identity.fullHash(data)
2020-08-11 18:15:23 +00:00
self.storagepath = Reticulum.bundlepath+"/"+self.id.hex()
self.datapath = self.storagepath+"/data"
self.metadatapath = self.storagepath+"/metadata"
if not os.path.isdir(self.storagepath):
os.makedirs(self.storagepath)
else:
RNS.log("Warning, bundle already exists in storage location, recreating", RNS.LOG_DEBUG)
self.data_file = open(self.datapath, "wb")
self.data_file.write(data)
self.data_file.close()
except Exception as e:
RNS.log("Error while initialising bundle from data, the contained exception was:", RNS.LOG_ERROR)
RNS.log(str(e))
self.state = Bundle.FULL_CUSTODY
elif data == None and filepath != None:
try:
input_file = open(filepath, "rb")
2020-08-12 12:06:29 +00:00
self.data_hash = RNS.Identity.fullHash(input_file.read())
2020-08-11 18:15:23 +00:00
input_file.seek(0)
self.storagepath = RNS.Reticulum.bundlepath+"/"+self.id.hex()
self.datapath = self.storagepath+"/data"
self.metadatapath = self.storagepath+"/metadata"
if not os.path.isdir(self.storagepath):
os.makedirs(self.storagepath)
else:
RNS.log("Warning, bundle already exists in storage location, recreating", RNS.LOG_DEBUG)
self.data_file = open(self.datapath, "wb")
self.data_file.write(input_file.read())
self.data_file.close()
input_file.close()
except Exception as e:
RNS.log("Error while reading input file for bundle, the contained exception was:", RNS.LOG_ERROR)
RNS.log(str(e))
self.state = Bundle.FULL_CUSTODY
else:
raise ValueError("Bundle cannot be created from data and file path at the same time")
2020-08-12 12:06:29 +00:00
# Prepare file handles and metadata
self.size = os.stat(self.datapath).st_size
if self.size < 1:
raise IOError("Bundle data is empty")
self.data_file = open(self.datapath, "rb")
elif advertisement_data != None:
2020-08-11 18:15:23 +00:00
# Incoming bundle transfer
2020-08-12 12:06:29 +00:00
self.id = advertisement_data[1]
self.destination_hash = advertisement_data[0]
2020-08-11 18:15:23 +00:00
self.state = Bundle.TAKING_CUSTODY
2020-08-12 12:06:29 +00:00
self.storagepath = Reticulum.bundlepath+"/"+self.id.hex()
self.datapath = self.storagepath+"/data"
self.metadatapath = self.storagepath+"/metadata"
if not os.path.isdir(self.storagepath):
os.makedirs(self.storagepath)
else:
RNS.log("Warning, bundle already exists in storage location, recreating", RNS.LOG_DEBUG)
self.data_file = open(self.datapath, "wb")
self.data_file.close()
self.size = advertisement_data[2]
self.data_file = open(self.datapath, "wb")
2020-08-11 18:15:23 +00:00
else:
raise ValueError("No source of data specified for bundle initialisation")
self.chunks = ((self.size-1)//Bundle.CHUNK_SIZE)+1
self.flush_metadata()
2020-08-12 12:06:29 +00:00
RNS.Transport.register_bundle(self)
2020-08-11 18:15:23 +00:00
except Exception as e:
RNS.log("Error while initialising bundle. The contained exception was:", RNS.LOG_ERROR)
RNS.log(str(e), RNS.LOG_ERROR)
2020-08-12 12:06:29 +00:00
# TODO: Remove
2020-08-11 18:15:23 +00:00
raise e
2020-08-12 12:06:29 +00:00
def get_packed_metadata(self):
metadata = {
"destination": self.destination,
"heartbeat": self.heartbeat,
"size": self.size,
"is_originator": self.is_originator
"state": self.state}
return umsgpack.packb(metadata)
2020-08-11 18:15:23 +00:00
def flush_metadata(self):
try:
self.meta_file = open(self.metadatapath, "wb")
2020-08-12 12:06:29 +00:00
self.meta_file.write(self.get_packed_metadata())
2020-08-11 18:15:23 +00:00
self.meta_file.close()
except Exception as e:
RNS.log("Error while flushing metadata for bundle "+RNS.prettyhexrep(self.id), RNS.LOG_ERROR)
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
2020-06-14 18:18:46 +00:00
2020-08-12 12:06:29 +00:00
def register_destinations(self, destination):
self.chunk_request_destination = RNS.Destination(None, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, "chunk", "request")
self.chunk_request_destination.link_established_callback(requester_connected)
def advertise(self, advertise_to):
advertisement = [
self.destination,
self.id,
self.size,
self.chunks]
advertisement_data = umsgpack.packb(advertisement)
advertisement_packet = RNS.Packet(advertise_to, advertisement_data)
advertisement.packet.send()
def requester_connected(self, link):
RNS.log("Requester connected to bundle "+RNS.prettyhexrep(self.id), RNS.LOG_DEBUG)
link.packet_callback(chunk_request)
def chunk_request(self, data, packet):
chunk_index = data[0]
RNS.log("Request for chunk "+str(chunk_index)+"/"+str(self.chunks)+" of bundle "+RNS.prettyhexrep(self.id), RNS.LOG_DEBUG)
if chunk_index < self.chunks:
self.emit_resource(packet.link, chunk_index)
else:
RNS.log("Bundle transfer client requested chunk index out of range, tearing down link.", RNS.LOG_ERROR)
packet.link.teardown()
def emit_resource(self, link, chunk_index):
if not self.transferring:
chunk_max = self.size-1
chunk_start = chunk_index*CHUNK_SIZE
chunk_end = (chunk_index+1)*CHUNK_SIZE-1
if chunk_end > chunk_max:
chunk_end = chunk_max
read_size = chunk_end - chunk_start
try:
file = open(self.datapath, "rb")
file.seek(chunk_start)
data = file.read(read_size)
chunk_resource = RNS.Resource(data, link, callback=resource_concluded)
chunk_resource.chunk_index = chunk_index
except Exception as e:
RNS.log("Could not read bundle data from storage, the contained exception was:", RNS.LOG_ERROR)
RNS.log(str(e))
link.teardown()
else:
RNS.log("Bundle chunk "+str(chunk_index)+" for "+RNS.prettyhexrep(self.id)+" was requested while a transfer was already in progress", RNS.LOG_ERROR)
def resource_concluded(self, resource):
RNS.log("Concluded transferring chunk "+str(resource.chunk_index)+"/"+str(self.chunks)+" of bundle "+RNS.prettyhexrep(self.id), RNS.LOG_DEBUG)
self.transferring = False
def resign_custody(self):
self.state = Bundle.NO_CUSTODY
self.heartbeat = time.time()
def custody_proof(self, proof):
pass
def remove(self):
try:
self.state = Bundle.REMOVED
RNS.Transport.deregister_destination(self.chunk_request_destination)
os.unlink(self.datapath)
os.unlink(self.metadatapath)
os.rmdir(self.storagepath)
except Exception as e:
RNS.log("Error while removing bundle from storage, the contained exception was:", RNS.LOG_ERROR)
RNS.log(str(e), RNS.LOG_ERROR)
2020-06-14 18:18:46 +00:00
class BundleAdvertisement:
pass