servlib/routing/ProphetNodeList.cc
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 #ifdef HAVE_CONFIG_H
       
    18 #  include <dtn-config.h>
       
    19 #endif
       
    20 #include "ProphetNodeList.h"
       
    21 
       
    22 namespace dtn
       
    23 {
       
    24 
       
    25 ProphetNodeList::ProphetNodeList()
       
    26 {
       
    27 }
       
    28 
       
    29 ProphetNodeList::~ProphetNodeList()
       
    30 {
       
    31     clear();
       
    32 }
       
    33     
       
    34 void
       
    35 ProphetNodeList::load(const prophet::Node* n)
       
    36 {
       
    37     log_debug_p("/dtn/route/nodelist","load");
       
    38     iterator i;
       
    39     ASSERT(n != NULL);
       
    40     ASSERT(! find(n->dest_id(), i));
       
    41 
       
    42     log_debug_p("/dtn/route/nodelist",
       
    43             "add new node for %s (age %u pv %.2f flags %s%s%s)",
       
    44             n->dest_id(),
       
    45             n->age(),
       
    46             n->p_value(),
       
    47             n->relay() ? "R" : "-",
       
    48             n->custody() ? "C" : "-",
       
    49             n->internet_gw() ? "I" : "-");
       
    50     // create and insert new object
       
    51     ProphetNode* a = new ProphetNode(*n);
       
    52     // internally and in ProphetStorage
       
    53     list_.insert(i,a);
       
    54 }
       
    55 
       
    56 void
       
    57 ProphetNodeList::update(const prophet::Node* n)
       
    58 {
       
    59     log_debug_p("/dtn/route/nodelist","update");
       
    60     iterator i;
       
    61     ASSERT(n != NULL);
       
    62     if (! find(n->dest_id(), i))
       
    63     {
       
    64         log_debug_p("/dtn/route/nodelist",
       
    65                 "add new node for %s",n->dest_id());
       
    66         // create and insert new object
       
    67         ProphetNode* a = new ProphetNode(*n);
       
    68         // internally and in ProphetStorage
       
    69         list_.insert(i,a);
       
    70         ProphetStore::instance()->add(a);
       
    71     }
       
    72     else
       
    73     {
       
    74         log_debug_p("/dtn/route/nodelist",
       
    75                 "update existing node for %s",n->dest_id());
       
    76         // update existing
       
    77         ProphetNode* a = static_cast<ProphetNode*>(*i);
       
    78         a->set_pvalue( n->p_value() );
       
    79         ProphetStore::instance()->update(a);
       
    80     }
       
    81 }
       
    82 
       
    83 void
       
    84 ProphetNodeList::del(const prophet::Node* n)
       
    85 {
       
    86     iterator i;
       
    87     ASSERT(n != NULL);
       
    88     if (find(n->dest_id(), i))
       
    89     {
       
    90         // remove from list and from ProphetStorage
       
    91         ProphetNode* a = static_cast<ProphetNode*>(*i);
       
    92         list_.erase(i);
       
    93         ProphetStore::instance()->del(a);
       
    94         delete a;
       
    95     }
       
    96 }
       
    97 
       
    98 const prophet::Node*
       
    99 ProphetNodeList::find(const std::string& dest_id) const
       
   100 {
       
   101     iterator i;
       
   102     ProphetNodeList* me = const_cast<ProphetNodeList*>(this);
       
   103     if (me == NULL) return NULL;
       
   104     if (me->find(dest_id,i))
       
   105         return *i;
       
   106     return NULL;
       
   107 }
       
   108 
       
   109 void
       
   110 ProphetNodeList::clone(prophet::Table* nodes,
       
   111                        const prophet::NodeParams* params)
       
   112 {
       
   113     if (nodes == NULL || params == NULL) return;
       
   114 
       
   115     std::list<const prophet::Node*> list(list_.begin(),list_.end());
       
   116     nodes->assign(list,params);
       
   117 }
       
   118 
       
   119 void
       
   120 ProphetNodeList::clear()
       
   121 {
       
   122     while (!list_.empty())
       
   123     {
       
   124         delete list_.front();
       
   125         list_.pop_front();
       
   126     }
       
   127 }
       
   128 
       
   129 bool
       
   130 ProphetNodeList::find(const std::string& dest_id, iterator& i)
       
   131 {
       
   132     i = list_.begin();
       
   133     while (i != list_.end() && (*i)->dest_id() < dest_id)
       
   134         i++;
       
   135     return (i != list_.end() && (*i)->dest_id() == dest_id);
       
   136 }
       
   137 
       
   138 }; // namespace dtn