Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_FW_FACE_TABLE_HPP |
| 8 | #define NFD_FW_FACE_TABLE_HPP |
| 9 | |
| 10 | #include "face/face.hpp" |
| 11 | #include "core/map-value-iterator.hpp" |
| 12 | |
| 13 | namespace nfd |
| 14 | { |
| 15 | |
| 16 | class Forwarder; |
| 17 | |
| 18 | /** \brief container of all Faces |
| 19 | */ |
| 20 | class FaceTable |
| 21 | { |
| 22 | public: |
| 23 | explicit |
| 24 | FaceTable(Forwarder& forwarder); |
| 25 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 26 | VIRTUAL_WITH_TESTS |
| 27 | ~FaceTable(); |
| 28 | |
| 29 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 30 | add(shared_ptr<Face> face); |
| 31 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 32 | VIRTUAL_WITH_TESTS shared_ptr<Face> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 33 | get(FaceId id) const; |
| 34 | |
| 35 | size_t |
| 36 | size() const; |
| 37 | |
| 38 | public: // enumeration |
| 39 | typedef std::map<FaceId, shared_ptr<Face> > FaceMap; |
| 40 | |
| 41 | /** \brief ForwarderIterator for shared_ptr<Face> |
| 42 | */ |
| 43 | typedef MapValueIterator<FaceMap> const_iterator; |
| 44 | |
| 45 | const_iterator |
| 46 | begin() const; |
| 47 | |
| 48 | const_iterator |
| 49 | end() const; |
| 50 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame^] | 51 | public: // events |
| 52 | /** \brief fires after a Face is added |
| 53 | */ |
| 54 | EventEmitter<shared_ptr<Face> > onAdd; |
| 55 | |
| 56 | /** \brief fires before a Face is removed |
| 57 | * |
| 58 | * FaceId is valid when this event is fired |
| 59 | */ |
| 60 | EventEmitter<shared_ptr<Face> > onRemove; |
| 61 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 62 | private: |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 63 | // remove is private because it's a subscriber of face.onFail event. |
| 64 | // face->close() closes a face and would trigger .remove(face) |
| 65 | void |
| 66 | remove(shared_ptr<Face> face); |
| 67 | |
| 68 | private: |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 69 | Forwarder& m_forwarder; |
| 70 | FaceId m_lastFaceId; |
| 71 | FaceMap m_faces; |
| 72 | }; |
| 73 | |
| 74 | inline shared_ptr<Face> |
| 75 | FaceTable::get(FaceId id) const |
| 76 | { |
| 77 | std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id); |
| 78 | return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second); |
| 79 | } |
| 80 | |
| 81 | inline size_t |
| 82 | FaceTable::size() const |
| 83 | { |
| 84 | return m_faces.size(); |
| 85 | } |
| 86 | |
| 87 | inline FaceTable::const_iterator |
| 88 | FaceTable::begin() const |
| 89 | { |
| 90 | return const_iterator(m_faces.begin()); |
| 91 | } |
| 92 | |
| 93 | inline FaceTable::const_iterator |
| 94 | FaceTable::end() const |
| 95 | { |
| 96 | return const_iterator(m_faces.end()); |
| 97 | } |
| 98 | |
| 99 | } // namespace nfd |
| 100 | |
| 101 | #endif // NFD_FW_FACE_TABLE_HPP |