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