Tidy up Resource::Manager APIs

This commit is contained in:
Vinnie Falco
2013-11-09 12:00:37 -08:00
parent 5f4a1917a6
commit b5f8d447a0
5 changed files with 29 additions and 15 deletions

View File

@@ -39,7 +39,7 @@ public:
Consumer& operator= (Consumer const& other); Consumer& operator= (Consumer const& other);
/** Return a human readable string uniquely identifying this consumer. */ /** Return a human readable string uniquely identifying this consumer. */
std::string label (); std::string to_string () const;
/** Returns `true` if this is a privileged endpoint. */ /** Returns `true` if this is a privileged endpoint. */
bool admin () const; bool admin () const;
@@ -77,6 +77,8 @@ private:
Entry* m_entry; Entry* m_entry;
}; };
std::ostream& operator<< (std::ostream& os, Consumer const& v);
} }
} }

View File

@@ -77,12 +77,12 @@ Consumer& Consumer::operator= (Consumer const& other)
return *this; return *this;
} }
std::string Consumer::label () std::string Consumer::to_string () const
{ {
if (m_logic == nullptr) if (m_logic == nullptr)
return "(none)"; return "(none)";
return m_entry->label(); return m_entry->to_string();
} }
bool Consumer::admin () const bool Consumer::admin () const
@@ -125,5 +125,11 @@ Entry& Consumer::entry()
return *m_entry; return *m_entry;
} }
std::ostream& operator<< (std::ostream& os, Consumer const& v)
{
os << v.to_string();
return os;
}
} }
} }

View File

@@ -36,7 +36,7 @@ struct Entry : public List <Entry>::Node
{ {
} }
std::string label() const std::string to_string() const
{ {
switch (key->kind) switch (key->kind)
{ {
@@ -91,6 +91,12 @@ struct Entry : public List <Entry>::Node
DiscreteTime whenExpires; DiscreteTime whenExpires;
}; };
std::ostream& operator<< (std::ostream& os, Entry const& v)
{
os << v.to_string();
return os;
}
} }
} }

View File

@@ -112,7 +112,7 @@ public:
} }
} }
m_journal.debug << "New inbound endpoint " << entry->label(); m_journal.debug << "New inbound endpoint " << entry;
return Consumer (*this, *entry); return Consumer (*this, *entry);
} }
@@ -144,7 +144,7 @@ public:
} }
} }
m_journal.debug << "New outbound endpoint " << entry->label(); m_journal.debug << "New outbound endpoint " << entry;
return Consumer (*this, *entry); return Consumer (*this, *entry);
} }
@@ -173,7 +173,7 @@ public:
} }
} }
m_journal.debug << "New admin endpoint " << entry->label(); m_journal.debug << "New admin endpoint " << entry;
return Consumer (*this, *entry); return Consumer (*this, *entry);
} }
@@ -184,7 +184,7 @@ public:
key.kind = kindAdmin; key.kind = kindAdmin;
key.name = name; key.name = name;
m_journal.info << "Elevate " << prior.label() << " to " << name; m_journal.info << "Elevate " << prior << " to " << name;
Entry* entry (nullptr); Entry* entry (nullptr);
@@ -315,7 +315,7 @@ public:
{ {
if (iter->whenExpires <= now) if (iter->whenExpires <= now)
{ {
m_journal.debug << "Expired " << iter->label(); m_journal.debug << "Expired " << *iter;
Table::iterator table_iter ( Table::iterator table_iter (
state->table.find (*iter->key)); state->table.find (*iter->key));
++iter; ++iter;
@@ -367,7 +367,7 @@ public:
{ {
if (--entry.refcount == 0) if (--entry.refcount == 0)
{ {
m_journal.debug << "Inactive " << entry.label(); m_journal.debug << "Inactive " << entry;
switch (entry.key->kind) switch (entry.key->kind)
{ {
case kindInbound: case kindInbound:
@@ -404,7 +404,7 @@ public:
{ {
DiscreteTime const now (m_clock()); DiscreteTime const now (m_clock());
int const balance (entry.add (fee.cost(), now)); int const balance (entry.add (fee.cost(), now));
m_journal.info << "Charging " << entry.label() << " for " << fee; m_journal.info << "Charging " << entry << " for " << fee;
return disposition (balance); return disposition (balance);
} }
@@ -420,7 +420,7 @@ public:
} }
if (notify) if (notify)
m_journal.info << "Load warning: " << entry.label(); m_journal.info << "Load warning: " << entry;
return notify; return notify;
} }
@@ -499,7 +499,7 @@ public:
PropertyStream::Map item (items); PropertyStream::Map item (items);
if (iter->refcount != 0) if (iter->refcount != 0)
item ["count"] = iter->refcount; item ["count"] = iter->refcount;
item ["name"] = iter->label(); item ["name"] = iter->to_string();
item ["balance"] = iter->balance(now); item ["balance"] = iter->balance(now);
if (iter->remote_balance != 0) if (iter->remote_balance != 0)
item ["remote_balance"] = iter->remote_balance; item ["remote_balance"] = iter->remote_balance;

View File

@@ -81,7 +81,7 @@ public:
{ {
IPAddress address (IPAddress::from_string ("207.127.82.1")); IPAddress address (IPAddress::from_string ("207.127.82.1"));
Consumer c (logic.newInboundEndpoint (address)); Consumer c (logic.newInboundEndpoint (address));
logMessage ("Charging " + c.label() + " 10,000 units"); logMessage ("Charging " + c.to_string() + " 10,000 units");
c.charge (10000); c.charge (10000);
for (int i = 0; i < 128; ++i) for (int i = 0; i < 128; ++i)
{ {
@@ -95,7 +95,7 @@ public:
{ {
IPAddress address (IPAddress::from_string ("207.127.82.2")); IPAddress address (IPAddress::from_string ("207.127.82.2"));
Consumer c (logic.newInboundEndpoint (address)); Consumer c (logic.newInboundEndpoint (address));
logMessage ("Charging " + c.label() + " 1000 units per second"); logMessage ("Charging " + c.to_string() + " 1000 units per second");
for (int i = 0; i < 128; ++i) for (int i = 0; i < 128; ++i)
{ {
c.charge (1000); c.charge (1000);