blob: 475990312366a89433bdedf05614b8e058634a4d [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>
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070028#include <boost/random.hpp>
29
30#include "sync-scheduler.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070031
Chaoyi Bian95a58c32012-03-09 15:43:59 -080032typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070033typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080034
Alexander Afanasyev60126e02012-10-05 16:54:42 -070035
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080036using namespace std;
37using namespace boost;
38
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070039INIT_LOGGER ("CcnxWrapper");
40
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080041namespace Sync {
42
Alexander Afanasyev60126e02012-10-05 16:54:42 -070043boost::shared_ptr<CcnxWrapper> CcnxWrapper::s_wrapper;
44
Alexander Afanasyev2247b302012-03-14 14:11:54 -070045#ifdef _DEBUG_WRAPPER_
46CcnxWrapper::CcnxWrapper(char c)
47#else
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080048CcnxWrapper::CcnxWrapper()
Alexander Afanasyev2247b302012-03-14 14:11:54 -070049#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080050 : m_handle (0)
51 , m_keyStore (0)
52 , m_keyLoactor (0)
53 , m_running (true)
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070054 , m_connected (false)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080055{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070056#ifdef _DEBUG_WRAPPER_
57 m_c = c;
58#endif
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070059 connectCcnd();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070060 initKeyStore ();
61 createKeyLocator ();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080062 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080063}
64
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070065void
66CcnxWrapper::connectCcnd()
67{
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070068 recursive_mutex::scoped_lock lock (m_mutex);
69
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070070 if (m_handle != 0) {
71 ccn_disconnect (m_handle);
72 ccn_destroy (&m_handle);
73 }
74
75 m_handle = ccn_create ();
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070076 _LOG_DEBUG("<<< connecting to ccnd");
77 if (ccn_connect(m_handle, NULL) < 0)
78 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070079 _LOG_DEBUG("<<< connecting to ccnd failed");
80 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
81 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070082 m_connected = true;
83
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070084 if (!m_registeredInterests.empty())
85 {
86 for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
87 {
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070088 // clearInterestFilter(it->first);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070089 setInterestFilter(it->first, it->second);
90 _LOG_DEBUG("<<< registering interest filter for: " << it->first);
91 }
92 }
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070093}
94
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080095CcnxWrapper::~CcnxWrapper()
96{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070097 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
98 {
99 recursive_mutex::scoped_lock lock(m_mutex);
100 m_running = false;
101 }
102
103 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800104 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700105 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800106 ccn_charbuf_destroy (&m_keyLoactor);
107 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800108}
109
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800110/// @cond include_hidden
111
112void
113CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800114{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800115 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800116 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
117 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
118 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800119 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800120 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800121 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
122 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800123 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800124}
125
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800126const ccn_pkey*
127CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800128{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800129 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800130}
131
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800132const unsigned char*
133CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800134{
135 return ccn_keystore_public_key_digest(m_keyStore);
136}
137
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800138ssize_t
139CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800140{
141 return ccn_keystore_public_key_digest_length(m_keyStore);
142}
143
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800144void
145CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800146{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800147 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800148 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
149 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
150 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800151}
152
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800153void
154CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800155{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700156 _LOG_FUNCTION (this);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700157 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
158 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800159
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700160 while (m_running)
161 {
162 try
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700163 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700164#ifdef _DEBUG_WRAPPER_
165 std::cout << m_c << flush;
166#endif
167 int res = 0;
168 {
169 recursive_mutex::scoped_lock lock (m_mutex);
170 res = ccn_run (m_handle, 0);
171 }
172
173 if (!m_running) break;
174
175 if (res < 0)
176 BOOST_THROW_EXCEPTION (CcnxOperationException()
177 << errmsg_info_str("ccn_run returned error"));
178
179
180 pollfd pfds[1];
181 {
182 recursive_mutex::scoped_lock lock (m_mutex);
183
184 pfds[0].fd = ccn_get_connection_fd (m_handle);
185 pfds[0].events = POLLIN;
186 if (ccn_output_is_pending (m_handle))
187 pfds[0].events |= POLLOUT;
188 }
189
190 int ret = poll (pfds, 1, 1);
191 if (ret < 0)
192 {
193 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
194 }
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800195 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700196 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700197 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700198 m_connected = false;
199 // probably ccnd has been stopped
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700200 // try reconnect with sleep
201 int interval = 1;
202 int maxInterval = 32;
203 while (m_running)
204 {
205 try
206 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700207 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
208
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700209 connectCcnd();
210 _LOG_DEBUG("reconnect to ccnd succeeded");
211 break;
212 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700213 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700214 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700215 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
216
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700217 // do exponential backup for reconnect interval
218 if (interval < maxInterval)
219 {
220 interval *= 2;
221 }
222 }
223 }
224 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700225 catch (const std::exception &exc)
226 {
227 // catch anything thrown within try block that derives from std::exception
228 std::cerr << exc.what();
229 }
230 catch (...)
231 {
232 cout << "UNKNOWN EXCEPTION !!!" << endl;
233 }
234
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700235 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800236}
237
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800238/// @endcond
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700239int
240CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) {
241 publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
242}
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800243
244int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700245CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800246{
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700247
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700248 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700249 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700250 return -1;
251
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700252 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800253 ccn_charbuf *pname = ccn_charbuf_create();
254 ccn_charbuf *signed_info = ccn_charbuf_create();
255 ccn_charbuf *content = ccn_charbuf_create();
256
257 ccn_name_from_uri(pname, name.c_str());
258 ccn_signed_info_create(signed_info,
259 getPublicKeyDigest(),
260 getPublicKeyDigestLength(),
261 NULL,
262 CCN_CONTENT_DATA,
263 freshness,
264 NULL,
265 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800266 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700267 (const unsigned char *)buf, len,
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800268 NULL, getPrivateKey()) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700269 {
270 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
271 _LOG_ERROR("<<< Encode content failed " << name);
272 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800273
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800274 if (ccn_put(m_handle, content->buf, content->length) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700275 {
276 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
277 _LOG_ERROR("<<< ccnput content failed " << name);
278 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800279
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800280 ccn_charbuf_destroy (&pname);
281 ccn_charbuf_destroy (&signed_info);
282 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700283 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800284}
285
286
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800287static ccn_upcall_res
288incomingInterest(ccn_closure *selfp,
289 ccn_upcall_kind kind,
290 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800291{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800292 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800293
294 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800295 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800296 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800297 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800298 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800299 return CCN_UPCALL_RESULT_OK;
300
301 case CCN_UPCALL_INTEREST:
302 break;
303
304 default:
305 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800306 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800307
Chaoyi Bian93d43102012-03-07 14:28:56 -0800308 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800309 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800310 {
311 char *comp;
312 size_t size;
313 interest += "/";
314 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800315 string compStr(comp, size);
316 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800317 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800318 (*f) (interest);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700319 _LOG_DEBUG("<<< processed interest: " << interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800320 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800321}
322
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800323static ccn_upcall_res
324incomingData(ccn_closure *selfp,
325 ccn_upcall_kind kind,
326 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800327{
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700328 ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800329
330 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800331 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800332 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700333 delete cp;
334 cp = NULL;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800335 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800336 return CCN_UPCALL_RESULT_OK;
337
338 case CCN_UPCALL_CONTENT:
339 break;
340
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700341 case CCN_UPCALL_INTEREST_TIMED_OUT: {
342 if (cp != NULL && cp->getRetry() > 0) {
343 cp->decRetry();
344 return CCN_UPCALL_RESULT_REEXPRESS;
345 }
346 return CCN_UPCALL_RESULT_OK;
347 }
348
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800349 default:
350 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800351 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800352
353 char *pcontent;
354 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800355 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 -0700356 {
357 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
358 _LOG_ERROR("<<< Decode content failed ");
359 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800360
Chaoyi Bian4194b742012-03-08 17:21:35 -0800361 string name;
362 for (int i = 0; i < info->content_comps->n - 1; i++)
363 {
364 char *comp;
365 size_t size;
366 name += "/";
367 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800368 string compStr(comp, size);
369 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800370 }
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700371
372 cp->runCallback(name, pcontent, len);
373
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800374 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800375}
376
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700377int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700378{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700379 DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700380 sendInterest(strInterest, pass);
381}
382
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700383int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700384{
385 RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback);
386 sendInterest(strInterest, pass);
387}
388
389int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800390{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700391 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700392 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700393 return -1;
394
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700395 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800396 ccn_charbuf *pname = ccn_charbuf_create();
397 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800398
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800399 ccn_name_from_uri (pname, strInterest.c_str());
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700400 dataClosure->data = dataPass;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800401
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800402 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800403 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700404 {
405 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
406 _LOG_ERROR("<<< Express interest failed: " << strInterest);
407 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800408
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700409 _LOG_DEBUG("<<< Sending interest: " << strInterest);
410
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800411 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700412 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800413}
414
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800415int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800416{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700417 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700418 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700419 return -1;
420
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800421 ccn_charbuf *pname = ccn_charbuf_create();
422 ccn_closure *interestClosure = new ccn_closure;
423
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800424 ccn_name_from_uri (pname, prefix.c_str());
425 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800426 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700427 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
428 if (ret < 0)
429 {
430 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
431 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800432
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700433 m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800434 ccn_charbuf_destroy(&pname);
435}
436
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700437void
438CcnxWrapper::clearInterestFilter (const std::string &prefix)
439{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700440 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700441 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700442 return;
443
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700444 std::cout << "clearInterestFilter" << std::endl;
445 ccn_charbuf *pname = ccn_charbuf_create();
446
447 ccn_name_from_uri (pname, prefix.c_str());
448 int ret = ccn_set_interest_filter (m_handle, pname, 0);
449 if (ret < 0)
450 {
451 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
452 }
453
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700454 m_registeredInterests.erase(prefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700455 ccn_charbuf_destroy(&pname);
456}
457
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700458DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700459{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700460 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700461}
462
463DataClosurePass::~DataClosurePass ()
464{
465 delete m_callback;
466 m_callback = NULL;
467}
468
469void
470DataClosurePass::runCallback(std::string name, const char *data, size_t len)
471{
472 string content(data, len);
473 if (m_callback != NULL) {
474 (*m_callback)(name, content);
475 }
476}
477
478
479RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
480{
481 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
482}
483
484RawDataClosurePass::~RawDataClosurePass ()
485{
486 delete m_callback;
487 m_callback = NULL;
488}
489
490void
491RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
492{
493 if (m_callback != NULL) {
494 (*m_callback)(name, data, len);
495 }
496}
497
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800498}