akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame^] | 1 | #include <ndn-cpp-dev/face.hpp> |
| 2 | #include "waiting-list.hpp" |
| 3 | |
| 4 | namespace nlsr { |
| 5 | static bool |
| 6 | waitingListCompare(const WaitingListEntry& w1, const std::string& respCert) |
| 7 | { |
| 8 | return w1.getResponsibleCert() == respCert; |
| 9 | } |
| 10 | |
| 11 | std::pair<WaitingListEntry, bool> |
| 12 | WaitingList::getWaitingListEntry(std::string respCert) |
| 13 | { |
| 14 | std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(), |
| 15 | m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert)); |
| 16 | if (it != m_waitingTable.end()) |
| 17 | { |
| 18 | return std::make_pair(*(it), true); |
| 19 | } |
| 20 | |
| 21 | WaitingListEntry wle; |
| 22 | return std::make_pair(wle, false); |
| 23 | |
| 24 | } |
| 25 | |
| 26 | bool |
| 27 | WaitingList::add(std::string respCert, std::string waitee) |
| 28 | { |
| 29 | std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(), |
| 30 | m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert)); |
| 31 | if (it == m_waitingTable.end()) |
| 32 | { |
| 33 | WaitingListEntry newWle(respCert); |
| 34 | newWle.addWaitee(waitee); |
| 35 | m_waitingTable.push_back(newWle); |
| 36 | return true; |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | return it->addWaitee(waitee); |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | WaitingList::remove(std::string respCert) |
| 47 | { |
| 48 | std::list<WaitingListEntry>::iterator it = std::find_if(m_waitingTable.begin(), |
| 49 | m_waitingTable.end(), ndn::bind(&waitingListCompare, _1, respCert)); |
| 50 | if (it == m_waitingTable.end()) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | m_waitingTable.erase(it); |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | std::ostream& |
| 63 | operator<<(std::ostream& os, WaitingList wl) |
| 64 | { |
| 65 | os << "-------Waiting List--------" << std::endl; |
| 66 | std::list<WaitingListEntry> wles = wl.getWaitingTable(); |
| 67 | for (std::list<WaitingListEntry> ::iterator it = wles.begin(); |
| 68 | it != wles.end(); ++it) |
| 69 | { |
| 70 | os << *(it) << std::endl; |
| 71 | } |
| 72 | return os; |
| 73 | } |
| 74 | }//namespace nlsr |