blob: d584be484a4fa0e6e852aff979468ba27b508382 [file] [log] [blame]
Alexander Afanasyevf278db32013-01-21 14:41:01 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 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 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080022#include "ccnx-wrapper.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080023extern "C" {
24#include <ccn/fetch.h>
25}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080026#include <poll.h>
27#include <boost/throw_exception.hpp>
28#include <boost/date_time/posix_time/posix_time.hpp>
29#include <boost/random.hpp>
Alexander Afanasyevf278db32013-01-21 14:41:01 -080030#include <boost/make_shared.hpp>
Zhenkai Zhucbbb8882013-01-25 13:49:12 -080031#include <boost/algorithm/string.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080032#include <sstream>
33
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -080034#include "logging.h"
35
36INIT_LOGGER ("Ccnx.Wrapper");
37
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
39typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
40
41using namespace std;
42using namespace boost;
43
44namespace Ccnx {
45
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080046CcnxWrapper::CcnxWrapper()
47 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080048 , m_running (true)
49 , m_connected (false)
50{
51 connectCcnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080052 m_thread = thread (&CcnxWrapper::ccnLoop, this);
53}
54
55void
56CcnxWrapper::connectCcnd()
57{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080058 //if (m_handle != 0) {
59 //ccn_disconnect (m_handle);
60 //ccn_destroy (&m_handle);
61 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080062
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080063 m_handle = ccn_create ();
Zhenkai Zhu7599db42013-01-28 10:32:53 -080064 //UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065 if (ccn_connect(m_handle, NULL) < 0)
66 {
67 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
68 }
69 m_connected = true;
70
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080071 //if (!m_registeredInterests.empty())
72 //{
73 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
74 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080075 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080076 // setInterestFilter(it->first, it->second);
77 //}
78 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080079}
80
81CcnxWrapper::~CcnxWrapper()
82{
83 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080084 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080085 m_running = false;
86 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080087
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080088 m_thread.join ();
89 ccn_disconnect (m_handle);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080090 //ccn_destroy (&m_handle);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080091}
92
93void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080094CcnxWrapper::ccnLoop ()
95{
96 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
97 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
98
99 while (m_running)
100 {
101 try
102 {
103 int res = 0;
104 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800105 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800106 res = ccn_run (m_handle, 0);
107 }
108
109 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800110
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800111 if (res < 0) {
112 _LOG_ERROR ("ccn_run returned negative status: " << res);
113 usleep (100000);
114 continue;
115 // BOOST_THROW_EXCEPTION (CcnxOperationException()
116 // << errmsg_info_str("ccn_run returned error"));
117 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800118
119
120 pollfd pfds[1];
121 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800122 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800123
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800124 pfds[0].fd = ccn_get_connection_fd (m_handle);
125 pfds[0].events = POLLIN;
126 if (ccn_output_is_pending (m_handle))
127 pfds[0].events |= POLLOUT;
128 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800129
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800130 int ret = poll (pfds, 1, 1);
131 if (ret < 0)
132 {
133 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
134 }
135 }
136 catch (CcnxOperationException &e)
137 {
138 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800139 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800140 throw e;
141 /*
142 m_connected = false;
143 // probably ccnd has been stopped
144 // try reconnect with sleep
145 int interval = 1;
146 int maxInterval = 32;
147 while (m_running)
148 {
149 try
150 {
151 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
152
153 connectCcnd();
154 _LOG_DEBUG("reconnect to ccnd succeeded");
155 break;
156 }
157 catch (CcnxOperationException &e)
158 {
159 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
160
161 // do exponential backup for reconnect interval
162 if (interval < maxInterval)
163 {
164 interval *= 2;
165 }
166 }
167 }
168 */
169 }
170 catch (const std::exception &exc)
171 {
172 // catch anything thrown within try block that derives from std::exception
173 std::cerr << exc.what();
174 }
175 catch (...)
176 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800177 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800178 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800179 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800180}
181
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800182Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800183CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800185 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
186 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800187 ccn_charbuf *content = ccn_charbuf_create();
188
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800189 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
190 sp.freshness = freshness;
191
192 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800193 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800194 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800195 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800196
197 Bytes bytes;
198 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800199
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800200 ccn_charbuf_destroy (&content);
201
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800202 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800203}
204
205int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800206CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800207{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800208 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800209 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800210 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800211 {
212 _LOG_TRACE ("<< not running or connected");
213 return -1;
214 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800215
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800216
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800217 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800218 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800219 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800220 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
221 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800222 else
223 {
224 _LOG_DEBUG ("<< putToCcnd");
225 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800226
227 return 0;
228}
229
230int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800231CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800232{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800233 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800234 return putToCcnd(co);
235}
236
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800237int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800238CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800239{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800240 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800241}
242
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800243
244static ccn_upcall_res
245incomingInterest(ccn_closure *selfp,
246 ccn_upcall_kind kind,
247 ccn_upcall_info *info)
248{
249 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800250 _LOG_TRACE (">> incomingInterest upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800251
252 switch (kind)
253 {
254 case CCN_UPCALL_FINAL: // effective in unit tests
255 delete f;
256 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800257 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800258 return CCN_UPCALL_RESULT_OK;
259
260 case CCN_UPCALL_INTEREST:
261 break;
262
263 default:
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800264 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265 return CCN_UPCALL_RESULT_OK;
266 }
267
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800268 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800269
270 (*f) (interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800271
272 _LOG_TRACE ("<< incomingInterest");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800273 return CCN_UPCALL_RESULT_OK;
274}
275
276static ccn_upcall_res
277incomingData(ccn_closure *selfp,
278 ccn_upcall_kind kind,
279 ccn_upcall_info *info)
280{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800281 Closure *cp = static_cast<Closure *> (selfp->data);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800282 _LOG_TRACE (">> incomingData upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800283
284 switch (kind)
285 {
286 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800287 delete cp;
288 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800289 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800290 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800291 return CCN_UPCALL_RESULT_OK;
292
293 case CCN_UPCALL_CONTENT:
294 break;
295
296 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800297 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800298 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800299 _LOG_TRACE ("<< incomingData timeout");
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800300 Name interest(info->interest_ccnb, info->interest_comps);
301 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
302 switch(rv)
303 {
304 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
305 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
306 default : break;
307 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800308 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800309 else
310 {
311 _LOG_TRACE ("<< incomingData timeout, but callback is not set...");
312 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800313 return CCN_UPCALL_RESULT_OK;
314 }
315
316 default:
317 return CCN_UPCALL_RESULT_OK;
318 }
319
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800320 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800321
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800322 // const unsigned char *pcontent;
323 // size_t len;
324 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
325 // {
326 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
327 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800328
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800329 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800330
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800331 // Bytes content;
332 // // copy content and do processing on the copy
333 // // otherwise the pointed memory may have been changed during the processing
334 // readRaw(content, pcontent, len);
335
336 cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800337
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 _LOG_TRACE (">> incomingData");
339
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800340 return CCN_UPCALL_RESULT_OK;
341}
342
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800343int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800344{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800345 _LOG_TRACE (">> sendInterest");
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800346 {
347 UniqueRecLock lock(m_mutex);
348 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800349 {
350 _LOG_ERROR ("<< sendInterest: not running or connected");
351 return -1;
352 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800353 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800354
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800355 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
356 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357 ccn_closure *dataClosure = new ccn_closure;
358
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800359 Closure *myClosure = closure.dup();
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800360 dataClosure->data = (void *)myClosure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800361
362 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800363
364 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
365 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800366 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800367 {
368 templ = selectorsPtr->getBuf();
369 }
370
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800371 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800372 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800373 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800374 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800375 }
376
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800377 _LOG_TRACE ("<< sendInterest");
378
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800379 return 0;
380}
381
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800382int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800383{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800384 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800385 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800386 if (!m_running || !m_connected)
387 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800388 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800389
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800390 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
391 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800392 ccn_closure *interestClosure = new ccn_closure;
393
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800394 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
395 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800396
397 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800398 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
399 if (ret < 0)
400 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800401 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800402 }
403
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800404 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800405
406 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800407
408 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800409}
410
411void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800412CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800413{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800414 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800415 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800416 if (!m_running || !m_connected)
417 return;
418
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800419 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
420 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800421
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800422 int ret = ccn_set_interest_filter (m_handle, pname, 0);
423 if (ret < 0)
424 {
425 }
426
427 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800428
429 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800430}
431
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800432Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800433CcnxWrapper::getLocalPrefix ()
434{
435 struct ccn * tmp_handle = ccn_create ();
436 int res = ccn_connect (tmp_handle, NULL);
437 if (res < 0)
438 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800439 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800440 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800441
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800442 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800443
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800444 struct ccn_charbuf *templ = ccn_charbuf_create();
445 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
446 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
447 ccn_charbuf_append_closer(templ); /* </Name> */
448 // XXX - use pubid if possible
449 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
450 ccnb_append_number(templ, 1);
451 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
452 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
453 ccn_charbuf_append_closer(templ); /* </Interest> */
454
455 struct ccn_charbuf *name = ccn_charbuf_create ();
456 res = ccn_name_from_uri (name, "/local/ndn/prefix");
457 if (res < 0) {
458 }
459 else
460 {
461 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800462
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800463 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800464 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800465 if (stream == NULL) {
466 }
467 else
468 {
469 ostringstream os;
470
471 int counter = 0;
472 char buf[256];
473 while (true) {
474 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800475
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800476 if (res == 0) {
477 break;
478 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800479
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800480 if (res > 0) {
481 os << string(buf, res);
482 } else if (res == CCN_FETCH_READ_NONE) {
483 if (counter < 2)
484 {
485 ccn_run(tmp_handle, 1000);
486 counter ++;
487 }
488 else
489 {
490 break;
491 }
492 } else if (res == CCN_FETCH_READ_END) {
493 break;
494 } else if (res == CCN_FETCH_READ_TIMEOUT) {
495 break;
496 } else {
497 break;
498 }
499 }
500 retval = os.str ();
501 stream = ccn_fetch_close(stream);
502 }
503 fetch = ccn_fetch_destroy(fetch);
504 }
505
506 ccn_charbuf_destroy (&name);
507
508 ccn_disconnect (tmp_handle);
509 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800510
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800511 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800512 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800513}
514
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800515}