Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 22 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 23 | extern "C" { |
| 24 | #include <ccn/fetch.h> |
| 25 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 26 | #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 Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 30 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 31 | #include <boost/algorithm/string.hpp> |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | #include <sstream> |
| 33 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 34 | #include "logging.h" |
| 35 | |
| 36 | INIT_LOGGER ("Ccnx.Wrapper"); |
| 37 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 38 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 39 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 40 | |
| 41 | using namespace std; |
| 42 | using namespace boost; |
| 43 | |
| 44 | namespace Ccnx { |
| 45 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 46 | CcnxWrapper::CcnxWrapper() |
| 47 | : m_handle (0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 48 | , m_running (true) |
| 49 | , m_connected (false) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 50 | , m_executor (new Executor(1)) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame^] | 52 | start (); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void |
| 56 | CcnxWrapper::connectCcnd() |
| 57 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 58 | //if (m_handle != 0) { |
| 59 | //ccn_disconnect (m_handle); |
| 60 | //ccn_destroy (&m_handle); |
| 61 | //} |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 62 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 63 | m_handle = ccn_create (); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 64 | //UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 65 | if (ccn_connect(m_handle, NULL) < 0) |
| 66 | { |
| 67 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 68 | } |
| 69 | m_connected = true; |
| 70 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 71 | //if (!m_registeredInterests.empty()) |
| 72 | //{ |
| 73 | // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 74 | //{ |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 75 | // clearInterestFilter(it->first); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 76 | // setInterestFilter(it->first, it->second); |
| 77 | //} |
| 78 | //} |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | CcnxWrapper::~CcnxWrapper() |
| 82 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame^] | 83 | shutdown (); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | CcnxWrapper::start () // called automatically in constructor |
| 88 | { |
| 89 | connectCcnd(); |
| 90 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 91 | m_executor->start(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | CcnxWrapper::shutdown () // called in destructor, but can called manually |
| 96 | { |
| 97 | m_executor->shutdown(); |
| 98 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 99 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 100 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 101 | m_running = false; |
| 102 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame^] | 104 | _LOG_DEBUG ("+++++++++SHUTDOWN+++++++"); |
| 105 | if (m_connected) |
| 106 | { |
| 107 | m_thread.join (); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 108 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame^] | 109 | ccn_disconnect (m_handle); |
| 110 | //ccn_destroy (&m_handle); |
| 111 | m_connected = false; |
| 112 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 116 | CcnxWrapper::ccnLoop () |
| 117 | { |
| 118 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 119 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 120 | |
| 121 | while (m_running) |
| 122 | { |
| 123 | try |
| 124 | { |
| 125 | int res = 0; |
| 126 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 127 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 128 | res = ccn_run (m_handle, 0); |
| 129 | } |
| 130 | |
| 131 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 133 | if (res < 0) { |
| 134 | _LOG_ERROR ("ccn_run returned negative status: " << res); |
| 135 | usleep (100000); |
| 136 | continue; |
| 137 | // BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 138 | // << errmsg_info_str("ccn_run returned error")); |
| 139 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 140 | |
| 141 | |
| 142 | pollfd pfds[1]; |
| 143 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 144 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 145 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 146 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 147 | pfds[0].events = POLLIN; |
| 148 | if (ccn_output_is_pending (m_handle)) |
| 149 | pfds[0].events |= POLLOUT; |
| 150 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 151 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 152 | int ret = poll (pfds, 1, 1); |
| 153 | if (ret < 0) |
| 154 | { |
| 155 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 156 | } |
| 157 | } |
| 158 | catch (CcnxOperationException &e) |
| 159 | { |
| 160 | // do not try reconnect for now |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 161 | cout << *get_error_info<errmsg_info_str> (e) << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 162 | throw e; |
| 163 | /* |
| 164 | m_connected = false; |
| 165 | // probably ccnd has been stopped |
| 166 | // try reconnect with sleep |
| 167 | int interval = 1; |
| 168 | int maxInterval = 32; |
| 169 | while (m_running) |
| 170 | { |
| 171 | try |
| 172 | { |
| 173 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 174 | |
| 175 | connectCcnd(); |
| 176 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 177 | break; |
| 178 | } |
| 179 | catch (CcnxOperationException &e) |
| 180 | { |
| 181 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 182 | |
| 183 | // do exponential backup for reconnect interval |
| 184 | if (interval < maxInterval) |
| 185 | { |
| 186 | interval *= 2; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | */ |
| 191 | } |
| 192 | catch (const std::exception &exc) |
| 193 | { |
| 194 | // catch anything thrown within try block that derives from std::exception |
| 195 | std::cerr << exc.what(); |
| 196 | } |
| 197 | catch (...) |
| 198 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 199 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 200 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 201 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 204 | Bytes |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 205 | CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 206 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame^] | 207 | { |
| 208 | UniqueRecLock lock(m_mutex); |
| 209 | if (!m_running || !m_connected) |
| 210 | { |
| 211 | _LOG_TRACE ("<< not running or connected"); |
| 212 | return Bytes (); |
| 213 | } |
| 214 | } |
| 215 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 216 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 217 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 218 | ccn_charbuf *content = ccn_charbuf_create(); |
| 219 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 220 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 221 | sp.freshness = freshness; |
| 222 | |
| 223 | if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 224 | { |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 225 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed")); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 226 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 227 | |
| 228 | Bytes bytes; |
| 229 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 230 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 231 | ccn_charbuf_destroy (&content); |
| 232 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 233 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 237 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 238 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 239 | _LOG_TRACE (">> putToCcnd"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 240 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 241 | if (!m_running || !m_connected) |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 242 | { |
| 243 | _LOG_TRACE ("<< not running or connected"); |
| 244 | return -1; |
| 245 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 246 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 247 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 248 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 249 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 250 | _LOG_ERROR ("ccn_put failed"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 251 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 252 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 253 | else |
| 254 | { |
| 255 | _LOG_DEBUG ("<< putToCcnd"); |
| 256 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 262 | CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 263 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 264 | Bytes co = createContentObject(name, buf, len, freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 265 | return putToCcnd(co); |
| 266 | } |
| 267 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 268 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 269 | CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 270 | { |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 271 | return publishData(name, head(content), content.size(), freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 274 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 275 | static void |
| 276 | deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple) |
| 277 | { |
| 278 | delete tuple->get<0> (); |
| 279 | delete tuple; |
| 280 | } |
| 281 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 282 | static ccn_upcall_res |
| 283 | incomingInterest(ccn_closure *selfp, |
| 284 | ccn_upcall_kind kind, |
| 285 | ccn_upcall_info *info) |
| 286 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 287 | CcnxWrapper::InterestCallback *f; |
| 288 | ExecutorPtr executor; |
| 289 | tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data); |
| 290 | tie (f, executor) = *realData; |
| 291 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 292 | switch (kind) |
| 293 | { |
| 294 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 295 | // delete closure; |
| 296 | executor->execute (bind (deleterInInterestTuple, realData)); |
| 297 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 298 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 299 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 300 | return CCN_UPCALL_RESULT_OK; |
| 301 | |
| 302 | case CCN_UPCALL_INTEREST: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 303 | _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 304 | break; |
| 305 | |
| 306 | default: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 307 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 308 | return CCN_UPCALL_RESULT_OK; |
| 309 | } |
| 310 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 311 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 312 | Selectors selectors(info->pi); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 313 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 314 | executor->execute (bind (*f, interest, selectors)); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 315 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 316 | // (*f) (interest); |
| 317 | // closure->runInterestCallback(interest); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 318 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 319 | return CCN_UPCALL_RESULT_OK; |
| 320 | } |
| 321 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 322 | static void |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 323 | deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple) |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 324 | { |
| 325 | delete tuple->get<0> (); |
| 326 | delete tuple; |
| 327 | } |
| 328 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 329 | static ccn_upcall_res |
| 330 | incomingData(ccn_closure *selfp, |
| 331 | ccn_upcall_kind kind, |
| 332 | ccn_upcall_info *info) |
| 333 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 334 | // Closure *cp = static_cast<Closure *> (selfp->data); |
| 335 | Closure *cp; |
| 336 | ExecutorPtr executor; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 337 | Selectors selectors; |
| 338 | tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data); |
| 339 | tie (cp, executor, selectors) = *realData; |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 340 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 341 | switch (kind) |
| 342 | { |
| 343 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 344 | executor->execute (bind (deleterInDataTuple, realData)); |
| 345 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 346 | cp = NULL; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 347 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 348 | _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 349 | return CCN_UPCALL_RESULT_OK; |
| 350 | |
| 351 | case CCN_UPCALL_CONTENT: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 352 | _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 356 | if (cp != NULL) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 357 | { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 358 | Name interest(info->interest_ccnb, info->interest_comps); |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 359 | _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 360 | executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 361 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 362 | else |
| 363 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 364 | _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 365 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 366 | return CCN_UPCALL_RESULT_OK; |
| 367 | } |
| 368 | |
| 369 | default: |
| 370 | return CCN_UPCALL_RESULT_OK; |
| 371 | } |
| 372 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 373 | PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E]); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 374 | |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 375 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 376 | executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 377 | _LOG_TRACE (">> incomingData"); |
| 378 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 379 | return CCN_UPCALL_RESULT_OK; |
| 380 | } |
| 381 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 382 | int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 383 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 384 | _LOG_TRACE (">> sendInterest: " << interest); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 385 | { |
| 386 | UniqueRecLock lock(m_mutex); |
| 387 | if (!m_running || !m_connected) |
Alexander Afanasyev | b3d4cd5 | 2013-01-28 11:20:52 -0800 | [diff] [blame] | 388 | { |
| 389 | _LOG_ERROR ("<< sendInterest: not running or connected"); |
| 390 | return -1; |
| 391 | } |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 392 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 393 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 394 | CcnxCharbufPtr namePtr = interest.toCcnxCharbuf(); |
| 395 | ccn_charbuf *pname = namePtr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 396 | ccn_closure *dataClosure = new ccn_closure; |
| 397 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 398 | // Closure *myClosure = new ExecutorClosure(closure, m_executor); |
| 399 | Closure *myClosure = closure.dup (); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 400 | dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 401 | |
| 402 | dataClosure->p = &incomingData; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 403 | |
| 404 | CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf(); |
| 405 | ccn_charbuf *templ = NULL; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 406 | if (selectorsPtr) |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 407 | { |
| 408 | templ = selectorsPtr->getBuf(); |
| 409 | } |
| 410 | |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 411 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 412 | if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 413 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 414 | _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 420 | int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 421 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 422 | _LOG_TRACE (">> setInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 423 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 424 | if (!m_running || !m_connected) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 425 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 426 | return -1; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 427 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 428 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 429 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 430 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 431 | ccn_closure *interestClosure = new ccn_closure; |
| 432 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 433 | // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor); |
| 434 | |
| 435 | interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 436 | interestClosure->p = &incomingInterest; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 437 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 438 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 439 | if (ret < 0) |
| 440 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 441 | _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 444 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 445 | |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 446 | _LOG_TRACE ("<< setInterestFilter"); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 447 | |
| 448 | return ret; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 452 | CcnxWrapper::clearInterestFilter (const Name &prefix) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 453 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 454 | _LOG_TRACE (">> clearInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 455 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 456 | if (!m_running || !m_connected) |
| 457 | return; |
| 458 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 459 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 460 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 461 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 462 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 463 | if (ret < 0) |
| 464 | { |
| 465 | } |
| 466 | |
| 467 | m_registeredInterests.erase(prefix); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 468 | |
| 469 | _LOG_TRACE ("<< clearInterestFilter"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 472 | Name |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 473 | CcnxWrapper::getLocalPrefix () |
| 474 | { |
| 475 | struct ccn * tmp_handle = ccn_create (); |
| 476 | int res = ccn_connect (tmp_handle, NULL); |
| 477 | if (res < 0) |
| 478 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 479 | return Name(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 480 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 481 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 482 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 483 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 484 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 485 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 486 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 487 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 488 | // XXX - use pubid if possible |
| 489 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 490 | ccnb_append_number(templ, 1); |
| 491 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 492 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 493 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 494 | |
| 495 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 496 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 497 | if (res < 0) { |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 502 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 503 | struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix", |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 504 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 505 | if (stream == NULL) { |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | ostringstream os; |
| 510 | |
| 511 | int counter = 0; |
| 512 | char buf[256]; |
| 513 | while (true) { |
| 514 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 515 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 516 | if (res == 0) { |
| 517 | break; |
| 518 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 519 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 520 | if (res > 0) { |
| 521 | os << string(buf, res); |
| 522 | } else if (res == CCN_FETCH_READ_NONE) { |
| 523 | if (counter < 2) |
| 524 | { |
| 525 | ccn_run(tmp_handle, 1000); |
| 526 | counter ++; |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | break; |
| 531 | } |
| 532 | } else if (res == CCN_FETCH_READ_END) { |
| 533 | break; |
| 534 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 535 | break; |
| 536 | } else { |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | retval = os.str (); |
| 541 | stream = ccn_fetch_close(stream); |
| 542 | } |
| 543 | fetch = ccn_fetch_destroy(fetch); |
| 544 | } |
| 545 | |
| 546 | ccn_charbuf_destroy (&name); |
| 547 | |
| 548 | ccn_disconnect (tmp_handle); |
| 549 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 550 | |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 551 | boost::algorithm::trim(retval); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 552 | return Name(retval); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 555 | } |