blob: 2867ecc0c09c4b8cc2400fe6ecd236d9603da542 [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{
52 connectCcnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080053 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Zhenkai Zhu1888f742013-01-28 12:47:33 -080054 m_executor->start();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080055}
56
57void
58CcnxWrapper::connectCcnd()
59{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080060 //if (m_handle != 0) {
61 //ccn_disconnect (m_handle);
62 //ccn_destroy (&m_handle);
63 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080064
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065 m_handle = ccn_create ();
Zhenkai Zhu7599db42013-01-28 10:32:53 -080066 //UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080067 if (ccn_connect(m_handle, NULL) < 0)
68 {
69 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
70 }
71 m_connected = true;
72
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080073 //if (!m_registeredInterests.empty())
74 //{
75 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
76 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080077 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080078 // setInterestFilter(it->first, it->second);
79 //}
80 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080081}
82
83CcnxWrapper::~CcnxWrapper()
84{
85 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080086 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080087 m_running = false;
88 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080089
Zhenkai Zhu1888f742013-01-28 12:47:33 -080090 m_executor->shutdown();
91
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080092 m_thread.join ();
93 ccn_disconnect (m_handle);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080094 //ccn_destroy (&m_handle);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080095}
96
97void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080098CcnxWrapper::ccnLoop ()
99{
100 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
101 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
102
103 while (m_running)
104 {
105 try
106 {
107 int res = 0;
108 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800109 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800110 res = ccn_run (m_handle, 0);
111 }
112
113 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800114
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800115 if (res < 0) {
116 _LOG_ERROR ("ccn_run returned negative status: " << res);
117 usleep (100000);
118 continue;
119 // BOOST_THROW_EXCEPTION (CcnxOperationException()
120 // << errmsg_info_str("ccn_run returned error"));
121 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800122
123
124 pollfd pfds[1];
125 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800126 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800127
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128 pfds[0].fd = ccn_get_connection_fd (m_handle);
129 pfds[0].events = POLLIN;
130 if (ccn_output_is_pending (m_handle))
131 pfds[0].events |= POLLOUT;
132 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800133
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800134 int ret = poll (pfds, 1, 1);
135 if (ret < 0)
136 {
137 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
138 }
139 }
140 catch (CcnxOperationException &e)
141 {
142 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800143 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800144 throw e;
145 /*
146 m_connected = false;
147 // probably ccnd has been stopped
148 // try reconnect with sleep
149 int interval = 1;
150 int maxInterval = 32;
151 while (m_running)
152 {
153 try
154 {
155 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
156
157 connectCcnd();
158 _LOG_DEBUG("reconnect to ccnd succeeded");
159 break;
160 }
161 catch (CcnxOperationException &e)
162 {
163 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
164
165 // do exponential backup for reconnect interval
166 if (interval < maxInterval)
167 {
168 interval *= 2;
169 }
170 }
171 }
172 */
173 }
174 catch (const std::exception &exc)
175 {
176 // catch anything thrown within try block that derives from std::exception
177 std::cerr << exc.what();
178 }
179 catch (...)
180 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800181 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800182 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800183 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184}
185
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800186Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800187CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800188{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800189 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
190 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800191 ccn_charbuf *content = ccn_charbuf_create();
192
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800193 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
194 sp.freshness = freshness;
195
196 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800197 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800198 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800199 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800200
201 Bytes bytes;
202 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800203
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800204 ccn_charbuf_destroy (&content);
205
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800206 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800207}
208
209int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800210CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800211{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800212 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800213 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800214 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800215 {
216 _LOG_TRACE ("<< not running or connected");
217 return -1;
218 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800219
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800220
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800221 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800222 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800223 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800224 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
225 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800226 else
227 {
228 _LOG_DEBUG ("<< putToCcnd");
229 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800230
231 return 0;
232}
233
234int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800235CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800236{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800237 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800238 return putToCcnd(co);
239}
240
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800241int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800242CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800243{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800244 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800245}
246
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800247
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800248static void
249deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple)
250{
251 delete tuple->get<0> ();
252 delete tuple;
253}
254
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800255static ccn_upcall_res
256incomingInterest(ccn_closure *selfp,
257 ccn_upcall_kind kind,
258 ccn_upcall_info *info)
259{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800260 CcnxWrapper::InterestCallback *f;
261 ExecutorPtr executor;
262 tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data);
263 tie (f, executor) = *realData;
264
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800265 _LOG_TRACE (">> incomingInterest upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800266
267 switch (kind)
268 {
269 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800270 // delete closure;
271 executor->execute (bind (deleterInInterestTuple, realData));
272
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800273 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800274 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800275 return CCN_UPCALL_RESULT_OK;
276
277 case CCN_UPCALL_INTEREST:
278 break;
279
280 default:
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800281 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800282 return CCN_UPCALL_RESULT_OK;
283 }
284
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800285 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800286 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800287
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800288 executor->execute (bind (*f, interest, selectors));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800289 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800290 // (*f) (interest);
291 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800292
293 _LOG_TRACE ("<< incomingInterest");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800294 return CCN_UPCALL_RESULT_OK;
295}
296
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800297static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800298deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800299{
300 delete tuple->get<0> ();
301 delete tuple;
302}
303
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800304static ccn_upcall_res
305incomingData(ccn_closure *selfp,
306 ccn_upcall_kind kind,
307 ccn_upcall_info *info)
308{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800309 // Closure *cp = static_cast<Closure *> (selfp->data);
310 Closure *cp;
311 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800312 Selectors selectors;
313 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
314 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800315
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800316 _LOG_TRACE (">> incomingData upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800317
318 switch (kind)
319 {
320 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800321 executor->execute (bind (deleterInDataTuple, realData));
322
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800323 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800324 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800325 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800326 return CCN_UPCALL_RESULT_OK;
327
328 case CCN_UPCALL_CONTENT:
329 break;
330
331 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800332 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800333 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800334 _LOG_TRACE ("<< incomingData timeout");
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800335 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800336 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800337 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 else
339 {
340 _LOG_TRACE ("<< incomingData timeout, but callback is not set...");
341 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342 return CCN_UPCALL_RESULT_OK;
343 }
344
345 default:
346 return CCN_UPCALL_RESULT_OK;
347 }
348
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800349 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800350
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800351 // const unsigned char *pcontent;
352 // size_t len;
353 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
354 // {
355 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
356 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800358 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800359
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800360 // Bytes content;
361 // // copy content and do processing on the copy
362 // // otherwise the pointed memory may have been changed during the processing
363 // readRaw(content, pcontent, len);
364
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800365 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800366 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
367 // cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800368
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800369 _LOG_TRACE (">> incomingData");
370
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800371 return CCN_UPCALL_RESULT_OK;
372}
373
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800374int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800375{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800376 _LOG_TRACE (">> sendInterest");
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800377 {
378 UniqueRecLock lock(m_mutex);
379 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800380 {
381 _LOG_ERROR ("<< sendInterest: not running or connected");
382 return -1;
383 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800384 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800385
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800386 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
387 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800388 ccn_closure *dataClosure = new ccn_closure;
389
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800390 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
391 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800392 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800393
394 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800395
396 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
397 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800398 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800399 {
400 templ = selectorsPtr->getBuf();
401 }
402
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800403 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800404 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800405 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800406 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800407 }
408
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800409 _LOG_TRACE ("<< sendInterest");
410
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800411 return 0;
412}
413
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800414int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800415{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800416 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800417 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800418 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800419 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800420 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800421 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800422
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800423 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
424 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800425 ccn_closure *interestClosure = new ccn_closure;
426
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800427 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
428
429 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 -0800430 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800431
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800432 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
433 if (ret < 0)
434 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800435 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800436 }
437
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800438 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800439
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800440 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800441
442 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800443}
444
445void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800446CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800447{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800448 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800449 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800450 if (!m_running || !m_connected)
451 return;
452
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800453 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
454 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800455
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800456 int ret = ccn_set_interest_filter (m_handle, pname, 0);
457 if (ret < 0)
458 {
459 }
460
461 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800462
463 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800464}
465
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800466Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800467CcnxWrapper::getLocalPrefix ()
468{
469 struct ccn * tmp_handle = ccn_create ();
470 int res = ccn_connect (tmp_handle, NULL);
471 if (res < 0)
472 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800473 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800474 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800475
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800476 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800477
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800478 struct ccn_charbuf *templ = ccn_charbuf_create();
479 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
480 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
481 ccn_charbuf_append_closer(templ); /* </Name> */
482 // XXX - use pubid if possible
483 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
484 ccnb_append_number(templ, 1);
485 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
486 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
487 ccn_charbuf_append_closer(templ); /* </Interest> */
488
489 struct ccn_charbuf *name = ccn_charbuf_create ();
490 res = ccn_name_from_uri (name, "/local/ndn/prefix");
491 if (res < 0) {
492 }
493 else
494 {
495 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800496
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800497 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800498 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800499 if (stream == NULL) {
500 }
501 else
502 {
503 ostringstream os;
504
505 int counter = 0;
506 char buf[256];
507 while (true) {
508 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800509
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800510 if (res == 0) {
511 break;
512 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800513
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800514 if (res > 0) {
515 os << string(buf, res);
516 } else if (res == CCN_FETCH_READ_NONE) {
517 if (counter < 2)
518 {
519 ccn_run(tmp_handle, 1000);
520 counter ++;
521 }
522 else
523 {
524 break;
525 }
526 } else if (res == CCN_FETCH_READ_END) {
527 break;
528 } else if (res == CCN_FETCH_READ_TIMEOUT) {
529 break;
530 } else {
531 break;
532 }
533 }
534 retval = os.str ();
535 stream = ccn_fetch_close(stream);
536 }
537 fetch = ccn_fetch_destroy(fetch);
538 }
539
540 ccn_charbuf_destroy (&name);
541
542 ccn_disconnect (tmp_handle);
543 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800544
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800545 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800546 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800547}
548
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800549}