blob: 420645d33e2b197f4daf6788c3069b6b037f3d92 [file] [log] [blame]
Alexander Afanasyev79a5bd62013-06-23 22:12:39 -07001/* -*- 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_HANDLER_H
24#define NDN_HANDLER_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
39namespace ns3 {
40namespace ndn {
41
42template<class Callback>
43struct FilterEntry : public ns3::SimpleRefCount< FilterEntry<Callback> >
44{
45public:
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
66public:
67 ns3::Ptr<const ns3::ndn::Name> m_prefix; ///< \brief Prefix of the PIT entry
68 Callback m_callback;
69};
70
71
72template<class Callback>
73struct 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
81struct 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 */
87class Handler
88 : public ns3::ndn::App
89{
90public:
91 typedef boost::function<void (std::string, std::string)> StringDataCallback;
92 typedef boost::function<void (std::string, Ptr<Packet>)> RawDataCallback;
93 typedef boost::function<void (std::string)> InterestCallback;
94
95
96 /**
97 * @brief initialize the handler; a lot of things needs to be done. 1) init
98 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
99 *
100 */
101 Handler();
102 ~Handler();
103
104 /**
105 * @brief send Interest; need to grab lock m_mutex first
106 *
107 * @param strInterest the Interest name
108 * @param dataCallback the callback function to deal with the returned data
109 * @return the return code of ccn_express_interest
110 */
111 // int
112 // sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback/*, int retry = 0*/);
113
114 int
115 sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback/*, int retry = 0*/);
116
117 /**
118 * @brief set Interest filter (specify what interest you want to receive)
119 *
120 * @param prefix the prefix of Interest
121 * @param interestCallback the callback function to deal with the returned data
122 * @return the return code of ccn_set_interest_filter
123 */
124 int
125 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
126
127 /**
128 * @brief clear Interest filter
129 * @param prefix the prefix of Interest
130 */
131 void
132 clearInterestFilter (const std::string &prefix);
133
134 /**
135 * @brief publish data and put it to local ccn content store; need to grab
136 * lock m_mutex first
137 *
138 * @param name the name for the data object
139 * @param dataBuffer the data to be published
140 * @param freshness the freshness time for the data object
141 * @return code generated by NDN library calls, >0 if success
142 */
143 int
144 publishStringData (const std::string &name, const std::string &dataBuffer, int freshness)
145 {
146 return publishRawData (name, dataBuffer.c_str(), dataBuffer.length(), freshness);
147 }
148
149 int
150 publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
151
152 // from ndn::App
153 virtual void
154 OnInterest (const ns3::Ptr<const ns3::ndn::Interest> &interest, ns3::Ptr<ns3::Packet> packet);
155
156 virtual void
157 OnContentObject (const ns3::Ptr<const ns3::ndn::ContentObject> &contentObject,
158 ns3::Ptr<ns3::Packet> payload);
159
160
161public:
162 // inherited from Application base class.
163 virtual void
164 StartApplication (); // Called at time specified by Start
165
166 virtual void
167 StopApplication (); // Called at time specified by Stop
168
169private:
170 ns3::UniformVariable m_rand; // nonce generator
171
172 FilterEntryContainer<RawDataCallback> m_dataCallbacks;
173 FilterEntryContainer<InterestCallback> m_interestCallbacks;
174};
175
176}
177}
178
179#endif // NDN_HANDLER_H