Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-ccnx-wrapper.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 24 | #include "sync-log.h" |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 25 | #include <poll.h> |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 26 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 27 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 28 | |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 29 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 30 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 31 | |
| 32 | using namespace std; |
| 33 | using namespace boost; |
| 34 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 35 | INIT_LOGGER ("CcnxWrapper"); |
| 36 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 37 | namespace Sync { |
| 38 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 39 | #ifdef _DEBUG_WRAPPER_ |
| 40 | CcnxWrapper::CcnxWrapper(char c) |
| 41 | #else |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 42 | CcnxWrapper::CcnxWrapper() |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 43 | #endif |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 44 | : m_handle (0) |
| 45 | , m_keyStore (0) |
| 46 | , m_keyLoactor (0) |
| 47 | , m_running (true) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 49 | #ifdef _DEBUG_WRAPPER_ |
| 50 | m_c = c; |
| 51 | #endif |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 52 | m_handle = ccn_create(); |
| 53 | connectCcnd(); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 54 | initKeyStore (); |
| 55 | createKeyLocator (); |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 56 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 59 | void |
| 60 | CcnxWrapper::connectCcnd() |
| 61 | { |
| 62 | /* |
| 63 | if (m_handle != NULL) |
| 64 | { |
| 65 | ccn_destroy(&m_handle); |
| 66 | } |
| 67 | */ |
| 68 | _LOG_DEBUG("<<< connecting to ccnd"); |
| 69 | if (ccn_connect(m_handle, NULL) < 0) |
| 70 | { |
| 71 | //ccn_destroy(&m_handle); |
| 72 | _LOG_DEBUG("<<< connecting to ccnd failed"); |
| 73 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 74 | } |
| 75 | } |
| 76 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 77 | CcnxWrapper::~CcnxWrapper() |
| 78 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 79 | // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl; |
| 80 | { |
| 81 | recursive_mutex::scoped_lock lock(m_mutex); |
| 82 | m_running = false; |
| 83 | } |
| 84 | |
| 85 | m_thread.join (); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 86 | ccn_disconnect (m_handle); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 87 | ccn_destroy (&m_handle); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 88 | ccn_charbuf_destroy (&m_keyLoactor); |
| 89 | ccn_keystore_destroy (&m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 92 | /// @cond include_hidden |
| 93 | |
| 94 | void |
| 95 | CcnxWrapper::createKeyLocator () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 96 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 97 | m_keyLoactor = ccn_charbuf_create(); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 98 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 99 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 100 | int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 101 | if (res >= 0) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 103 | ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */ |
| 104 | ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 105 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 108 | const ccn_pkey* |
| 109 | CcnxWrapper::getPrivateKey () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 110 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 111 | return ccn_keystore_private_key (m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 114 | const unsigned char* |
| 115 | CcnxWrapper::getPublicKeyDigest () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 116 | { |
| 117 | return ccn_keystore_public_key_digest(m_keyStore); |
| 118 | } |
| 119 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 120 | ssize_t |
| 121 | CcnxWrapper::getPublicKeyDigestLength () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 122 | { |
| 123 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 124 | } |
| 125 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 126 | void |
| 127 | CcnxWrapper::initKeyStore () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 128 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 129 | m_keyStore = ccn_keystore_create (); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 130 | string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore"); |
| 131 | if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0) |
| 132 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str())); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 135 | void |
| 136 | CcnxWrapper::ccnLoop () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 137 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 138 | _LOG_FUNCTION (this); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 139 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 140 | while (m_running) |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 141 | { |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 142 | try |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 143 | { |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 144 | #ifdef _DEBUG_WRAPPER_ |
| 145 | std::cout << m_c << flush; |
| 146 | #endif |
| 147 | int res = 0; |
| 148 | { |
| 149 | recursive_mutex::scoped_lock lock (m_mutex); |
| 150 | res = ccn_run (m_handle, 0); |
| 151 | } |
| 152 | |
| 153 | if (!m_running) break; |
| 154 | |
| 155 | if (res < 0) |
| 156 | BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 157 | << errmsg_info_str("ccn_run returned error")); |
| 158 | |
| 159 | |
| 160 | pollfd pfds[1]; |
| 161 | { |
| 162 | recursive_mutex::scoped_lock lock (m_mutex); |
| 163 | |
| 164 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 165 | pfds[0].events = POLLIN; |
| 166 | if (ccn_output_is_pending (m_handle)) |
| 167 | pfds[0].events |= POLLOUT; |
| 168 | } |
| 169 | |
| 170 | int ret = poll (pfds, 1, 1); |
| 171 | if (ret < 0) |
| 172 | { |
| 173 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 174 | } |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 175 | } |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 176 | catch (CcnxOperationException e) |
| 177 | { |
| 178 | // probably ccnd has been stoped |
| 179 | // try reconnect with sleep |
| 180 | int interval = 1; |
| 181 | int maxInterval = 32; |
| 182 | while (m_running) |
| 183 | { |
| 184 | try |
| 185 | { |
| 186 | sleep(1); |
| 187 | connectCcnd(); |
| 188 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 189 | break; |
| 190 | } |
| 191 | catch (CcnxOperationException e) |
| 192 | { |
| 193 | sleep(interval); |
| 194 | // do exponential backup for reconnect interval |
| 195 | if (interval < maxInterval) |
| 196 | { |
| 197 | interval *= 2; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 205 | /// @endcond |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 206 | int |
| 207 | CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) { |
| 208 | publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness); |
| 209 | } |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 210 | |
| 211 | int |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 212 | CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 213 | { |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 214 | |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 215 | recursive_mutex::scoped_lock lock(m_mutex); |
| 216 | if (!m_running) |
| 217 | return -1; |
| 218 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 219 | // cout << "Publish: " << name << endl; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 220 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 221 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 222 | ccn_charbuf *content = ccn_charbuf_create(); |
| 223 | |
| 224 | ccn_name_from_uri(pname, name.c_str()); |
| 225 | ccn_signed_info_create(signed_info, |
| 226 | getPublicKeyDigest(), |
| 227 | getPublicKeyDigestLength(), |
| 228 | NULL, |
| 229 | CCN_CONTENT_DATA, |
| 230 | freshness, |
| 231 | NULL, |
| 232 | m_keyLoactor); |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 233 | if(ccn_encode_ContentObject(content, pname, signed_info, |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 234 | (const unsigned char *)buf, len, |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 235 | NULL, getPrivateKey()) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 236 | { |
| 237 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed")); |
| 238 | _LOG_ERROR("<<< Encode content failed " << name); |
| 239 | } |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 240 | |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 241 | if (ccn_put(m_handle, content->buf, content->length) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 242 | { |
| 243 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 244 | _LOG_ERROR("<<< ccnput content failed " << name); |
| 245 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 246 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 247 | ccn_charbuf_destroy (&pname); |
| 248 | ccn_charbuf_destroy (&signed_info); |
| 249 | ccn_charbuf_destroy (&content); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 250 | return 0; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 254 | static ccn_upcall_res |
| 255 | incomingInterest(ccn_closure *selfp, |
| 256 | ccn_upcall_kind kind, |
| 257 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 258 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 259 | CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 260 | |
| 261 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 262 | { |
Zhenkai Zhu | cd74759 | 2012-03-09 12:08:17 -0800 | [diff] [blame] | 263 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 264 | delete f; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 265 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 266 | return CCN_UPCALL_RESULT_OK; |
| 267 | |
| 268 | case CCN_UPCALL_INTEREST: |
| 269 | break; |
| 270 | |
| 271 | default: |
| 272 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 273 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 274 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 275 | string interest; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 276 | for (int i = 0; i < info->interest_comps->n - 1; i++) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 277 | { |
| 278 | char *comp; |
| 279 | size_t size; |
| 280 | interest += "/"; |
| 281 | ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 282 | string compStr(comp, size); |
| 283 | interest += compStr; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 284 | } |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 285 | (*f) (interest); |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 286 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 289 | static ccn_upcall_res |
| 290 | incomingData(ccn_closure *selfp, |
| 291 | ccn_upcall_kind kind, |
| 292 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 293 | { |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 294 | ClosurePass *cp = static_cast<ClosurePass *> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 295 | |
| 296 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 297 | { |
Zhenkai Zhu | cd74759 | 2012-03-09 12:08:17 -0800 | [diff] [blame] | 298 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 299 | delete cp; |
| 300 | cp = NULL; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 301 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 302 | return CCN_UPCALL_RESULT_OK; |
| 303 | |
| 304 | case CCN_UPCALL_CONTENT: |
| 305 | break; |
| 306 | |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 307 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
| 308 | if (cp != NULL && cp->getRetry() > 0) { |
| 309 | cp->decRetry(); |
| 310 | return CCN_UPCALL_RESULT_REEXPRESS; |
| 311 | } |
| 312 | return CCN_UPCALL_RESULT_OK; |
| 313 | } |
| 314 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 315 | default: |
| 316 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 317 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 318 | |
| 319 | char *pcontent; |
| 320 | size_t len; |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 321 | if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 322 | { |
| 323 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed")); |
| 324 | _LOG_ERROR("<<< Decode content failed "); |
| 325 | } |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 326 | |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 327 | string name; |
| 328 | for (int i = 0; i < info->content_comps->n - 1; i++) |
| 329 | { |
| 330 | char *comp; |
| 331 | size_t size; |
| 332 | name += "/"; |
| 333 | ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 334 | string compStr(comp, size); |
| 335 | name += compStr; |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 336 | } |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 337 | |
| 338 | cp->runCallback(name, pcontent, len); |
| 339 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 340 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 343 | int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 344 | { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 345 | DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 346 | sendInterest(strInterest, pass); |
| 347 | } |
| 348 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 349 | int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 350 | { |
| 351 | RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback); |
| 352 | sendInterest(strInterest, pass); |
| 353 | } |
| 354 | |
| 355 | int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 356 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 357 | recursive_mutex::scoped_lock lock(m_mutex); |
| 358 | if (!m_running) |
| 359 | return -1; |
| 360 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 361 | // std::cout << "Send interests for " << strInterest << std::endl; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 362 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 363 | ccn_closure *dataClosure = new ccn_closure; |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 364 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 365 | ccn_name_from_uri (pname, strInterest.c_str()); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 366 | dataClosure->data = dataPass; |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 367 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 368 | dataClosure->p = &incomingData; |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 369 | if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame^] | 370 | { |
| 371 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed")); |
| 372 | _LOG_ERROR("<<< Express interest failed: " << strInterest); |
| 373 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 374 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 375 | ccn_charbuf_destroy (&pname); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 376 | return 0; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 379 | int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 380 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 381 | recursive_mutex::scoped_lock lock(m_mutex); |
| 382 | if (!m_running) |
| 383 | return -1; |
| 384 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 385 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 386 | ccn_closure *interestClosure = new ccn_closure; |
| 387 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 388 | ccn_name_from_uri (pname, prefix.c_str()); |
| 389 | interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 390 | interestClosure->p = &incomingInterest; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 391 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 392 | if (ret < 0) |
| 393 | { |
| 394 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret)); |
| 395 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 396 | |
| 397 | ccn_charbuf_destroy(&pname); |
| 398 | } |
| 399 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 400 | void |
| 401 | CcnxWrapper::clearInterestFilter (const std::string &prefix) |
| 402 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 403 | recursive_mutex::scoped_lock lock(m_mutex); |
| 404 | if (!m_running) |
| 405 | return; |
| 406 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 407 | std::cout << "clearInterestFilter" << std::endl; |
| 408 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 409 | |
| 410 | ccn_name_from_uri (pname, prefix.c_str()); |
| 411 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 412 | if (ret < 0) |
| 413 | { |
| 414 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret)); |
| 415 | } |
| 416 | |
| 417 | ccn_charbuf_destroy(&pname); |
| 418 | } |
| 419 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 420 | DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 421 | { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 422 | m_callback = new CcnxWrapper::StringDataCallback (strDataCallback); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | DataClosurePass::~DataClosurePass () |
| 426 | { |
| 427 | delete m_callback; |
| 428 | m_callback = NULL; |
| 429 | } |
| 430 | |
| 431 | void |
| 432 | DataClosurePass::runCallback(std::string name, const char *data, size_t len) |
| 433 | { |
| 434 | string content(data, len); |
| 435 | if (m_callback != NULL) { |
| 436 | (*m_callback)(name, content); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | |
| 441 | RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL) |
| 442 | { |
| 443 | m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback); |
| 444 | } |
| 445 | |
| 446 | RawDataClosurePass::~RawDataClosurePass () |
| 447 | { |
| 448 | delete m_callback; |
| 449 | m_callback = NULL; |
| 450 | } |
| 451 | |
| 452 | void |
| 453 | RawDataClosurePass::runCallback(std::string name, const char *data, size_t len) |
| 454 | { |
| 455 | if (m_callback != NULL) { |
| 456 | (*m_callback)(name, data, len); |
| 457 | } |
| 458 | } |
| 459 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 460 | } |