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