Namespace Schema Tree

exception ndn.schema.schema_tree.LocalResourceNotExistError(name)

Raised when trying to fetch a local resource that does not exist. Used only when LocalOnly is attached to the node.

class ndn.schema.schema_tree.MatchedNode(root, node, name, pos, env, policies)

MatchedNode represents a matched static tree node. That is, a node with all name patterns on the path from the root to it assigned to some value. For example, if the tree contains a node N on the path /a/<b>/<c>, and the user use the Name /a/x/y to match, then a matched node (N, {‘b’: ‘x’, ‘c’: ‘y’}) will be returned.

Variables:
  • root (Node) – the root of the static tree.

  • node (Node) – the matched node of the static tree.

  • name (FormalName) – the name used to match.

  • pos (int) – an integer indicating the length the name is matched. Generally, it equals the length of name.

  • env (Dict[str, Any]) – a dict containing the value all pattern variables matched on the path.

  • policies (Dict[Type[policy.Policy], policy.Policy]) – a dict collecting all policies that apply to this node. For each type of policy, the one attached on the nearst ancestor is collected here.

app()

The NDNApp the static tree is attached to.

Return type:

NDNApp

Returns:

the NDNApp.

async express(app_param=None, **kwargs)

Try to fetch the data, called by the node’s need function. It will search the local cache, and examines the local resource. If the corresponding Data cannot be found in the two places, it encrypts the app_param and expresses the Interest.

Note

This function only sends out an Interest packet when the Data is not cached locally.

Parameters:
  • app_param (Union[bytes, bytearray, memoryview, None]) – the ApplicationParameter of the Interest.

  • kwargs – other parameters of the Interest.

Returns:

whatever process_data returns. Generally this function is only called at the default node, so the return value is a tuple of the content and a dict containing metadata.

finer_match(new_name)

Do a finer match based on current match. new_name must include current name as its prefix. For example, if the current match name is /a/b and we want to get the matched node for /a/b/c, then we can call finer_match with /a/b/c.

Parameters:

new_name (List[Union[bytes, bytearray, memoryview]]) – the new name to be matched. Must include current name as its prefix.

Returns:

the new matched node.

need(**kwargs)

Consume an object corresponding to this node. Specific node type may have customized processing pipeline. For example, a SegmentedNode can do reassembly here. By default it sends an Interest packet to fetch a Data.

MatchedNode’s need simply calls the node’s need function.

Parameters:

kwargs – arguments from user input.

Returns:

the object needed, whose format is defined by specific node type. By default, it returns a tuple of the content and a dict of metadata.

async on_data(meta_info, content, raw_packet)

Called when a Data packet comes. It saves the Data packet into the cache, decrypts the content, and calls the node’s process_data function.

Parameters:
  • meta_info (MetaInfo) – the MetaInfo of the incoming Data packet.

  • content (Union[bytes, bytearray, memoryview, None]) – the content of the Data.

  • raw_packet (Union[bytes, bytearray, memoryview]) – the raw Data packet.

Returns:

whatever process_data returns.

async on_interest(param, app_param, raw_packet)

Called when an Interest packet comes. It looks up the cache and returns a Data packet if it exists. Otherwise, it decrypts ApplicationParameters and calls the node’s process_int function.

Parameters:
  • param (InterestParam) – the parameters of the incoming Interest.

  • app_param (Union[bytes, bytearray, memoryview, None]) – the ApplicationParameters of the Interest.

  • raw_packet (Union[bytes, bytearray, memoryview]) – the raw Interest packet.

provide(content, **kwargs)

Produce an object corresponding to this node, and make all generated Data packets available. Specific node type may have customized processing pipeline. For example, a SegmentedNode can do segmentation here. By default it makes a Data packet out of content and put it into the cache.

MatchedNode’s provide simply calls the node’s provide function.

Parameters:
  • content – the content of the object.

  • kwargs – other arguments from user input. Defined by specific node type.

async put_data(content=None, send_packet=False, **kwargs)

Generate the Data packet out of content. This function encrypts the content, encodes and signs the packet, saves it into the cache, and optionally sends it to the face. This function is called by the node’s provide function.

Parameters:
  • content (Union[bytes, bytearray, memoryview, None]) – the Data content.

  • send_packet (bool) – whether sends the Data packet to the face.

  • kwargs – other arguments generating the Data packet.

class ndn.schema.schema_tree.Node(parent=None)

Node represents a node in the static namespace tree.

Variables:
  • policies (Dict[Type[policy.Policy], policy.Policy]) – policies attached to this node

  • prefix (FormalName) – the prefix of the root node of the tree. Generally not set for other nodes.

  • ~.app (Optional[NDNApp]) – the NDNApp this static tree is attached to. Only available at the root.

async attach(app, prefix)

Attach this node to a specified NDNApp, register all name prefixes. This node becomes the root node of the application static tree. prefix is the prefix of the tree, which will be prepended to all names under this tree. For example, if prefix='/a/blog', then the node with path /articles from this node will become /a/blog/articles.

Warning

The way to register prefixes is still under discussion. Currently, we register the nodes that we can reach without going through a pattern. Also, there is no detach function yet, and no means to change the static tree after it’s attached.

Parameters:
  • app (NDNApp) – the NDNApp to be attached to.

  • prefix (Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview]) – the prefix of the static tree.

Returns:

whether succeeded or not.

exist(key)

If it has a child with specified name component or nme pattern.

Parameters:

key – a name component (bytes) or a patten (tuple).

Returns:

whether the child node exists

get_policy(typ)

Get the policy of specified type that applies to this node. It can be attached to this node or a parent of this node.

Parameters:

typ (Type[Policy]) – a policy type

Returns:

the policy. None if there does not exist one.

match(name)

Start from this node, go the path that matches with the name, and return the node it reaches when it cannot go further.

Parameters:

name (Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview]) – an NDN name.

Returns:

a MatchedNode, which contains the destination node and variables matched.

async need(match, **kwargs)

Consume an object corresponding to this node. Specific node type can override this function to have customized processing pipeline. For example, a SegmentedNode can do reassembly here. By default it sends an Interest packet to fetch a Data.

Parameters:
  • match – the matched node object of this node.

  • kwargs – other arguments from user input.

Returns:

This is defined by the node type. By default it returns what process_data() returns. That is, a tuple of contect and metadata dict.

async on_register(root, app, prefix, cached)

Called when the root node root is attached to app, and the attach() wants to register prefixed under the subtree rooted at this node.

Parameters:
  • root – the root of the static tree.

  • app (NDNApp) – the NDNApp to be attached to.

  • prefix (List[Union[bytes, bytearray, memoryview]]) – the prefix of the static tree.

  • cached (bool) – If there is a cache policy that applies to this node.

Returns:

whether succeeded or not.

async process_data(match, meta_info, content, raw_packet)

Processing an incoming Data packet. Specific node type can override this function to have customized processing pipeline. By default it returns the content.

Parameters:
  • match – the matched node object of this node.

  • meta_info (MetaInfo) – the MetaInfo of the Data packet.

  • content (Union[bytes, bytearray, memoryview, None]) – the content of the Data packet.

  • raw_packet (Union[bytes, bytearray, memoryview]) – the raw Data packet.

Returns:

a tuple, whose first element is data content after processing, and second is a dict[str, Any] containing metadata.

async process_int(match, param, app_param, raw_packet)

Processing an incoming Interest packet. Specific node type can override this function to have customized processing pipeline.

Note

This function will not be called if the Interest packet is satisfied with a cached Data packet.

Parameters:
  • match – the matched node object of this node.

  • param (InterestParam) – the parameters of the Interest packet.

  • app_param (Union[bytes, bytearray, memoryview, None]) – the ApplicationParameters of the Interest packet.

  • raw_packet (Union[bytes, bytearray, memoryview]) – the raw Interest packet.

async provide(match, content, **kwargs)

Produce an object corresponding to this node, and make all generated Data packets available. Specific node type can override this function to have customized processing pipeline. For example, a SegmentedNode can do segmentation here. By default it makes a Data packet out of content and put it into the cache.

Parameters:
  • match – the matched node object of this node.

  • content – the content of the object.

  • kwargs – other arguments from user input.

set_policy(typ, value)

Attach a policy to this node.

Parameters:
  • typ (Type[Policy]) – the policy type.

  • value (Policy) – the policy to be attached to this node.

exception ndn.schema.schema_tree.NodeExistsError(pattern)

Raised when trying to create a node which already exists.