Creating the Tree and Adding Messages

The patricia tree is a data structure commonly used in networking applications to facilitate lookups with keys that are strings.

This structure has the ability to contain large ranges of values, and is especially suited to the hierarchical organization of IP addresses and other configuration values.

The SDK supplies APIs for working with patricia trees. These APIs are contained in libjuniper, in the file patricia.h. The application uses these APIs as follows:

  1. In config.c, define a variable using the predefined type patroot, for the initial root node of the tree:

    static patroot root;
    

  2. Call an API to initialize the tree:

    void
     init_messages_ds (void)
     {
         patricia_root_init(&root, FALSE, MESSAGE_STR_SIZE, 0);
     }
    
    

  3. Define a structure, helloworld_data_t, to store the data, in the header file helloworldd_config.h. The first field is a node in the tree, and the second field stores data (that is, the message):

    typedef struct helloworld_data_s {
         patnode    node;                          
         char       message[MESSAGE_STR_SIZE];     
     } helloworld_data_t;
    
    

  4. Use the PATNODE_TO_STRUCT macro to define functions that hide the patricia structure from the rest of the code.

    For details about how to use this macro, see Using the Patricia Tree Library.

  5. Call APIs from libjuniper to add data to the tree. The following function allocates memory for the data, adds it to the tree, and performs basic error checking ( INSIST_ERR is defined in junos_trace.h, in libjunos-sdk ):

    static helloworld_data_t *
    add_message (const char * message)
    {
        helloworld_data_t *data;
    
        /* allocate & initialize data */
        data = calloc(1, sizeof(helloworld_data_t)); /* calloc zeros mem */
        INSIST_ERR(data != NULL); /* check that allocation worked */
        INSIST_ERR(strlen(message) < MESSAGE_STR_SIZE); /* check for length */
        
        strcpy(data->message, message);
    
        /* add to data_root patricia tree */
        patricia_node_init_length(&data->node,
                                  strlen(data->message) + 1);
                                  
        if (!patricia_add(&root, &data->node)) {
            return NULL;
        }
    
        return data;
    }
    
    

Creating the User Interface


2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:26:47 2010 for Juniper Networks Partner Solution Development Platform JUNOS SDK 10.2R1 by Doxygen 1.4.5