Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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. |
| 10 | * |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef NFD_DAEMON_FACE_INTERNAL_CLIENT_FACE_HPP |
| 27 | #define NFD_DAEMON_FACE_INTERNAL_CLIENT_FACE_HPP |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | |
| 31 | #include <ndn-cxx/face.hpp> |
| 32 | #include <ndn-cxx/security/key-chain.hpp> |
| 33 | #include <ndn-cxx/transport/transport.hpp> |
| 34 | |
| 35 | namespace nfd { |
| 36 | |
| 37 | class InternalFace; |
| 38 | class InternalClientFace; |
| 39 | |
| 40 | shared_ptr<InternalClientFace> |
| 41 | makeInternalClientFace(const shared_ptr<InternalFace>& face, ndn::KeyChain& keyChain); |
| 42 | |
| 43 | /** \brief a client face that is connected to NFD InternalFace |
| 44 | */ |
| 45 | class InternalClientFace : public ndn::Face |
| 46 | { |
| 47 | public: |
| 48 | template<typename Packet> |
| 49 | void |
| 50 | receive(const Packet& packet); |
| 51 | |
| 52 | private: |
| 53 | class Transport : public ndn::Transport |
| 54 | { |
| 55 | public: |
| 56 | signal::Signal<Transport, Block> onSendBlock; |
| 57 | |
| 58 | void |
| 59 | receive(const Block& block); |
| 60 | |
| 61 | virtual void |
| 62 | close() DECL_OVERRIDE; |
| 63 | |
| 64 | virtual void |
| 65 | send(const Block& wire) DECL_OVERRIDE; |
| 66 | |
| 67 | virtual void |
| 68 | send(const Block& header, const Block& payload) DECL_OVERRIDE; |
| 69 | |
| 70 | virtual void |
| 71 | pause() DECL_OVERRIDE; |
| 72 | |
| 73 | virtual void |
| 74 | resume() DECL_OVERRIDE; |
| 75 | }; |
| 76 | |
| 77 | private: |
| 78 | InternalClientFace(const shared_ptr<InternalFace>& face, |
| 79 | const shared_ptr<InternalClientFace::Transport>& transport, |
| 80 | ndn::KeyChain& keyChain); |
| 81 | |
| 82 | void |
| 83 | construct(); |
| 84 | |
| 85 | friend shared_ptr<InternalClientFace> |
| 86 | makeInternalClientFace(const shared_ptr<InternalFace>& face, |
| 87 | ndn::KeyChain& keyChain); |
| 88 | |
| 89 | private: |
| 90 | shared_ptr<InternalFace> m_face; |
| 91 | shared_ptr<InternalClientFace::Transport> m_transport; |
| 92 | }; |
| 93 | |
| 94 | template<typename Packet> |
| 95 | void |
| 96 | InternalClientFace::receive(const Packet& packet) |
| 97 | { |
| 98 | if (!packet.getLocalControlHeader().empty(ndn::nfd::LocalControlHeader::ENCODE_ALL)) { |
| 99 | |
| 100 | Block header = packet.getLocalControlHeader() |
| 101 | .wireEncode(packet, ndn::nfd::LocalControlHeader::ENCODE_ALL); |
| 102 | Block payload = packet.wireEncode(); |
| 103 | |
| 104 | ndn::EncodingBuffer encoder(header.size() + payload.size(), |
| 105 | header.size() + payload.size()); |
| 106 | encoder.appendByteArray(header.wire(), header.size()); |
| 107 | encoder.appendByteArray(payload.wire(), payload.size()); |
| 108 | |
| 109 | m_transport->receive(encoder.block()); |
| 110 | } |
| 111 | else { |
| 112 | m_transport->receive(packet.wireEncode()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } // namespace nfd |
| 117 | |
| 118 | #endif // NFD_DAEMON_FACE_INTERNAL_CLIENT_FACE_HPP |