blob: 697901ca1a9cb5c10dea674c1659bb496f7f8638 [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)
50{
51 connectCcnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080052 m_thread = thread (&CcnxWrapper::ccnLoop, this);
53}
54
55void
56CcnxWrapper::connectCcnd()
57{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080058 //if (m_handle != 0) {
59 //ccn_disconnect (m_handle);
60 //ccn_destroy (&m_handle);
61 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080062
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080063 m_handle = ccn_create ();
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080064 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065 if (ccn_connect(m_handle, NULL) < 0)
66 {
67 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
68 }
69 m_connected = true;
70
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080071 //if (!m_registeredInterests.empty())
72 //{
73 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
74 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080075 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080076 // setInterestFilter(it->first, it->second);
77 //}
78 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080079}
80
81CcnxWrapper::~CcnxWrapper()
82{
83 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080084 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080085 m_running = false;
86 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080087
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080088 m_thread.join ();
89 ccn_disconnect (m_handle);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080090 //ccn_destroy (&m_handle);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080091}
92
93void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080094CcnxWrapper::ccnLoop ()
95{
96 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
97 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
98
99 while (m_running)
100 {
101 try
102 {
103 int res = 0;
104 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800105 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800106 res = ccn_run (m_handle, 0);
107 }
108
109 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800110
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800111 if (res < 0) {
112 _LOG_ERROR ("ccn_run returned negative status: " << res);
113 usleep (100000);
114 continue;
115 // BOOST_THROW_EXCEPTION (CcnxOperationException()
116 // << errmsg_info_str("ccn_run returned error"));
117 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800118
119
120 pollfd pfds[1];
121 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800122 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800123
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800124 pfds[0].fd = ccn_get_connection_fd (m_handle);
125 pfds[0].events = POLLIN;
126 if (ccn_output_is_pending (m_handle))
127 pfds[0].events |= POLLOUT;
128 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800129
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800130 int ret = poll (pfds, 1, 1);
131 if (ret < 0)
132 {
133 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
134 }
135 }
136 catch (CcnxOperationException &e)
137 {
138 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800139 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800140 throw e;
141 /*
142 m_connected = false;
143 // probably ccnd has been stopped
144 // try reconnect with sleep
145 int interval = 1;
146 int maxInterval = 32;
147 while (m_running)
148 {
149 try
150 {
151 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
152
153 connectCcnd();
154 _LOG_DEBUG("reconnect to ccnd succeeded");
155 break;
156 }
157 catch (CcnxOperationException &e)
158 {
159 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
160
161 // do exponential backup for reconnect interval
162 if (interval < maxInterval)
163 {
164 interval *= 2;
165 }
166 }
167 }
168 */
169 }
170 catch (const std::exception &exc)
171 {
172 // catch anything thrown within try block that derives from std::exception
173 std::cerr << exc.what();
174 }
175 catch (...)
176 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800177 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800178 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800179 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800180}
181
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800182Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800183CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800185 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
186 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800187 ccn_charbuf *content = ccn_charbuf_create();
188
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800189 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
190 sp.freshness = freshness;
191
192 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800193 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800194 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800195 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800196
197 Bytes bytes;
198 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800199
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800200 ccn_charbuf_destroy (&content);
201
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800202 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800203}
204
205int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800206CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800207{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800208 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800209 if (!m_running || !m_connected)
210 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800211
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800212
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800213 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800214 {
215 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
216 }
217
218 return 0;
219}
220
221int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800222CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800223{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800224 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800225 return putToCcnd(co);
226}
227
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800228int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800229CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800230{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800231 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800232}
233
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800234
235static ccn_upcall_res
236incomingInterest(ccn_closure *selfp,
237 ccn_upcall_kind kind,
238 ccn_upcall_info *info)
239{
240 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
241
242 switch (kind)
243 {
244 case CCN_UPCALL_FINAL: // effective in unit tests
245 delete f;
246 delete selfp;
247 return CCN_UPCALL_RESULT_OK;
248
249 case CCN_UPCALL_INTEREST:
250 break;
251
252 default:
253 return CCN_UPCALL_RESULT_OK;
254 }
255
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800256 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800257
258 (*f) (interest);
259 return CCN_UPCALL_RESULT_OK;
260}
261
262static ccn_upcall_res
263incomingData(ccn_closure *selfp,
264 ccn_upcall_kind kind,
265 ccn_upcall_info *info)
266{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800267 Closure *cp = static_cast<Closure *> (selfp->data);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800268
269 switch (kind)
270 {
271 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800272 delete cp;
273 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800274 delete selfp;
275 return CCN_UPCALL_RESULT_OK;
276
277 case CCN_UPCALL_CONTENT:
278 break;
279
280 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800281 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800282 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800283 Name interest(info->interest_ccnb, info->interest_comps);
284 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
285 switch(rv)
286 {
287 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
288 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
289 default : break;
290 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800291 }
292 return CCN_UPCALL_RESULT_OK;
293 }
294
295 default:
296 return CCN_UPCALL_RESULT_OK;
297 }
298
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800299 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800300
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800301 // const unsigned char *pcontent;
302 // size_t len;
303 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
304 // {
305 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
306 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800307
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800308 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800309
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800310 // Bytes content;
311 // // copy content and do processing on the copy
312 // // otherwise the pointed memory may have been changed during the processing
313 // readRaw(content, pcontent, len);
314
315 cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800316
317 return CCN_UPCALL_RESULT_OK;
318}
319
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800320int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800321{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800322 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800323 if (!m_running || !m_connected)
324 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800325
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800326 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
327 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800328 ccn_closure *dataClosure = new ccn_closure;
329
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800330 Closure *myClosure = closure.dup();
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800331 dataClosure->data = (void *)myClosure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800332
333 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800334
335 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
336 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800337 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800338 {
339 templ = selectorsPtr->getBuf();
340 }
341
342 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800343 {
344 }
345
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800346 return 0;
347}
348
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800349int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800350{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800351 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800352 if (!m_running || !m_connected)
353 return -1;
354
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800355 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
356 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357 ccn_closure *interestClosure = new ccn_closure;
358
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800359 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
360 interestClosure->p = &incomingInterest;
361 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
362 if (ret < 0)
363 {
364 }
365
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800366 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800367
368 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800369}
370
371void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800372CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800373{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800374 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800375 if (!m_running || !m_connected)
376 return;
377
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800378 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
379 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800380
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800381 int ret = ccn_set_interest_filter (m_handle, pname, 0);
382 if (ret < 0)
383 {
384 }
385
386 m_registeredInterests.erase(prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800387}
388
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800389Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800390CcnxWrapper::getLocalPrefix ()
391{
392 struct ccn * tmp_handle = ccn_create ();
393 int res = ccn_connect (tmp_handle, NULL);
394 if (res < 0)
395 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800396 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800397 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800398
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800399 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800400
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800401 struct ccn_charbuf *templ = ccn_charbuf_create();
402 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
403 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
404 ccn_charbuf_append_closer(templ); /* </Name> */
405 // XXX - use pubid if possible
406 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
407 ccnb_append_number(templ, 1);
408 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
409 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
410 ccn_charbuf_append_closer(templ); /* </Interest> */
411
412 struct ccn_charbuf *name = ccn_charbuf_create ();
413 res = ccn_name_from_uri (name, "/local/ndn/prefix");
414 if (res < 0) {
415 }
416 else
417 {
418 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800419
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800420 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800421 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800422 if (stream == NULL) {
423 }
424 else
425 {
426 ostringstream os;
427
428 int counter = 0;
429 char buf[256];
430 while (true) {
431 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800432
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800433 if (res == 0) {
434 break;
435 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800436
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800437 if (res > 0) {
438 os << string(buf, res);
439 } else if (res == CCN_FETCH_READ_NONE) {
440 if (counter < 2)
441 {
442 ccn_run(tmp_handle, 1000);
443 counter ++;
444 }
445 else
446 {
447 break;
448 }
449 } else if (res == CCN_FETCH_READ_END) {
450 break;
451 } else if (res == CCN_FETCH_READ_TIMEOUT) {
452 break;
453 } else {
454 break;
455 }
456 }
457 retval = os.str ();
458 stream = ccn_fetch_close(stream);
459 }
460 fetch = ccn_fetch_destroy(fetch);
461 }
462
463 ccn_charbuf_destroy (&name);
464
465 ccn_disconnect (tmp_handle);
466 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800467
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800468 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800469 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800470}
471
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800472}