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