blob: b742548fd0513ff7b1231dd174e1e8a2b55df90b [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 Afanasyev4f9ea482012-03-15 11:57:29 -070029#include "sync-log.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) {
248 publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness);
249}
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);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700387 sendInterest(strInterest, pass);
388}
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);
393 sendInterest(strInterest, pass);
394}
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);
442}
443
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700444void
445CcnxWrapper::clearInterestFilter (const std::string &prefix)
446{
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700447 recursive_mutex::scoped_lock lock(m_mutex);
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700448 if (!m_running || !m_connected)
Alexander Afanasyev2c180772012-03-13 23:58:46 -0700449 return;
450
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700451 std::cout << "clearInterestFilter" << std::endl;
452 ccn_charbuf *pname = ccn_charbuf_create();
453
454 ccn_name_from_uri (pname, prefix.c_str());
455 int ret = ccn_set_interest_filter (m_handle, pname, 0);
456 if (ret < 0)
457 {
458 BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret));
459 }
460
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700461 m_registeredInterests.erase(prefix);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700462 ccn_charbuf_destroy(&pname);
463}
464
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700465string
466CcnxWrapper::getLocalPrefix ()
467{
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700468 struct ccn * tmp_handle = ccn_create ();
469 int res = ccn_connect (tmp_handle, NULL);
470 if (res < 0)
471 {
472 _LOG_ERROR ("connecting to ccnd failed");
473 return "";
474 }
475
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700476 string retval = "";
477
478 struct ccn_charbuf *templ = ccn_charbuf_create();
479 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
480 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
481 ccn_charbuf_append_closer(templ); /* </Name> */
482 // XXX - use pubid if possible
483 ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG);
484 ccnb_append_number(templ, 1);
485 ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */
486 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2);
487 ccn_charbuf_append_closer(templ); /* </Interest> */
488
489 struct ccn_charbuf *name = ccn_charbuf_create ();
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700490 res = ccn_name_from_uri (name, "/local/ndn/prefix");
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700491 if (res < 0) {
492 _LOG_ERROR ("Something wrong with `ccn_name_from_uri` call");
493 }
494 else
495 {
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700496 struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle);
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700497
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700498 struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix",
499 NULL, 4, CCN_V_HIGHEST, 0);
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700500 if (stream == NULL) {
501 _LOG_ERROR ("Cannot create ccn_fetch_stream");
502 }
503 else
504 {
505 ostringstream os;
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700506
507 int counter = 0;
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700508 char buf[256];
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700509 while (true) {
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700510 res = ccn_fetch_read (stream, buf, sizeof(buf));
Alexander Afanasyevcf301962012-10-08 13:25:35 -0700511
512 if (res == 0) {
513 break;
514 }
515
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700516 if (res > 0) {
517 os << string(buf, res);
518 } else if (res == CCN_FETCH_READ_NONE) {
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700519 if (counter < 2)
520 {
521 ccn_run(tmp_handle, 1000);
522 counter ++;
523 }
524 else
525 {
526 break;
527 }
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700528 } else if (res == CCN_FETCH_READ_END) {
529 break;
530 } else if (res == CCN_FETCH_READ_TIMEOUT) {
531 break;
532 } else {
533 _LOG_ERROR ("fatal error. should be reported");
534 break;
535 }
536 }
537 retval = os.str ();
538 stream = ccn_fetch_close(stream);
539 }
540 fetch = ccn_fetch_destroy(fetch);
541 }
542
543 ccn_charbuf_destroy (&name);
544
Alexander Afanasyev7f680292012-10-08 14:30:42 -0700545 ccn_disconnect (tmp_handle);
546 ccn_destroy (&tmp_handle);
547
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700548 return retval;
549}
550
551
552
553
554
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700555DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL)
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700556{
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700557 m_callback = new CcnxWrapper::StringDataCallback (strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700558}
559
560DataClosurePass::~DataClosurePass ()
561{
562 delete m_callback;
563 m_callback = NULL;
564}
565
566void
567DataClosurePass::runCallback(std::string name, const char *data, size_t len)
568{
569 string content(data, len);
570 if (m_callback != NULL) {
571 (*m_callback)(name, content);
572 }
573}
574
575
576RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL)
577{
578 m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback);
579}
580
581RawDataClosurePass::~RawDataClosurePass ()
582{
583 delete m_callback;
584 m_callback = NULL;
585}
586
587void
588RawDataClosurePass::runCallback(std::string name, const char *data, size_t len)
589{
590 if (m_callback != NULL) {
591 (*m_callback)(name, data, len);
592 }
593}
594
Chaoyi Bian3e6e5142012-03-06 22:32:19 -0800595}