blob: d1c4672416240dca4a8c7adc99235b37f0831093 [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
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265 switch (kind)
266 {
267 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800268 // delete closure;
269 executor->execute (bind (deleterInInterestTuple, realData));
270
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800271 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800272 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800273 return CCN_UPCALL_RESULT_OK;
274
275 case CCN_UPCALL_INTEREST:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800276 _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800277 break;
278
279 default:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800280 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800281 return CCN_UPCALL_RESULT_OK;
282 }
283
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800284 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800285 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800286
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800287 executor->execute (bind (*f, interest, selectors));
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
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800292 return CCN_UPCALL_RESULT_OK;
293}
294
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800295static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800296deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800297{
298 delete tuple->get<0> ();
299 delete tuple;
300}
301
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800302static ccn_upcall_res
303incomingData(ccn_closure *selfp,
304 ccn_upcall_kind kind,
305 ccn_upcall_info *info)
306{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800307 // Closure *cp = static_cast<Closure *> (selfp->data);
308 Closure *cp;
309 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800310 Selectors selectors;
311 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
312 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800313
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800314 switch (kind)
315 {
316 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800317 executor->execute (bind (deleterInDataTuple, realData));
318
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800319 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800320 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800321 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800322 return CCN_UPCALL_RESULT_OK;
323
324 case CCN_UPCALL_CONTENT:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800325 _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800326 break;
327
328 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800329 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800330 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800331 Name interest(info->interest_ccnb, info->interest_comps);
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800332 _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps));
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800333 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800335 else
336 {
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800337 _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800339 return CCN_UPCALL_RESULT_OK;
340 }
341
342 default:
343 return CCN_UPCALL_RESULT_OK;
344 }
345
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800346 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800348 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800349 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800350 _LOG_TRACE (">> incomingData");
351
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800352 return CCN_UPCALL_RESULT_OK;
353}
354
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800355int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800356{
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800357 _LOG_TRACE (">> sendInterest: " << interest);
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800358 {
359 UniqueRecLock lock(m_mutex);
360 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800361 {
362 _LOG_ERROR ("<< sendInterest: not running or connected");
363 return -1;
364 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800365 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800366
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800367 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
368 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800369 ccn_closure *dataClosure = new ccn_closure;
370
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800371 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
372 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800373 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800374
375 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800376
377 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
378 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800379 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800380 {
381 templ = selectorsPtr->getBuf();
382 }
383
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800384 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800385 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800386 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800387 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800388 }
389
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800390 return 0;
391}
392
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800393int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800394{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800395 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800396 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800397 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800398 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800399 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800400 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800401
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800402 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
403 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800404 ccn_closure *interestClosure = new ccn_closure;
405
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800406 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
407
408 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 -0800409 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800410
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800411 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
412 if (ret < 0)
413 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800414 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800415 }
416
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800417 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800418
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800419 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800420
421 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800422}
423
424void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800425CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800426{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800427 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800428 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800429 if (!m_running || !m_connected)
430 return;
431
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800432 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
433 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800434
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800435 int ret = ccn_set_interest_filter (m_handle, pname, 0);
436 if (ret < 0)
437 {
438 }
439
440 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800441
442 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800443}
444
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800445Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800446CcnxWrapper::getLocalPrefix ()
447{
448 struct ccn * tmp_handle = ccn_create ();
449 int res = ccn_connect (tmp_handle, NULL);
450 if (res < 0)
451 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800452 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800453 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800454
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800455 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800456
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800457 struct ccn_charbuf *templ = ccn_charbuf_create();
458 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
459 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
460 ccn_charbuf_append_closer(templ); /* </Name> */
461 // XXX - use pubid if possible
462 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
463 ccnb_append_number(templ, 1);
464 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
465 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
466 ccn_charbuf_append_closer(templ); /* </Interest> */
467
468 struct ccn_charbuf *name = ccn_charbuf_create ();
469 res = ccn_name_from_uri (name, "/local/ndn/prefix");
470 if (res < 0) {
471 }
472 else
473 {
474 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800475
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800476 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800477 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800478 if (stream == NULL) {
479 }
480 else
481 {
482 ostringstream os;
483
484 int counter = 0;
485 char buf[256];
486 while (true) {
487 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800488
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800489 if (res == 0) {
490 break;
491 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800492
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800493 if (res > 0) {
494 os << string(buf, res);
495 } else if (res == CCN_FETCH_READ_NONE) {
496 if (counter < 2)
497 {
498 ccn_run(tmp_handle, 1000);
499 counter ++;
500 }
501 else
502 {
503 break;
504 }
505 } else if (res == CCN_FETCH_READ_END) {
506 break;
507 } else if (res == CCN_FETCH_READ_TIMEOUT) {
508 break;
509 } else {
510 break;
511 }
512 }
513 retval = os.str ();
514 stream = ccn_fetch_close(stream);
515 }
516 fetch = ccn_fetch_destroy(fetch);
517 }
518
519 ccn_charbuf_destroy (&name);
520
521 ccn_disconnect (tmp_handle);
522 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800523
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800524 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800525 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800526}
527
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800528}