CustodyTimer.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include <algorithm>
00019 #include <oasys/util/OptParser.h>
00020 
00021 #include "Bundle.h"
00022 #include "BundleDaemon.h"
00023 #include "BundleEvent.h"
00024 #include "CustodyTimer.h"
00025 
00026 namespace dtn {
00027 
00035 CustodyTimerSpec CustodyTimerSpec::defaults_(30 * 60, 25, 0);
00036 
00037 //----------------------------------------------------------------------
00038 u_int32_t
00039 CustodyTimerSpec::calculate_timeout(const Bundle* bundle) const
00040 {
00041     u_int32_t timeout = min_;
00042     timeout += (u_int32_t)((double)lifetime_pct_ * bundle->expiration_ / 100.0);
00043 
00044     if (max_ != 0) {
00045         timeout = std::min(timeout, max_);
00046     }
00047     
00048     log_debug_p("/dtn/bundle/custody_timer", "calculate_timeout: "
00049                 "min %u, lifetime_pct %u, expiration %u, max %u: timeout %u",
00050                 min_, lifetime_pct_, bundle->expiration_, max_, timeout);
00051     return timeout;
00052 }
00053 
00054 //----------------------------------------------------------------------
00055 int
00056 CustodyTimerSpec::parse_options(int argc, const char* argv[],
00057                                 const char** invalidp)
00058 {
00059     oasys::OptParser p;
00060     p.addopt(new oasys::UIntOpt("custody_timer_min", &min_));
00061     p.addopt(new oasys::UIntOpt("custody_timer_lifetime_pct", &lifetime_pct_));
00062     p.addopt(new oasys::UIntOpt("custody_timer_max", &max_));
00063     return p.parse_and_shift(argc, argv, invalidp);
00064 }
00065 
00066 //----------------------------------------------------------------------
00067 CustodyTimer::CustodyTimer(const struct timeval& xmit_time,
00068                            const CustodyTimerSpec& spec,
00069                            Bundle* bundle, Link* link)
00070     : Logger("CustodyTimer", "/dtn/bundle/custody_timer"),
00071       bundle_(bundle, "CustodyTimer"), link_(link)
00072 {
00073     struct timeval tv = xmit_time;
00074     u_int32_t delay = spec.calculate_timeout(bundle);
00075     tv.tv_sec += delay;
00076 
00077     struct timeval now;
00078     ::gettimeofday(&now, 0);
00079     log_info("scheduling timer: xmit_time %u.%u delay %u secs "
00080              "(in %lu msecs) for *%p",
00081              (u_int)xmit_time.tv_sec, (u_int)xmit_time.tv_usec, delay,
00082              TIMEVAL_DIFF_MSEC(tv, now), bundle);
00083     schedule_at(&tv);
00084 }
00085 
00086 //----------------------------------------------------------------------
00087 void
00088 CustodyTimer::timeout(const struct timeval& now)
00089 {
00090     (void)now;
00091     log_info("CustodyTimer::timeout");
00092     BundleDaemon::post(new CustodyTimeoutEvent(bundle_.object(), link_));
00093 }
00094 
00095 } // namespace dtn

Generated on Sat Sep 8 08:43:26 2007 for DTN Reference Implementation by  doxygen 1.5.3