blob: 559cfad158e0eeaa0c9ac9de57ecee3df0e598e3 [file] [log] [blame]
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080022
23#include "sync-ccnx-wrapper.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070024#include "sync-log.h"
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080025#include <poll.h>
Zhenkai Zhu009ff792012-03-09 12:37:52 -080026#include <boost/throw_exception.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070027#include <boost/date_time/posix_time/posix_time.hpp>
28
Chaoyi Bian95a58c32012-03-09 15:43:59 -080029typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070030typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080031
32using namespace std;
33using namespace boost;
34
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070035INIT_LOGGER ("CcnxWrapper");
36
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080037namespace Sync {
38
Alexander Afanasyev2247b302012-03-14 14:11:54 -070039#ifdef _DEBUG_WRAPPER_
40CcnxWrapper::CcnxWrapper(char c)
41#else
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080042CcnxWrapper::CcnxWrapper()
Alexander Afanasyev2247b302012-03-14 14:11:54 -070043#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080044 : m_handle (0)
45 , m_keyStore (0)
46 , m_keyLoactor (0)
47 , m_running (true)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080048{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070049#ifdef _DEBUG_WRAPPER_
50 m_c = c;
51#endif
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070052 m_handle = ccn_create ();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070053 initKeyStore ();
54 createKeyLocator ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -080055 if (ccn_connect(m_handle, NULL) < 0)
56 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -080057 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080058}
59
60CcnxWrapper::~CcnxWrapper()
61{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070062 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
63 {
64 recursive_mutex::scoped_lock lock(m_mutex);
65 m_running = false;
66 }
67
68 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080069 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -070070 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080071 ccn_charbuf_destroy (&m_keyLoactor);
72 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080073}
74
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080075/// @cond include_hidden
76
77void
78CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080079{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080080 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080081 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
82 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
83 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080084 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -080085 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
87 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080088 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080089}
90
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080091const ccn_pkey*
92CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080093{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080094 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080095}
96
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080097const unsigned char*
98CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080099{
100 return ccn_keystore_public_key_digest(m_keyStore);
101}
102
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800103ssize_t
104CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800105{
106 return ccn_keystore_public_key_digest_length(m_keyStore);
107}
108
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800109void
110CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800112 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800113 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
114 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
115 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116}
117
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800118void
119CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800120{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700121 _LOG_FUNCTION (this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800122
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800123 while (m_running)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800124 {
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700125#ifdef _DEBUG_WRAPPER_
126 std::cout << m_c << flush;
127#endif
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700128 int res = 0;
129 {
130 recursive_mutex::scoped_lock lock (m_mutex);
131 res = ccn_run (m_handle, 0);
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700132
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700133 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700134
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700135 if (!m_running) break;
136
137 if (res < 0)
138 BOOST_THROW_EXCEPTION (CcnxOperationException()
139 << errmsg_info_str("ccn_run returned error"));
140
Alexander Afanasyev2247b302012-03-14 14:11:54 -0700141
142 pollfd pfds[1];
143 {
144 recursive_mutex::scoped_lock lock (m_mutex);
145
146 pfds[0].fd = ccn_get_connection_fd (m_handle);
147 pfds[0].events = POLLIN;
148 if (ccn_output_is_pending (m_handle))
149 pfds[0].events |= POLLOUT;
150 }
151
152 int ret = poll (pfds, 1, 1);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700153 if (ret < 0)
154 {
155 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800156 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800157 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800158}
159
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800160/// @endcond
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700161int
162CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) {
163 publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
164}
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800165
166int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700167CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800168{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700169 recursive_mutex::scoped_lock lock(m_mutex);
170 if (!m_running)
171 return -1;
172
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700173 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800174 ccn_charbuf *pname = ccn_charbuf_create();
175 ccn_charbuf *signed_info = ccn_charbuf_create();
176 ccn_charbuf *content = ccn_charbuf_create();
177
178 ccn_name_from_uri(pname, name.c_str());
179 ccn_signed_info_create(signed_info,
180 getPublicKeyDigest(),
181 getPublicKeyDigestLength(),
182 NULL,
183 CCN_CONTENT_DATA,
184 freshness,
185 NULL,
186 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800187 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700188 (const unsigned char *)buf, len,
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800189 NULL, getPrivateKey()) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800190 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800191
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800192 if (ccn_put(m_handle, content->buf, content->length) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800193 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800194
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800195 ccn_charbuf_destroy (&pname);
196 ccn_charbuf_destroy (&signed_info);
197 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700198 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800199}
200
201
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800202static ccn_upcall_res
203incomingInterest(ccn_closure *selfp,
204 ccn_upcall_kind kind,
205 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800206{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800207 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800208
209 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800210 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800211 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800212 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800213 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800214 return CCN_UPCALL_RESULT_OK;
215
216 case CCN_UPCALL_INTEREST:
217 break;
218
219 default:
220 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800221 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800222
Chaoyi Bian93d43102012-03-07 14:28:56 -0800223 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800224 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800225 {
226 char *comp;
227 size_t size;
228 interest += "/";
229 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800230 string compStr(comp, size);
231 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800232 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800233 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800234 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800235}
236
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800237static ccn_upcall_res
238incomingData(ccn_closure *selfp,
239 ccn_upcall_kind kind,
240 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800241{
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700242 ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800243
244 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800245 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800246 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700247 delete cp;
248 cp = NULL;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800249 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800250 return CCN_UPCALL_RESULT_OK;
251
252 case CCN_UPCALL_CONTENT:
253 break;
254
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700255 case CCN_UPCALL_INTEREST_TIMED_OUT: {
256 if (cp != NULL && cp->getRetry() > 0) {
257 cp->decRetry();
258 return CCN_UPCALL_RESULT_REEXPRESS;
259 }
260 return CCN_UPCALL_RESULT_OK;
261 }
262
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800263 default:
264 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800265 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800266
267 char *pcontent;
268 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800269 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800270 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800271
Chaoyi Bian4194b742012-03-08 17:21:35 -0800272 string name;
273 for (int i = 0; i < info->content_comps->n - 1; i++)
274 {
275 char *comp;
276 size_t size;
277 name += "/";
278 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800279 string compStr(comp, size);
280 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800281 }
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700282
283 cp->runCallback(name, pcontent, len);
284
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800285 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800286}
287
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700288int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700289{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700290 DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700291 sendInterest(strInterest, pass);
292}
293
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700294int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700295{
296 RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback);
297 sendInterest(strInterest, pass);
298}
299
300int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800301{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700302 recursive_mutex::scoped_lock lock(m_mutex);
303 if (!m_running)
304 return -1;
305
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700306 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800307 ccn_charbuf *pname = ccn_charbuf_create();
308 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800309
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800310 ccn_name_from_uri (pname, strInterest.c_str());
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700311 dataClosure->data = dataPass;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800312
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800313 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800314 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800315 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800316
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800317 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700318 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800319}
320
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800321int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800322{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700323 recursive_mutex::scoped_lock lock(m_mutex);
324 if (!m_running)
325 return -1;
326
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800327 ccn_charbuf *pname = ccn_charbuf_create();
328 ccn_closure *interestClosure = new ccn_closure;
329
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800330 ccn_name_from_uri (pname, prefix.c_str());
331 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800332 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700333 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
334 if (ret < 0)
335 {
336 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
337 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800338
339 ccn_charbuf_destroy(&pname);
340}
341
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700342void
343CcnxWrapper::clearInterestFilter (const std::string &prefix)
344{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700345 recursive_mutex::scoped_lock lock(m_mutex);
346 if (!m_running)
347 return;
348
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700349 std::cout << "clearInterestFilter" << std::endl;
350 ccn_charbuf *pname = ccn_charbuf_create();
351
352 ccn_name_from_uri (pname, prefix.c_str());
353 int ret = ccn_set_interest_filter (m_handle, pname, 0);
354 if (ret < 0)
355 {
356 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
357 }
358
359 ccn_charbuf_destroy(&pname);
360}
361
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700362DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700363{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700364 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700365}
366
367DataClosurePass::~DataClosurePass ()
368{
369 delete m_callback;
370 m_callback = NULL;
371}
372
373void
374DataClosurePass::runCallback(std::string name, const char *data, size_t len)
375{
376 string content(data, len);
377 if (m_callback != NULL) {
378 (*m_callback)(name, content);
379 }
380}
381
382
383RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
384{
385 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
386}
387
388RawDataClosurePass::~RawDataClosurePass ()
389{
390 delete m_callback;
391 m_callback = NULL;
392}
393
394void
395RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
396{
397 if (m_callback != NULL) {
398 (*m_callback)(name, data, len);
399 }
400}
401
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800402}