blob: c9ffe274ad2ff0a072f59ca9b75488fc43d7b5df [file] [log] [blame]
Alexander Afanasyev181d7e52012-04-09 13:54:11 -07001/* -*- 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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#ifndef SYNC_CCNX_WRAPPER_H
24#define SYNC_CCNX_WRAPPER_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/ccnx-app.h>
34#include <ns3/ccnx-name-components.h>
Alexander Afanasyevd7fc7c02012-04-19 15:36:26 -070035#include <ns3/ccnx-name-components-hash-helper.h>
Alexander Afanasyev181d7e52012-04-09 13:54:11 -070036
37#include <boost/multi_index_container.hpp>
38#include <boost/multi_index/hashed_index.hpp>
39#include <boost/multi_index/mem_fun.hpp>
40
41/**
42 * \defgroup sync SYNC protocol
43 *
44 * Implementation of SYNC protocol
45 */
46namespace Sync {
47
48template<class Callback>
49struct CcnxFilterEntry
50{
51public:
52 CcnxFilterEntry (ns3::Ptr<const ns3::CcnxNameComponents> prefix)
53 : m_prefix (prefix)
54 { }
55
56 const ns3::CcnxNameComponents &
57 GetPrefix () const
58 { return *m_prefix; }
59
60 void
61 AddCallback (Callback callback)
62 {
63 m_callback = callback;
64 }
65
66 void
67 ClearCallback ()
68 {
69 m_callback = 0;
70 }
71
72public:
73 ns3::Ptr<const ns3::CcnxNameComponents> m_prefix; ///< \brief Prefix of the PIT entry
74 Callback m_callback;
75};
76
77
78template<class Callback>
79struct CcnxFilterEntryContainer
80{
81 typedef
82 boost::multi_index::multi_index_container<
83 CcnxFilterEntry<Callback>,
84 boost::multi_index::indexed_by<
85 // indexed by hash
86 boost::multi_index::hashed_unique<
87 boost::multi_index::const_mem_fun< CcnxFilterEntry<Callback>, const ns3::CcnxNameComponents&, &CcnxFilterEntry<Callback>::GetPrefix >,
88 ns3::CcnxPrefixHash
89 >
90 >
91 > type;
92};
93
94
95
96struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
97/**
98 * \ingroup sync
99 * @brief A wrapper for ccnx library; clients of this code do not need to deal
100 * with ccnx library
101 */
102class CcnxWrapper
103 : public ns3::CcnxApp
104{
105public:
106 typedef boost::function<void (std::string, std::string)> DataCallback;
107 typedef boost::function<void (std::string)> InterestCallback;
108
109 /**
110 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
111 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
112 *
113 */
114 CcnxWrapper();
115 ~CcnxWrapper();
116
117 // from CcnxApp
118 /**
119 * @brief Should be called after Node pointer is set to create face and start application
120 */
121 virtual void
122 StartApplication ();
123
124 /**
125 * @brief Stop application
126 */
127 virtual void
128 StopApplication ();
129
130 /**
131 * @brief send Interest; need to grab lock m_mutex first
132 *
133 * @param strInterest the Interest name
134 * @param dataCallback the callback function to deal with the returned data
135 * @return the return code of ccn_express_interest
136 */
137 int
138 sendInterest (const std::string &strInterest, const DataCallback &dataCallback);
139
140 /**
141 * @brief set Interest filter (specify what interest you want to receive)
142 *
143 * @param prefix the prefix of Interest
144 * @param interestCallback the callback function to deal with the returned data
145 * @return the return code of ccn_set_interest_filter
146 */
147 int
148 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
149
150 /**
151 * @brief clear Interest filter
152 * @param prefix the prefix of Interest
153 */
154 void
155 clearInterestFilter (const std::string &prefix);
156
157 /**
158 * @brief publish data and put it to local ccn content store; need to grab
159 * lock m_mutex first
160 *
161 * @param name the name for the data object
162 * @param dataBuffer the data to be published
163 * @param freshness the freshness time for the data object
164 * @return code generated by ccnx library calls, >0 if success
165 */
166 int
167 publishData (const std::string &name, const std::string &dataBuffer, int freshness);
168
169 // from CcnxApp
170 virtual void
171 OnInterest (const ns3::Ptr<const ns3::CcnxInterestHeader> &interest, ns3::Ptr<ns3::Packet> packet);
172
173 virtual void
174 OnContentObject (const ns3::Ptr<const ns3::CcnxContentObjectHeader> &contentObject,
175 ns3::Ptr<ns3::Packet> payload);
176
177private:
178 CcnxFilterEntryContainer<InterestCallback>::type::iterator
179 InterestCallbackLookup (const ns3::CcnxNameComponents &name);
180
181 CcnxFilterEntryContainer<DataCallback>::type::iterator
182 DataCallbackLookup (const ns3::CcnxNameComponents &name);
183
184private:
185 ns3::UniformVariable m_rand; // nonce generator
186
187 CcnxFilterEntryContainer<DataCallback>::type m_dataCallbacks;
188 CcnxFilterEntryContainer<InterestCallback>::type m_interestCallbacks;
189};
190
191typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
192
193} // Sync
194
195#endif // SYNC_CCNX_WRAPPER_H