blob: ba7e973365342837f7e0dabec5edb3e75a1ea6bd [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
35using namespace std;
36using namespace boost;
37
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070038INIT_LOGGER ("CcnxWrapper");
39
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080040namespace Sync {
41
Alexander Afanasyev2247b302012-03-14 14:11:54 -070042#ifdef _DEBUG_WRAPPER_
43CcnxWrapper::CcnxWrapper(char c)
44#else
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080045CcnxWrapper::CcnxWrapper()
Alexander Afanasyev2247b302012-03-14 14:11:54 -070046#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080047 : m_handle (0)
48 , m_keyStore (0)
49 , m_keyLoactor (0)
50 , m_running (true)
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070051 , m_connected (false)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080052{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070053#ifdef _DEBUG_WRAPPER_
54 m_c = c;
55#endif
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070056 connectCcnd();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070057 initKeyStore ();
58 createKeyLocator ();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080059 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080060}
61
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070062void
63CcnxWrapper::connectCcnd()
64{
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070065 recursive_mutex::scoped_lock lock (m_mutex);
66
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070067 if (m_handle != 0) {
68 ccn_disconnect (m_handle);
69 ccn_destroy (&m_handle);
70 }
71
72 m_handle = ccn_create ();
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070073 _LOG_DEBUG("<<< connecting to ccnd");
74 if (ccn_connect(m_handle, NULL) < 0)
75 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070076 _LOG_DEBUG("<<< connecting to ccnd failed");
77 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
78 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070079 m_connected = true;
80
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070081 if (!m_registeredInterests.empty())
82 {
83 for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
84 {
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070085 // clearInterestFilter(it->first);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070086 setInterestFilter(it->first, it->second);
87 _LOG_DEBUG("<<< registering interest filter for: " << it->first);
88 }
89 }
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070090}
91
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080092CcnxWrapper::~CcnxWrapper()
93{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070094 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
95 {
96 recursive_mutex::scoped_lock lock(m_mutex);
97 m_running = false;
98 }
99
100 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800101 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700102 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800103 ccn_charbuf_destroy (&m_keyLoactor);
104 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800105}
106
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800107/// @cond include_hidden
108
109void
110CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800112 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800113 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
114 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
115 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800116 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800117 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800118 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
119 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800120 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800121}
122
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800123const ccn_pkey*
124CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800125{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800126 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800127}
128
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800129const unsigned char*
130CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800131{
132 return ccn_keystore_public_key_digest(m_keyStore);
133}
134
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800135ssize_t
136CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800137{
138 return ccn_keystore_public_key_digest_length(m_keyStore);
139}
140
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800141void
142CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800143{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800144 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800145 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
146 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
147 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800148}
149
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800150void
151CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800152{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700153 _LOG_FUNCTION (this);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700154 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
155 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800156
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700157 while (m_running)
158 {
159 try
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700160 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700161#ifdef _DEBUG_WRAPPER_
162 std::cout << m_c << flush;
163#endif
164 int res = 0;
165 {
166 recursive_mutex::scoped_lock lock (m_mutex);
167 res = ccn_run (m_handle, 0);
168 }
169
170 if (!m_running) break;
171
172 if (res < 0)
173 BOOST_THROW_EXCEPTION (CcnxOperationException()
174 << errmsg_info_str("ccn_run returned error"));
175
176
177 pollfd pfds[1];
178 {
179 recursive_mutex::scoped_lock lock (m_mutex);
180
181 pfds[0].fd = ccn_get_connection_fd (m_handle);
182 pfds[0].events = POLLIN;
183 if (ccn_output_is_pending (m_handle))
184 pfds[0].events |= POLLOUT;
185 }
186
187 int ret = poll (pfds, 1, 1);
188 if (ret < 0)
189 {
190 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
191 }
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800192 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700193 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700194 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700195 m_connected = false;
196 // probably ccnd has been stopped
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700197 // try reconnect with sleep
198 int interval = 1;
199 int maxInterval = 32;
200 while (m_running)
201 {
202 try
203 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700204 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
205
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700206 connectCcnd();
207 _LOG_DEBUG("reconnect to ccnd succeeded");
208 break;
209 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700210 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700211 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700212 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
213
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700214 // do exponential backup for reconnect interval
215 if (interval < maxInterval)
216 {
217 interval *= 2;
218 }
219 }
220 }
221 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700222 catch (const std::exception &exc)
223 {
224 // catch anything thrown within try block that derives from std::exception
225 std::cerr << exc.what();
226 }
227 catch (...)
228 {
229 cout << "UNKNOWN EXCEPTION !!!" << endl;
230 }
231
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700232 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800233}
234
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800235/// @endcond
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700236int
237CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) {
238 publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
239}
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800240
241int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700242CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800243{
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700244
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700245 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700246 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700247 return -1;
248
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700249 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800250 ccn_charbuf *pname = ccn_charbuf_create();
251 ccn_charbuf *signed_info = ccn_charbuf_create();
252 ccn_charbuf *content = ccn_charbuf_create();
253
254 ccn_name_from_uri(pname, name.c_str());
255 ccn_signed_info_create(signed_info,
256 getPublicKeyDigest(),
257 getPublicKeyDigestLength(),
258 NULL,
259 CCN_CONTENT_DATA,
260 freshness,
261 NULL,
262 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800263 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700264 (const unsigned char *)buf, len,
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800265 NULL, getPrivateKey()) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700266 {
267 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
268 _LOG_ERROR("<<< Encode content failed " << name);
269 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800270
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800271 if (ccn_put(m_handle, content->buf, content->length) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700272 {
273 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
274 _LOG_ERROR("<<< ccnput content failed " << name);
275 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800276
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800277 ccn_charbuf_destroy (&pname);
278 ccn_charbuf_destroy (&signed_info);
279 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700280 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800281}
282
283
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800284static ccn_upcall_res
285incomingInterest(ccn_closure *selfp,
286 ccn_upcall_kind kind,
287 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800288{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800289 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800290
291 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800292 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800293 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800294 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800295 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800296 return CCN_UPCALL_RESULT_OK;
297
298 case CCN_UPCALL_INTEREST:
299 break;
300
301 default:
302 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800303 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800304
Chaoyi Bian93d43102012-03-07 14:28:56 -0800305 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800306 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800307 {
308 char *comp;
309 size_t size;
310 interest += "/";
311 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800312 string compStr(comp, size);
313 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800314 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800315 (*f) (interest);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700316 _LOG_DEBUG("<<< processed interest: " << interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800317 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800318}
319
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800320static ccn_upcall_res
321incomingData(ccn_closure *selfp,
322 ccn_upcall_kind kind,
323 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800324{
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700325 ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800326
327 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800328 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800329 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700330 delete cp;
331 cp = NULL;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800332 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800333 return CCN_UPCALL_RESULT_OK;
334
335 case CCN_UPCALL_CONTENT:
336 break;
337
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700338 case CCN_UPCALL_INTEREST_TIMED_OUT: {
339 if (cp != NULL && cp->getRetry() > 0) {
340 cp->decRetry();
341 return CCN_UPCALL_RESULT_REEXPRESS;
342 }
343 return CCN_UPCALL_RESULT_OK;
344 }
345
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800346 default:
347 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800348 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800349
350 char *pcontent;
351 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800352 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 -0700353 {
354 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
355 _LOG_ERROR("<<< Decode content failed ");
356 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800357
Chaoyi Bian4194b742012-03-08 17:21:35 -0800358 string name;
359 for (int i = 0; i < info->content_comps->n - 1; i++)
360 {
361 char *comp;
362 size_t size;
363 name += "/";
364 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800365 string compStr(comp, size);
366 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800367 }
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700368
369 cp->runCallback(name, pcontent, len);
370
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800371 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800372}
373
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700374int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700375{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700376 DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700377 sendInterest(strInterest, pass);
378}
379
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700380int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700381{
382 RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback);
383 sendInterest(strInterest, pass);
384}
385
386int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800387{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700388 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700389 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700390 return -1;
391
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700392 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800393 ccn_charbuf *pname = ccn_charbuf_create();
394 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800395
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800396 ccn_name_from_uri (pname, strInterest.c_str());
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700397 dataClosure->data = dataPass;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800398
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800399 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800400 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700401 {
402 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
403 _LOG_ERROR("<<< Express interest failed: " << strInterest);
404 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800405
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700406 _LOG_DEBUG("<<< Sending interest: " << strInterest);
407
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800408 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700409 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800410}
411
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800412int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800413{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700414 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700415 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700416 return -1;
417
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800418 ccn_charbuf *pname = ccn_charbuf_create();
419 ccn_closure *interestClosure = new ccn_closure;
420
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800421 ccn_name_from_uri (pname, prefix.c_str());
422 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800423 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700424 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
425 if (ret < 0)
426 {
427 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
428 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800429
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700430 m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800431 ccn_charbuf_destroy(&pname);
432}
433
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700434void
435CcnxWrapper::clearInterestFilter (const std::string &prefix)
436{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700437 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700438 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700439 return;
440
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700441 std::cout << "clearInterestFilter" << std::endl;
442 ccn_charbuf *pname = ccn_charbuf_create();
443
444 ccn_name_from_uri (pname, prefix.c_str());
445 int ret = ccn_set_interest_filter (m_handle, pname, 0);
446 if (ret < 0)
447 {
448 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
449 }
450
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700451 m_registeredInterests.erase(prefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700452 ccn_charbuf_destroy(&pname);
453}
454
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700455DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700456{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700457 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700458}
459
460DataClosurePass::~DataClosurePass ()
461{
462 delete m_callback;
463 m_callback = NULL;
464}
465
466void
467DataClosurePass::runCallback(std::string name, const char *data, size_t len)
468{
469 string content(data, len);
470 if (m_callback != NULL) {
471 (*m_callback)(name, content);
472 }
473}
474
475
476RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
477{
478 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
479}
480
481RawDataClosurePass::~RawDataClosurePass ()
482{
483 delete m_callback;
484 m_callback = NULL;
485}
486
487void
488RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
489{
490 if (m_callback != NULL) {
491 (*m_callback)(name, data, len);
492 }
493}
494
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800495}