servlib/prophet/AckList.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Baylor University
       
     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 _PROPHET_ACK_LIST_H_
       
    18 #define _PROPHET_ACK_LIST_H_
       
    19 
       
    20 #include <set>
       
    21 #include "Ack.h"
       
    22 #include "Bundle.h"
       
    23 #include "PointerList.h"
       
    24 
       
    25 namespace prophet
       
    26 {
       
    27 
       
    28 struct AckComp : public std::less<Ack*>
       
    29 {
       
    30     bool operator() (const Ack* a, const Ack* b) const
       
    31     {
       
    32         return *a < *b;
       
    33     }
       
    34 }; // AckComp
       
    35 
       
    36 // forward declaration
       
    37 class BundleCore;
       
    38 
       
    39 /**
       
    40  * Section 3.5 (p. 16) describes Prophet ACKs as needing to persist
       
    41  * in a node's storage beyond the lifetime of the bundle they represent.
       
    42  * ProphetAckList is that persistence (but not [yet] serializable to
       
    43  * permanent storage).
       
    44  */
       
    45 class AckList
       
    46 {
       
    47 public:
       
    48     /**
       
    49      * Default constructor
       
    50      */
       
    51     AckList() {}
       
    52 
       
    53     /**
       
    54      * Copy constructor
       
    55      */
       
    56     AckList(const AckList& list)
       
    57         : acks_(list.acks_) {}
       
    58 
       
    59     /**
       
    60      * Destructor
       
    61      */
       
    62     ~AckList();
       
    63 
       
    64     /**
       
    65      * Convenience method for inserting Ack into list; return true
       
    66      * upon success, else false if Ack already exists
       
    67      *
       
    68      * Expiration time stamp is actually an offset in seconds, from
       
    69      * creation time. Default is 0 (use offset of one day, 86400 sec)
       
    70      */
       
    71     bool insert(const std::string& dest_id, u_int32_t cts,
       
    72                 u_int32_t seq = 0, u_int32_t ets = 0);
       
    73 
       
    74     /**
       
    75      * Convenience method for inserting Ack into list; return true
       
    76      * upon success, else false if Ack already exists
       
    77      */
       
    78     bool insert(const Bundle* b, const BundleCore* core);
       
    79 
       
    80     /**
       
    81      * Insert Ack, return true on success, else false if Ack exists
       
    82      */
       
    83     bool insert(const Ack* ack);
       
    84 
       
    85     /**
       
    86      * Export list of Acks to PointerList<Ack>, return number of
       
    87      * elements exported
       
    88      */
       
    89     size_t clone(PointerList<Ack>& list) const;
       
    90 
       
    91     /**
       
    92      * Given a destination ID, return the number of Acks that match
       
    93      * (exact match only, no pattern matches). If list is non NULL,
       
    94      * fill with Acks that match
       
    95      */
       
    96     size_t fetch(const std::string& dest_id, PointerList<Ack>* list) const;
       
    97 
       
    98     /**
       
    99      * Visit every ACK in the list, and delete those for which the
       
   100      * expiration has passed; return the number of elements deleted
       
   101      */
       
   102     size_t expire();
       
   103 
       
   104     /**
       
   105      * Number of elements currently in list
       
   106      */
       
   107     size_t size() const { return acks_.size(); }
       
   108     
       
   109     /**
       
   110      * Convenience function to answer the question of whether this 
       
   111      * Bundle has been Ack'd
       
   112      */
       
   113     bool is_ackd(const std::string& dest_id,
       
   114                  u_int32_t cts, u_int32_t seq) const;
       
   115 
       
   116     /**
       
   117      * Accessor
       
   118      */
       
   119     bool empty() const { return acks_.empty(); }
       
   120 
       
   121 protected:
       
   122     typedef std::set<Ack*,AckComp> palist;
       
   123     palist acks_;
       
   124 
       
   125 }; // AckList
       
   126 
       
   127 }; // namespace prophet
       
   128 
       
   129 #endif // _PROPHET_ACK_LIST_H_