Exemple #1
0
def main(args):
    # Packet encoder
    encoder = NdnTlvEncoder(
    ) if args.format == 'ndntlv' else SimpleStringEncoder

    # Generate interest packet
    first_interest: Interest = Interest(args.name)
    encoded_first_interest = encoder.encode(first_interest)

    # Send interest packet
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(5)
    sock.bind(("0.0.0.0", 0))
    try:
        resolved_hostname = socket.gethostbyname(args.ip)
    except:
        print("Resolution of hostname failed.")
        sys.exit(-2)
    sock.sendto(encoded_first_interest, (resolved_hostname, args.port))

    # Receive content object
    try:
        wire_packet_first, addr = sock.recvfrom(8192)
    except:
        print("Timeout.")
        sys.exit(-1)

    # Wait
    time_to_wait = int(encoder.decode_data(wire_packet_first)[1])
    print("Waiting for result: " + str(time_to_wait))
    time.sleep(time_to_wait * 1.2)

    # Send second interest
    new_components = args.name.split("/")[1:-1]
    new_components.append("resultpNFN")
    new_name = "/" + '/'.join(new_components)
    second_interest: Interest = Interest(new_name)
    encoded_second_interest = encoder.encode(second_interest)
    sock.sendto(encoded_second_interest, (resolved_hostname, args.port))

    # Receive result
    try:
        wire_packet_second, addr = sock.recvfrom(8192)
    except:
        print("Timeout.")
        sys.exit(-1)

    # Print
    if args.plain is False:
        printer = NdnTlvPrinter(wire_packet_second)
        printer.formatted_print()
    else:
        encoder = NdnTlvEncoder()
        if encoder.is_content(wire_packet_second):
            sys.stdout.buffer.write(
                encoder.decode_data(wire_packet_second)[1] + b"\n")
        else:
            sys.exit(-2)
Exemple #2
0
def main(args):

    # Packet encoder
    encoder = NdnTlvEncoder(
    ) if args.format == 'ndntlv' else SimpleStringEncoder()

    # Generate interest packet
    interest: Interest = Interest(args.name)
    encoded_interest = encoder.encode(interest)

    # Send interest packet
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.settimeout(args.timeout)
    sock.bind(("0.0.0.0", 0))
    try:
        resolved_hostname = socket.gethostbyname(args.ip)
    except:
        print("Resolution of hostname failed.")
        sys.exit(-2)
    sock.sendto(encoded_interest, (resolved_hostname, args.port))

    # Receive content object
    try:
        wire_packet, addr = sock.recvfrom(8192)
    except:
        print("Timeout.")
        sys.exit(-1)

    # Print
    if args.plain is False:
        printer = NdnTlvPrinter(wire_packet)
        printer.formatted_print()
    else:
        encoder = NdnTlvEncoder()
        if encoder.is_content(wire_packet):
            sys.stdout.buffer.write(encoder.decode_data(wire_packet)[1])
        else:
            sys.exit(-2)