blob: f5d20c039038fb92402806782dfa9cf236aaa8a3 [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
34typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
35typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
36
37using namespace std;
38using namespace boost;
39
40namespace Ccnx {
41
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080042CcnxWrapper::CcnxWrapper()
43 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080044 , m_running (true)
45 , m_connected (false)
46{
47 connectCcnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080048 m_thread = thread (&CcnxWrapper::ccnLoop, this);
49}
50
51void
52CcnxWrapper::connectCcnd()
53{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080054 //if (m_handle != 0) {
55 //ccn_disconnect (m_handle);
56 //ccn_destroy (&m_handle);
57 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080058
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080059 m_handle = ccn_create ();
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080060 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080061 if (ccn_connect(m_handle, NULL) < 0)
62 {
63 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
64 }
65 m_connected = true;
66
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080067 //if (!m_registeredInterests.empty())
68 //{
69 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
70 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080071 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080072 // setInterestFilter(it->first, it->second);
73 //}
74 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080075}
76
77CcnxWrapper::~CcnxWrapper()
78{
79 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080080 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080081 m_running = false;
82 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080083
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080084 m_thread.join ();
85 ccn_disconnect (m_handle);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080086 //ccn_destroy (&m_handle);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080087}
88
89void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080090CcnxWrapper::ccnLoop ()
91{
92 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
93 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
94
95 while (m_running)
96 {
97 try
98 {
99 int res = 0;
100 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800101 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800102 res = ccn_run (m_handle, 0);
103 }
104
105 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800106
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800107 if (res < 0)
108 BOOST_THROW_EXCEPTION (CcnxOperationException()
109 << errmsg_info_str("ccn_run returned error"));
110
111
112 pollfd pfds[1];
113 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800114 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800115
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800116 pfds[0].fd = ccn_get_connection_fd (m_handle);
117 pfds[0].events = POLLIN;
118 if (ccn_output_is_pending (m_handle))
119 pfds[0].events |= POLLOUT;
120 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800121
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800122 int ret = poll (pfds, 1, 1);
123 if (ret < 0)
124 {
125 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
126 }
127 }
128 catch (CcnxOperationException &e)
129 {
130 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800131 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800132 throw e;
133 /*
134 m_connected = false;
135 // probably ccnd has been stopped
136 // try reconnect with sleep
137 int interval = 1;
138 int maxInterval = 32;
139 while (m_running)
140 {
141 try
142 {
143 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
144
145 connectCcnd();
146 _LOG_DEBUG("reconnect to ccnd succeeded");
147 break;
148 }
149 catch (CcnxOperationException &e)
150 {
151 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
152
153 // do exponential backup for reconnect interval
154 if (interval < maxInterval)
155 {
156 interval *= 2;
157 }
158 }
159 }
160 */
161 }
162 catch (const std::exception &exc)
163 {
164 // catch anything thrown within try block that derives from std::exception
165 std::cerr << exc.what();
166 }
167 catch (...)
168 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800169 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800170 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800171 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800172}
173
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800174Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800175CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800176{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800177 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
178 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800179 ccn_charbuf *content = ccn_charbuf_create();
180
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800181 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
182 sp.freshness = freshness;
183
184 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800185 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800186 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800187 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800188
189 Bytes bytes;
190 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800191
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800192 ccn_charbuf_destroy (&content);
193
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800194 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800195}
196
197int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800198CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800199{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800200 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800201 if (!m_running || !m_connected)
202 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800203
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800204
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800205 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800206 {
207 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
208 }
209
210 return 0;
211}
212
213int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800214CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800215{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800216 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800217 return putToCcnd(co);
218}
219
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800220int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800221CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800222{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800223 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800224}
225
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800226
227static ccn_upcall_res
228incomingInterest(ccn_closure *selfp,
229 ccn_upcall_kind kind,
230 ccn_upcall_info *info)
231{
232 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
233
234 switch (kind)
235 {
236 case CCN_UPCALL_FINAL: // effective in unit tests
237 delete f;
238 delete selfp;
239 return CCN_UPCALL_RESULT_OK;
240
241 case CCN_UPCALL_INTEREST:
242 break;
243
244 default:
245 return CCN_UPCALL_RESULT_OK;
246 }
247
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800248 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800249
250 (*f) (interest);
251 return CCN_UPCALL_RESULT_OK;
252}
253
254static ccn_upcall_res
255incomingData(ccn_closure *selfp,
256 ccn_upcall_kind kind,
257 ccn_upcall_info *info)
258{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800259 Closure *cp = static_cast<Closure *> (selfp->data);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800260
261 switch (kind)
262 {
263 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800264 delete cp;
265 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800266 delete selfp;
267 return CCN_UPCALL_RESULT_OK;
268
269 case CCN_UPCALL_CONTENT:
270 break;
271
272 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800273 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800274 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800275 Name interest(info->interest_ccnb, info->interest_comps);
276 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
277 switch(rv)
278 {
279 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
280 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
281 default : break;
282 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800283 }
284 return CCN_UPCALL_RESULT_OK;
285 }
286
287 default:
288 return CCN_UPCALL_RESULT_OK;
289 }
290
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800291 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800292
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800293 // const unsigned char *pcontent;
294 // size_t len;
295 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
296 // {
297 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
298 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800299
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800300 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800301
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800302 // Bytes content;
303 // // copy content and do processing on the copy
304 // // otherwise the pointed memory may have been changed during the processing
305 // readRaw(content, pcontent, len);
306
307 cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800308
309 return CCN_UPCALL_RESULT_OK;
310}
311
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800312int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800313{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800314 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800315 if (!m_running || !m_connected)
316 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800317
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800318 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
319 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800320 ccn_closure *dataClosure = new ccn_closure;
321
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800322 Closure *myClosure = closure.dup();
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800323 dataClosure->data = (void *)myClosure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800324
325 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800326
327 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
328 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800329 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800330 {
331 templ = selectorsPtr->getBuf();
332 }
333
334 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800335 {
336 }
337
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800338 return 0;
339}
340
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800341int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800343 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800344 if (!m_running || !m_connected)
345 return -1;
346
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800347 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
348 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800349 ccn_closure *interestClosure = new ccn_closure;
350
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800351 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
352 interestClosure->p = &incomingInterest;
353 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
354 if (ret < 0)
355 {
356 }
357
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800358 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800359
360 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800361}
362
363void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800364CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800365{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800366 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800367 if (!m_running || !m_connected)
368 return;
369
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800370 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
371 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800372
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800373 int ret = ccn_set_interest_filter (m_handle, pname, 0);
374 if (ret < 0)
375 {
376 }
377
378 m_registeredInterests.erase(prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800379}
380
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800381Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800382CcnxWrapper::getLocalPrefix ()
383{
384 struct ccn * tmp_handle = ccn_create ();
385 int res = ccn_connect (tmp_handle, NULL);
386 if (res < 0)
387 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800388 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800389 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800390
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800391 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800392
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800393 struct ccn_charbuf *templ = ccn_charbuf_create();
394 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
395 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
396 ccn_charbuf_append_closer(templ); /* </Name> */
397 // XXX - use pubid if possible
398 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
399 ccnb_append_number(templ, 1);
400 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
401 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
402 ccn_charbuf_append_closer(templ); /* </Interest> */
403
404 struct ccn_charbuf *name = ccn_charbuf_create ();
405 res = ccn_name_from_uri (name, "/local/ndn/prefix");
406 if (res < 0) {
407 }
408 else
409 {
410 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800411
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800412 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800413 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800414 if (stream == NULL) {
415 }
416 else
417 {
418 ostringstream os;
419
420 int counter = 0;
421 char buf[256];
422 while (true) {
423 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800424
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800425 if (res == 0) {
426 break;
427 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800428
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800429 if (res > 0) {
430 os << string(buf, res);
431 } else if (res == CCN_FETCH_READ_NONE) {
432 if (counter < 2)
433 {
434 ccn_run(tmp_handle, 1000);
435 counter ++;
436 }
437 else
438 {
439 break;
440 }
441 } else if (res == CCN_FETCH_READ_END) {
442 break;
443 } else if (res == CCN_FETCH_READ_TIMEOUT) {
444 break;
445 } else {
446 break;
447 }
448 }
449 retval = os.str ();
450 stream = ccn_fetch_close(stream);
451 }
452 fetch = ccn_fetch_destroy(fetch);
453 }
454
455 ccn_charbuf_destroy (&name);
456
457 ccn_disconnect (tmp_handle);
458 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800459
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800460 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800461 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800462}
463
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800464}