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