CustodyTimer.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  *  2006 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <algorithm>
00040 #include <oasys/util/OptParser.h>
00041 
00042 #include "Bundle.h"
00043 #include "BundleDaemon.h"
00044 #include "BundleEvent.h"
00045 #include "CustodyTimer.h"
00046 
00047 namespace dtn {
00048 
00056 CustodyTimerSpec CustodyTimerSpec::defaults_(30 * 60, 25, 0);
00057 
00058 //----------------------------------------------------------------------
00059 u_int32_t
00060 CustodyTimerSpec::calculate_timeout(const Bundle* bundle) const
00061 {
00062     u_int32_t timeout = min_;
00063     timeout += (u_int32_t)((double)lifetime_pct_ * bundle->expiration_ / 100.0);
00064 
00065     if (max_ != 0) {
00066         timeout = std::min(timeout, max_);
00067     }
00068     
00069     log_debug("/dtn/bundle/custody_timer", "calculate_timeout: "
00070               "min %u, lifetime_pct %u, expiration %u, max %u: timeout %u",
00071               min_, lifetime_pct_, bundle->expiration_, max_, timeout);
00072     return timeout;
00073 }
00074 
00075 //----------------------------------------------------------------------
00076 int
00077 CustodyTimerSpec::parse_options(int argc, const char* argv[],
00078                                 const char** invalidp)
00079 {
00080     oasys::OptParser p;
00081     p.addopt(new oasys::UIntOpt("custody_timer_min", &min_));
00082     p.addopt(new oasys::UIntOpt("custody_timer_lifetime_pct", &lifetime_pct_));
00083     p.addopt(new oasys::UIntOpt("custody_timer_max", &max_));
00084     return p.parse_and_shift(argc, argv, invalidp);
00085 }
00086 
00087 //----------------------------------------------------------------------
00088 CustodyTimer::CustodyTimer(const struct timeval& xmit_time,
00089                            const CustodyTimerSpec& spec,
00090                            Bundle* bundle, Link* link)
00091     : Logger("CustodyTimer", "/dtn/bundle/custody_timer"),
00092       bundle_(bundle, "CustodyTimer"), link_(link)
00093 {
00094     struct timeval tv = xmit_time;
00095     u_int32_t delay = spec.calculate_timeout(bundle);
00096     tv.tv_sec += delay;
00097 
00098     struct timeval now;
00099     ::gettimeofday(&now, 0);
00100     log_info("scheduling timer: xmit_time %u.%u delay %u secs "
00101              "(in %lu msecs) for *%p",
00102              (u_int)xmit_time.tv_sec, (u_int)xmit_time.tv_usec, delay,
00103              TIMEVAL_DIFF_MSEC(tv, now), bundle);
00104     schedule_at(&tv);
00105 }
00106 
00107 //----------------------------------------------------------------------
00108 void
00109 CustodyTimer::timeout(const struct timeval& now)
00110 {
00111     (void)now;
00112     log_info("CustodyTimer::timeout");
00113     BundleDaemon::post(new CustodyTimeoutEvent(bundle_.object(), link_));
00114 }
00115 
00116 } // namespace dtn

Generated on Fri Dec 22 14:47:58 2006 for DTN Reference Implementation by  doxygen 1.5.1