00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00030
00031
00032 static patroot root;
00033
00034
00035
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
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
00082 data = calloc(1, sizeof(counter_data_t));
00083 INSIST_ERR(data != NULL);
00084 INSIST_ERR(strlen(message) < MESSAGE_STR_SIZE);
00085
00086 strcpy(data->message, message);
00087
00088
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
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;
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
00186
00187 if (dax_get_object_by_path(NULL, data_config_name, &dop, FALSE)) {
00188
00189 if (dax_is_changed(dop)) {
00190
00191
00192
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) {
00201 dax_error(dop, "Failed to add message to configuration");
00202 status = EFAIL;
00203 } else if(!check) {
00204 reset_message(msg);
00205 }
00206
00207 } else {
00208 dax_error(dop, "Couldn't read message from configuration");
00209 status = EFAIL;
00210 }
00211 }
00212 } else {
00213
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