Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 25 | #ifndef NFD_DAEMON_FW_FACE_TABLE_HPP |
| 26 | #define NFD_DAEMON_FW_FACE_TABLE_HPP |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 27 | |
| 28 | #include "face/face.hpp" |
| 29 | #include "core/map-value-iterator.hpp" |
| 30 | |
| 31 | namespace nfd |
| 32 | { |
| 33 | |
| 34 | class Forwarder; |
| 35 | |
| 36 | /** \brief container of all Faces |
| 37 | */ |
| 38 | class FaceTable |
| 39 | { |
| 40 | public: |
| 41 | explicit |
| 42 | FaceTable(Forwarder& forwarder); |
| 43 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 44 | VIRTUAL_WITH_TESTS |
| 45 | ~FaceTable(); |
| 46 | |
| 47 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 48 | add(shared_ptr<Face> face); |
| 49 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 50 | VIRTUAL_WITH_TESTS shared_ptr<Face> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 51 | get(FaceId id) const; |
| 52 | |
| 53 | size_t |
| 54 | size() const; |
| 55 | |
| 56 | public: // enumeration |
| 57 | typedef std::map<FaceId, shared_ptr<Face> > FaceMap; |
| 58 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 59 | /** \brief ForwardIterator for shared_ptr<Face> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 60 | */ |
| 61 | typedef MapValueIterator<FaceMap> const_iterator; |
| 62 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 63 | /** \brief ReverseIterator for shared_ptr<Face> |
| 64 | */ |
| 65 | typedef MapValueReverseIterator<FaceMap> const_reverse_iterator; |
| 66 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 67 | const_iterator |
| 68 | begin() const; |
| 69 | |
| 70 | const_iterator |
| 71 | end() const; |
| 72 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 73 | const_reverse_iterator |
| 74 | rbegin() const; |
| 75 | |
| 76 | const_reverse_iterator |
| 77 | rend() const; |
| 78 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 79 | public: // events |
| 80 | /** \brief fires after a Face is added |
| 81 | */ |
| 82 | EventEmitter<shared_ptr<Face> > onAdd; |
| 83 | |
| 84 | /** \brief fires before a Face is removed |
| 85 | * |
| 86 | * FaceId is valid when this event is fired |
| 87 | */ |
| 88 | EventEmitter<shared_ptr<Face> > onRemove; |
| 89 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 90 | private: |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 91 | // remove is private because it's a subscriber of face.onFail event. |
| 92 | // face->close() closes a face and would trigger .remove(face) |
| 93 | void |
| 94 | remove(shared_ptr<Face> face); |
| 95 | |
| 96 | private: |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 97 | Forwarder& m_forwarder; |
| 98 | FaceId m_lastFaceId; |
| 99 | FaceMap m_faces; |
| 100 | }; |
| 101 | |
| 102 | inline shared_ptr<Face> |
| 103 | FaceTable::get(FaceId id) const |
| 104 | { |
| 105 | std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id); |
| 106 | return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second); |
| 107 | } |
| 108 | |
| 109 | inline size_t |
| 110 | FaceTable::size() const |
| 111 | { |
| 112 | return m_faces.size(); |
| 113 | } |
| 114 | |
| 115 | inline FaceTable::const_iterator |
| 116 | FaceTable::begin() const |
| 117 | { |
| 118 | return const_iterator(m_faces.begin()); |
| 119 | } |
| 120 | |
| 121 | inline FaceTable::const_iterator |
| 122 | FaceTable::end() const |
| 123 | { |
| 124 | return const_iterator(m_faces.end()); |
| 125 | } |
| 126 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 127 | inline FaceTable::const_reverse_iterator |
| 128 | FaceTable::rbegin() const |
| 129 | { |
| 130 | return const_reverse_iterator(m_faces.rbegin()); |
| 131 | } |
| 132 | |
| 133 | inline FaceTable::const_reverse_iterator |
| 134 | FaceTable::rend() const |
| 135 | { |
| 136 | return const_reverse_iterator(m_faces.rend()); |
| 137 | } |
| 138 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 139 | } // namespace nfd |
| 140 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 141 | #endif // NFD_DAEMON_FW_FACE_TABLE_HPP |