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