blob: 7f2e5676f5b45b319c536bf95ac176fb4a055f5f [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 Zhu1ddeb6f2012-12-27 14:04:18 -080031#include <sstream>
32
33typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
34typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
35
36using namespace std;
37using namespace boost;
38
39namespace Ccnx {
40
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080041CcnxWrapper::CcnxWrapper()
42 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080043 , m_running (true)
44 , m_connected (false)
45{
46 connectCcnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080047 m_thread = thread (&CcnxWrapper::ccnLoop, this);
48}
49
50void
51CcnxWrapper::connectCcnd()
52{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080053 //if (m_handle != 0) {
54 //ccn_disconnect (m_handle);
55 //ccn_destroy (&m_handle);
56 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080057
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080058 m_handle = ccn_create ();
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080059 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080060 if (ccn_connect(m_handle, NULL) < 0)
61 {
62 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
63 }
64 m_connected = true;
65
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080066 //if (!m_registeredInterests.empty())
67 //{
68 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
69 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080070 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080071 // setInterestFilter(it->first, it->second);
72 //}
73 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080074}
75
76CcnxWrapper::~CcnxWrapper()
77{
78 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -080079 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080080 m_running = false;
81 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080082
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080083 m_thread.join ();
84 ccn_disconnect (m_handle);
Zhenkai Zhue29616f2013-01-14 15:40:57 -080085 //ccn_destroy (&m_handle);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080086}
87
88void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080089CcnxWrapper::ccnLoop ()
90{
91 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
92 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
93
94 while (m_running)
95 {
96 try
97 {
98 int res = 0;
99 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800100 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800101 res = ccn_run (m_handle, 0);
102 }
103
104 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800105
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800106 if (res < 0)
107 BOOST_THROW_EXCEPTION (CcnxOperationException()
108 << errmsg_info_str("ccn_run returned error"));
109
110
111 pollfd pfds[1];
112 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800113 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800114
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800115 pfds[0].fd = ccn_get_connection_fd (m_handle);
116 pfds[0].events = POLLIN;
117 if (ccn_output_is_pending (m_handle))
118 pfds[0].events |= POLLOUT;
119 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800120
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800121 int ret = poll (pfds, 1, 1);
122 if (ret < 0)
123 {
124 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
125 }
126 }
127 catch (CcnxOperationException &e)
128 {
129 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800130 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800131 throw e;
132 /*
133 m_connected = false;
134 // probably ccnd has been stopped
135 // try reconnect with sleep
136 int interval = 1;
137 int maxInterval = 32;
138 while (m_running)
139 {
140 try
141 {
142 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
143
144 connectCcnd();
145 _LOG_DEBUG("reconnect to ccnd succeeded");
146 break;
147 }
148 catch (CcnxOperationException &e)
149 {
150 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
151
152 // do exponential backup for reconnect interval
153 if (interval < maxInterval)
154 {
155 interval *= 2;
156 }
157 }
158 }
159 */
160 }
161 catch (const std::exception &exc)
162 {
163 // catch anything thrown within try block that derives from std::exception
164 std::cerr << exc.what();
165 }
166 catch (...)
167 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800168 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800169 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800170 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800171}
172
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800173Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800174CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800175{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800176 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
177 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800178 ccn_charbuf *content = ccn_charbuf_create();
179
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800180 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
181 sp.freshness = freshness;
182
183 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800185 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800186 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800187
188 Bytes bytes;
189 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800190
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800191 ccn_charbuf_destroy (&content);
192
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800193 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800194}
195
196int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800197CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800198{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800199 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800200 if (!m_running || !m_connected)
201 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800202
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800203
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800204 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800205 {
206 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
207 }
208
209 return 0;
210}
211
212int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800213CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800214{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800215 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800216 return putToCcnd(co);
217}
218
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800219int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800220CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800221{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800222 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800223}
224
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800225
226static ccn_upcall_res
227incomingInterest(ccn_closure *selfp,
228 ccn_upcall_kind kind,
229 ccn_upcall_info *info)
230{
231 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
232
233 switch (kind)
234 {
235 case CCN_UPCALL_FINAL: // effective in unit tests
236 delete f;
237 delete selfp;
238 return CCN_UPCALL_RESULT_OK;
239
240 case CCN_UPCALL_INTEREST:
241 break;
242
243 default:
244 return CCN_UPCALL_RESULT_OK;
245 }
246
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800247 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800248
249 (*f) (interest);
250 return CCN_UPCALL_RESULT_OK;
251}
252
253static ccn_upcall_res
254incomingData(ccn_closure *selfp,
255 ccn_upcall_kind kind,
256 ccn_upcall_info *info)
257{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800258 Closure *cp = static_cast<Closure *> (selfp->data);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800259
260 switch (kind)
261 {
262 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800263 delete cp;
264 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265 delete selfp;
266 return CCN_UPCALL_RESULT_OK;
267
268 case CCN_UPCALL_CONTENT:
269 break;
270
271 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800272 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800273 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800274 Name interest(info->interest_ccnb, info->interest_comps);
275 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
276 switch(rv)
277 {
278 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
279 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
280 default : break;
281 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800282 }
283 return CCN_UPCALL_RESULT_OK;
284 }
285
286 default:
287 return CCN_UPCALL_RESULT_OK;
288 }
289
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800290 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800291
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800292 // const unsigned char *pcontent;
293 // size_t len;
294 // if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
295 // {
296 // // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
297 // }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800298
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800299 // Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800300
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800301 // Bytes content;
302 // // copy content and do processing on the copy
303 // // otherwise the pointed memory may have been changed during the processing
304 // readRaw(content, pcontent, len);
305
306 cp->runDataCallback (pco->name (), pco);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800307
308 return CCN_UPCALL_RESULT_OK;
309}
310
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800311int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800312{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800313 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800314 if (!m_running || !m_connected)
315 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800316
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800317 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
318 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800319 ccn_closure *dataClosure = new ccn_closure;
320
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800321 Closure *myClosure = closure.dup();
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800322 dataClosure->data = (void *)myClosure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800323
324 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800325
326 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
327 ccn_charbuf *templ = NULL;
328 if (selectorsPtr != CcnxCharbuf::Null)
329 {
330 templ = selectorsPtr->getBuf();
331 }
332
333 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334 {
335 }
336
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800337 return 0;
338}
339
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800340int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800341{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800342 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800343 if (!m_running || !m_connected)
344 return -1;
345
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800346 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
347 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800348 ccn_closure *interestClosure = new ccn_closure;
349
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800350 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
351 interestClosure->p = &incomingInterest;
352 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
353 if (ret < 0)
354 {
355 }
356
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800357 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800358
359 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800360}
361
362void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800363CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800364{
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800365 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800366 if (!m_running || !m_connected)
367 return;
368
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800369 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
370 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800371
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800372 int ret = ccn_set_interest_filter (m_handle, pname, 0);
373 if (ret < 0)
374 {
375 }
376
377 m_registeredInterests.erase(prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800378}
379
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800380Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800381CcnxWrapper::getLocalPrefix ()
382{
383 struct ccn * tmp_handle = ccn_create ();
384 int res = ccn_connect (tmp_handle, NULL);
385 if (res < 0)
386 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800387 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800388 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800389
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800390 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800391
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800392 struct ccn_charbuf *templ = ccn_charbuf_create();
393 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
394 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
395 ccn_charbuf_append_closer(templ); /* </Name> */
396 // XXX - use pubid if possible
397 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
398 ccnb_append_number(templ, 1);
399 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
400 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
401 ccn_charbuf_append_closer(templ); /* </Interest> */
402
403 struct ccn_charbuf *name = ccn_charbuf_create ();
404 res = ccn_name_from_uri (name, "/local/ndn/prefix");
405 if (res < 0) {
406 }
407 else
408 {
409 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800410
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800411 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800412 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800413 if (stream == NULL) {
414 }
415 else
416 {
417 ostringstream os;
418
419 int counter = 0;
420 char buf[256];
421 while (true) {
422 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800423
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800424 if (res == 0) {
425 break;
426 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800427
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800428 if (res > 0) {
429 os << string(buf, res);
430 } else if (res == CCN_FETCH_READ_NONE) {
431 if (counter < 2)
432 {
433 ccn_run(tmp_handle, 1000);
434 counter ++;
435 }
436 else
437 {
438 break;
439 }
440 } else if (res == CCN_FETCH_READ_END) {
441 break;
442 } else if (res == CCN_FETCH_READ_TIMEOUT) {
443 break;
444 } else {
445 break;
446 }
447 }
448 retval = os.str ();
449 stream = ccn_fetch_close(stream);
450 }
451 fetch = ccn_fetch_destroy(fetch);
452 }
453
454 ccn_charbuf_destroy (&name);
455
456 ccn_disconnect (tmp_handle);
457 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800458
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800459 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800460}
461
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800462}