Ejemplo n.º 1
0
def main():
    # The default Face will connect using a Unix socket, or to "localhost".
    face = Face()

    # Use the system default key chain and certificate name to sign.
    keyChain = KeyChain()
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())

    prefix = Namespace("/ndn/test/status", keyChain)

    dump("Register prefix", prefix.name)
    # Set the face and register to receive Interests.
    prefix.setFace(face,
      lambda prefixName: dump("Register failed for prefix", prefixName))

    handler = GeneralizedObjectHandler()
    # Each generalized object will have a 1000 millisecond freshness period.
    metaInfo = MetaInfo()
    metaInfo.setFreshnessPeriod(1000.0)

    # This is called when the library receives an Interest which is not
    # satisfied by Data already in the Namespace tree.
    def onObjectNeeded(namespace, neededNamespace, callbackId):
        if not (neededNamespace is prefix):
            # This is not the expected Namespace.
            return False

        # Make a version from the current time.
        versionNamespace = prefix[
          Name.Component.fromVersion(Common.getNowMilliseconds())]
        # The metaInfo has the freshness period.
        versionNamespace.setNewDataMetaInfo(metaInfo)
        dump("Producing the generalized object for", versionNamespace.name)
        handler.setObject(
          versionNamespace, Blob("Status as of " + str(datetime.datetime.now())),
          "text/html")
        return True

    prefix.addOnObjectNeeded(onObjectNeeded)

    while True:
        face.processEvents()
        # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
        time.sleep(0.01)
Ejemplo n.º 2
0
def main(args):
    face = Face()

    keyChain = KeyChain()
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())

    gobj = Namespace(args['<prefix>'], keyChain)

    gobj.setFace(
        face,
        lambda prefixName: dump("Register failed for prefix", prefixName))

    handler = GeneralizedObjectHandler()

    metaInfo = MetaInfo()
    if args['--freshness']:
        metaInfo.setFreshnessPeriod(float(args['--freshness']))
    else:
        metaInfo.setFreshnessPeriod(1000.0)

    def onObjectNeeded(namespace, neededNamespace, callbackId):
        if not (neededNamespace is gobj):
            # This is not the expected Namespace.
            return False

        # Make a version from the current time.
        versionNamespace = gobj[Name.Component.fromVersion(
            Common.getNowMilliseconds())]
        # The metaInfo has the freshness period.
        versionNamespace.setNewDataMetaInfo(metaInfo)

        with open(args['<file>'], 'r') as f:
            handler.setObject(versionNamespace, Blob(f.read()), "text/html")
        return True

    gobj.addOnObjectNeeded(onObjectNeeded)
    while True:
        face.processEvents()
        time.sleep(0.01)
Ejemplo n.º 3
0
def main():
    # The default Face will connect using a Unix socket, or to "localhost".
    face = Face()

    # Create an in-memory key chain with default keys.
    keyChain = KeyChain("pib-memory:", "tpm-memory:")
    keyChain.importSafeBag(
        SafeBag(Name("/testname/KEY/123"),
                Blob(DEFAULT_RSA_PRIVATE_KEY_DER, False),
                Blob(DEFAULT_RSA_PUBLIC_KEY_DER, False)))
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
    # Enable Interest loopback so that the EncryptorV2 can fetch Data packets
    # from the AccessManagerV2.
    face.setInterestLoopbackEnabled(True)

    contentPrefix = Name("/testname/content")
    contentNamespace = Namespace(contentPrefix, keyChain)

    # We know the content has two segments.
    metaInfo = MetaInfo()
    metaInfo.setFinalBlockId(Name().appendSegment(1)[0])
    contentNamespace.setNewDataMetaInfo(metaInfo)

    ckPrefix = Name("/some/ck/prefix")
    encryptor = prepareData(ckPrefix, keyChain, face)

    # Make the callback to produce a Data packet for a content segment.
    def onObjectNeeded(namespace, neededNamespace, id):
        if not (len(neededNamespace.name) == len(contentPrefix) + 1
                and contentPrefix.isPrefixOf(neededNamespace.name)
                and neededNamespace.name[-1].isSegment()):
            # Not a content segment, ignore.
            return False

        # Get the segment number.
        segment = neededNamespace.name[-1].toSegment()
        if not (segment >= 0 and segment <= 1):
            # An invalid segment was requested.
            return False

        segmentContent = ("This test message was decrypted"
                          if segment == 0 else " from segments.")
        # Now call serializeObject which will answer the pending incoming Interest.
        dump("Producing Data", neededNamespace.name)
        neededNamespace.serializeObject(
            encryptor.encrypt(Blob(segmentContent)).wireEncodeV2())

        return True

    contentNamespace.addOnObjectNeeded(onObjectNeeded)

    dump("Register prefix", contentNamespace.name)
    # Set the face and register to receive Interests.
    contentNamespace.setFace(
        face,
        lambda prefixName: dump("Register failed for prefix", prefixName))

    while True:
        face.processEvents()
        # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
        time.sleep(0.01)