blob: 19517b7ca8f60e250c48a7c4ad13fdd576898cb4 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5b43f9a2016-07-19 13:15:56 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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"
Junxiao Shiafbd74d2014-11-29 21:18:17 -070030#include <boost/range/adaptor/map.hpp>
Junxiao Shia4f2be82014-03-02 22:56:41 -070031
Junxiao Shi1e064172014-12-14 19:37:46 -070032namespace nfd {
Junxiao Shia4f2be82014-03-02 22:56:41 -070033
34class Forwarder;
35
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000036/** \brief container of all faces
Junxiao Shia4f2be82014-03-02 22:56:41 -070037 */
Junxiao Shi7b984c62014-07-17 22:18:34 -070038class FaceTable : noncopyable
Junxiao Shia4f2be82014-03-02 22:56:41 -070039{
40public:
41 explicit
42 FaceTable(Forwarder& forwarder);
43
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070044 VIRTUAL_WITH_TESTS
45 ~FaceTable();
46
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000047 /** \brief add a face
48 *
49 * FaceTable obtains shared ownership of the face.
50 * The channel or protocol factory that creates the face may retain ownership.
51 */
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070052 VIRTUAL_WITH_TESTS void
Junxiao Shia4f2be82014-03-02 22:56:41 -070053 add(shared_ptr<Face> face);
54
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000055 /** \brief add a special Face with a reserved FaceId
56 */
Junxiao Shi7b984c62014-07-17 22:18:34 -070057 VIRTUAL_WITH_TESTS void
58 addReserved(shared_ptr<Face> face, FaceId faceId);
59
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000060 /** \brief get face by FaceId
61 * \return a face if found, nullptr if not found;
62 * face->shared_from_this() can be used if shared_ptr<Face> is desired
63 */
64 VIRTUAL_WITH_TESTS Face*
Junxiao Shia4f2be82014-03-02 22:56:41 -070065 get(FaceId id) const;
66
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000067 /** \return count of faces
68 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070069 size_t
70 size() const;
71
72public: // enumeration
Junxiao Shiafbd74d2014-11-29 21:18:17 -070073 typedef std::map<FaceId, shared_ptr<Face>> FaceMap;
74
75 typedef boost::select_second_const_range<FaceMap> ForwardRange;
Junxiao Shia4f2be82014-03-02 22:56:41 -070076
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070077 /** \brief ForwardIterator for shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070078 */
Junxiao Shiafbd74d2014-11-29 21:18:17 -070079 typedef boost::range_iterator<ForwardRange>::type const_iterator;
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070080
Junxiao Shia4f2be82014-03-02 22:56:41 -070081 const_iterator
82 begin() const;
83
84 const_iterator
85 end() const;
86
Junxiao Shi1e064172014-12-14 19:37:46 -070087public: // signals
Junxiao Shibd392bf2014-03-17 15:54:11 -070088 /** \brief fires after a Face is added
89 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070090 signal::Signal<FaceTable, shared_ptr<Face>> afterAdd;
Junxiao Shibd392bf2014-03-17 15:54:11 -070091
92 /** \brief fires before a Face is removed
93 *
94 * FaceId is valid when this event is fired
95 */
Junxiao Shicde37ad2015-12-24 01:02:05 -070096 signal::Signal<FaceTable, shared_ptr<Face>> beforeRemove;
Junxiao Shibd392bf2014-03-17 15:54:11 -070097
Junxiao Shia4f2be82014-03-02 22:56:41 -070098private:
Junxiao Shi7b984c62014-07-17 22:18:34 -070099 void
100 addImpl(shared_ptr<Face> face, FaceId faceId);
101
Junxiao Shic542b2b2014-03-16 21:45:52 -0700102 void
Junxiao Shicde37ad2015-12-24 01:02:05 -0700103 remove(shared_ptr<Face> face);
Junxiao Shic542b2b2014-03-16 21:45:52 -0700104
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700105 ForwardRange
106 getForwardRange() const;
107
Junxiao Shic542b2b2014-03-16 21:45:52 -0700108private:
Junxiao Shia4f2be82014-03-02 22:56:41 -0700109 Forwarder& m_forwarder;
110 FaceId m_lastFaceId;
111 FaceMap m_faces;
112};
113
Junxiao Shia4f2be82014-03-02 22:56:41 -0700114} // namespace nfd
115
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700116#endif // NFD_DAEMON_FW_FACE_TABLE_HPP