blob: a255cfeef2ddc56667c444bbe6982ee50e454160 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7b984c62014-07-17 22:18:34 -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 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Shi7b984c62014-07-17 22:18:34 -070024 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FW_FACE_TABLE_HPP
27#define NFD_DAEMON_FW_FACE_TABLE_HPP
Junxiao Shia4f2be82014-03-02 22:56:41 -070028
29#include "face/face.hpp"
30#include "core/map-value-iterator.hpp"
31
32namespace nfd
33{
34
35class Forwarder;
36
37/** \brief container of all Faces
38 */
Junxiao Shi7b984c62014-07-17 22:18:34 -070039class FaceTable : noncopyable
Junxiao Shia4f2be82014-03-02 22:56:41 -070040{
41public:
42 explicit
43 FaceTable(Forwarder& forwarder);
44
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070045 VIRTUAL_WITH_TESTS
46 ~FaceTable();
47
48 VIRTUAL_WITH_TESTS void
Junxiao Shia4f2be82014-03-02 22:56:41 -070049 add(shared_ptr<Face> face);
50
Junxiao Shi7b984c62014-07-17 22:18:34 -070051 /// add a special Face with a reserved FaceId
52 VIRTUAL_WITH_TESTS void
53 addReserved(shared_ptr<Face> face, FaceId faceId);
54
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070055 VIRTUAL_WITH_TESTS shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070056 get(FaceId id) const;
57
58 size_t
59 size() const;
60
61public: // enumeration
62 typedef std::map<FaceId, shared_ptr<Face> > FaceMap;
63
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070064 /** \brief ForwardIterator for shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070065 */
66 typedef MapValueIterator<FaceMap> const_iterator;
67
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070068 /** \brief ReverseIterator for shared_ptr<Face>
69 */
70 typedef MapValueReverseIterator<FaceMap> const_reverse_iterator;
71
Junxiao Shia4f2be82014-03-02 22:56:41 -070072 const_iterator
73 begin() const;
74
75 const_iterator
76 end() const;
77
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070078 const_reverse_iterator
79 rbegin() const;
80
81 const_reverse_iterator
82 rend() const;
83
Junxiao Shibd392bf2014-03-17 15:54:11 -070084public: // 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 Shia4f2be82014-03-02 22:56:41 -070095private:
Junxiao Shi7b984c62014-07-17 22:18:34 -070096 void
97 addImpl(shared_ptr<Face> face, FaceId faceId);
98
Junxiao Shic542b2b2014-03-16 21:45:52 -070099 // remove is private because it's a subscriber of face.onFail event.
Junxiao Shi7b984c62014-07-17 22:18:34 -0700100 // face->close() closes a face and triggers .remove(face)
Junxiao Shic542b2b2014-03-16 21:45:52 -0700101 void
102 remove(shared_ptr<Face> face);
103
104private:
Junxiao Shia4f2be82014-03-02 22:56:41 -0700105 Forwarder& m_forwarder;
106 FaceId m_lastFaceId;
107 FaceMap m_faces;
108};
109
110inline shared_ptr<Face>
111FaceTable::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
117inline size_t
118FaceTable::size() const
119{
120 return m_faces.size();
121}
122
123inline FaceTable::const_iterator
124FaceTable::begin() const
125{
126 return const_iterator(m_faces.begin());
127}
128
129inline FaceTable::const_iterator
130FaceTable::end() const
131{
132 return const_iterator(m_faces.end());
133}
134
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -0700135inline FaceTable::const_reverse_iterator
136FaceTable::rbegin() const
137{
138 return const_reverse_iterator(m_faces.rbegin());
139}
140
141inline FaceTable::const_reverse_iterator
142FaceTable::rend() const
143{
144 return const_reverse_iterator(m_faces.rend());
145}
146
Junxiao Shia4f2be82014-03-02 22:56:41 -0700147} // namespace nfd
148
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700149#endif // NFD_DAEMON_FW_FACE_TABLE_HPP