RegistrationTable.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include "APIRegistration.h"
00040 #include "RegistrationTable.h"
00041 #include "bundling/BundleEvent.h"
00042 #include "bundling/BundleDaemon.h"
00043 #include "storage/RegistrationStore.h"
00044 
00045 namespace dtn {
00046 
00047 RegistrationTable::RegistrationTable()
00048     : Logger("RegistrationTable", "/dtn/registration/table")
00049 {
00050 }
00051 
00052 RegistrationTable::~RegistrationTable()
00053 {
00054     NOTREACHED;
00055 }
00056 
00057 //----------------------------------------------------------------------
00058 bool
00059 RegistrationTable::find(u_int32_t regid, RegistrationList::iterator* iter)
00060 {
00061     Registration* reg;
00062 
00063     for (*iter = reglist_.begin(); *iter != reglist_.end(); ++(*iter)) {
00064         reg = *(*iter);
00065 
00066         if (reg->regid() == regid) {
00067             return true;
00068         }
00069     }
00070 
00071     return false;
00072 }
00073 
00074 //----------------------------------------------------------------------
00075 Registration*
00076 RegistrationTable::get(u_int32_t regid) const
00077 {
00078     RegistrationList::iterator iter;
00079 
00080     // the const_cast lets us use the same find method for get as we
00081     // use for add/del
00082     if (const_cast<RegistrationTable*>(this)->find(regid, &iter)) {
00083         return *iter;
00084     }
00085     return NULL;
00086 }
00087 
00088 //----------------------------------------------------------------------
00089 Registration*
00090 RegistrationTable::get(const EndpointIDPattern& eid) const
00091 {
00092     Registration* reg;
00093     RegistrationList::const_iterator iter;
00094     
00095     for (iter = reglist_.begin(); iter != reglist_.end(); ++iter) {
00096         reg = *iter;
00097         if (reg->endpoint().equals(eid)) {
00098             return reg;
00099         }
00100     }
00101     
00102     return NULL;
00103 }
00104 
00105 //----------------------------------------------------------------------
00106 bool
00107 RegistrationTable::add(Registration* reg, bool add_to_store)
00108 {
00109     // put it in the list
00110     reglist_.push_back(reg);
00111 
00112     // don't store (or log) default registrations 
00113     if (!add_to_store || reg->regid() <= Registration::MAX_RESERVED_REGID) {
00114         return true;
00115     }
00116 
00117     // now, all we should get are API registrations
00118     APIRegistration* api_reg = dynamic_cast<APIRegistration*>(reg);
00119     if (api_reg == NULL) {
00120         log_err("non-api registration %d passed to registration store",
00121                 reg->regid());
00122         return false;
00123     }
00124     
00125     log_info("adding registration %d/%s", reg->regid(),
00126              reg->endpoint().c_str());
00127     
00128     if (! RegistrationStore::instance()->add(api_reg)) {
00129         log_err("error adding registration %d/%s: error in persistent store",
00130                 reg->regid(), reg->endpoint().c_str());
00131         return false;
00132     }
00133     
00134     return true;
00135 }
00136 
00137 //----------------------------------------------------------------------
00138 bool
00139 RegistrationTable::del(u_int32_t regid)
00140 {
00141     RegistrationList::iterator iter;
00142 
00143     log_info("removing registration %d", regid);
00144     
00145     if (! find(regid, &iter)) {
00146         log_err("error removing registration %d: no matching registration",
00147                 regid);
00148         return false;
00149     }
00150 
00151     if (! RegistrationStore::instance()->del(regid)) {
00152         log_err("error removing registration %d: error in persistent store",
00153                 regid);
00154         return false;
00155     }
00156 
00157     reglist_.erase(iter);
00158 
00159     return true;
00160 }
00161 
00162 //----------------------------------------------------------------------
00163 bool
00164 RegistrationTable::update(Registration* reg)
00165 {
00166     log_info("updating registration %d/%s",
00167              reg->regid(), reg->endpoint().c_str());
00168 
00169     APIRegistration* api_reg = dynamic_cast<APIRegistration*>(reg);
00170     if (api_reg == NULL) {
00171         log_err("non-api registration %d passed to registration store",
00172                 reg->regid());
00173         return false;
00174     }
00175     
00176     if (! RegistrationStore::instance()->update(api_reg)) {
00177         log_err("error updating registration %d/%s: error in persistent store",
00178                 reg->regid(), reg->endpoint().c_str());
00179         return false;
00180     }
00181 
00182     return true;
00183 }
00184 
00185 //----------------------------------------------------------------------
00186 int
00187 RegistrationTable::get_matching(const EndpointID& demux,
00188                                 RegistrationList* reg_list) const
00189 {
00190     int count = 0;
00191     
00192     RegistrationList::const_iterator iter;
00193     Registration* reg;
00194 
00195     log_debug("get_matching %s", demux.c_str());
00196 
00197     for (iter = reglist_.begin(); iter != reglist_.end(); ++iter) {
00198         reg = *iter;
00199 
00200         if (reg->endpoint().match(demux)) {
00201             log_debug("matched registration %d %s",
00202                       reg->regid(), reg->endpoint().c_str());
00203             count++;
00204             reg_list->push_back(reg);
00205         }
00206     }
00207 
00208     log_debug("get_matching %s: returned %d matches", demux.c_str(), count);
00209     return count;
00210 }
00211 
00212 
00213 void
00214 RegistrationTable::dump(oasys::StringBuffer* buf) const
00215 {
00216     RegistrationList::const_iterator i;
00217     for (i = reglist_.begin(); i != reglist_.end(); ++i)
00218     {
00219         Registration* reg = *i;
00220 
00221         buf->appendf("id %u: %s %s (%s%s) expiration %d\n",
00222                      reg->regid(),
00223                      reg->active() ? "active" : "passive",
00224                      reg->endpoint().c_str(),
00225                      Registration::failure_action_toa(reg->failure_action()),
00226                      reg->failure_action() == Registration::EXEC ?
00227                         reg->script().c_str() : "",
00228                      reg->expiration());
00229     }
00230 }
00231 
00232 } // namespace dtn

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1