blob: 5f1ffc94da9cbcd3f57b3967b75408ae6495db71 [file] [log] [blame]
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -08001#include "ccnx-wrapper.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -08002extern "C" {
3#include <ccn/fetch.h>
4}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -08005#include <poll.h>
6#include <boost/throw_exception.hpp>
7#include <boost/date_time/posix_time/posix_time.hpp>
8#include <boost/random.hpp>
9#include <sstream>
10
11typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
12typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
13
14using namespace std;
15using namespace boost;
16
17namespace Ccnx {
18
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080019void
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080020readRaw(Bytes &bytes, const unsigned char *src, size_t len)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080021{
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080022 if (len > 0)
23 {
24 bytes.resize(len);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080025 memcpy(&bytes[0], src, len);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080026 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080027}
28
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080029const unsigned char *
30head(const Bytes &bytes)
31{
32 return &bytes[0];
33}
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080034
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080035CcnxWrapper::CcnxWrapper()
36 : m_handle (0)
37 , m_keyStore (0)
38 , m_keyLoactor (0)
39 , m_running (true)
40 , m_connected (false)
41{
42 connectCcnd();
43 initKeyStore ();
44 createKeyLocator ();
45 m_thread = thread (&CcnxWrapper::ccnLoop, this);
46}
47
48void
49CcnxWrapper::connectCcnd()
50{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080051 //if (m_handle != 0) {
52 //ccn_disconnect (m_handle);
53 //ccn_destroy (&m_handle);
54 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080055
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080056 m_handle = ccn_create ();
Zhenkai Zhud34106a2013-01-04 15:51:55 -080057 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080058 if (ccn_connect(m_handle, NULL) < 0)
59 {
60 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
61 }
62 m_connected = true;
63
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080064 //if (!m_registeredInterests.empty())
65 //{
66 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
67 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080068 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080069 // setInterestFilter(it->first, it->second);
70 //}
71 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080072}
73
74CcnxWrapper::~CcnxWrapper()
75{
76 {
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080077 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080078 m_running = false;
79 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080080
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080081 m_thread.join ();
82 ccn_disconnect (m_handle);
83 ccn_destroy (&m_handle);
84 ccn_charbuf_destroy (&m_keyLoactor);
85 ccn_keystore_destroy (&m_keyStore);
86}
87
88void
89CcnxWrapper::createKeyLocator ()
90{
91 m_keyLoactor = ccn_charbuf_create();
92 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
93 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
94 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
95 if (res >= 0)
96 {
97 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
98 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
99 }
100}
101
102const ccn_pkey*
103CcnxWrapper::getPrivateKey ()
104{
105 return ccn_keystore_private_key (m_keyStore);
106}
107
108const unsigned char*
109CcnxWrapper::getPublicKeyDigest ()
110{
111 return ccn_keystore_public_key_digest(m_keyStore);
112}
113
114ssize_t
115CcnxWrapper::getPublicKeyDigestLength ()
116{
117 return ccn_keystore_public_key_digest_length(m_keyStore);
118}
119
120void
121CcnxWrapper::initKeyStore ()
122{
123 m_keyStore = ccn_keystore_create ();
124 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
125 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
126 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
127}
128
129void
130CcnxWrapper::ccnLoop ()
131{
132 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
133 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
134
135 while (m_running)
136 {
137 try
138 {
139 int res = 0;
140 {
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800141 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800142 res = ccn_run (m_handle, 0);
143 }
144
145 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800146
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800147 if (res < 0)
148 BOOST_THROW_EXCEPTION (CcnxOperationException()
149 << errmsg_info_str("ccn_run returned error"));
150
151
152 pollfd pfds[1];
153 {
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800154 UniqueRecLock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800155
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800156 pfds[0].fd = ccn_get_connection_fd (m_handle);
157 pfds[0].events = POLLIN;
158 if (ccn_output_is_pending (m_handle))
159 pfds[0].events |= POLLOUT;
160 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800161
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800162 int ret = poll (pfds, 1, 1);
163 if (ret < 0)
164 {
165 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
166 }
167 }
168 catch (CcnxOperationException &e)
169 {
170 // do not try reconnect for now
171 throw e;
172 /*
173 m_connected = false;
174 // probably ccnd has been stopped
175 // try reconnect with sleep
176 int interval = 1;
177 int maxInterval = 32;
178 while (m_running)
179 {
180 try
181 {
182 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
183
184 connectCcnd();
185 _LOG_DEBUG("reconnect to ccnd succeeded");
186 break;
187 }
188 catch (CcnxOperationException &e)
189 {
190 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
191
192 // do exponential backup for reconnect interval
193 if (interval < maxInterval)
194 {
195 interval *= 2;
196 }
197 }
198 }
199 */
200 }
201 catch (const std::exception &exc)
202 {
203 // catch anything thrown within try block that derives from std::exception
204 std::cerr << exc.what();
205 }
206 catch (...)
207 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800208 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800209 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800210 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800211}
212
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800213Bytes
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800214CcnxWrapper::createContentObject(const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800215{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800216 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
217 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800218 ccn_charbuf *signed_info = ccn_charbuf_create();
219 ccn_charbuf *content = ccn_charbuf_create();
220
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800221 ccn_signed_info_create(signed_info,
222 getPublicKeyDigest(),
223 getPublicKeyDigestLength(),
224 NULL,
225 CCN_CONTENT_DATA,
226 freshness,
227 NULL,
228 m_keyLoactor);
229 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800230 buf, len,
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800231 NULL, getPrivateKey()) < 0)
232 {
233 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
234 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800235
236 Bytes bytes;
237 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800238
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800239 ccn_charbuf_destroy (&signed_info);
240 ccn_charbuf_destroy (&content);
241
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800242 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800243}
244
245int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800246CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800247{
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800248 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800249 if (!m_running || !m_connected)
250 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800251
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800252
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800253 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800254 {
255 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
256 }
257
258 return 0;
259}
260
261int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800262CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800263{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800264 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800265 return putToCcnd(co);
266}
267
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800268int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800269CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800270{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800271 publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800272}
273
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800274
275static ccn_upcall_res
276incomingInterest(ccn_closure *selfp,
277 ccn_upcall_kind kind,
278 ccn_upcall_info *info)
279{
280 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
281
282 switch (kind)
283 {
284 case CCN_UPCALL_FINAL: // effective in unit tests
285 delete f;
286 delete selfp;
287 return CCN_UPCALL_RESULT_OK;
288
289 case CCN_UPCALL_INTEREST:
290 break;
291
292 default:
293 return CCN_UPCALL_RESULT_OK;
294 }
295
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800296 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800297
298 (*f) (interest);
299 return CCN_UPCALL_RESULT_OK;
300}
301
302static ccn_upcall_res
303incomingData(ccn_closure *selfp,
304 ccn_upcall_kind kind,
305 ccn_upcall_info *info)
306{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800307 Closure *cp = static_cast<Closure *> (selfp->data);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800308
309 switch (kind)
310 {
311 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800312 delete selfp;
313 return CCN_UPCALL_RESULT_OK;
314
315 case CCN_UPCALL_CONTENT:
316 break;
317
318 case CCN_UPCALL_INTEREST_TIMED_OUT: {
319 if (cp != NULL && cp->getRetry() > 0) {
320 cp->decRetry();
321 return CCN_UPCALL_RESULT_REEXPRESS;
322 }
323
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800324 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800325 Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800326 switch(rv)
327 {
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800328 case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
329 case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800330 default : break;
331 }
332 return CCN_UPCALL_RESULT_OK;
333 }
334
335 default:
336 return CCN_UPCALL_RESULT_OK;
337 }
338
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800339 const unsigned char *pcontent;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800340 size_t len;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800341 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342 {
343 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
344 }
345
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800346 Name name(info->content_ccnb, info->content_comps);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800347
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800348 Bytes content;
349 // copy content and do processing on the copy
350 // otherwise the pointed memory may have been changed during the processing
351 readRaw(content, pcontent, len);
352
353 cp->runDataCallback(name, content);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800354
355 return CCN_UPCALL_RESULT_OK;
356}
357
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800358int CcnxWrapper::sendInterest (const Name &interest, Closure *closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800359{
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800360 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800361 if (!m_running || !m_connected)
362 return -1;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800363
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800364 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
365 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800366 ccn_closure *dataClosure = new ccn_closure;
367
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800368 dataClosure->data = (void *)closure;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800369
370 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800371
372 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
373 ccn_charbuf *templ = NULL;
374 if (selectorsPtr != CcnxCharbuf::Null)
375 {
376 templ = selectorsPtr->getBuf();
377 }
378
379 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800380 {
381 }
382
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800383 return 0;
384}
385
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800386int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800387{
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800388 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800389 if (!m_running || !m_connected)
390 return -1;
391
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800392 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
393 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800394 ccn_closure *interestClosure = new ccn_closure;
395
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800396 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
397 interestClosure->p = &incomingInterest;
398 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
399 if (ret < 0)
400 {
401 }
402
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800403 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800404}
405
406void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800407CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800408{
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800409 UniqueRecLock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800410 if (!m_running || !m_connected)
411 return;
412
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800413 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
414 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800415
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800416 int ret = ccn_set_interest_filter (m_handle, pname, 0);
417 if (ret < 0)
418 {
419 }
420
421 m_registeredInterests.erase(prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800422}
423
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800424Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800425CcnxWrapper::getLocalPrefix ()
426{
427 struct ccn * tmp_handle = ccn_create ();
428 int res = ccn_connect (tmp_handle, NULL);
429 if (res < 0)
430 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800431 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800432 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800433
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800434 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800435
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800436 struct ccn_charbuf *templ = ccn_charbuf_create();
437 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
438 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
439 ccn_charbuf_append_closer(templ); /* </Name> */
440 // XXX - use pubid if possible
441 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
442 ccnb_append_number(templ, 1);
443 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
444 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
445 ccn_charbuf_append_closer(templ); /* </Interest> */
446
447 struct ccn_charbuf *name = ccn_charbuf_create ();
448 res = ccn_name_from_uri (name, "/local/ndn/prefix");
449 if (res < 0) {
450 }
451 else
452 {
453 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800454
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800455 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800456 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800457 if (stream == NULL) {
458 }
459 else
460 {
461 ostringstream os;
462
463 int counter = 0;
464 char buf[256];
465 while (true) {
466 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800467
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800468 if (res == 0) {
469 break;
470 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800471
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800472 if (res > 0) {
473 os << string(buf, res);
474 } else if (res == CCN_FETCH_READ_NONE) {
475 if (counter < 2)
476 {
477 ccn_run(tmp_handle, 1000);
478 counter ++;
479 }
480 else
481 {
482 break;
483 }
484 } else if (res == CCN_FETCH_READ_END) {
485 break;
486 } else if (res == CCN_FETCH_READ_TIMEOUT) {
487 break;
488 } else {
489 break;
490 }
491 }
492 retval = os.str ();
493 stream = ccn_fetch_close(stream);
494 }
495 fetch = ccn_fetch_destroy(fetch);
496 }
497
498 ccn_charbuf_destroy (&name);
499
500 ccn_disconnect (tmp_handle);
501 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800502
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800503 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800504}
505
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800506}