00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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 }