Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | */ |
| 22 | |
| 23 | #ifndef NDN_API_FACE_H |
| 24 | #define NDN_API_FACE_H |
| 25 | |
| 26 | #include <boost/exception/all.hpp> |
| 27 | #include <boost/function.hpp> |
| 28 | #include <string> |
| 29 | |
| 30 | #include <ns3/ptr.h> |
| 31 | #include <ns3/node.h> |
| 32 | #include <ns3/random-variable.h> |
| 33 | #include <ns3/ndn-app.h> |
| 34 | #include <ns3/ndn-name.h> |
| 35 | |
| 36 | #include <ns3/ndnSIM/utils/trie/trie-with-policy.h> |
| 37 | #include <ns3/ndnSIM/utils/trie/counting-policy.h> |
| 38 | |
| 39 | namespace ns3 { |
| 40 | namespace ndn { |
| 41 | |
| 42 | template<class Callback> |
| 43 | struct FilterEntry : public ns3::SimpleRefCount< FilterEntry<Callback> > |
| 44 | { |
| 45 | public: |
| 46 | FilterEntry (ns3::Ptr<const ns3::ndn::Name> prefix) |
| 47 | : m_prefix (prefix) |
| 48 | { } |
| 49 | |
| 50 | const ns3::ndn::Name & |
| 51 | GetPrefix () const |
| 52 | { return *m_prefix; } |
| 53 | |
| 54 | void |
| 55 | AddCallback (Callback callback) |
| 56 | { |
| 57 | m_callback = callback; |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | ClearCallback () |
| 62 | { |
| 63 | m_callback = 0; |
| 64 | } |
| 65 | |
| 66 | public: |
| 67 | ns3::Ptr<const ns3::ndn::Name> m_prefix; ///< \brief Prefix of the PIT entry |
| 68 | Callback m_callback; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | template<class Callback> |
| 73 | struct FilterEntryContainer : |
| 74 | public ns3::ndn::ndnSIM::trie_with_policy<ns3::ndn::Name, |
| 75 | ns3::ndn::ndnSIM::smart_pointer_payload_traits< FilterEntry<Callback> >, |
| 76 | ns3::ndn::ndnSIM::counting_policy_traits> |
| 77 | { |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | struct OperationException : virtual boost::exception, virtual std::exception { }; |
| 82 | /** |
| 83 | * \ingroup sync |
| 84 | * @brief A handler for NDN; clients of this code do not need to deal |
| 85 | * with NDN API directly |
| 86 | */ |
| 87 | class ApiFace |
| 88 | // : private ns3::ndn::Face |
| 89 | { |
| 90 | public: |
| 91 | // typedef boost::function<void (const ndn::Name &, Ptr<Packet>)> DataCallback; |
| 92 | // typedef boost::function<void (const ndn::Name &)> InterestCallback; |
| 93 | |
| 94 | // /** |
| 95 | // * @brief initialize the handler; a lot of things needs to be done. 1) init |
| 96 | // * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run |
| 97 | // * |
| 98 | // */ |
| 99 | // ApiFace (Ptr<Node> node); |
| 100 | // ~ApiFace (); |
| 101 | |
| 102 | // /** |
| 103 | // * @brief send Interest; need to grab lock m_mutex first |
| 104 | // * |
| 105 | // * @param name the Interest name |
| 106 | // * @param dataCallback the callback function to deal with the returned data |
| 107 | // * @return the return code of ccn_express_interest |
| 108 | // */ |
| 109 | // // int |
| 110 | // // sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback/*, int retry = 0*/); |
| 111 | |
| 112 | // int |
| 113 | // sendInterest (const ndn::Name &name, const DataCallback &rawDataCallback/*, int retry = 0*/); |
| 114 | |
| 115 | // /** |
| 116 | // * @brief set Interest filter (specify what interest you want to receive) |
| 117 | // * |
| 118 | // * @param prefix the prefix of Interest |
| 119 | // * @param interestCallback the callback function to deal with the returned data |
| 120 | // * @return the return code of ccn_set_interest_filter |
| 121 | // */ |
| 122 | // int |
| 123 | // setInterestFilter (const ndn::Name &prefix, const InterestCallback &interestCallback); |
| 124 | |
| 125 | // /** |
| 126 | // * @brief clear Interest filter |
| 127 | // * @param prefix the prefix of Interest |
| 128 | // */ |
| 129 | // void |
| 130 | // clearInterestFilter (const std::string &prefix); |
| 131 | |
| 132 | /** |
| 133 | * @brief publish data and put it to local ccn content store; need to grab |
| 134 | * lock m_mutex first |
| 135 | * |
| 136 | * @param name the name for the data object |
| 137 | * @param dataBuffer the data to be published |
| 138 | * @param freshness the freshness time for the data object |
| 139 | * @return code generated by NDN library calls, >0 if success |
| 140 | */ |
| 141 | // int |
| 142 | // publishStringData (const std::string &name, const std::string &dataBuffer, int freshness) |
| 143 | // { |
| 144 | // return publishRawData (name, dataBuffer.c_str(), dataBuffer.length(), freshness); |
| 145 | // } |
| 146 | |
| 147 | // int |
| 148 | // publishRawData (const std::string &name, const char *buf, size_t len, int freshness); |
| 149 | |
| 150 | private: |
| 151 | // // from ndn::App |
| 152 | // virtual void |
| 153 | // OnInterest (const ns3::Ptr<const ns3::ndn::Interest> &interest, ns3::Ptr<ns3::Packet> packet); |
| 154 | |
| 155 | // virtual void |
| 156 | // OnContentObject (const ns3::Ptr<const ns3::ndn::ContentObject> &contentObject, |
| 157 | // ns3::Ptr<ns3::Packet> payload); |
| 158 | |
| 159 | private: |
| 160 | ns3::UniformVariable m_rand; // nonce generator |
| 161 | |
| 162 | FilterEntryContainer<RawDataCallback> m_dataCallbacks; |
| 163 | FilterEntryContainer<InterestCallback> m_interestCallbacks; |
| 164 | }; |
| 165 | |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #endif // NDN_API_HANDLER_H |