servlib/bundling/CustodyTimer.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2006 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 #ifndef _CUSTODYTIMER_H_
       
    18 #define _CUSTODYTIMER_H_
       
    19 
       
    20 #include <oasys/serialize/Serialize.h>
       
    21 #include <oasys/thread/Timer.h>
       
    22 #include <oasys/util/Time.h>
       
    23 #include "bundling/BundleRef.h"
       
    24 #include "contacts/Link.h"
       
    25 
       
    26 namespace dtn {
       
    27 
       
    28 class Bundle;
       
    29 
       
    30 /**
       
    31  * Utility class to abstract out various parameters that can be used
       
    32  * to calculate custody retransmission timers. This means that future
       
    33  * extensions that take into account other parameters or factors can
       
    34  * simply extend this class and modify the calculate_timeout()
       
    35  * function to add new features.
       
    36  *
       
    37  * The current basic scheme calculates the timer as:
       
    38  * <code>
       
    39  * timer = min((min_ + (lifetime_pct_ * bundle->lifetime_ / 100)), max_)
       
    40  * </code>
       
    41  *
       
    42  * In other words, this class allows a retransmisison to be specified
       
    43  * according to a minimum timer (min_), a multiplying factor based on
       
    44  * the bundle's lifetime (lifetime_pct_), and a maximum bound (max_).
       
    45  * All values are in seconds.
       
    46  */
       
    47 class CustodyTimerSpec : public oasys::SerializableObject {
       
    48 public:
       
    49     /**
       
    50      * Custody timer defaults, values set in the static initializer.
       
    51      */
       
    52     static CustodyTimerSpec defaults_;
       
    53     
       
    54     /**
       
    55      * Constructor.
       
    56      */
       
    57     CustodyTimerSpec(u_int32_t min,
       
    58                      u_int32_t lifetime_pct,
       
    59                      u_int32_t max)
       
    60         : min_(min), lifetime_pct_(lifetime_pct), max_(max) {}
       
    61 
       
    62     /**
       
    63      * Default Constructor.
       
    64      */
       
    65     CustodyTimerSpec()
       
    66         : min_(defaults_.min_),
       
    67           lifetime_pct_(defaults_.lifetime_pct_),
       
    68           max_(defaults_.max_) {}
       
    69 
       
    70     /**
       
    71      * Calculate the appropriate timeout for the given bundle.
       
    72      */
       
    73     u_int32_t calculate_timeout(const Bundle* bundle) const;
       
    74 
       
    75     /**
       
    76      * Parse options to set the fields of the custody timer. Shifts
       
    77      * any non-matching options to the beginning of the vector by
       
    78      * using OptParser::parse_and_shift.
       
    79      *
       
    80      * @return the number of parsed options
       
    81      */
       
    82     int parse_options(int argc, const char* argv[],
       
    83                       const char** invalidp = NULL);
       
    84 
       
    85     void serialize(oasys::SerializeAction* a);
       
    86 
       
    87     u_int32_t min_;		///< min timer
       
    88     u_int32_t lifetime_pct_;	///< percentage of lifetime
       
    89     u_int32_t max_;		///< upper bound
       
    90 };
       
    91 
       
    92 /**
       
    93  * A custody transfer timer. Each bundle stores a vector of these
       
    94  * timers, as the bundle may be in flight on multiple links
       
    95  * concurrently, each having distinct retransmission timer parameters.
       
    96  * When a given timer fires, a retransmission is expected to be
       
    97  * initiated on only one link.
       
    98  * 
       
    99  * When a successful custody signal is received, the daemon will
       
   100  * cancel all timers and release custody, informing the routers as
       
   101  * such. If a failed custody signal is received or a timer fires, it
       
   102  * is up to the router to initiate a retransmission on one or more
       
   103  * links.
       
   104  */
       
   105 class CustodyTimer : public oasys::Timer, public oasys::Logger {
       
   106 public:
       
   107     /** Constructor */
       
   108     CustodyTimer(const oasys::Time& xmit_time,
       
   109                  const CustodyTimerSpec& spec,
       
   110                  Bundle* bundle, const LinkRef& link);
       
   111 
       
   112     /** Virtual timeout function */
       
   113     void timeout(const struct timeval& now);
       
   114 
       
   115     ///< The bundle for whom the the timer refers
       
   116     BundleRef bundle_;
       
   117 
       
   118     ///< The link that it was transmitted on
       
   119     LinkRef link_;
       
   120 };
       
   121 
       
   122 /**
       
   123  * Class for a vector of custody timers.
       
   124  */
       
   125 class CustodyTimerVec : public std::vector<CustodyTimer*> {};
       
   126 
       
   127 } // namespace dtn
       
   128 
       
   129 #endif /* _CUSTODYTIMER_H_ */