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