예제 #1
0
def printFaceStatuses(encodedMessage):
    """
    This is called when all the segments are received to decode the
    encodedMessage repeated TLV FaceStatus messages and display the values.

    :param Blob encodedMessage: The repeated TLV-encoded FaceStatus.
    """
    faceStatusMessage = face_status_pb2.FaceStatusMessage()
    ProtobufTlv.decode(faceStatusMessage, encodedMessage)

    dump("Faces:");
    for faceStatus in faceStatusMessage.face_status:
        line = ""
        # Format to look the same as "nfd-status -f".
        line += ("  faceid=" + str(faceStatus.face_id) +
            " remote=" + faceStatus.uri +
            " local=" + faceStatus.local_uri)
        if faceStatus.HasField("expiration_period"):
            # Convert milliseconds to seconds.
            line += (" expires=" +
              str(round(faceStatus.expiration_period / 1000.0)) + "s")
        line += (" counters={" + "in={" + str(faceStatus.n_in_interests) +
          "i " + str(faceStatus.n_in_datas) + "d " + str(faceStatus.n_in_bytes) + "B}" +
          " out={" + str(faceStatus.n_out_interests) + "i "+ str(faceStatus.n_out_datas) +
          "d " + str(faceStatus.n_out_bytes) + "B}" + "}" +
          " " + ("local" if faceStatus.face_scope == 1 else "non-local") +
          " " + ("permanent" if faceStatus.face_persistency == 2 else
                 ("on-demand" if faceStatus.face_persistency == 1 else "persistent")) +
          " " + ("multi-access" if faceStatus.link_type == 1 else "point-to-point"))

        dump(line)
예제 #2
0
def processFaceStatus(encodedFaceStatus, prefix, uri, face, enabled):
    """
    This is called when all the segments are received to decode the
    encodedFaceStatus as a TLV FaceStatus message. If the face ID exists for the
    face URL, use it to call registerRoute(), otherwise send a
    /localhost/nfd/faces/create command to create the face.

    :param Blob encodedFaceStatus: The TLV-encoded FaceStatus.
    :param Name prefix: The prefix name to register.
    :param str uri: The remote URI in case we need to tell NFD to create a face.
    :param Face face: The Face which is used to sign the command interest and
      call expressInterest.
    :param enabled: On success or error, set enabled[0] = False.
    :type enabled: An array with one bool element
    """
    if encodedFaceStatus.size() == 0:
        # No result, so we need to tell NFD to create the face.
        # Encode the ControlParameters.
        message = \
          control_parameters_pb2.ControlParametersTypes.ControlParametersMessage()
        message.control_parameters.uri = uri
        encodedControlParameters = ProtobufTlv.encode(message)

        interest = Interest(Name("/localhost/nfd/faces/create"))
        interest.getName().append(encodedControlParameters)
        interest.setInterestLifetimeMilliseconds(10000)

        def onData(localInterest, data):
            processCreateFaceResponse(data.getContent(), prefix, face, enabled)

        def onTimeout(localInterest):
            enabled[0] = False
            dump("Face create command timed out.")

        # Sign and express the interest.
        face.makeCommandInterest(interest)
        face.expressInterest(interest, onData, onTimeout)
    else:
        decodedFaceStatus = face_status_pb2.FaceStatusMessage()
        ProtobufTlv.decode(decodedFaceStatus, encodedFaceStatus)

        faceId = decodedFaceStatus.face_status[0].face_id

        dump("Found face ID ", faceId)
        registerRoute(prefix, faceId, face, enabled)