blob: 175da8f4a1668fe15c48fb131c758be823c75976 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 Shia4f2be82014-03-02 22:56:41 -070024
25#ifndef NFD_FW_FACE_TABLE_HPP
26#define NFD_FW_FACE_TABLE_HPP
27
28#include "face/face.hpp"
29#include "core/map-value-iterator.hpp"
30
31namespace nfd
32{
33
34class Forwarder;
35
36/** \brief container of all Faces
37 */
38class FaceTable
39{
40public:
41 explicit
42 FaceTable(Forwarder& forwarder);
43
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070044 VIRTUAL_WITH_TESTS
45 ~FaceTable();
46
47 VIRTUAL_WITH_TESTS void
Junxiao Shia4f2be82014-03-02 22:56:41 -070048 add(shared_ptr<Face> face);
49
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070050 VIRTUAL_WITH_TESTS shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070051 get(FaceId id) const;
52
53 size_t
54 size() const;
55
56public: // enumeration
57 typedef std::map<FaceId, shared_ptr<Face> > FaceMap;
58
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070059 /** \brief ForwardIterator for shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070060 */
61 typedef MapValueIterator<FaceMap> const_iterator;
62
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070063 /** \brief ReverseIterator for shared_ptr<Face>
64 */
65 typedef MapValueReverseIterator<FaceMap> const_reverse_iterator;
66
Junxiao Shia4f2be82014-03-02 22:56:41 -070067 const_iterator
68 begin() const;
69
70 const_iterator
71 end() const;
72
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070073 const_reverse_iterator
74 rbegin() const;
75
76 const_reverse_iterator
77 rend() const;
78
Junxiao Shibd392bf2014-03-17 15:54:11 -070079public: // 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 Shia4f2be82014-03-02 22:56:41 -070090private:
Junxiao Shic542b2b2014-03-16 21:45:52 -070091 // 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
96private:
Junxiao Shia4f2be82014-03-02 22:56:41 -070097 Forwarder& m_forwarder;
98 FaceId m_lastFaceId;
99 FaceMap m_faces;
100};
101
102inline shared_ptr<Face>
103FaceTable::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
109inline size_t
110FaceTable::size() const
111{
112 return m_faces.size();
113}
114
115inline FaceTable::const_iterator
116FaceTable::begin() const
117{
118 return const_iterator(m_faces.begin());
119}
120
121inline FaceTable::const_iterator
122FaceTable::end() const
123{
124 return const_iterator(m_faces.end());
125}
126
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -0700127inline FaceTable::const_reverse_iterator
128FaceTable::rbegin() const
129{
130 return const_reverse_iterator(m_faces.rbegin());
131}
132
133inline FaceTable::const_reverse_iterator
134FaceTable::rend() const
135{
136 return const_reverse_iterator(m_faces.rend());
137}
138
Junxiao Shia4f2be82014-03-02 22:56:41 -0700139} // namespace nfd
140
141#endif // NFD_FW_FACE_TABLE_HPP