blob: 7da4affa3aec8460dca7e4c1fd27d34d73c41887 [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 Shib84e6742016-07-19 13:16:22 +000030#include <boost/range/adaptor/indirected.hpp>
Junxiao Shiafbd74d2014-11-29 21:18:17 -070031#include <boost/range/adaptor/map.hpp>
Junxiao Shia4f2be82014-03-02 22:56:41 -070032
Junxiao Shi1e064172014-12-14 19:37:46 -070033namespace nfd {
Junxiao Shia4f2be82014-03-02 22:56:41 -070034
35class Forwarder;
36
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000037/** \brief container of all faces
Junxiao Shia4f2be82014-03-02 22:56:41 -070038 */
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
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000048 /** \brief add a face
49 *
50 * FaceTable obtains shared ownership of the face.
51 * The channel or protocol factory that creates the face may retain ownership.
52 */
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070053 VIRTUAL_WITH_TESTS void
Junxiao Shia4f2be82014-03-02 22:56:41 -070054 add(shared_ptr<Face> face);
55
Junxiao Shiae04d342016-07-19 13:20:22 +000056 /** \brief add a special face with a reserved FaceId
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000057 */
Junxiao Shi7b984c62014-07-17 22:18:34 -070058 VIRTUAL_WITH_TESTS void
59 addReserved(shared_ptr<Face> face, FaceId faceId);
60
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000061 /** \brief get face by FaceId
62 * \return a face if found, nullptr if not found;
63 * face->shared_from_this() can be used if shared_ptr<Face> is desired
64 */
65 VIRTUAL_WITH_TESTS Face*
Junxiao Shia4f2be82014-03-02 22:56:41 -070066 get(FaceId id) const;
67
Junxiao Shi5b43f9a2016-07-19 13:15:56 +000068 /** \return count of faces
69 */
Junxiao Shia4f2be82014-03-02 22:56:41 -070070 size_t
71 size() const;
72
73public: // enumeration
Junxiao Shiafbd74d2014-11-29 21:18:17 -070074 typedef std::map<FaceId, shared_ptr<Face>> FaceMap;
75
Junxiao Shib84e6742016-07-19 13:16:22 +000076 typedef boost::indirected_range<const boost::select_second_const_range<FaceMap>> ForwardRange;
Junxiao Shia4f2be82014-03-02 22:56:41 -070077
Junxiao Shib84e6742016-07-19 13:16:22 +000078 /** \brief ForwardIterator for Face&
Junxiao Shia4f2be82014-03-02 22:56:41 -070079 */
Junxiao Shiafbd74d2014-11-29 21:18:17 -070080 typedef boost::range_iterator<ForwardRange>::type const_iterator;
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070081
Junxiao Shia4f2be82014-03-02 22:56:41 -070082 const_iterator
83 begin() const;
84
85 const_iterator
86 end() const;
87
Junxiao Shi1e064172014-12-14 19:37:46 -070088public: // signals
Junxiao Shiae04d342016-07-19 13:20:22 +000089 /** \brief fires after a face is added
Junxiao Shibd392bf2014-03-17 15:54:11 -070090 */
Junxiao Shiae04d342016-07-19 13:20:22 +000091 signal::Signal<FaceTable, Face&> afterAdd;
Junxiao Shibd392bf2014-03-17 15:54:11 -070092
Junxiao Shiae04d342016-07-19 13:20:22 +000093 /** \brief fires before a face is removed
Junxiao Shibd392bf2014-03-17 15:54:11 -070094 *
Junxiao Shiae04d342016-07-19 13:20:22 +000095 * When this signal is emitted, face is still in FaceTable and has valid FaceId.
Junxiao Shibd392bf2014-03-17 15:54:11 -070096 */
Junxiao Shiae04d342016-07-19 13:20:22 +000097 signal::Signal<FaceTable, Face&> beforeRemove;
Junxiao Shibd392bf2014-03-17 15:54:11 -070098
Junxiao Shia4f2be82014-03-02 22:56:41 -070099private:
Junxiao Shi7b984c62014-07-17 22:18:34 -0700100 void
101 addImpl(shared_ptr<Face> face, FaceId faceId);
102
Junxiao Shic542b2b2014-03-16 21:45:52 -0700103 void
Junxiao Shiae04d342016-07-19 13:20:22 +0000104 remove(FaceId faceId);
Junxiao Shic542b2b2014-03-16 21:45:52 -0700105
Junxiao Shiafbd74d2014-11-29 21:18:17 -0700106 ForwardRange
107 getForwardRange() const;
108
Junxiao Shic542b2b2014-03-16 21:45:52 -0700109private:
Junxiao Shia4f2be82014-03-02 22:56:41 -0700110 Forwarder& m_forwarder;
111 FaceId m_lastFaceId;
112 FaceMap m_faces;
113};
114
Junxiao Shia4f2be82014-03-02 22:56:41 -0700115} // namespace nfd
116
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700117#endif // NFD_DAEMON_FW_FACE_TABLE_HPP