コード例 #1
0
def http_reassemble(request):

    # Wait for an HTTP POST request.
    if request.method == 'POST':

        # Get request JSON.
        print("[REASSEMBLE] POST RECEIVED")
        request_dict = request.get_json()
        print('Received HTTP message: {}'.format(request_dict))

        current_window = int(request_dict["current_window"])
        last_index = int(request_dict["last_index"])
        header_bytes = int(request_dict["header_bytes"])

        # Initialize Cloud Storage variables.
        # BUCKET_NAME = 'sigfoxschc'
        # BUCKET_NAME = 'wyschc-niclabs'
        BUCKET_NAME = config.BUCKET_NAME

        # Initialize SCHC variables.
        profile_uplink = Sigfox("UPLINK", "ACK ON ERROR", header_bytes)
        n = profile_uplink.N
        # Find the index of the first empty blob:

        print("[REASSEMBLE] Reassembling...")

        # Get all the fragments into an array in the format "fragment = [header, payload]"
        fragments = []

        # TODO: This assumes that the last received message is in the last window.
        for i in range(current_window + 1):
            for j in range(2**n - 1):
                print("Loading fragment {}".format(j))
                fragment_file = read_blob(
                    BUCKET_NAME,
                    "all_windows/window_%d/fragment_%d_%d" % (i, i, j))
                print(fragment_file)
                ultimate_header = fragment_file[:header_bytes]
                ultimate_payload = fragment_file[header_bytes:]
                ultimate_fragment = [
                    ultimate_header.encode(),
                    ultimate_payload.encode()
                ]
                fragments.append(ultimate_fragment)
                if i == current_window and j == last_index:
                    break

        # Instantiate a Reassembler and start reassembling.
        reassembler = Reassembler(profile_uplink, fragments)
        payload = bytearray(reassembler.reassemble())

        # Upload the full message.
        upload_blob(BUCKET_NAME, payload.decode("utf-8"), "Reassembled_Packet")

        return '', 204
コード例 #2
0
def reassemble():

    if request.method == "POST":
        # Get request JSON.
        print("[RSMB] POST RECEIVED")
        request_dict = request.get_json()
        print('Received HTTP message: {}'.format(request_dict))

        current_window = int(request_dict["current_window"])
        # last_index = int(request_dict["last_index"])
        header_bytes = int(request_dict["header_bytes"])

        # Initialize SCHC variables.
        profile = SigfoxProfile("UPLINK", "ACK ON ERROR", header_bytes)

        print("[RSMB] Loading fragments")

        # Get all the fragments into an array in the format "fragment = [header, payload]"
        fragments = []

        # For each window, load every fragment into the fragments array
        # Note: This assumes all fragments are ordered
        for i in range(current_window + 1):
            for j in range(profile.WINDOW_SIZE):
                if not exists_blob(f"all_windows/window_{i}/fragment_{i}_{j}"):
                    continue
                fragment_file = read_blob(
                    f"all_windows/window_{i}/fragment_{i}_{j}")
                header = fragment_file[:header_bytes].encode()
                payload = fragment_file[header_bytes:].encode()
                fragment = [header, payload]
                fragments.append(fragment)

        print(len(fragments))
        # Instantiate a Reassembler and start reassembling.
        reassembler = Reassembler(profile, fragments)
        payload = bytearray(reassembler.reassemble())
        # Upload the full message.
        upload_blob(payload.decode("utf-8"), "SCHC_PACKET")
        with open(config.RECEIVED, "w") as f:
            f.write(payload.decode())

        if filecmp.cmp(config.PAYLOAD, config.RECEIVED):
            print("The reassembled file is equal to the original message.")
        else:
            print("The reassembled file is corrupt.")

        # start_request(url=config.LOCAL_CLEAN_URL,
        #               body={"header_bytes": header_bytes})

        return '', 204
コード例 #3
0
			# If the last received fragment is an All-1, start reassembling.
			if fragment_message.is_all_1():

				# If everything has gone according to plan, there shouldn't be any empty spaces
				# between two received fragments. So the first occurrence of an empty space should be the position
				# of the final fragment.
				last_index = all_windows[current_window].index([b'', b''])
				all_windows[current_window][last_index] = data

				for i in range(2 ** m):
					for j in range(2 ** n - 1):
						fragments.append(all_windows[i][j])

				# Reassemble.
				print("[ALL1] Last fragment. Reassembling...")
				reassembler = Reassembler(profile_uplink, fragments)
				payload = bytearray(reassembler.reassemble())

				# Send the last ACK with the C bit set to 1.
				print("[ALL1] Reassembled: Sending last ACK")

				# "if the C bit is set to 1, no bitmap is carried."
				bitmap = ''
				for k in range(profile_uplink.BITMAP_SIZE):
					bitmap += '0'

				last_ack = ACK(profile_downlink, rule_id, dtag, w, bitmap, '1')
				the_socket.sendto(last_ack.to_bytes(), address)

				# End loop
				break
コード例 #4
0
def reassemble():

    CLEANUP_URL = "https://localhost:5000/cleanup"

    if request.method == "POST":
        print("[RSMB] The reassembler has been launched.")
        # Get request JSON.
        request_dict = request.get_json()
        print(f'[RSMB] Received HTTP message: {request_dict}')

        current_window = int(request_dict["current_window"])
        last_index = int(request_dict["last_index"])
        header_bytes = int(request_dict["header_bytes"])

        # Initialize Cloud Storage variables.
        BUCKET_NAME = config.BUCKET_NAME

        # Initialize SCHC variables.
        profile_uplink = Sigfox("UPLINK", "ACK ON ERROR", header_bytes)
        n = profile_uplink.N

        print("[RSMB] Loading fragments")

        # Get all the fragments into an array in the format "fragment = [header, payload]"
        fragments = []

        # For each window, load every fragment into the fragments array
        for i in range(current_window + 1):
            for j in range(2**n - 1):
                print(f"[RSMB] Loading fragment {j}")
                fragment_file = read_blob(
                    BUCKET_NAME, f"all_windows/window_{i}/fragment_{i}_{j}")
                print(f"[RSMB] Fragment data: {fragment_file}")
                header = fragment_file[:header_bytes]
                payload = fragment_file[header_bytes:]
                fragment = [header.encode(), payload.encode()]
                fragments.append(fragment)
                if i == current_window and j == last_index:
                    break

        # Instantiate a Reassembler and start reassembling.
        print("[RSMB] Reassembling")
        reassembler = Reassembler(profile_uplink, fragments)
        payload = bytearray(reassembler.reassemble()).decode("utf-8")

        print("[RSMB] Uploading result")
        with open(config.PAYLOAD, "w") as file:
            file.write(payload)
        # Upload the full message.
        upload_blob_using_threads(BUCKET_NAME, payload, "PAYLOAD")

        if filecmp.cmp(config.PAYLOAD, config.MESSAGE):
            print("The reassembled file is equal to the original message.")
        else:
            print("The reassembled file is corrupt.")

        try:
            _ = requests.post(url=CLEANUP_URL,
                              json={"header_bytes": header_bytes},
                              timeout=0.1)
        except requests.exceptions.ReadTimeout:
            pass

        return '', 204