00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
00081
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
00110 reglist_.push_back(reg);
00111
00112
00113 if (!add_to_store || reg->regid() <= Registration::MAX_RESERVED_REGID) {
00114 return true;
00115 }
00116
00117
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 }