blob: a9f0821fc3cc5f0d0ee8ff25ae9591d1b1d8ecbb [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 Zhu5acebd72013-02-07 19:59:25 -080046static int
47ccn_encode_Signature(struct ccn_charbuf *buf,
48 const char *digest_algorithm,
49 const void *witness,
50 size_t witness_size,
51 const struct ccn_signature *signature,
52 size_t signature_size)
53{
54 int res = 0;
55
56 if (signature == NULL)
57 return(-1);
58
59 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG);
60
61 if (digest_algorithm != NULL) {
62 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_DigestAlgorithm, CCN_DTAG);
63 res |= ccn_charbuf_append_tt(buf, strlen(digest_algorithm), CCN_UDATA);
64 res |= ccn_charbuf_append_string(buf, digest_algorithm);
65 res |= ccn_charbuf_append_closer(buf);
66 }
67
68 if (witness != NULL) {
69 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Witness, CCN_DTAG);
70 res |= ccn_charbuf_append_tt(buf, witness_size, CCN_BLOB);
71 res |= ccn_charbuf_append(buf, witness, witness_size);
72 res |= ccn_charbuf_append_closer(buf);
73 }
74
75 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG);
76 res |= ccn_charbuf_append_tt(buf, signature_size, CCN_BLOB);
77 res |= ccn_charbuf_append(buf, signature, signature_size);
78 res |= ccn_charbuf_append_closer(buf);
79
80 res |= ccn_charbuf_append_closer(buf);
81
82 return(res == 0 ? 0 : -1);
83}
84
85static int
86ccn_pack_ContentObject(struct ccn_charbuf *buf,
87 const struct ccn_charbuf *Name,
88 const struct ccn_charbuf *SignedInfo,
89 const void *data,
90 size_t size,
91 const char *digest_algorithm,
92 const struct ccn_pkey *private_key
93 )
94{
95 int res = 0;
96 struct ccn_sigc *sig_ctx;
97 struct ccn_signature *signature;
98 struct ccn_charbuf *content_header;
99 size_t closer_start;
100
101 content_header = ccn_charbuf_create();
102 res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG);
103 if (size != 0)
104 res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB);
105 closer_start = content_header->length;
106 res |= ccn_charbuf_append_closer(content_header);
107 if (res < 0)
108 return(-1);
109 sig_ctx = ccn_sigc_create();
110 size_t sig_size = ccn_sigc_signature_max_size(sig_ctx, private_key);
111 signature = (ccn_signature *)calloc(1, sig_size);
112 ccn_sigc_destroy(&sig_ctx);
113 res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG);
114
115 res |= ccn_encode_Signature(buf, digest_algorithm,
116 NULL, 0, signature, sig_size);
117 res |= ccn_charbuf_append_charbuf(buf, Name);
118 res |= ccn_charbuf_append_charbuf(buf, SignedInfo);
119 res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size);
120 res |= ccn_charbuf_append_closer(buf);
121 free(signature);
122 ccn_charbuf_destroy(&content_header);
123 return(res == 0 ? 0 : -1);
124}
125
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800126CcnxWrapper::CcnxWrapper()
127 : m_handle (0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128 , m_running (true)
129 , m_connected (false)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800130 , m_executor (new Executor(1))
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800131 , m_keystore(NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800132{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800133 start ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800134}
135
136void
137CcnxWrapper::connectCcnd()
138{
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800139 //if (m_handle != 0) {
140 //ccn_disconnect (m_handle);
141 //ccn_destroy (&m_handle);
142 //}
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800143
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800144 m_handle = ccn_create ();
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800145 //UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800146 if (ccn_connect(m_handle, NULL) < 0)
147 {
148 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
149 }
150 m_connected = true;
151
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800152 //if (!m_registeredInterests.empty())
153 //{
154 // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
155 //{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800156 // clearInterestFilter(it->first);
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800157 // setInterestFilter(it->first, it->second);
158 //}
159 //}
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800160}
161
162CcnxWrapper::~CcnxWrapper()
163{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800164 shutdown ();
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800165 if (m_keystore != NULL)
166 {
167 ccn_keystore_destroy(&m_keystore);
168 }
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800169}
170
171void
172CcnxWrapper::start () // called automatically in constructor
173{
174 connectCcnd();
175 m_thread = thread (&CcnxWrapper::ccnLoop, this);
176 m_executor->start();
177}
178
179void
180CcnxWrapper::shutdown () // called in destructor, but can called manually
181{
182 m_executor->shutdown();
183
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800184 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800185 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800186 m_running = false;
187 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800188
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800189 _LOG_DEBUG ("+++++++++SHUTDOWN+++++++");
190 if (m_connected)
191 {
192 m_thread.join ();
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800193
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800194 ccn_disconnect (m_handle);
195 //ccn_destroy (&m_handle);
196 m_connected = false;
197 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800198}
199
200void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800201CcnxWrapper::ccnLoop ()
202{
203 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
204 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
205
206 while (m_running)
207 {
208 try
209 {
210 int res = 0;
211 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800212 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800213 res = ccn_run (m_handle, 0);
214 }
215
216 if (!m_running) break;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800217
Alexander Afanasyev9c9c3012013-01-25 23:44:25 -0800218 if (res < 0) {
219 _LOG_ERROR ("ccn_run returned negative status: " << res);
220 usleep (100000);
221 continue;
222 // BOOST_THROW_EXCEPTION (CcnxOperationException()
223 // << errmsg_info_str("ccn_run returned error"));
224 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800225
226
227 pollfd pfds[1];
228 {
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800229 UniqueRecLock lock(m_mutex);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800230
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800231 pfds[0].fd = ccn_get_connection_fd (m_handle);
232 pfds[0].events = POLLIN;
233 if (ccn_output_is_pending (m_handle))
234 pfds[0].events |= POLLOUT;
235 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800236
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800237 int ret = poll (pfds, 1, 1);
238 if (ret < 0)
239 {
240 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
241 }
242 }
243 catch (CcnxOperationException &e)
244 {
245 // do not try reconnect for now
Zhenkai Zhu6e7d4d22013-01-15 18:18:18 -0800246 cout << *get_error_info<errmsg_info_str> (e) << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800247 throw e;
248 /*
249 m_connected = false;
250 // probably ccnd has been stopped
251 // try reconnect with sleep
252 int interval = 1;
253 int maxInterval = 32;
254 while (m_running)
255 {
256 try
257 {
258 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
259
260 connectCcnd();
261 _LOG_DEBUG("reconnect to ccnd succeeded");
262 break;
263 }
264 catch (CcnxOperationException &e)
265 {
266 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
267
268 // do exponential backup for reconnect interval
269 if (interval < maxInterval)
270 {
271 interval *= 2;
272 }
273 }
274 }
275 */
276 }
277 catch (const std::exception &exc)
278 {
279 // catch anything thrown within try block that derives from std::exception
280 std::cerr << exc.what();
281 }
282 catch (...)
283 {
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800284 cout << "UNKNOWN EXCEPTION !!!" << endl;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800285 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800286 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800287}
288
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800289Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800290CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800291{
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800292 {
293 UniqueRecLock lock(m_mutex);
294 if (!m_running || !m_connected)
295 {
296 _LOG_TRACE ("<< not running or connected");
297 return Bytes ();
298 }
299 }
300
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800301 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
302 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800303 ccn_charbuf *content = ccn_charbuf_create();
304
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800305 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
306 sp.freshness = freshness;
307
308 if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800309 {
Zhenkai Zhud9d03f62013-01-04 16:57:46 -0800310 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed"));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800311 }
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800312
313 Bytes bytes;
314 readRaw(bytes, content->buf, content->length);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800315
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800316 ccn_charbuf_destroy (&content);
317
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800318 return bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800319}
320
321int
Zhenkai Zhubad089c2012-12-28 10:28:27 -0800322CcnxWrapper::putToCcnd (const Bytes &contentObject)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800323{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800324 _LOG_TRACE (">> putToCcnd");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800325 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800326 if (!m_running || !m_connected)
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800327 {
328 _LOG_TRACE ("<< not running or connected");
329 return -1;
330 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800331
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800332
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800333 if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800334 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800335 _LOG_ERROR ("ccn_put failed");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800336 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
337 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800338 else
339 {
340 _LOG_DEBUG ("<< putToCcnd");
341 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800342
343 return 0;
344}
345
346int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800347CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800348{
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800349 Bytes co = createContentObject(name, buf, len, freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800350 return putToCcnd(co);
351}
352
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800353int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800354CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800355{
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800356 return publishData(name, head(content), content.size(), freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800357}
358
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800359int
360CcnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness)
361{
362 return publishUnsignedData(name, head(content), content.size(), freshness);
363}
364
365int
366CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness)
367{
368 {
369 UniqueRecLock lock(m_mutex);
370 if (!m_running || !m_connected)
371 {
372 _LOG_TRACE ("<< not running or connected");
373 return -1;
374 }
375 }
376
377 CcnxCharbufPtr ptr = name.toCcnxCharbuf();
378 ccn_charbuf *pname = ptr->getBuf();
379 ccn_charbuf *content = ccn_charbuf_create();
380 ccn_charbuf *signed_info = ccn_charbuf_create();
381
382 if (m_keystore == NULL)
383 {
384 m_keystore = ccn_keystore_create ();
385 string keystoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
386 if (ccn_keystore_init (m_keystore, (char *)keystoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
387 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keystoreFile.c_str()));
388 }
389
390 int res = ccn_signed_info_create(signed_info,
391 ccn_keystore_public_key_digest(m_keystore),
392 ccn_keystore_public_key_digest_length(m_keystore),
393 NULL,
394 CCN_CONTENT_DATA,
395 freshness,
396 NULL,
397 NULL
398 );
399
400 ccn_pack_ContentObject(content, pname, signed_info, buf, len, NULL, ccn_keystore_private_key (m_keystore));
401
402 Bytes bytes;
403 readRaw(bytes, content->buf, content->length);
404
405 ccn_charbuf_destroy (&content);
406 ccn_charbuf_destroy (&signed_info);
407
408 return putToCcnd (bytes);
409}
410
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800411
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800412static void
413deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple)
414{
415 delete tuple->get<0> ();
416 delete tuple;
417}
418
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800419static ccn_upcall_res
420incomingInterest(ccn_closure *selfp,
421 ccn_upcall_kind kind,
422 ccn_upcall_info *info)
423{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800424 CcnxWrapper::InterestCallback *f;
425 ExecutorPtr executor;
426 tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data);
427 tie (f, executor) = *realData;
428
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800429 switch (kind)
430 {
431 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800432 // delete closure;
433 executor->execute (bind (deleterInInterestTuple, realData));
434
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800435 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800436 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800437 return CCN_UPCALL_RESULT_OK;
438
439 case CCN_UPCALL_INTEREST:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800440 _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800441 break;
442
443 default:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800444 _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800445 return CCN_UPCALL_RESULT_OK;
446 }
447
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800448 Name interest(info->interest_ccnb, info->interest_comps);
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800449 Selectors selectors(info->pi);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800450
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800451 executor->execute (bind (*f, interest, selectors));
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800452 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800453 // (*f) (interest);
454 // closure->runInterestCallback(interest);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800455
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800456 return CCN_UPCALL_RESULT_OK;
457}
458
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800459static void
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800460deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple)
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800461{
462 delete tuple->get<0> ();
463 delete tuple;
464}
465
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800466static ccn_upcall_res
467incomingData(ccn_closure *selfp,
468 ccn_upcall_kind kind,
469 ccn_upcall_info *info)
470{
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800471 // Closure *cp = static_cast<Closure *> (selfp->data);
472 Closure *cp;
473 ExecutorPtr executor;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800474 Selectors selectors;
475 tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data);
476 tie (cp, executor, selectors) = *realData;
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800477
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800478 switch (kind)
479 {
480 case CCN_UPCALL_FINAL: // effecitve in unit tests
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800481 executor->execute (bind (deleterInDataTuple, realData));
482
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800483 cp = NULL;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800484 delete selfp;
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800485 _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800486 return CCN_UPCALL_RESULT_OK;
487
488 case CCN_UPCALL_CONTENT:
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800489 _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800490 break;
491
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800492 case CCN_UPCALL_CONTENT_UNVERIFIED:
493 _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps));
494 break;
495
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800496 case CCN_UPCALL_INTEREST_TIMED_OUT: {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800497 if (cp != NULL)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800498 {
Zhenkai Zhu8339e912013-01-18 18:10:17 -0800499 Name interest(info->interest_ccnb, info->interest_comps);
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800500 _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps));
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800501 executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors));
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800502 }
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800503 else
504 {
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800505 _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800506 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800507 return CCN_UPCALL_RESULT_OK;
508 }
509
510 default:
Zhenkai Zhu5acebd72013-02-07 19:59:25 -0800511 _LOG_TRACE(">> unknown upcall type");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800512 return CCN_UPCALL_RESULT_OK;
513 }
514
Alexander Afanasyevf278db32013-01-21 14:41:01 -0800515 PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800516
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800517 // this will be run in executor
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800518 executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco));
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800519 _LOG_TRACE (">> incomingData");
520
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800521 return CCN_UPCALL_RESULT_OK;
522}
523
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800524int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800525{
Alexander Afanasyev6aec3bf2013-02-05 11:25:52 -0800526 _LOG_TRACE (">> sendInterest: " << interest);
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800527 {
528 UniqueRecLock lock(m_mutex);
529 if (!m_running || !m_connected)
Alexander Afanasyevb3d4cd52013-01-28 11:20:52 -0800530 {
531 _LOG_ERROR ("<< sendInterest: not running or connected");
532 return -1;
533 }
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800534 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800535
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800536 CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
537 ccn_charbuf *pname = namePtr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800538 ccn_closure *dataClosure = new ccn_closure;
539
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800540 // Closure *myClosure = new ExecutorClosure(closure, m_executor);
541 Closure *myClosure = closure.dup ();
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -0800542 dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800543
544 dataClosure->p = &incomingData;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800545
546 CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
547 ccn_charbuf *templ = NULL;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800548 if (selectorsPtr)
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800549 {
550 templ = selectorsPtr->getBuf();
551 }
552
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800553 UniqueRecLock lock(m_mutex);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800554 if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800555 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800556 _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800557 }
558
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800559 return 0;
560}
561
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800562int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800563{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800564 _LOG_TRACE (">> setInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800565 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800566 if (!m_running || !m_connected)
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800567 {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800568 return -1;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800569 }
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800570
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800571 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
572 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800573 ccn_closure *interestClosure = new ccn_closure;
574
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800575 // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor);
576
577 interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800578 interestClosure->p = &incomingInterest;
Zhenkai Zhu7599db42013-01-28 10:32:53 -0800579
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800580 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
581 if (ret < 0)
582 {
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800583 _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800584 }
585
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800586 m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
Alexander Afanasyev816251e2013-01-28 16:16:49 -0800587
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800588 _LOG_TRACE ("<< setInterestFilter");
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800589
590 return ret;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800591}
592
593void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800594CcnxWrapper::clearInterestFilter (const Name &prefix)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800595{
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800596 _LOG_TRACE (">> clearInterestFilter");
Alexander Afanasyev20bc8e22013-01-17 10:56:04 -0800597 UniqueRecLock lock(m_mutex);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800598 if (!m_running || !m_connected)
599 return;
600
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800601 CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
602 ccn_charbuf *pname = ptr->getBuf();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800603
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800604 int ret = ccn_set_interest_filter (m_handle, pname, 0);
605 if (ret < 0)
606 {
607 }
608
609 m_registeredInterests.erase(prefix);
Alexander Afanasyevd9826662013-01-28 11:18:06 -0800610
611 _LOG_TRACE ("<< clearInterestFilter");
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800612}
613
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800614Name
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800615CcnxWrapper::getLocalPrefix ()
616{
617 struct ccn * tmp_handle = ccn_create ();
618 int res = ccn_connect (tmp_handle, NULL);
619 if (res < 0)
620 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800621 return Name();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800622 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800623
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800624 string retval = "";
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800625
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800626 struct ccn_charbuf *templ = ccn_charbuf_create();
627 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
628 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
629 ccn_charbuf_append_closer(templ); /* </Name> */
630 // XXX - use pubid if possible
631 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
632 ccnb_append_number(templ, 1);
633 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
634 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
635 ccn_charbuf_append_closer(templ); /* </Interest> */
636
637 struct ccn_charbuf *name = ccn_charbuf_create ();
638 res = ccn_name_from_uri (name, "/local/ndn/prefix");
639 if (res < 0) {
640 }
641 else
642 {
643 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800644
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800645 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800646 NULL, 4, CCN_V_HIGHEST, 0);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800647 if (stream == NULL) {
648 }
649 else
650 {
651 ostringstream os;
652
653 int counter = 0;
654 char buf[256];
655 while (true) {
656 res = ccn_fetch_read (stream, buf, sizeof(buf));
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800657
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800658 if (res == 0) {
659 break;
660 }
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800661
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800662 if (res > 0) {
663 os << string(buf, res);
664 } else if (res == CCN_FETCH_READ_NONE) {
665 if (counter < 2)
666 {
667 ccn_run(tmp_handle, 1000);
668 counter ++;
669 }
670 else
671 {
672 break;
673 }
674 } else if (res == CCN_FETCH_READ_END) {
675 break;
676 } else if (res == CCN_FETCH_READ_TIMEOUT) {
677 break;
678 } else {
679 break;
680 }
681 }
682 retval = os.str ();
683 stream = ccn_fetch_close(stream);
684 }
685 fetch = ccn_fetch_destroy(fetch);
686 }
687
688 ccn_charbuf_destroy (&name);
689
690 ccn_disconnect (tmp_handle);
691 ccn_destroy (&tmp_handle);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800692
Zhenkai Zhucbbb8882013-01-25 13:49:12 -0800693 boost::algorithm::trim(retval);
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800694 return Name(retval);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800695}
696
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800697}