blob: c824e729138968df77030d36f0c5a73a8652107e [file] [log] [blame]
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080022
23#include "sync-ccnx-wrapper.h"
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -070024
25extern "C" {
26#include <ccn/fetch.h>
27}
28
Alexander Afanasyevce001692013-07-14 11:34:41 -070029#include "sync-logging.h"
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080030#include <poll.h>
Zhenkai Zhu009ff792012-03-09 12:37:52 -080031#include <boost/throw_exception.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070032#include <boost/date_time/posix_time/posix_time.hpp>
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070033#include <boost/random.hpp>
34
35#include "sync-scheduler.h"
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070036
Chaoyi Bian95a58c32012-03-09 15:43:59 -080037typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
Alexander Afanasyev387ac952012-03-11 23:49:27 -070038typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080039
Alexander Afanasyev60126e02012-10-05 16:54:42 -070040
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080041using namespace std;
42using namespace boost;
43
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070044INIT_LOGGER ("CcnxWrapper");
45
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080046namespace Sync {
47
Alexander Afanasyev2247b302012-03-14 14:11:54 -070048#ifdef _DEBUG_WRAPPER_
49CcnxWrapper::CcnxWrapper(char c)
50#else
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080051CcnxWrapper::CcnxWrapper()
Alexander Afanasyev2247b302012-03-14 14:11:54 -070052#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080053 : m_handle (0)
54 , m_keyStore (0)
55 , m_keyLoactor (0)
56 , m_running (true)
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070057 , m_connected (false)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080058{
Alexander Afanasyev2247b302012-03-14 14:11:54 -070059#ifdef _DEBUG_WRAPPER_
60 m_c = c;
61#endif
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070062 connectCcnd();
Alexander Afanasyev2c180772012-03-13 23:58:46 -070063 initKeyStore ();
64 createKeyLocator ();
Alexander Afanasyev1285b382012-03-08 16:40:27 -080065 m_thread = thread (&CcnxWrapper::ccnLoop, this);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080066}
67
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070068void
69CcnxWrapper::connectCcnd()
70{
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070071 recursive_mutex::scoped_lock lock (m_mutex);
72
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070073 if (m_handle != 0) {
74 ccn_disconnect (m_handle);
75 ccn_destroy (&m_handle);
76 }
77
78 m_handle = ccn_create ();
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070079 _LOG_DEBUG("<<< connecting to ccnd");
80 if (ccn_connect(m_handle, NULL) < 0)
81 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070082 _LOG_DEBUG("<<< connecting to ccnd failed");
83 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed"));
84 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -070085 m_connected = true;
86
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070087 if (!m_registeredInterests.empty())
88 {
89 for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
90 {
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070091 // clearInterestFilter(it->first);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070092 setInterestFilter(it->first, it->second);
93 _LOG_DEBUG("<<< registering interest filter for: " << it->first);
94 }
95 }
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -070096}
97
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080098CcnxWrapper::~CcnxWrapper()
99{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700100 // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl;
101 {
102 recursive_mutex::scoped_lock lock(m_mutex);
103 m_running = false;
104 }
105
106 m_thread.join ();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800107 ccn_disconnect (m_handle);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700108 ccn_destroy (&m_handle);
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800109 ccn_charbuf_destroy (&m_keyLoactor);
110 ccn_keystore_destroy (&m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800111}
112
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800113/// @cond include_hidden
114
115void
116CcnxWrapper::createKeyLocator ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800117{
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800118 m_keyLoactor = ccn_charbuf_create();
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800119 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG);
120 ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG);
121 int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800122 if (res >= 0)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800123 {
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800124 ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */
125 ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800126 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800127}
128
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800129const ccn_pkey*
130CcnxWrapper::getPrivateKey ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800131{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800132 return ccn_keystore_private_key (m_keyStore);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800133}
134
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800135const unsigned char*
136CcnxWrapper::getPublicKeyDigest ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800137{
138 return ccn_keystore_public_key_digest(m_keyStore);
139}
140
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800141ssize_t
142CcnxWrapper::getPublicKeyDigestLength ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800143{
144 return ccn_keystore_public_key_digest_length(m_keyStore);
145}
146
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800147void
148CcnxWrapper::initKeyStore ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800149{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800150 m_keyStore = ccn_keystore_create ();
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800151 string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore");
152 if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0)
153 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str()));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800154}
155
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800156void
157CcnxWrapper::ccnLoop ()
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800158{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700159 _LOG_FUNCTION (this);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700160 static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0)));
161 static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800162
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700163 while (m_running)
164 {
165 try
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700166 {
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700167#ifdef _DEBUG_WRAPPER_
168 std::cout << m_c << flush;
169#endif
170 int res = 0;
171 {
172 recursive_mutex::scoped_lock lock (m_mutex);
173 res = ccn_run (m_handle, 0);
174 }
175
176 if (!m_running) break;
177
178 if (res < 0)
179 BOOST_THROW_EXCEPTION (CcnxOperationException()
180 << errmsg_info_str("ccn_run returned error"));
181
182
183 pollfd pfds[1];
184 {
185 recursive_mutex::scoped_lock lock (m_mutex);
186
187 pfds[0].fd = ccn_get_connection_fd (m_handle);
188 pfds[0].events = POLLIN;
189 if (ccn_output_is_pending (m_handle))
190 pfds[0].events |= POLLOUT;
191 }
192
193 int ret = poll (pfds, 1, 1);
194 if (ret < 0)
195 {
196 BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)"));
197 }
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800198 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700199 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700200 {
Zhenkai Zhu9587cd62012-10-17 11:58:37 -0700201 // do not try reconnect for now
202 throw e;
203 /*
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700204 m_connected = false;
205 // probably ccnd has been stopped
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700206 // try reconnect with sleep
207 int interval = 1;
208 int maxInterval = 32;
209 while (m_running)
210 {
211 try
212 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700213 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
214
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700215 connectCcnd();
216 _LOG_DEBUG("reconnect to ccnd succeeded");
217 break;
218 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700219 catch (CcnxOperationException &e)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700220 {
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700221 this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ()));
222
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700223 // do exponential backup for reconnect interval
224 if (interval < maxInterval)
225 {
226 interval *= 2;
227 }
228 }
229 }
Zhenkai Zhu9587cd62012-10-17 11:58:37 -0700230 */
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700231 }
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700232 catch (const std::exception &exc)
233 {
234 // catch anything thrown within try block that derives from std::exception
235 std::cerr << exc.what();
236 }
237 catch (...)
238 {
239 cout << "UNKNOWN EXCEPTION !!!" << endl;
240 }
241
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700242 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800243}
244
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800245/// @endcond
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700246int
247CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) {
Alexander Afanasyevce001692013-07-14 11:34:41 -0700248 return publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700249}
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800250
251int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700252CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800253{
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700254
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700255 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700256 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700257 return -1;
258
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700259 // cout << "Publish: " << name << endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800260 ccn_charbuf *pname = ccn_charbuf_create();
261 ccn_charbuf *signed_info = ccn_charbuf_create();
262 ccn_charbuf *content = ccn_charbuf_create();
263
264 ccn_name_from_uri(pname, name.c_str());
265 ccn_signed_info_create(signed_info,
266 getPublicKeyDigest(),
267 getPublicKeyDigestLength(),
268 NULL,
269 CCN_CONTENT_DATA,
270 freshness,
271 NULL,
272 m_keyLoactor);
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800273 if(ccn_encode_ContentObject(content, pname, signed_info,
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700274 (const unsigned char *)buf, len,
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800275 NULL, getPrivateKey()) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700276 {
277 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
278 _LOG_ERROR("<<< Encode content failed " << name);
279 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800280
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800281 if (ccn_put(m_handle, content->buf, content->length) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700282 {
283 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
284 _LOG_ERROR("<<< ccnput content failed " << name);
285 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800286
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800287 ccn_charbuf_destroy (&pname);
288 ccn_charbuf_destroy (&signed_info);
289 ccn_charbuf_destroy (&content);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700290 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800291}
292
293
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800294static ccn_upcall_res
295incomingInterest(ccn_closure *selfp,
296 ccn_upcall_kind kind,
297 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800298{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800299 CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800300
301 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800302 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800303 case CCN_UPCALL_FINAL: // effective in unit tests
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800304 delete f;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800305 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800306 return CCN_UPCALL_RESULT_OK;
307
308 case CCN_UPCALL_INTEREST:
309 break;
310
311 default:
312 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800313 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800314
Chaoyi Bian93d43102012-03-07 14:28:56 -0800315 string interest;
Chaoyi Bian02dba3c2012-03-07 21:45:22 -0800316 for (int i = 0; i < info->interest_comps->n - 1; i++)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800317 {
318 char *comp;
319 size_t size;
320 interest += "/";
321 ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800322 string compStr(comp, size);
323 interest += compStr;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800324 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800325 (*f) (interest);
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700326 _LOG_DEBUG("<<< processed interest: " << interest);
Chaoyi Bian93d43102012-03-07 14:28:56 -0800327 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800328}
329
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800330static ccn_upcall_res
331incomingData(ccn_closure *selfp,
332 ccn_upcall_kind kind,
333 ccn_upcall_info *info)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800334{
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700335 ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800336
337 switch (kind)
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800338 {
Zhenkai Zhucd747592012-03-09 12:08:17 -0800339 case CCN_UPCALL_FINAL: // effecitve in unit tests
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700340 delete cp;
341 cp = NULL;
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800342 delete selfp;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800343 return CCN_UPCALL_RESULT_OK;
344
345 case CCN_UPCALL_CONTENT:
346 break;
347
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700348 case CCN_UPCALL_INTEREST_TIMED_OUT: {
349 if (cp != NULL && cp->getRetry() > 0) {
350 cp->decRetry();
351 return CCN_UPCALL_RESULT_REEXPRESS;
352 }
353 return CCN_UPCALL_RESULT_OK;
354 }
355
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800356 default:
357 return CCN_UPCALL_RESULT_OK;
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800358 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800359
360 char *pcontent;
361 size_t len;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800362 if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700363 {
364 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
365 _LOG_ERROR("<<< Decode content failed ");
366 }
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800367
Chaoyi Bian4194b742012-03-08 17:21:35 -0800368 string name;
369 for (int i = 0; i < info->content_comps->n - 1; i++)
370 {
371 char *comp;
372 size_t size;
373 name += "/";
374 ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800375 string compStr(comp, size);
376 name += compStr;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800377 }
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700378
379 cp->runCallback(name, pcontent, len);
380
Chaoyi Bian11f294f2012-03-08 14:28:06 -0800381 return CCN_UPCALL_RESULT_OK;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800382}
383
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700384int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700385{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700386 DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback);
Alexander Afanasyevce001692013-07-14 11:34:41 -0700387 return sendInterest(strInterest, pass);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700388}
389
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700390int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700391{
392 RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback);
Alexander Afanasyevce001692013-07-14 11:34:41 -0700393 return sendInterest(strInterest, pass);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700394}
395
396int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800397{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700398 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700399 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700400 return -1;
401
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700402 // std::cout << "Send interests for " << strInterest << std::endl;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800403 ccn_charbuf *pname = ccn_charbuf_create();
404 ccn_closure *dataClosure = new ccn_closure;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800405
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800406 ccn_name_from_uri (pname, strInterest.c_str());
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700407 dataClosure->data = dataPass;
Chaoyi Bian95a58c32012-03-09 15:43:59 -0800408
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800409 dataClosure->p = &incomingData;
Zhenkai Zhu009ff792012-03-09 12:37:52 -0800410 if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700411 {
412 // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed"));
413 _LOG_ERROR("<<< Express interest failed: " << strInterest);
414 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800415
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700416 _LOG_DEBUG("<<< Sending interest: " << strInterest);
417
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800418 ccn_charbuf_destroy (&pname);
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700419 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800420}
421
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800422int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800423{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700424 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700425 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700426 return -1;
427
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800428 ccn_charbuf *pname = ccn_charbuf_create();
429 ccn_closure *interestClosure = new ccn_closure;
430
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800431 ccn_name_from_uri (pname, prefix.c_str());
432 interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800433 interestClosure->p = &incomingInterest;
Alexander Afanasyev387ac952012-03-11 23:49:27 -0700434 int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
435 if (ret < 0)
436 {
437 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
438 }
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800439
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700440 m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback));
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800441 ccn_charbuf_destroy(&pname);
Alexander Afanasyevce001692013-07-14 11:34:41 -0700442
443 return 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800444}
445
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700446void
447CcnxWrapper::clearInterestFilter (const std::string &prefix)
448{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700449 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700450 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700451 return;
452
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700453 std::cout << "clearInterestFilter" << std::endl;
454 ccn_charbuf *pname = ccn_charbuf_create();
455
456 ccn_name_from_uri (pname, prefix.c_str());
457 int ret = ccn_set_interest_filter (m_handle, pname, 0);
458 if (ret < 0)
459 {
460 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
461 }
462
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700463 m_registeredInterests.erase(prefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700464 ccn_charbuf_destroy(&pname);
465}
466
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700467string
468CcnxWrapper::getLocalPrefix ()
469{
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700470 struct ccn * tmp_handle = ccn_create ();
471 int res = ccn_connect (tmp_handle, NULL);
472 if (res < 0)
473 {
474 _LOG_ERROR ("connecting to ccnd failed");
475 return "";
476 }
477
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700478 string retval = "";
479
480 struct ccn_charbuf *templ = ccn_charbuf_create();
481 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
482 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
483 ccn_charbuf_append_closer(templ); /* </Name> */
484 // XXX - use pubid if possible
485 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
486 ccnb_append_number(templ, 1);
487 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
488 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
489 ccn_charbuf_append_closer(templ); /* </Interest> */
490
491 struct ccn_charbuf *name = ccn_charbuf_create ();
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700492 res = ccn_name_from_uri (name, "/local/ndn/prefix");
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700493 if (res < 0) {
494 _LOG_ERROR ("Something wrong with `ccn_name_from_uri` call");
495 }
496 else
497 {
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700498 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700499
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700500 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
501 NULL, 4, CCN_V_HIGHEST, 0);
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700502 if (stream == NULL) {
503 _LOG_ERROR ("Cannot create ccn_fetch_stream");
504 }
505 else
506 {
507 ostringstream os;
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700508
509 int counter = 0;
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700510 char buf[256];
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700511 while (true) {
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700512 res = ccn_fetch_read (stream, buf, sizeof(buf));
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700513
514 if (res == 0) {
515 break;
516 }
517
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700518 if (res > 0) {
519 os << string(buf, res);
520 } else if (res == CCN_FETCH_READ_NONE) {
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700521 if (counter < 2)
522 {
523 ccn_run(tmp_handle, 1000);
524 counter ++;
525 }
526 else
527 {
528 break;
529 }
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700530 } else if (res == CCN_FETCH_READ_END) {
531 break;
532 } else if (res == CCN_FETCH_READ_TIMEOUT) {
533 break;
534 } else {
535 _LOG_ERROR ("fatal error. should be reported");
536 break;
537 }
538 }
539 retval = os.str ();
540 stream = ccn_fetch_close(stream);
541 }
542 fetch = ccn_fetch_destroy(fetch);
543 }
544
545 ccn_charbuf_destroy (&name);
546
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700547 ccn_disconnect (tmp_handle);
548 ccn_destroy (&tmp_handle);
549
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700550 return retval;
551}
552
553
554
555
556
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700557DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700558{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700559 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700560}
561
562DataClosurePass::~DataClosurePass ()
563{
564 delete m_callback;
565 m_callback = NULL;
566}
567
568void
569DataClosurePass::runCallback(std::string name, const char *data, size_t len)
570{
571 string content(data, len);
572 if (m_callback != NULL) {
573 (*m_callback)(name, content);
574 }
575}
576
577
578RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
579{
580 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
581}
582
583RawDataClosurePass::~RawDataClosurePass ()
584{
585 delete m_callback;
586 m_callback = NULL;
587}
588
589void
590RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
591{
592 if (m_callback != NULL) {
593 (*m_callback)(name, data, len);
594 }
595}
596
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800597}