blob: 0453789e8960a629002dc1111576c687927461cc [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 Zhu1ddeb6f2012-12-27 14:04:18 -0800286
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800287 executor->execute (bind (*f, interest));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800288 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800289 // (*f) (interest);
290 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800291
292 _LOG_TRACE ("<< incomingInterest");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800293 return CCN_UPCALL_RESULT_OK;
294}
295
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800296static void
297deleterInDataTuple (tuple<Closure *, ExecutorPtr> *tuple)
298{
299 delete tuple->get<0> ();
300 delete tuple;
301}
302
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800303static ccn_upcall_res
304incomingData(ccn_closure *selfp,
305 ccn_upcall_kind kind,
306 ccn_upcall_info *info)
307{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800308 // Closure *cp = static_cast<Closure *> (selfp->data);
309 Closure *cp;
310 ExecutorPtr executor;
311 tuple<Closure *, ExecutorPtr> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr>* > (selfp->data);
312 tie (cp, executor) = *realData;
313
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800314 _LOG_TRACE (">> incomingData upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800315
316 switch (kind)
317 {
318 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800319 executor->execute (bind (deleterInDataTuple, realData));
320
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800321 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800322 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800323 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800324 return CCN_UPCALL_RESULT_OK;
325
326 case CCN_UPCALL_CONTENT:
327 break;
328
329 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800330 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800331 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800332 _LOG_TRACE ("<< incomingData timeout");
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800333 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800334 // We can not run timeout callback in executor, because we need the return value
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800335 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
336 switch(rv)
337 {
338 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
339 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
340 default : break;
341 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800343 else
344 {
345 _LOG_TRACE ("<< incomingData timeout, but callback is not set...");
346 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347 return CCN_UPCALL_RESULT_OK;
348 }
349
350 default:
351 return CCN_UPCALL_RESULT_OK;
352 }
353
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800354 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800355
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800356 // const unsigned char *pcontent;
357 // size_t len;
358 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
359 // {
360 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
361 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800362
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800363 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800364
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800365 // Bytes content;
366 // // copy content and do processing on the copy
367 // // otherwise the pointed memory may have been changed during the processing
368 // readRaw(content, pcontent, len);
369
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800370 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800371 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
372 // cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800373
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800374 _LOG_TRACE (">> incomingData");
375
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800376 return CCN_UPCALL_RESULT_OK;
377}
378
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800379int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800380{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800381 _LOG_TRACE (">> sendInterest");
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800382 {
383 UniqueRecLock lock(m_mutex);
384 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800385 {
386 _LOG_ERROR ("<< sendInterest: not running or connected");
387 return -1;
388 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800389 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800390
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800391 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
392 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800393 ccn_closure *dataClosure = new ccn_closure;
394
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800395 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
396 Closure *myClosure = closure.dup ();
397 dataClosure->data = new tuple<Closure*, ExecutorPtr> (myClosure, m_executor);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800398
399 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800400
401 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
402 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800403 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800404 {
405 templ = selectorsPtr->getBuf();
406 }
407
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800408 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800409 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800410 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800411 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800412 }
413
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800414 _LOG_TRACE ("<< sendInterest");
415
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800416 return 0;
417}
418
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800419int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800420{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800421 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800422 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800423 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800424 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800425 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800426 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800427
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800428 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
429 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800430 ccn_closure *interestClosure = new ccn_closure;
431
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800432 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
433
434 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 -0800435 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800436
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800437 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
438 if (ret < 0)
439 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800440 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800441 }
442
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800443 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800444
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800445 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800446
447 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800448}
449
450void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800451CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800452{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800453 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800454 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800455 if (!m_running || !m_connected)
456 return;
457
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800458 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
459 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800460
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800461 int ret = ccn_set_interest_filter (m_handle, pname, 0);
462 if (ret < 0)
463 {
464 }
465
466 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800467
468 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800469}
470
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800471Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800472CcnxWrapper::getLocalPrefix ()
473{
474 struct ccn * tmp_handle = ccn_create ();
475 int res = ccn_connect (tmp_handle, NULL);
476 if (res < 0)
477 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800478 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800479 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800480
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800481 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800482
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800483 struct ccn_charbuf *templ = ccn_charbuf_create();
484 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
485 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
486 ccn_charbuf_append_closer(templ); /* </Name> */
487 // XXX - use pubid if possible
488 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
489 ccnb_append_number(templ, 1);
490 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
491 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
492 ccn_charbuf_append_closer(templ); /* </Interest> */
493
494 struct ccn_charbuf *name = ccn_charbuf_create ();
495 res = ccn_name_from_uri (name, "/local/ndn/prefix");
496 if (res < 0) {
497 }
498 else
499 {
500 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800501
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800502 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800503 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800504 if (stream == NULL) {
505 }
506 else
507 {
508 ostringstream os;
509
510 int counter = 0;
511 char buf[256];
512 while (true) {
513 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800514
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800515 if (res == 0) {
516 break;
517 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800518
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800519 if (res > 0) {
520 os << string(buf, res);
521 } else if (res == CCN_FETCH_READ_NONE) {
522 if (counter < 2)
523 {
524 ccn_run(tmp_handle, 1000);
525 counter ++;
526 }
527 else
528 {
529 break;
530 }
531 } else if (res == CCN_FETCH_READ_END) {
532 break;
533 } else if (res == CCN_FETCH_READ_TIMEOUT) {
534 break;
535 } else {
536 break;
537 }
538 }
539 retval = os.str ();
540 stream = ccn_fetch_close(stream);
541 }
542 fetch = ccn_fetch_destroy(fetch);
543 }
544
545 ccn_charbuf_destroy (&name);
546
547 ccn_disconnect (tmp_handle);
548 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800549
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800550 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800551 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800552}
553
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800554}