counterd_config.c

Go to the documentation of this file.
00001 /*
00002  * $Id: counterd_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 "counterd_config.h"
00026 #include "counterd_message.h"
00027 #include COUNTERD_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, counter_data_t, node)
00043 
00044 
00045 
00054 static int
00055 delete_message (counter_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 counter_data_t *
00077 add_message (const char * message)
00078 {
00079     counter_data_t *data;
00080 
00081     /* allocate & initialize data */
00082     data = calloc(1, sizeof(counter_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     counter_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 counter_data_t *
00140 first_message (void)
00141 {
00142     return data_entry(patricia_find_next(&root, NULL));
00143 }
00144 
00145 
00155 counter_data_t *
00156 next_message (counter_data_t * data)
00157 {
00158     return data_entry(patricia_find_next(&root, (data ? &(data->node) : NULL)));
00159 }
00160 
00161 
00172 int
00173 counterd_config_read (int check)
00174 {
00175 
00176     ddl_handle_t * dop = NULL; /* DDL Object Pointer */
00177     const char   * data_config_name[] =  { DDLNAME_SYNC,
00178                                            DDLNAME_SYNC_COUNTER,
00179                                            DDLNAME_SYNC_COUNTER_MESSAGE,
00180                                            NULL};
00181     char          msg[MESSAGE_STR_SIZE];
00182     int           status = SUCCESS;
00183 
00184     /*
00185      * read counterd data configuration
00186      */
00187     if (dax_get_object_by_path(NULL, data_config_name, &dop, FALSE)) {
00188 
00189         if (dax_is_changed(dop)) {
00190             /* if message has changed */
00191 
00192             /* clear stored config data, & repopulate with new config data */
00193             if((status = clear_messages()) == EFAIL) {
00194                 dax_error(dop, "Failed to clear stored configuration");
00195             }
00196     
00197             if (dax_get_stringr_by_aid(dop, COUNTER_MESSAGE_CONTENTS, 
00198                     msg, sizeof(msg))) {
00199     
00200                 if(add_message(msg) == NULL) { /* add this message */
00201                     dax_error(dop, "Failed to add message to configuration");
00202                     status = EFAIL;
00203                 } else if(!check) {
00204                     reset_message(msg); /* message added, GENCFG reset */
00205                 }
00206     
00207             } else {
00208                 dax_error(dop, "Couldn't read message from configuration");
00209                 status = EFAIL;
00210             }
00211         }
00212     } else {
00213         /* clear stored config data because we have no configure data */
00214         if((status = clear_messages()) == EFAIL) {
00215             dax_error(dop, "Failed to clear stored configuration");
00216         }
00217     }
00218 
00219     if (dop) {
00220         dax_release_object(&dop);
00221     }
00222     
00223     return status;
00224 }
00225 
00226 

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