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) 2012 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 | #include "ndn-api-face.h" |
| 24 | |
| 25 | #include <boost/throw_exception.hpp> |
| 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 27 | #include <boost/lambda/lambda.hpp> |
| 28 | #include <boost/lambda/bind.hpp> |
| 29 | namespace ll = boost::lambda; |
| 30 | |
| 31 | #include <ns3/packet.h> |
| 32 | |
| 33 | #include <ns3/ndn-interest.h> |
| 34 | #include <ns3/ndn-content-object.h> |
| 35 | #include <ns3/ndn-face.h> |
| 36 | #include <ns3/ndn-fib.h> |
| 37 | #include <ns3/log.h> |
| 38 | |
| 39 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 40 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 41 | |
| 42 | using namespace std; |
| 43 | using namespace boost; |
| 44 | using namespace ns3; |
| 45 | |
| 46 | #define INIT_LOGGER NS_LOG_COMPONENT_DEFINE |
| 47 | #define _LOG_DEBUG NS_LOG_DEBUG |
| 48 | #define _LOG_TRACE NS_LOG_TRACE |
| 49 | #define _LOG_INFO NS_LOG_INFO |
| 50 | |
| 51 | |
| 52 | INIT_LOGGER ("ndn.ApiFace"); |
| 53 | |
| 54 | namespace ns3 { |
| 55 | namespace ndn { |
| 56 | |
| 57 | // ApiFace::ApiFace() |
| 58 | // : m_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 59 | // { |
| 60 | // } |
| 61 | |
| 62 | // ApiFace::~ApiFace() |
| 63 | // { |
| 64 | // } |
| 65 | |
| 66 | // int |
| 67 | // ApiFace::publishRawData (const std::string &name, const char *buf, size_t len, int freshness) |
| 68 | // { |
| 69 | // // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 70 | // _LOG_INFO (">> publishRawData " << name); |
| 71 | |
| 72 | // static ndn::ContentObjectTail trailer; |
| 73 | |
| 74 | // ndn::ContentObject data; |
| 75 | // data.SetName (name); |
| 76 | // data.SetFreshness (Seconds (freshness)); |
| 77 | |
| 78 | // Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t*> (buf), len); |
| 79 | // // packet->AddPacketTag (CreateObject<TypeTag> (TypeTag::DATA)); |
| 80 | // packet->AddHeader (data); |
| 81 | // packet->AddTrailer (trailer); |
| 82 | |
| 83 | // m_protocolHandler (packet); |
| 84 | |
| 85 | // m_transmittedContentObjects (&data, packet, this, m_face); |
| 86 | |
| 87 | // return 0; |
| 88 | // } |
| 89 | |
| 90 | |
| 91 | // void |
| 92 | // RawDataCallback2StringDataCallback (ApiFace::StringDataCallback callback, std::string str, const char *buf, size_t len) |
| 93 | // { |
| 94 | // callback (str, string (buf, len)); |
| 95 | // } |
| 96 | |
| 97 | // int |
| 98 | // ApiFace::sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback/*, int retry*/) |
| 99 | // { |
| 100 | // return sendInterest (strInterest, boost::bind (RawDataCallback2StringDataCallback, strDataCallback, _1, _2, _3)); |
| 101 | // } |
| 102 | |
| 103 | // int ApiFace::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback) |
| 104 | // { |
| 105 | // // NS_LOG_INFO ("Requesting Interest: \n" << interestHeader); |
| 106 | // _LOG_INFO (">> I " << strInterest); |
| 107 | // Ptr<ndn::Name> name = Create<ndn::Name> (strInterest); |
| 108 | |
| 109 | // ndn::Interest interestHeader; |
| 110 | // interestHeader.SetNonce (m_rand.GetValue ()); |
| 111 | // interestHeader.SetName (*name); |
| 112 | // interestHeader.SetInterestLifetime (Seconds (9.9)); // really long-lived interests |
| 113 | |
| 114 | // Ptr<Packet> packet = Create<Packet> (); |
| 115 | // // packet->AddPacketTag (CreateObject<TypeTag> (TypeTag::INTEREST)); |
| 116 | // packet->AddHeader (interestHeader); |
| 117 | |
| 118 | // // NS_LOG_DEBUG (interestHeader); |
| 119 | |
| 120 | // // Record the callback |
| 121 | // FilterEntryContainer<RawDataCallback>::iterator entry = m_dataCallbacks.find_exact (*name); |
| 122 | // if (entry == m_dataCallbacks.end ()) |
| 123 | // { |
| 124 | // pair<FilterEntryContainer<RawDataCallback>::iterator, bool> status = |
| 125 | // m_dataCallbacks.insert (*name, Create< FilterEntry<RawDataCallback> > (name)); |
| 126 | |
| 127 | // entry = status.first; |
| 128 | // } |
| 129 | // entry->payload ()->AddCallback (rawDataCallback); |
| 130 | |
| 131 | // m_protocolHanler (packet); |
| 132 | // m_transmittedInterests (&interestHeader, this, m_face); |
| 133 | |
| 134 | // return 0; |
| 135 | // } |
| 136 | |
| 137 | // int ApiFace::setInterestFilter (const string &prefix, const InterestCallback &interestCallback) |
| 138 | // { |
| 139 | // NS_LOG_DEBUG ("== setInterestFilter " << prefix << " (" << GetNode ()->GetId () << ")"); |
| 140 | // Ptr<ndn::Name> name = Create<ndn::Name> (prefix); |
| 141 | |
| 142 | // FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.find_exact (*name); |
| 143 | // if (entry == m_interestCallbacks.end ()) |
| 144 | // { |
| 145 | // pair<FilterEntryContainer<InterestCallback>::iterator, bool> status = |
| 146 | // m_interestCallbacks.insert (*name, Create < FilterEntry<InterestCallback> > (name)); |
| 147 | |
| 148 | // entry = status.first; |
| 149 | // } |
| 150 | |
| 151 | // entry->payload ()->AddCallback (interestCallback); |
| 152 | |
| 153 | // // creating actual face |
| 154 | // Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 155 | // Ptr<ndn::fib::Entry> fibEntry = fib->Add (name, m_face, 0); |
| 156 | // fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
| 157 | |
| 158 | // return 0; |
| 159 | // } |
| 160 | |
| 161 | // void |
| 162 | // ApiFace::clearInterestFilter (const std::string &prefix) |
| 163 | // { |
| 164 | // Ptr<ndn::Name> name = Create<ndn::Name> (prefix); |
| 165 | |
| 166 | // FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.find_exact (*name); |
| 167 | // if (entry == m_interestCallbacks.end ()) |
| 168 | // return; |
| 169 | |
| 170 | // entry->payload ()->ClearCallback (); |
| 171 | // } |
| 172 | |
| 173 | // void |
| 174 | // ApiFace::OnInterest (const Ptr<const ndn::Interest> &interest, Ptr<Packet> packet) |
| 175 | // { |
| 176 | // ndn::App::OnInterest (interest, packet); |
| 177 | |
| 178 | // // the app cannot set several filters for the same prefix |
| 179 | // FilterEntryContainer<InterestCallback>::iterator entry = m_interestCallbacks.longest_prefix_match (interest->GetName ()); |
| 180 | // if (entry == m_interestCallbacks.end ()) |
| 181 | // { |
| 182 | // _LOG_DEBUG ("No Interest callback set"); |
| 183 | // return; |
| 184 | // } |
| 185 | |
| 186 | // entry->payload ()->m_callback (lexical_cast<string> (interest->GetName ())); |
| 187 | // } |
| 188 | |
| 189 | // void |
| 190 | // ApiFace::OnContentObject (const Ptr<const ndn::ContentObject> &contentObject, |
| 191 | // Ptr<Packet> payload) |
| 192 | // { |
| 193 | // ndn::App::OnContentObject (contentObject, payload); |
| 194 | // NS_LOG_DEBUG ("<< D " << contentObject->GetName ()); |
| 195 | |
| 196 | // FilterEntryContainer<RawDataCallback>::iterator entry = m_dataCallbacks.longest_prefix_match (contentObject->GetName ()); |
| 197 | // if (entry == m_dataCallbacks.end ()) |
| 198 | // { |
| 199 | // _LOG_DEBUG ("No Data callback set"); |
| 200 | // return; |
| 201 | // } |
| 202 | |
| 203 | // while (entry != m_dataCallbacks.end ()) |
| 204 | // { |
| 205 | // entry->payload ()->m_callback (lexical_cast<string> (contentObject->GetName ()), payload); |
| 206 | // m_dataCallbacks.erase (entry); |
| 207 | |
| 208 | // entry = m_dataCallbacks.longest_prefix_match (contentObject->GetName ()); |
| 209 | // } |
| 210 | // } |
| 211 | |
| 212 | } |
| 213 | } |