ped_schedule.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ped_schedule.c 366303 2010-03-05 13:14:01Z taoliu $
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  
00021 #include <sync/common.h>
00022 #include "ped_schedule.h"
00023 #include "ped_services.h"
00024 #include "ped_logging.h"
00025 
00026 #include PE_OUT_H
00027 
00028 /*** Constants ***/
00029 
00030 
00031 /*** Data Structures ***/
00032 
00033 static evTimerID  timer_id;    
00034 static boolean    timer_set;   
00035 static uint32_t   period;      
00036 
00037 extern evContext  ped_ctx;  
00038 
00039 
00040 /*** STATIC/INTERNAL Functions ***/
00041 
00057 static void
00058 ped_restart(evContext ctx __unused,
00059             void * uap  __unused,
00060             struct timespec due __unused,
00061             struct timespec inter __unused)
00062 {
00063     junos_trace(PED_TRACEFLAG_NORMAL, "%s", __func__);
00064 
00065     ped_schedule_reset();
00066 }
00067 
00091 static void
00092 ped_periodic(evContext ctx __unused,
00093              void * uap  __unused,
00094              struct timespec due __unused,
00095              struct timespec inter __unused)
00096 {
00097     static struct timespec first_call_time;
00098     static int attempts = 0;
00099 
00100     struct timespec time_difference;
00101 
00102     junos_trace(PED_TRACEFLAG_HB, "%s", __func__);
00103     
00104     // check if we've been processing these a lot _recently_
00105     // by a lot we mean more than normal due to errors and retries
00106     
00107     if(++attempts > MAX_RETRY_ATTEMPTS) {
00108         
00109         time_difference = evSubTime(evNowTime(), first_call_time);
00110         
00111         if(evCmpTime(time_difference, evConsTime(SCHEDULE_STOP_RETRY, 0)) < 0) {
00112             
00113             // we've had more than MAX_RETRY_ATTEMPTS in less than
00114             // SCHEDULE_STOP_RETRY seconds... STOP retrying!
00115             attempts = 0;
00116 
00117             // clear any periodic events associated with this timer
00118             if(timer_set && evClearTimer(ped_ctx, timer_id)) {
00119                 ERRMSG(PED, TRACE_LOG_ERR,
00120                         "%s: evClearTimer() FAILED!", __func__);
00121             }
00122 
00123             // restart trying later (a one-shot event/non-periodic)
00124             // Wait for SCHEDULE_RESTART_TIME seconds !
00125             if(evSetTimer(ped_ctx, ped_restart, NULL,
00126                   evAddTime(evNowTime(), evConsTime(SCHEDULE_RESTART_TIME, 0)),
00127                   evConsTime(0, 0), &timer_id)) {
00128         
00129                 ERRMSG(PED, TRACE_LOG_ERR,
00130                         "%s: evSetTimer() FAILED! Cannot continue "
00131                         "retries or periodic events", __func__);
00132 
00133                 timer_set = FALSE;
00134             } else {
00135                 timer_set = TRUE;
00136             }
00137             return;
00138         }
00139 
00140         // hasn't been _recently_ so just proceed...
00141         attempts = 0;
00142     }
00143 
00144     if(attempts == 1)
00145         first_call_time = evNowTime(); // record this "first" event
00146 
00147     // Process periodic events:
00148     if(!check_psd_hb()) {
00149         ped_schedule_reset(); // upon failure re-try immediately 
00150     }
00151 }
00152 
00153 /*** GLOBAL/EXTERNAL Functions ***/
00154 
00155 
00163 int
00164 ped_schedule_init(void)
00165 {
00166     junos_trace(PED_TRACEFLAG_NORMAL, "%s", __func__);
00167 
00168     timer_set = FALSE;
00169     period = SCHEDULE_PERIOD;
00170     
00171     return 0;
00172 }
00173 
00174 
00186 void
00187 ped_schedule_reset(void)
00188 {
00189     junos_trace(PED_TRACEFLAG_NORMAL, "%s", __func__);
00190 
00191     // unset it then set it again
00192     if(timer_set && evClearTimer(ped_ctx, timer_id)) {
00193         ERRMSG(PED, TRACE_LOG_ERR,
00194                 "%s: evClearTimer() FAILED!", __func__);
00195     }
00196 
00197     /*
00198      * Set the timer:
00199      * immediate one-shot event at time (now + SCHEDULE_IMMEDIATE_DELAY), and
00200      * periodocally afterward every period seconds
00201      */
00202     if(evSetTimer(ped_ctx, ped_periodic, NULL,
00203             evAddTime(evNowTime(), evConsTime(SCHEDULE_IMMEDIATE_DELAY, 0)),
00204             evConsTime(period, 0), &timer_id)) {
00205 
00206         ERRMSG(PED, TRACE_LOG_ERR,
00207                 "%s: evSetTimer() FAILED! Cannot continue "
00208                 "retries or periodic events", __func__);
00209 
00210         timer_set = FALSE;
00211     } else {
00212         timer_set = TRUE;
00213     }
00214 }
00215 
00216 
00220 void
00221 ped_schedule_stop(void)
00222 {
00223     // unset it
00224     if(timer_set && evClearTimer(ped_ctx, timer_id)) {
00225         ERRMSG(PED, TRACE_LOG_ERR,
00226                 "%s: evClearTimer() FAILED!", __func__);
00227     }
00228 }
00229 

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:27:06 2010 for SDK Your Net Corporation Policy Manager Example: Policy Enforcement Daemon 1.0 by Doxygen 1.5.1