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