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> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 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" |
| 24 | #include <poll.h> |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
| 27 | using namespace boost; |
| 28 | |
| 29 | namespace Sync { |
| 30 | |
| 31 | CcnxWrapper::CcnxWrapper() |
| 32 | { |
| 33 | m_handle = ccn_create(); |
| 34 | ccn_connect(m_handle, NULL); |
| 35 | initKeyStore(); |
| 36 | createKeyLocator(); |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 37 | m_thread = thread(&CcnxWrapper::ccnLoop, this); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | CcnxWrapper::~CcnxWrapper() |
| 41 | { |
| 42 | running = false; |
| 43 | m_thread.join(); |
| 44 | ccn_disconnect(m_handle); |
| 45 | ccn_destroy(&m_handle); |
| 46 | ccn_charbuf_destroy(&m_keyLoactor); |
| 47 | ccn_keystore_destroy(&m_keyStore); |
| 48 | } |
| 49 | |
| 50 | void CcnxWrapper::createKeyLocator() |
| 51 | { |
| 52 | int res; |
| 53 | |
| 54 | m_keyLoactor = ccn_charbuf_create(); |
| 55 | ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 56 | ccn_charbuf_append_tt(m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 57 | res = ccn_append_pubkey_blob(m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
| 58 | if (res >= 0) |
| 59 | { |
| 60 | ccn_charbuf_append_closer(m_keyLoactor); /* </Key> */ |
| 61 | ccn_charbuf_append_closer(m_keyLoactor); /* </KeyLocator> */ |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | const ccn_pkey* CcnxWrapper::getPrivateKey() |
| 66 | { |
| 67 | return ccn_keystore_private_key(m_keyStore); |
| 68 | } |
| 69 | |
| 70 | const unsigned char* CcnxWrapper::getPublicKeyDigest() |
| 71 | { |
| 72 | return ccn_keystore_public_key_digest(m_keyStore); |
| 73 | } |
| 74 | |
| 75 | ssize_t CcnxWrapper::getPublicKeyDigestLength() |
| 76 | { |
| 77 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 78 | } |
| 79 | |
| 80 | void CcnxWrapper::initKeyStore() |
| 81 | { |
| 82 | ccn_charbuf *temp = ccn_charbuf_create(); |
| 83 | m_keyStore = ccn_keystore_create(); |
| 84 | ccn_charbuf_putf(temp, "%s/.ccnx/.ccnx_keystore", getenv("HOME")); |
| 85 | ccn_keystore_init(m_keyStore, ccn_charbuf_as_string(temp), (char*)"Th1s1sn0t8g00dp8ssw0rd."); |
| 86 | ccn_charbuf_destroy(&temp); |
| 87 | } |
| 88 | |
| 89 | void CcnxWrapper::ccnLoop() |
| 90 | { |
| 91 | pollfd pfds[1]; |
| 92 | int res = ccn_run(m_handle, 0); |
| 93 | |
| 94 | pfds[0].fd = ccn_get_connection_fd(m_handle); |
| 95 | pfds[0].events = POLLIN; |
| 96 | |
| 97 | while (running) |
| 98 | { |
| 99 | if (res >= 0) |
| 100 | { |
| 101 | int ret = poll(pfds, 1, 100); |
| 102 | if (ret >= 0) |
| 103 | { |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 104 | recursive_mutex::scoped_lock lock(m_mutex); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 105 | res = ccn_run(m_handle, 0); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 111 | int CcnxWrapper::publishData(string name, string dataBuffer, int freshness) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 112 | { |
| 113 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 114 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 115 | ccn_charbuf *content = ccn_charbuf_create(); |
| 116 | |
| 117 | ccn_name_from_uri(pname, name.c_str()); |
| 118 | ccn_signed_info_create(signed_info, |
| 119 | getPublicKeyDigest(), |
| 120 | getPublicKeyDigestLength(), |
| 121 | NULL, |
| 122 | CCN_CONTENT_DATA, |
| 123 | freshness, |
| 124 | NULL, |
| 125 | m_keyLoactor); |
| 126 | ccn_encode_ContentObject(content, pname, signed_info, |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 127 | dataBuffer.c_str(), dataBuffer.length(), |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 128 | NULL, getPrivateKey()); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 129 | recursive_mutex::scoped_lock lock(m_mutex); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 130 | ccn_put(m_handle, content->buf, content->length); |
| 131 | |
| 132 | ccn_charbuf_destroy(&pname); |
| 133 | ccn_charbuf_destroy(&signed_info); |
| 134 | ccn_charbuf_destroy(&content); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static ccn_upcall_res incomingInterest( |
| 139 | ccn_closure *selfp, |
| 140 | ccn_upcall_kind kind, |
| 141 | ccn_upcall_info *info) |
| 142 | { |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 143 | function<void (string)> f = *(function<void (string)> *)selfp->data; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 144 | |
| 145 | switch (kind) |
| 146 | { |
| 147 | case CCN_UPCALL_FINAL: |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 148 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 149 | return CCN_UPCALL_RESULT_OK; |
| 150 | |
| 151 | case CCN_UPCALL_INTEREST: |
| 152 | break; |
| 153 | |
| 154 | default: |
| 155 | return CCN_UPCALL_RESULT_OK; |
| 156 | } |
| 157 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 158 | string interest; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 159 | for (int i = 0; i < info->interest_comps->n - 1; i++) |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 160 | { |
| 161 | char *comp; |
| 162 | size_t size; |
Chaoyi Bian | bc52752 | 2012-03-07 21:50:11 -0800 | [diff] [blame] | 163 | interest += "/"; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 164 | ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size); |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 165 | interest += comp; |
| 166 | } |
| 167 | f(interest); |
| 168 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static ccn_upcall_res incomingData( |
| 172 | ccn_closure *selfp, |
| 173 | ccn_upcall_kind kind, |
| 174 | ccn_upcall_info *info) |
| 175 | { |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 176 | function<void (string)> f = *(function<void (string)> *)selfp->data; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 177 | |
| 178 | switch (kind) |
| 179 | { |
| 180 | case CCN_UPCALL_FINAL: |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 181 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 182 | return CCN_UPCALL_RESULT_OK; |
| 183 | |
| 184 | case CCN_UPCALL_CONTENT: |
| 185 | break; |
| 186 | |
| 187 | default: |
| 188 | return CCN_UPCALL_RESULT_OK; |
| 189 | } |
| 190 | |
| 191 | char *pcontent; |
| 192 | size_t len; |
| 193 | ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len); |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 194 | f((string)pcontent); |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 195 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 198 | int CcnxWrapper::sendInterest(string strInterest, function<void (string)> dataCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 199 | { |
| 200 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 201 | ccn_closure *dataClosure = new ccn_closure; |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 202 | function<void (string)> *f = new function<void (string)>(dataCallback); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 203 | |
| 204 | ccn_name_from_uri(pname, strInterest.c_str()); |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 205 | dataClosure->data = f; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 206 | dataClosure->p = &incomingData; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame^] | 207 | recursive_mutex::scoped_lock lock(m_mutex); |
| 208 | ccn_express_interest(m_handle, pname, dataClosure, NULL); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 209 | |
| 210 | ccn_charbuf_destroy(&pname); |
| 211 | } |
| 212 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 213 | int CcnxWrapper::setInterestFilter(string prefix, function<void (string)> interestCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 214 | { |
| 215 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 216 | ccn_closure *interestClosure = new ccn_closure; |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 217 | function<void (string)> *f = new function<void (string)>(interestCallback); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 218 | |
| 219 | ccn_name_from_uri(pname, prefix.c_str()); |
Chaoyi Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 220 | interestClosure->data = f; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 221 | interestClosure->p = &incomingInterest; |
| 222 | ccn_set_interest_filter(m_handle, pname, interestClosure); |
| 223 | |
| 224 | ccn_charbuf_destroy(&pname); |
| 225 | } |
| 226 | |
| 227 | } |