helloworldd_config.c

Go to the documentation of this file.
00001 /*
00002  * $Id: helloworldd_config.c 346460 2009-11-14 05:06:47Z ssiano $
00003  *
00004  * This code is provided as is by Juniper Networks SDK Developer Support.
00005  * It is provided with no warranties or guarantees, and Juniper Networks
00006  * will not provide support or maintenance of this code in any fashion.
00007  * The code is provided only to help a developer better understand how
00008  * the SDK can be used.
00009  *
00010  * Copyright (c) 2007-2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <ddl/dax.h>
00023 #include <jnx/patricia.h>
00024 #include <jnx/junos_trace.h>
00025 #include "helloworldd_config.h"
00026 
00027 #include HELLOWORLD_OUT_H
00028 
00029 /*** Data Structures ***/
00030 
00031 
00032 static patroot root; 
00033 
00034 
00035 /*** STATIC/INTERNAL Functions ***/
00036 
00037 
00042 PATNODE_TO_STRUCT(data_entry, helloworld_data_t, node)
00043 
00044 
00045 
00054 static int
00055 delete_message (helloworld_data_t *data)
00056 {
00057     /* remove data from patricia tree */
00058     if (!patricia_delete(&root, &data->node)) {
00059         free(data);
00060         return EFAIL;
00061     }
00062 
00063     free(data);
00064     return SUCCESS;
00065 }
00066 
00067 
00076 static helloworld_data_t *
00077 add_message (const char * message)
00078 {
00079     helloworld_data_t *data;
00080 
00081     /* allocate & initialize data */
00082     data = calloc(1, sizeof(helloworld_data_t)); /* calloc zeros mem */
00083     INSIST_ERR(data != NULL); /* check that allocation worked */
00084     INSIST_ERR(strlen(message) < MESSAGE_STR_SIZE); /* check for length */
00085     
00086     strcpy(data->message, message);
00087 
00088     /* add to data_root patricia tree */
00089     patricia_node_init_length(&data->node,
00090                               strlen(data->message) + 1);
00091                               
00092     if (!patricia_add(&root, &data->node)) {
00093         free(data);
00094         return NULL;
00095     }
00096 
00097     return data;
00098 }
00099 
00100 
00106 static int
00107 clear_messages (void)
00108 {
00109     helloworld_data_t *data = NULL;
00110 
00111     while ((data = first_message()) != NULL) {
00112         if(delete_message(data) == EFAIL) {
00113             return EFAIL;
00114         }
00115     }
00116     return SUCCESS;
00117 }
00118 
00119 
00120 /*** GLOBAL/EXTERNAL Functions ***/
00121 
00122 
00127 void
00128 init_messages_ds (void)
00129 {
00130     patricia_root_init(&root, FALSE, MESSAGE_STR_SIZE, 0);
00131 }
00132 
00133 
00139 helloworld_data_t *
00140 first_message (void)
00141 {
00142     return data_entry(patricia_find_next(&root, NULL));
00143 }
00144 
00145 
00155 helloworld_data_t *
00156 next_message (helloworld_data_t * data)
00157 {
00158     return data_entry(patricia_find_next(&root, (data ? &(data->node) : NULL)));
00159 }
00160 
00161 
00172 int
00173 helloworld_config_read (int check __unused)
00174 {
00175 
00176     ddl_handle_t  * dop = NULL; /* DDL Object Pointer */
00177     const char    * hw_data_config_name[] = 
00178                                             {DDLNAME_SYNC,
00179                                              DDLNAME_SYNC_HELLOWORLD,
00180                                              DDLNAME_SYNC_HELLOWORLD_MESSAGE,
00181                                              NULL};
00182     char          hw_msg[MESSAGE_STR_SIZE];
00183     int           status = SUCCESS;
00184 
00185     /*
00186      * read helloworldd data configuration
00187      */
00188     if (dax_get_object_by_path(NULL, hw_data_config_name, &dop, FALSE)) {
00189 
00190         if (dax_is_changed(dop)) {
00191             /* if message has changed */
00192 
00193             /* clear stored config data, & repopulate with new config data */
00194             if((status = clear_messages()) == EFAIL) {
00195                 dax_error(dop, "Failed to clear stored configuration");
00196             }
00197     
00198             if (dax_get_stringr_by_aid(dop, HELLOWORLD_MESSAGE_CONTENTS, 
00199                     hw_msg, sizeof(hw_msg))) {
00200     
00201                 if(add_message(hw_msg) == NULL) { /* add this message */
00202                     dax_error(dop, "Failed to add message to configuration");
00203                     status = EFAIL;
00204                 }
00205     
00206             } else {
00207                 dax_error(dop, "Couldn't read message from configuration");
00208                 status = EFAIL;
00209             }
00210         }
00211     } else {
00212         /* clear stored config data because we have no configure data */
00213         if((status = clear_messages()) == EFAIL) {
00214             dax_error(dop, "Failed to clear stored configuration");
00215         }
00216     }
00217 
00218     if (dop) {
00219         dax_release_object(&dop);
00220     }
00221     
00222     return status;
00223 }
00224 
00225 

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:59 2010 for SDK Your Net Corporation Helloworld Example 1.0 by Doxygen 1.5.1