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 <oasys/thread/SpinLock.h>
00022 #include <oasys/util/StringBuffer.h>
00023 #include "ForwardingLog.h"
00024 #include "conv_layers/ConvergenceLayer.h"
00025 #include "reg/Registration.h"
00026
00027 namespace dtn {
00028
00029
00030 ForwardingLog::ForwardingLog(oasys::SpinLock* lock)
00031 : lock_(lock)
00032 {
00033 }
00034
00035
00036 bool
00037 ForwardingLog::get_latest_entry(const LinkRef& link, ForwardingInfo* info) const
00038 {
00039 oasys::ScopeLock l(lock_, "ForwardingLog::get_latest_state");
00040
00041
00042 Log::const_reverse_iterator iter;
00043 for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00044 {
00045 if (iter->link_name() == link->name_str())
00046 {
00047
00048
00049
00050 ASSERT(iter->remote_eid() == EndpointID::NULL_EID() ||
00051 iter->remote_eid() == link->remote_eid());
00052 *info = *iter;
00053 return true;
00054 }
00055 }
00056
00057 return false;
00058 }
00059
00060
00061 ForwardingLog::state_t
00062 ForwardingLog::get_latest_entry(const LinkRef& link) const
00063 {
00064 ForwardingInfo info;
00065 if (! get_latest_entry(link, &info)) {
00066 return ForwardingInfo::NONE;
00067 }
00068
00069 return info.state();
00070 }
00071
00072
00073 bool
00074 ForwardingLog::get_latest_entry(const Registration* reg,
00075 ForwardingInfo* info) const
00076 {
00077 oasys::ScopeLock l(lock_, "ForwardingLog::get_latest_state");
00078
00079
00080 Log::const_reverse_iterator iter;
00081 for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00082 {
00083 if (iter->regid() == reg->regid())
00084 {
00085
00086
00087
00088
00089 ASSERT(iter->remote_eid() == EndpointID::NULL_EID() ||
00090 iter->remote_eid() == reg->endpoint());
00091 *info = *iter;
00092 return true;
00093 }
00094 }
00095
00096 return false;
00097 }
00098
00099
00100 ForwardingLog::state_t
00101 ForwardingLog::get_latest_entry(const Registration* reg) const
00102 {
00103 ForwardingInfo info;
00104 if (! get_latest_entry(reg, &info)) {
00105 return ForwardingInfo::NONE;
00106 }
00107
00108 return info.state();
00109 }
00110
00111
00112 bool
00113 ForwardingLog::get_latest_entry(state_t state, ForwardingInfo* info) const
00114 {
00115 oasys::ScopeLock l(lock_, "ForwardingLog::get_latest_state");
00116
00117
00118 Log::const_reverse_iterator iter;
00119 for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00120 {
00121 if (iter->state() == state)
00122 {
00123 *info = *iter;
00124 return true;
00125 }
00126 }
00127
00128 return false;
00129 }
00130
00131
00132 size_t
00133 ForwardingLog::get_count(unsigned int states,
00134 unsigned int actions) const
00135 {
00136 size_t ret = 0;
00137
00138 oasys::ScopeLock l(lock_, "ForwardingLog::get_count");
00139
00140 Log::const_iterator iter;
00141 for (iter = log_.begin(); iter != log_.end(); ++iter)
00142 {
00143 if ((iter->state() & states) != 0 &&
00144 (iter->action() & actions) != 0)
00145 {
00146 ++ret;
00147 }
00148 }
00149
00150 return ret;
00151 }
00152
00153
00154 size_t
00155 ForwardingLog::get_count(const EndpointID& eid,
00156 unsigned int states,
00157 unsigned int actions) const
00158 {
00159 size_t ret = 0;
00160
00161 oasys::ScopeLock l(lock_, "ForwardingLog::get_count");
00162
00163 Log::const_iterator iter;
00164 for (iter = log_.begin(); iter != log_.end(); ++iter)
00165 {
00166 if ((iter->remote_eid() == EndpointIDPattern::WILDCARD_EID() ||
00167 iter->remote_eid() == eid) &&
00168 (iter->state() & states) != 0 &&
00169 (iter->action() & actions) != 0)
00170 {
00171 ++ret;
00172 }
00173 }
00174
00175 return ret;
00176 }
00177
00178
00179 void
00180 ForwardingLog::dump(oasys::StringBuffer* buf) const
00181 {
00182 oasys::ScopeLock l(lock_, "ForwardingLog::dump");
00183 Log::const_iterator iter;
00184 for (iter = log_.begin(); iter != log_.end(); ++iter)
00185 {
00186 const ForwardingInfo* info = &(*iter);
00187
00188 buf->appendf("\t%s -> %s [%s] %s at %u.%u "
00189 "[custody min %d pct %d max %d]\n",
00190 ForwardingInfo::state_to_str(info->state()),
00191 info->link_name().c_str(),
00192 info->remote_eid().c_str(),
00193 ForwardingInfo::action_to_str(info->action()),
00194 info->timestamp().sec_,
00195 info->timestamp().usec_,
00196 info->custody_spec().min_,
00197 info->custody_spec().lifetime_pct_,
00198 info->custody_spec().max_);
00199 }
00200 }
00201
00202
00203 void
00204 ForwardingLog::add_entry(const LinkRef& link,
00205 ForwardingInfo::action_t action,
00206 state_t state,
00207 const CustodyTimerSpec& custody_timer)
00208 {
00209 oasys::ScopeLock l(lock_, "ForwardingLog::add_entry");
00210
00211 log_.push_back(ForwardingInfo(state, action, link->name_str(), 0xffffffff,
00212 link->remote_eid(), custody_timer));
00213 }
00214
00215
00216 void
00217 ForwardingLog::add_entry(const LinkRef& link,
00218 ForwardingInfo::action_t action,
00219 state_t state)
00220 {
00221 CustodyTimerSpec default_spec;
00222 add_entry(link, action, state, default_spec);
00223 }
00224
00225
00226 void
00227 ForwardingLog::add_entry(const Registration* reg,
00228 ForwardingInfo::action_t action,
00229 state_t state)
00230 {
00231 oasys::ScopeLock l(lock_, "ForwardingLog::add_entry");
00232
00233 oasys::StringBuffer name("registration-%d", reg->regid());
00234 CustodyTimerSpec spec;
00235
00236 log_.push_back(ForwardingInfo(state, action, name.c_str(), reg->regid(),
00237 reg->endpoint(), spec));
00238 }
00239
00240
00241 void
00242 ForwardingLog::add_entry(const EndpointID& eid,
00243 ForwardingInfo::action_t action,
00244 state_t state)
00245 {
00246 oasys::ScopeLock l(lock_, "ForwardingLog::add_entry");
00247
00248 oasys::StringBuffer name("eid-%s", eid.c_str());
00249 CustodyTimerSpec custody_timer;
00250
00251 log_.push_back(ForwardingInfo(state, action, name.c_str(), 0xffffffff,
00252 eid, custody_timer));
00253 }
00254
00255
00256 bool
00257 ForwardingLog::update(const LinkRef& link, state_t state)
00258 {
00259 oasys::ScopeLock l(lock_, "ForwardingLog::update");
00260
00261 Log::reverse_iterator iter;
00262 for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00263 {
00264 if (iter->link_name() == link->name_str())
00265 {
00266
00267
00268
00269 ASSERT(iter->remote_eid() == EndpointID::NULL_EID() ||
00270 iter->remote_eid() == link->remote_eid());
00271 iter->set_state(state);
00272 return true;
00273 }
00274 }
00275
00276 return false;
00277 }
00278
00279
00280 void
00281 ForwardingLog::update_all(state_t old_state, state_t new_state)
00282 {
00283 oasys::ScopeLock l(lock_, "ForwardingLog::update_all");
00284
00285 Log::reverse_iterator iter;
00286 for (iter = log_.rbegin(); iter != log_.rend(); ++iter)
00287 {
00288 if (iter->state() == old_state)
00289 {
00290 iter->set_state(new_state);
00291 }
00292 }
00293 }
00294
00295
00296 void
00297 ForwardingLog::clear()
00298 {
00299 oasys::ScopeLock l(lock_, "ForwardingLog::clear");
00300 log_.clear();
00301 }
00302
00303 }