blob: bc1ea0f86462d8547917bcabe9fde13f9be7eee9 [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
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070052 m_handle = ccn_create();
53 connectCcnd();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070054 initKeyStore ();
55 createKeyLocator ();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080056 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080057}
58
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070059void
60CcnxWrapper::connectCcnd()
61{
62 /*
63 if (m_handle != NULL)
64 {
65 ccn_destroy(&m_handle);
66 }
67 */
68 _LOG_DEBUG("<<< connecting to ccnd");
69 if (ccn_connect(m_handle, NULL) < 0)
70 {
71 //ccn_destroy(&m_handle);
72 _LOG_DEBUG("<<< connecting to ccnd failed");
73 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
74 }
75}
76
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080077CcnxWrapper::~CcnxWrapper()
78{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070079 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
80 {
81 recursive_mutex::scoped_lock lock(m_mutex);
82 m_running = false;
83 }
84
85 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080086 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -070087 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080088 ccn_charbuf_destroy (&m_keyLoactor);
89 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080090}
91
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080092/// @cond include_hidden
93
94void
95CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080096{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080097 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080098 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
99 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
100 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800101 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800102 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800103 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
104 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800105 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800106}
107
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800108const ccn_pkey*
109CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800110{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800111 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800112}
113
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800114const unsigned char*
115CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116{
117 return ccn_keystore_public_key_digest(m_keyStore);
118}
119
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800120ssize_t
121CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800122{
123 return ccn_keystore_public_key_digest_length(m_keyStore);
124}
125
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800126void
127CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800128{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800129 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800130 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
131 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
132 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800133}
134
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800135void
136CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800137{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700138 _LOG_FUNCTION (this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800139
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700140 while (m_running)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700141 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700142 try
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700143 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700144#ifdef _DEBUG_WRAPPER_
145 std::cout << m_c << flush;
146#endif
147 int res = 0;
148 {
149 recursive_mutex::scoped_lock lock (m_mutex);
150 res = ccn_run (m_handle, 0);
151 }
152
153 if (!m_running) break;
154
155 if (res < 0)
156 BOOST_THROW_EXCEPTION (CcnxOperationException()
157 << errmsg_info_str("ccn_run returned error"));
158
159
160 pollfd pfds[1];
161 {
162 recursive_mutex::scoped_lock lock (m_mutex);
163
164 pfds[0].fd = ccn_get_connection_fd (m_handle);
165 pfds[0].events = POLLIN;
166 if (ccn_output_is_pending (m_handle))
167 pfds[0].events |= POLLOUT;
168 }
169
170 int ret = poll (pfds, 1, 1);
171 if (ret < 0)
172 {
173 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
174 }
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800175 }
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700176 catch (CcnxOperationException e)
177 {
178 // probably ccnd has been stoped
179 // try reconnect with sleep
180 int interval = 1;
181 int maxInterval = 32;
182 while (m_running)
183 {
184 try
185 {
186 sleep(1);
187 connectCcnd();
188 _LOG_DEBUG("reconnect to ccnd succeeded");
189 break;
190 }
191 catch (CcnxOperationException e)
192 {
193 sleep(interval);
194 // do exponential backup for reconnect interval
195 if (interval < maxInterval)
196 {
197 interval *= 2;
198 }
199 }
200 }
201 }
202 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800203}
204
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800205/// @endcond
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700206int
207CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) {
208 publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
209}
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800210
211int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700212CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800213{
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700214
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700215 recursive_mutex::scoped_lock lock(m_mutex);
216 if (!m_running)
217 return -1;
218
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700219 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800220 ccn_charbuf *pname = ccn_charbuf_create();
221 ccn_charbuf *signed_info = ccn_charbuf_create();
222 ccn_charbuf *content = ccn_charbuf_create();
223
224 ccn_name_from_uri(pname, name.c_str());
225 ccn_signed_info_create(signed_info,
226 getPublicKeyDigest(),
227 getPublicKeyDigestLength(),
228 NULL,
229 CCN_CONTENT_DATA,
230 freshness,
231 NULL,
232 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800233 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700234 (const unsigned char *)buf, len,
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800235 NULL, getPrivateKey()) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700236 {
237 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
238 _LOG_ERROR("<<< Encode content failed " << name);
239 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800240
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800241 if (ccn_put(m_handle, content->buf, content->length) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700242 {
243 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
244 _LOG_ERROR("<<< ccnput content failed " << name);
245 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800246
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800247 ccn_charbuf_destroy (&pname);
248 ccn_charbuf_destroy (&signed_info);
249 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700250 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800251}
252
253
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800254static ccn_upcall_res
255incomingInterest(ccn_closure *selfp,
256 ccn_upcall_kind kind,
257 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800258{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800259 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800260
261 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800262 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800263 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800264 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800265 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800266 return CCN_UPCALL_RESULT_OK;
267
268 case CCN_UPCALL_INTEREST:
269 break;
270
271 default:
272 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800273 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800274
Chaoyi Bian93d43102012-03-07 14:28:56 -0800275 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800276 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800277 {
278 char *comp;
279 size_t size;
280 interest += "/";
281 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800282 string compStr(comp, size);
283 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800284 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800285 (*f) (interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800286 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800287}
288
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800289static ccn_upcall_res
290incomingData(ccn_closure *selfp,
291 ccn_upcall_kind kind,
292 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800293{
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700294 ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800295
296 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800297 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800298 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700299 delete cp;
300 cp = NULL;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800301 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800302 return CCN_UPCALL_RESULT_OK;
303
304 case CCN_UPCALL_CONTENT:
305 break;
306
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700307 case CCN_UPCALL_INTEREST_TIMED_OUT: {
308 if (cp != NULL && cp->getRetry() > 0) {
309 cp->decRetry();
310 return CCN_UPCALL_RESULT_REEXPRESS;
311 }
312 return CCN_UPCALL_RESULT_OK;
313 }
314
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800315 default:
316 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800317 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800318
319 char *pcontent;
320 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800321 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700322 {
323 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
324 _LOG_ERROR("<<< Decode content failed ");
325 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800326
Chaoyi Bian4194b742012-03-08 17:21:35 -0800327 string name;
328 for (int i = 0; i < info->content_comps->n - 1; i++)
329 {
330 char *comp;
331 size_t size;
332 name += "/";
333 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800334 string compStr(comp, size);
335 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800336 }
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700337
338 cp->runCallback(name, pcontent, len);
339
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800340 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800341}
342
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700343int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700344{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700345 DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700346 sendInterest(strInterest, pass);
347}
348
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700349int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700350{
351 RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback);
352 sendInterest(strInterest, pass);
353}
354
355int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800356{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700357 recursive_mutex::scoped_lock lock(m_mutex);
358 if (!m_running)
359 return -1;
360
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700361 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800362 ccn_charbuf *pname = ccn_charbuf_create();
363 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800364
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800365 ccn_name_from_uri (pname, strInterest.c_str());
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700366 dataClosure->data = dataPass;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800367
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800368 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800369 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700370 {
371 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
372 _LOG_ERROR("<<< Express interest failed: " << strInterest);
373 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800374
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800375 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700376 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800377}
378
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800379int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800380{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700381 recursive_mutex::scoped_lock lock(m_mutex);
382 if (!m_running)
383 return -1;
384
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800385 ccn_charbuf *pname = ccn_charbuf_create();
386 ccn_closure *interestClosure = new ccn_closure;
387
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800388 ccn_name_from_uri (pname, prefix.c_str());
389 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800390 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700391 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
392 if (ret < 0)
393 {
394 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
395 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800396
397 ccn_charbuf_destroy(&pname);
398}
399
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700400void
401CcnxWrapper::clearInterestFilter (const std::string &prefix)
402{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700403 recursive_mutex::scoped_lock lock(m_mutex);
404 if (!m_running)
405 return;
406
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700407 std::cout << "clearInterestFilter" << std::endl;
408 ccn_charbuf *pname = ccn_charbuf_create();
409
410 ccn_name_from_uri (pname, prefix.c_str());
411 int ret = ccn_set_interest_filter (m_handle, pname, 0);
412 if (ret < 0)
413 {
414 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
415 }
416
417 ccn_charbuf_destroy(&pname);
418}
419
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700420DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700421{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700422 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700423}
424
425DataClosurePass::~DataClosurePass ()
426{
427 delete m_callback;
428 m_callback = NULL;
429}
430
431void
432DataClosurePass::runCallback(std::string name, const char *data, size_t len)
433{
434 string content(data, len);
435 if (m_callback != NULL) {
436 (*m_callback)(name, content);
437 }
438}
439
440
441RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
442{
443 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
444}
445
446RawDataClosurePass::~RawDataClosurePass ()
447{
448 delete m_callback;
449 m_callback = NULL;
450}
451
452void
453RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
454{
455 if (m_callback != NULL) {
456 (*m_callback)(name, data, len);
457 }
458}
459
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800460}