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