00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "APIRegistration.h"
00019 #include "RegistrationTable.h"
00020 #include "bundling/BundleEvent.h"
00021 #include "bundling/BundleDaemon.h"
00022 #include "storage/RegistrationStore.h"
00023
00024 namespace dtn {
00025
00026
00027 RegistrationTable::RegistrationTable()
00028 : Logger("RegistrationTable", "/dtn/registration/table")
00029 {
00030 }
00031
00032
00033 RegistrationTable::~RegistrationTable()
00034 {
00035 while (! reglist_.empty()) {
00036 delete reglist_.front();
00037 reglist_.pop_front();
00038 }
00039 }
00040
00041
00042 bool
00043 RegistrationTable::find(u_int32_t regid, RegistrationList::iterator* iter)
00044 {
00045 Registration* reg;
00046
00047 for (*iter = reglist_.begin(); *iter != reglist_.end(); ++(*iter)) {
00048 reg = *(*iter);
00049
00050 if (reg->regid() == regid) {
00051 return true;
00052 }
00053 }
00054
00055 return false;
00056 }
00057
00058
00059 Registration*
00060 RegistrationTable::get(u_int32_t regid) const
00061 {
00062 oasys::ScopeLock l(&lock_, "RegistrationTable");
00063
00064 RegistrationList::iterator iter;
00065
00066
00067
00068 if (const_cast<RegistrationTable*>(this)->find(regid, &iter)) {
00069 return *iter;
00070 }
00071 return NULL;
00072 }
00073
00074
00075 Registration*
00076 RegistrationTable::get(const EndpointIDPattern& eid) const
00077 {
00078 Registration* reg;
00079 RegistrationList::const_iterator iter;
00080
00081 for (iter = reglist_.begin(); iter != reglist_.end(); ++iter) {
00082 reg = *iter;
00083 if (reg->endpoint().equals(eid)) {
00084 return reg;
00085 }
00086 }
00087
00088 return NULL;
00089 }
00090
00091
00092 bool
00093 RegistrationTable::add(Registration* reg, bool add_to_store)
00094 {
00095 oasys::ScopeLock l(&lock_, "RegistrationTable");
00096
00097
00098 reglist_.push_back(reg);
00099
00100
00101 if (!add_to_store || reg->regid() <= Registration::MAX_RESERVED_REGID) {
00102 return true;
00103 }
00104
00105
00106 APIRegistration* api_reg = dynamic_cast<APIRegistration*>(reg);
00107 if (api_reg == NULL) {
00108 log_err("non-api registration %d passed to registration store",
00109 reg->regid());
00110 return false;
00111 }
00112
00113 log_info("adding registration %d/%s", reg->regid(),
00114 reg->endpoint().c_str());
00115
00116 if (! RegistrationStore::instance()->add(api_reg)) {
00117 log_err("error adding registration %d/%s: error in persistent store",
00118 reg->regid(), reg->endpoint().c_str());
00119 return false;
00120 }
00121
00122 return true;
00123 }
00124
00125
00126 bool
00127 RegistrationTable::del(u_int32_t regid)
00128 {
00129 oasys::ScopeLock l(&lock_, "RegistrationTable");
00130
00131 RegistrationList::iterator iter;
00132
00133 log_info("removing registration %d", regid);
00134
00135 if (! find(regid, &iter)) {
00136 log_err("error removing registration %d: no matching registration",
00137 regid);
00138 return false;
00139 }
00140
00141 if (! RegistrationStore::instance()->del(regid)) {
00142 log_err("error removing registration %d: error in persistent store",
00143 regid);
00144 return false;
00145 }
00146
00147 reglist_.erase(iter);
00148
00149 return true;
00150 }
00151
00152
00153 bool
00154 RegistrationTable::update(Registration* reg)
00155 {
00156 oasys::ScopeLock l(&lock_, "RegistrationTable");
00157
00158 log_info("updating registration %d/%s",
00159 reg->regid(), reg->endpoint().c_str());
00160
00161 APIRegistration* api_reg = dynamic_cast<APIRegistration*>(reg);
00162 if (api_reg == NULL) {
00163 log_err("non-api registration %d passed to registration store",
00164 reg->regid());
00165 return false;
00166 }
00167
00168 if (! RegistrationStore::instance()->update(api_reg)) {
00169 log_err("error updating registration %d/%s: error in persistent store",
00170 reg->regid(), reg->endpoint().c_str());
00171 return false;
00172 }
00173
00174 return true;
00175 }
00176
00177
00178 int
00179 RegistrationTable::get_matching(const EndpointID& demux,
00180 RegistrationList* reg_list) const
00181 {
00182 oasys::ScopeLock l(&lock_, "RegistrationTable");
00183
00184 int count = 0;
00185
00186 RegistrationList::const_iterator iter;
00187 Registration* reg;
00188
00189 log_debug("get_matching %s", demux.c_str());
00190
00191 for (iter = reglist_.begin(); iter != reglist_.end(); ++iter) {
00192 reg = *iter;
00193
00194 if (reg->endpoint().match(demux)) {
00195 log_debug("matched registration %d %s",
00196 reg->regid(), reg->endpoint().c_str());
00197 count++;
00198 reg_list->push_back(reg);
00199 }
00200 }
00201
00202 log_debug("get_matching %s: returned %d matches", demux.c_str(), count);
00203 return count;
00204 }
00205
00206
00207 void
00208 RegistrationTable::dump(oasys::StringBuffer* buf) const
00209 {
00210 oasys::ScopeLock l(&lock_, "RegistrationTable");
00211
00212 RegistrationList::const_iterator i;
00213 for (i = reglist_.begin(); i != reglist_.end(); ++i)
00214 {
00215 Registration* reg = *i;
00216
00217 buf->appendf("id %u: %s %s (%s%s) [expiration %d]\n",
00218 reg->regid(),
00219 reg->active() ? "active" : "passive",
00220 reg->endpoint().c_str(),
00221 Registration::failure_action_toa(reg->failure_action()),
00222 reg->failure_action() == Registration::EXEC ?
00223 reg->script().c_str() : "",
00224 reg->expiration());
00225 }
00226 }
00227
00232 const RegistrationList *
00233 RegistrationTable::reg_list() const
00234 {
00235 ASSERTF(lock_.is_locked_by_me(),
00236 "RegistrationTable::reg_list must be called while holding lock");
00237 return ®list_;
00238 }
00239
00240 }