Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
RouterInfo: remove Is* caps functions, replace with single HasCap
Browse files Browse the repository at this point in the history
Removes caps functions which were not *in* the act of X but rather have
the *capability* of X, reduces function count by using available enum.
  • Loading branch information
anonimal committed Apr 22, 2017
1 parent 8ae8693 commit 1f5a458
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/core/router/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ void RouterContext::SetFloodfill(
}

void RouterContext::SetHighBandwidth() {
if (!m_RouterInfo.IsHighBandwidth()) {
m_RouterInfo.SetCaps(
m_RouterInfo.GetCaps() | core::RouterInfo::Cap::HighBandwidth);
auto cap = core::RouterInfo::Cap::HighBandwidth;
if (!m_RouterInfo.HasCap(cap)) {
m_RouterInfo.SetCaps(m_RouterInfo.GetCaps() | cap);
UpdateRouterInfo();
}
}

void RouterContext::SetLowBandwidth() {
if (m_RouterInfo.IsHighBandwidth()) {
m_RouterInfo.SetCaps(
m_RouterInfo.GetCaps() & ~core::RouterInfo::Cap::HighBandwidth);
auto cap = core::RouterInfo::Cap::HighBandwidth;
if (m_RouterInfo.HasCap(cap)) {
m_RouterInfo.SetCaps(m_RouterInfo.GetCaps() & ~cap);
UpdateRouterInfo();
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/core/router/info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ void RouterInfo::CreateRouterInfo(
{
router_info.WriteByteAndString(GetTrait(Trait::SSU));

// Get SSU capabilities
// Get/Set SSU capabilities flags
std::string caps;
if (IsPeerTesting())
if (HasCap(Cap::SSUTesting))
caps += GetTrait(CapFlag::SSUTesting);
if (IsIntroducer())
if (HasCap(Cap::SSUIntroducer))
caps += GetTrait(CapFlag::SSUIntroducer);

// Write SSU capabilities
Expand Down Expand Up @@ -837,10 +837,6 @@ void RouterInfo::DeleteProperty(
m_Options.erase(key);
}

bool RouterInfo::IsFloodfill() const {
return m_Caps & Cap::Floodfill;
}

bool RouterInfo::IsNTCP(
bool v4only) const {
if (v4only)
Expand Down Expand Up @@ -896,7 +892,7 @@ void RouterInfo::DisableV6() {
}

bool RouterInfo::UsesIntroducer() const {
return m_Caps & Cap::Unreachable; // non-reachable
return HasCap(Cap::Unreachable); // Router is unreachable, must use introducer
}

const RouterInfo::Address* RouterInfo::GetNTCPAddress(
Expand Down Expand Up @@ -947,10 +943,10 @@ const std::string RouterInfo::GetDescription(const std::string& tabs) const
for (const auto& opt : m_Options)
ss << tabs << "\t\t[" << opt.first << "] : [" << opt.second << "]" << std::endl;
ss << tabs << "\tSSU Caps: ["
<< (IsPeerTesting() ? GetTrait(CapFlag::SSUTesting)
: GetTrait(CapFlag::Unknown))
<< (IsIntroducer() ? GetTrait(CapFlag::SSUIntroducer)
: GetTrait(CapFlag::Unknown))
<< (HasCap(Cap::SSUTesting) ? GetTrait(CapFlag::SSUTesting)
: GetTrait(CapFlag::Unknown))
<< (HasCap(Cap::SSUIntroducer) ? GetTrait(CapFlag::SSUIntroducer)
: GetTrait(CapFlag::Unknown))
<< "]" << std::endl;
ss << tabs << "\tAddresses(" << m_Addresses.size() << "): " << std::endl;
for (const auto& address : m_Addresses)
Expand Down
22 changes: 6 additions & 16 deletions src/core/router/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,6 @@ class RouterInfo : public RoutingDestination {
m_Options.clear();
}

bool IsFloodfill() const;

bool IsNTCP(
bool v4only = true) const;

Expand All @@ -466,20 +464,11 @@ class RouterInfo : public RoutingDestination {

bool UsesIntroducer() const;

bool IsIntroducer() const {
return m_Caps & Cap::SSUIntroducer;
}

bool IsPeerTesting() const {
return m_Caps & Cap::SSUTesting;
}

bool IsHidden() const {
return m_Caps & Cap::Hidden;
}

bool IsHighBandwidth() const {
return m_Caps & Cap::HighBandwidth;
bool HasCap(Cap cap) const
{
bool has_cap = m_Caps & cap;
LOG(debug) << "RouterInfo: " << __func__ << ": " << has_cap;
return has_cap;
}

std::uint8_t GetCaps() const {
Expand Down Expand Up @@ -555,6 +544,7 @@ class RouterInfo : public RoutingDestination {
return m_Options;
}

// TODO(anonimal): really?...
bool IsDestination() const {
return false;
}
Expand Down
19 changes: 10 additions & 9 deletions src/core/router/net_db/impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void NetDb::AddRouterInfo(
std::unique_lock<std::mutex> l(m_RouterInfosMutex);
m_RouterInfos[r->GetIdentHash()] = r;
}
if (r->IsFloodfill()) {
if (r->HasCap(RouterInfo::Cap::Floodfill)) {
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
m_Floodfills.push_back(r);
}
Expand Down Expand Up @@ -351,7 +351,7 @@ bool NetDb::Load()
router->DeleteBuffer();
router->ClearProperties(); // properties are not used for regular routers
m_RouterInfos.insert(std::make_pair(router->GetIdentHash(), router));
if (router->IsFloodfill())
if (router->HasCap(RouterInfo::Cap::Floodfill))
m_Floodfills.push_back(router);
num_routers++;
}
Expand Down Expand Up @@ -451,7 +451,7 @@ void NetDb::SaveUpdated() {
if (is_removed)
deleted_count++;
// delete from floodfills list
if (it.second->IsFloodfill()) {
if (it.second->HasCap(RouterInfo::Cap::Floodfill)) {
std::unique_lock<std::mutex> l(m_FloodfillsMutex);
m_Floodfills.remove(it.second);
}
Expand Down Expand Up @@ -867,41 +867,42 @@ void NetDb::Publish() {
}
}

// TODO(anonimal): refactor these getters into fewer functions
std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter() const {
return GetRandomRouter(
[](std::shared_ptr<const RouterInfo> router)->bool {
return !router->IsHidden();
return !router->HasCap(RouterInfo::Cap::Hidden);
});
}

std::shared_ptr<const RouterInfo> NetDb::GetRandomRouter(
std::shared_ptr<const RouterInfo> compatible_with) const {
return GetRandomRouter(
[compatible_with](std::shared_ptr<const RouterInfo> router)->bool {
return !router->IsHidden() && router != compatible_with &&
return !router->HasCap(RouterInfo::Cap::Hidden) && router != compatible_with &&
router->IsCompatible(*compatible_with);
});
}

std::shared_ptr<const RouterInfo> NetDb::GetRandomPeerTestRouter() const {
return GetRandomRouter(
[](std::shared_ptr<const RouterInfo> router)->bool {
return !router->IsHidden() && router->IsPeerTesting();
return !router->HasCap(RouterInfo::Cap::Hidden) && router->HasCap(RouterInfo::Cap::SSUTesting);
});
}

std::shared_ptr<const RouterInfo> NetDb::GetRandomIntroducer() const {
return GetRandomRouter(
[](std::shared_ptr<const RouterInfo> router)->bool {
return !router->IsHidden() && router->IsIntroducer();
return !router->HasCap(RouterInfo::Cap::Hidden) && router->HasCap(RouterInfo::Cap::SSUIntroducer);
});
}

std::shared_ptr<const RouterInfo> NetDb::GetHighBandwidthRandomRouter(
std::shared_ptr<const RouterInfo> compatible_with) const {
return GetRandomRouter(
[compatible_with](std::shared_ptr<const RouterInfo> router)->bool {
return !router->IsHidden() &&
return !router->HasCap(RouterInfo::Cap::Hidden) &&
router != compatible_with &&
router->IsCompatible(*compatible_with) &&
(router->GetCaps() & RouterInfo::Cap::HighBandwidth);
Expand Down Expand Up @@ -1014,7 +1015,7 @@ std::shared_ptr<const RouterInfo> NetDb::GetClosestNonFloodfill(
min_metric.SetMax();
// must be called from NetDb thread only
for (auto it : m_RouterInfos) {
if (!it.second->IsFloodfill()) {
if (!it.second->HasCap(RouterInfo::Cap::Floodfill)) {
XORMetric m = dest_key ^ it.first;
if (m < min_metric && !excluded.count(it.first)) {
min_metric = m;
Expand Down
2 changes: 1 addition & 1 deletion src/core/router/transports/impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ bool Transports::IsBandwidthExceeded() const {
LOG(debug) << "Transports: bandwidth has been exceeded";
return true;
}
if (kovri::context.GetRouterInfo().IsHighBandwidth())
if (kovri::context.GetRouterInfo().HasCap(RouterInfo::Cap::HighBandwidth))
LOG(debug) << "Transports: bandwidth has not been exceeded";
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/router/transports/ssu/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void SSUSession::SendSessionCreated(
s.Insert<std::uint16_t> (htobe16(address->port)); // our port

std::uint32_t relay_tag = 0;
if (kovri::context.GetRouterInfo().IsIntroducer()) {
if (kovri::context.GetRouterInfo().HasCap(RouterInfo::Cap::SSUIntroducer)) {
relay_tag = kovri::core::Rand<std::uint32_t>();
if (!relay_tag)
relay_tag = 1;
Expand Down Expand Up @@ -1408,7 +1408,7 @@ void SSUSession::Established() {
// send database store
m_Data.Send(CreateDatabaseStoreMsg());
transports.PeerConnected(shared_from_this());
if (m_PeerTest && (m_RemoteRouter && m_RemoteRouter->IsPeerTesting()))
if (m_PeerTest && (m_RemoteRouter && m_RemoteRouter->HasCap(RouterInfo::Cap::SSUTesting)))
SendPeerTest();
ScheduleTermination();
}
Expand Down

0 comments on commit 1f5a458

Please sign in to comment.