blob: 43c97808c5680e87dcc85f93d5422afedd64a76b [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
248static ccn_upcall_res
249incomingInterest(ccn_closure *selfp,
250 ccn_upcall_kind kind,
251 ccn_upcall_info *info)
252{
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800253 ExecutorInterestClosure *closure = static_cast<ExecutorInterestClosure*> (selfp->data);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800254 _LOG_TRACE (">> incomingInterest upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800255
256 switch (kind)
257 {
258 case CCN_UPCALL_FINAL: // effective in unit tests
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800259 delete closure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800260 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800261 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800262 return CCN_UPCALL_RESULT_OK;
263
264 case CCN_UPCALL_INTEREST:
265 break;
266
267 default:
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800268 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800269 return CCN_UPCALL_RESULT_OK;
270 }
271
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800272 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800273
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800274 // this will be run in executor
275 closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800276
277 _LOG_TRACE ("<< incomingInterest");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800278 return CCN_UPCALL_RESULT_OK;
279}
280
281static ccn_upcall_res
282incomingData(ccn_closure *selfp,
283 ccn_upcall_kind kind,
284 ccn_upcall_info *info)
285{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800286 Closure *cp = static_cast<Closure *> (selfp->data);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800287 _LOG_TRACE (">> incomingData upcall");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800288
289 switch (kind)
290 {
291 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800292 delete cp;
293 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800294 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800295 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800296 return CCN_UPCALL_RESULT_OK;
297
298 case CCN_UPCALL_CONTENT:
299 break;
300
301 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800302 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800303 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800304 _LOG_TRACE ("<< incomingData timeout");
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800305 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800306 // We can not run timeout callback in executor, because we need the return value
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800307 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
308 switch(rv)
309 {
310 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
311 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
312 default : break;
313 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800314 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800315 else
316 {
317 _LOG_TRACE ("<< incomingData timeout, but callback is not set...");
318 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800319 return CCN_UPCALL_RESULT_OK;
320 }
321
322 default:
323 return CCN_UPCALL_RESULT_OK;
324 }
325
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800326 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800327
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800328 // const unsigned char *pcontent;
329 // size_t len;
330 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
331 // {
332 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
333 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800335 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800336
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800337 // Bytes content;
338 // // copy content and do processing on the copy
339 // // otherwise the pointed memory may have been changed during the processing
340 // readRaw(content, pcontent, len);
341
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800342 // this will be run in executor
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800343 cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800344
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800345 _LOG_TRACE (">> incomingData");
346
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347 return CCN_UPCALL_RESULT_OK;
348}
349
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800350int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800351{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800352 _LOG_TRACE (">> sendInterest");
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800353 {
354 UniqueRecLock lock(m_mutex);
355 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800356 {
357 _LOG_ERROR ("<< sendInterest: not running or connected");
358 return -1;
359 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800360 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800361
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800362 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
363 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800364 ccn_closure *dataClosure = new ccn_closure;
365
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800366 Closure *myClosure = new ExecutorClosure(closure, m_executor);
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800367 dataClosure->data = (void *)myClosure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800368
369 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800370
371 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
372 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800373 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800374 {
375 templ = selectorsPtr->getBuf();
376 }
377
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800378 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800379 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800380 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800381 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800382 }
383
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800384 _LOG_TRACE ("<< sendInterest");
385
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800386 return 0;
387}
388
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800389int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800390{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800391 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800392 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800393 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800394 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800395 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800396 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800397
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800398 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
399 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800400 ccn_closure *interestClosure = new ccn_closure;
401
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800402 interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800403 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800404
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800405 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
406 if (ret < 0)
407 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800408 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800409 }
410
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800411 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800412
413 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800414
415 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800416}
417
418void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800419CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800420{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800421 _LOG_TRACE (">> clearInterestFilter");
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)
424 return;
425
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800426 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
427 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800428
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800429 int ret = ccn_set_interest_filter (m_handle, pname, 0);
430 if (ret < 0)
431 {
432 }
433
434 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800435
436 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800437}
438
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800439Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800440CcnxWrapper::getLocalPrefix ()
441{
442 struct ccn * tmp_handle = ccn_create ();
443 int res = ccn_connect (tmp_handle, NULL);
444 if (res < 0)
445 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800446 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800447 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800448
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800449 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800450
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800451 struct ccn_charbuf *templ = ccn_charbuf_create();
452 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
453 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
454 ccn_charbuf_append_closer(templ); /* </Name> */
455 // XXX - use pubid if possible
456 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
457 ccnb_append_number(templ, 1);
458 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
459 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
460 ccn_charbuf_append_closer(templ); /* </Interest> */
461
462 struct ccn_charbuf *name = ccn_charbuf_create ();
463 res = ccn_name_from_uri (name, "/local/ndn/prefix");
464 if (res < 0) {
465 }
466 else
467 {
468 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800469
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800470 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800471 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800472 if (stream == NULL) {
473 }
474 else
475 {
476 ostringstream os;
477
478 int counter = 0;
479 char buf[256];
480 while (true) {
481 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800482
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800483 if (res == 0) {
484 break;
485 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800486
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800487 if (res > 0) {
488 os << string(buf, res);
489 } else if (res == CCN_FETCH_READ_NONE) {
490 if (counter < 2)
491 {
492 ccn_run(tmp_handle, 1000);
493 counter ++;
494 }
495 else
496 {
497 break;
498 }
499 } else if (res == CCN_FETCH_READ_END) {
500 break;
501 } else if (res == CCN_FETCH_READ_TIMEOUT) {
502 break;
503 } else {
504 break;
505 }
506 }
507 retval = os.str ();
508 stream = ccn_fetch_close(stream);
509 }
510 fetch = ccn_fetch_destroy(fetch);
511 }
512
513 ccn_charbuf_destroy (&name);
514
515 ccn_disconnect (tmp_handle);
516 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800517
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800518 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800519 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800520}
521
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800522}