blob: 5a6cfd30f7ef2086336f4bb3677b59c17f77fed5 [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)
Zhenkai Zhu1888f742013-01-28 12:47:33 -080050 , m_executor (new Executor(1))
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080051{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080052 start ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080053}
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{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080083 shutdown ();
84}
85
86void
87CcnxWrapper::start () // called automatically in constructor
88{
89 connectCcnd();
90 m_thread = thread (&CcnxWrapper::ccnLoop, this);
91 m_executor->start();
92}
93
94void
95CcnxWrapper::shutdown () // called in destructor, but can called manually
96{
97 m_executor->shutdown();
98
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080099 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800100 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800101 m_running = false;
102 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800103
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800104 _LOG_DEBUG ("+++++++++SHUTDOWN+++++++");
105 if (m_connected)
106 {
107 m_thread.join ();
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800108
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800109 ccn_disconnect (m_handle);
110 //ccn_destroy (&m_handle);
111 m_connected = false;
112 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800113}
114
115void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800116CcnxWrapper::ccnLoop ()
117{
118 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
119 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
120
121 while (m_running)
122 {
123 try
124 {
125 int res = 0;
126 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800127 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128 res = ccn_run (m_handle, 0);
129 }
130
131 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800132
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800133 if (res < 0) {
134 _LOG_ERROR ("ccn_run returned negative status: " << res);
135 usleep (100000);
136 continue;
137 // BOOST_THROW_EXCEPTION (CcnxOperationException()
138 // << errmsg_info_str("ccn_run returned error"));
139 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800140
141
142 pollfd pfds[1];
143 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800144 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800145
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800146 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 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800151
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800152 int ret = poll (pfds, 1, 1);
153 if (ret < 0)
154 {
155 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
156 }
157 }
158 catch (CcnxOperationException &e)
159 {
160 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800161 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800162 throw e;
163 /*
164 m_connected = false;
165 // probably ccnd has been stopped
166 // try reconnect with sleep
167 int interval = 1;
168 int maxInterval = 32;
169 while (m_running)
170 {
171 try
172 {
173 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
174
175 connectCcnd();
176 _LOG_DEBUG("reconnect to ccnd succeeded");
177 break;
178 }
179 catch (CcnxOperationException &e)
180 {
181 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
182
183 // do exponential backup for reconnect interval
184 if (interval < maxInterval)
185 {
186 interval *= 2;
187 }
188 }
189 }
190 */
191 }
192 catch (const std::exception &exc)
193 {
194 // catch anything thrown within try block that derives from std::exception
195 std::cerr << exc.what();
196 }
197 catch (...)
198 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800199 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800200 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800201 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800202}
203
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800204Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800205CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800206{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800207 {
208 UniqueRecLock lock(m_mutex);
209 if (!m_running || !m_connected)
210 {
211 _LOG_TRACE ("<< not running or connected");
212 return Bytes ();
213 }
214 }
215
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800216 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
217 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800218 ccn_charbuf *content = ccn_charbuf_create();
219
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800220 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
221 sp.freshness = freshness;
222
223 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800224 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800225 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800226 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800227
228 Bytes bytes;
229 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800230
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800231 ccn_charbuf_destroy (&content);
232
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800233 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800234}
235
236int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800237CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800238{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800239 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800240 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800241 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800242 {
243 _LOG_TRACE ("<< not running or connected");
244 return -1;
245 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800246
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800247
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800248 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800249 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800250 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800251 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
252 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800253 else
254 {
255 _LOG_DEBUG ("<< putToCcnd");
256 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800257
258 return 0;
259}
260
261int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800262CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800263{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800264 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265 return putToCcnd(co);
266}
267
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800268int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800269CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800270{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800271 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800272}
273
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800274
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800275static void
276deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple)
277{
278 delete tuple->get<0> ();
279 delete tuple;
280}
281
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800282static ccn_upcall_res
283incomingInterest(ccn_closure *selfp,
284 ccn_upcall_kind kind,
285 ccn_upcall_info *info)
286{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800287 CcnxWrapper::InterestCallback *f;
288 ExecutorPtr executor;
289 tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data);
290 tie (f, executor) = *realData;
291
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800292 switch (kind)
293 {
294 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800295 // delete closure;
296 executor->execute (bind (deleterInInterestTuple, realData));
297
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800298 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800299 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800300 return CCN_UPCALL_RESULT_OK;
301
302 case CCN_UPCALL_INTEREST:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800303 _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800304 break;
305
306 default:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800307 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800308 return CCN_UPCALL_RESULT_OK;
309 }
310
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800311 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800312 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800313
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800314 executor->execute (bind (*f, interest, selectors));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800315 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800316 // (*f) (interest);
317 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800318
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800319 return CCN_UPCALL_RESULT_OK;
320}
321
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800322static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800323deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800324{
325 delete tuple->get<0> ();
326 delete tuple;
327}
328
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800329static ccn_upcall_res
330incomingData(ccn_closure *selfp,
331 ccn_upcall_kind kind,
332 ccn_upcall_info *info)
333{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800334 // Closure *cp = static_cast<Closure *> (selfp->data);
335 Closure *cp;
336 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800337 Selectors selectors;
338 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
339 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800340
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800341 switch (kind)
342 {
343 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800344 executor->execute (bind (deleterInDataTuple, realData));
345
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800346 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800348 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800349 return CCN_UPCALL_RESULT_OK;
350
351 case CCN_UPCALL_CONTENT:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800352 _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800353 break;
354
355 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800356 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800358 Name interest(info->interest_ccnb, info->interest_comps);
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800359 _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps));
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800360 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800361 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800362 else
363 {
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800364 _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800365 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800366 return CCN_UPCALL_RESULT_OK;
367 }
368
369 default:
370 return CCN_UPCALL_RESULT_OK;
371 }
372
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800373 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800374
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800375 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800376 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800377 _LOG_TRACE (">> incomingData");
378
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800379 return CCN_UPCALL_RESULT_OK;
380}
381
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800382int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800383{
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800384 _LOG_TRACE (">> sendInterest: " << interest);
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800385 {
386 UniqueRecLock lock(m_mutex);
387 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800388 {
389 _LOG_ERROR ("<< sendInterest: not running or connected");
390 return -1;
391 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800392 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800393
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800394 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
395 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800396 ccn_closure *dataClosure = new ccn_closure;
397
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800398 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
399 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800400 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800401
402 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800403
404 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
405 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800406 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800407 {
408 templ = selectorsPtr->getBuf();
409 }
410
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800411 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800412 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800413 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800414 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800415 }
416
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800417 return 0;
418}
419
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800420int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800421{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800422 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800423 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800424 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800425 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800426 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800427 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800428
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800429 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
430 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800431 ccn_closure *interestClosure = new ccn_closure;
432
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800433 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
434
435 interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800436 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800437
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800438 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
439 if (ret < 0)
440 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800441 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800442 }
443
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800444 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800445
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800446 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800447
448 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800449}
450
451void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800452CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800453{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800454 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800455 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800456 if (!m_running || !m_connected)
457 return;
458
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800459 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
460 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800461
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800462 int ret = ccn_set_interest_filter (m_handle, pname, 0);
463 if (ret < 0)
464 {
465 }
466
467 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800468
469 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800470}
471
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800472Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800473CcnxWrapper::getLocalPrefix ()
474{
475 struct ccn * tmp_handle = ccn_create ();
476 int res = ccn_connect (tmp_handle, NULL);
477 if (res < 0)
478 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800479 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800480 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800481
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800482 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800483
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800484 struct ccn_charbuf *templ = ccn_charbuf_create();
485 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
486 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
487 ccn_charbuf_append_closer(templ); /* </Name> */
488 // XXX - use pubid if possible
489 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
490 ccnb_append_number(templ, 1);
491 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
492 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
493 ccn_charbuf_append_closer(templ); /* </Interest> */
494
495 struct ccn_charbuf *name = ccn_charbuf_create ();
496 res = ccn_name_from_uri (name, "/local/ndn/prefix");
497 if (res < 0) {
498 }
499 else
500 {
501 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800502
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800503 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800504 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800505 if (stream == NULL) {
506 }
507 else
508 {
509 ostringstream os;
510
511 int counter = 0;
512 char buf[256];
513 while (true) {
514 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800515
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800516 if (res == 0) {
517 break;
518 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800519
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800520 if (res > 0) {
521 os << string(buf, res);
522 } else if (res == CCN_FETCH_READ_NONE) {
523 if (counter < 2)
524 {
525 ccn_run(tmp_handle, 1000);
526 counter ++;
527 }
528 else
529 {
530 break;
531 }
532 } else if (res == CCN_FETCH_READ_END) {
533 break;
534 } else if (res == CCN_FETCH_READ_TIMEOUT) {
535 break;
536 } else {
537 break;
538 }
539 }
540 retval = os.str ();
541 stream = ccn_fetch_close(stream);
542 }
543 fetch = ccn_fetch_destroy(fetch);
544 }
545
546 ccn_charbuf_destroy (&name);
547
548 ccn_disconnect (tmp_handle);
549 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800550
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800551 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800552 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800553}
554
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800555}