akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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 | */ |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | using boost::test_tools::output_test_stream; |
| 26 | |
| 27 | #include <boost/make_shared.hpp> |
| 28 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 29 | |
| 30 | #include "sync-logging.h" |
| 31 | #include "sync-socket.h" |
| 32 | #include <ndn-cpp-dev/security/validator-null.hpp> |
| 33 | |
| 34 | extern "C" { |
| 35 | #include <unistd.h> |
| 36 | } |
| 37 | |
| 38 | using namespace Sync; |
| 39 | using namespace std; |
| 40 | using namespace boost; |
| 41 | |
| 42 | INIT_LOGGER ("Test.AppSocket"); |
| 43 | |
| 44 | #define PRINT |
| 45 | //std::cout << "Line: " << __LINE__ << std::endl; |
| 46 | |
| 47 | class TestSocketApp { |
| 48 | public: |
| 49 | TestSocketApp() |
| 50 | : sum(0) |
| 51 | {} |
| 52 | |
| 53 | map<ndn::Name, string> data; |
| 54 | void set(const ndn::shared_ptr<const ndn::Data>& dataPacket) { |
| 55 | // _LOG_FUNCTION (this << ", " << str1); |
| 56 | ndn::Name dataName(dataPacket->getName()); |
| 57 | string str2(reinterpret_cast<const char*>(dataPacket->getContent().value()), dataPacket->getContent().value_size()); |
| 58 | data.insert(make_pair(dataName, str2)); |
| 59 | // cout << str1 << ", " << str2 << endl; |
| 60 | } |
| 61 | |
| 62 | void set(ndn::Name name, const char * buf, int len) { |
| 63 | string str2(buf, len); |
| 64 | data.insert(make_pair(name, str2)); |
| 65 | } |
| 66 | |
| 67 | void setNum(const ndn::shared_ptr<const ndn::Data>& dataPacket) { |
| 68 | int n = dataPacket->getContent().value_size() / 4; |
| 69 | uint32_t *numbers = new uint32_t [n]; |
| 70 | memcpy(numbers, dataPacket->getContent().value(), dataPacket->getContent().value_size()); |
| 71 | for (int i = 0; i < n; i++) { |
| 72 | sum += numbers[i]; |
| 73 | } |
| 74 | delete numbers; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | void setNum(ndn::Name name, const char * buf, int len) { |
| 79 | int n = len / 4; |
| 80 | int *numbers = new int [n]; |
| 81 | memcpy(numbers, buf, len); |
| 82 | for (int i = 0; i < n; i++) { |
| 83 | sum += numbers[i]; |
| 84 | } |
| 85 | delete numbers; |
| 86 | } |
| 87 | |
| 88 | uint32_t sum; |
| 89 | |
| 90 | void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) { |
| 91 | int n = v.size(); |
| 92 | |
| 93 | PRINT |
| 94 | |
| 95 | for (int i = 0; i < n; i++) { |
| 96 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 97 | //PRINT |
| 98 | socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1)); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) { |
| 104 | int n = v.size(); |
| 105 | |
| 106 | PRINT |
| 107 | |
| 108 | // std::cout << "In fetchNumbers. size of v is: " << n << std::endl; |
| 109 | for (int i = 0; i < n; i++) { |
| 110 | // std::cout << "In fetchNumbers. v[i].low is (" <<v[i].low.getSession() <<", " << v[i].low.getSeq() << ") v[i].high is ("<<v[i].high.getSession() <<", " <<v[i].high.getSeq()<<")" << std::endl; |
| 111 | for(SeqNo s = v[i].low; s <= v[i].high; ++s) { |
| 112 | PRINT |
| 113 | socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1)); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void pass(const string &prefix) { |
| 119 | } |
| 120 | |
| 121 | string toString(){ |
| 122 | map<ndn::Name, string>::iterator it = data.begin(); |
| 123 | string str = "\n"; |
| 124 | for (; it != data.end(); ++it){ |
| 125 | str += "<"; |
| 126 | str += it->first.toUri(); |
| 127 | str += "|"; |
| 128 | str += it->second; |
| 129 | str += ">"; |
| 130 | str += "\n"; |
| 131 | } |
| 132 | |
| 133 | return str; |
| 134 | } |
| 135 | |
| 136 | }; |
| 137 | |
| 138 | class TestSet1{ |
| 139 | public: |
| 140 | TestSet1(ndn::shared_ptr<boost::asio::io_service> ioService) |
| 141 | : m_validator(new ndn::ValidatorNull()) |
| 142 | , m_face1(new ndn::Face(ioService)) |
| 143 | , m_face2(new ndn::Face(ioService)) |
| 144 | , m_face3(new ndn::Face(ioService)) |
| 145 | , m_p1("/irl.cs.ucla.edu") |
| 146 | , m_p2("/yakshi.org") |
| 147 | , m_p3("/google.com") |
| 148 | , m_syncPrefix("/let/us/sync") |
| 149 | {} |
| 150 | |
| 151 | void |
| 152 | createSyncSocket1() |
| 153 | { |
| 154 | _LOG_DEBUG ("s1"); |
| 155 | m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1, |
| 156 | bind(&TestSocketApp::fetchAll, &m_a1, _1, _2), |
| 157 | bind(&TestSocketApp::pass, &m_a1, _1)); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | createSyncSocket2() |
| 162 | { |
| 163 | _LOG_DEBUG ("s2"); |
| 164 | m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2, |
| 165 | bind(&TestSocketApp::fetchAll, &m_a2, _1, _2), |
| 166 | bind(&TestSocketApp::pass, &m_a2, _1)); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | createSyncSocket3() |
| 171 | { |
| 172 | _LOG_DEBUG ("s3"); |
| 173 | m_s3 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face3, |
| 174 | bind(&TestSocketApp::fetchAll, &m_a3, _1, _2), |
| 175 | bind(&TestSocketApp::pass, &m_a3, _1)); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | publishSocket1(uint32_t session, string data) |
| 180 | { |
| 181 | _LOG_DEBUG ("s1 publish"); |
| 182 | m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | publishSocket2(uint32_t session, string data) |
| 187 | { |
| 188 | _LOG_DEBUG ("s2 publish"); |
| 189 | m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | publishSocket3(uint32_t session, string data) |
| 194 | { |
| 195 | _LOG_DEBUG ("s3 publish"); |
| 196 | m_s3->publishData (m_p3, session, data.c_str(), data.size(), 1000); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | setSocket1(string suffix, string data) |
| 201 | { |
| 202 | _LOG_DEBUG ("a1 set"); |
| 203 | ndn::Name name = m_p1; |
| 204 | name.append(suffix); |
| 205 | m_a1.set (name, data.c_str(), data.size()); |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | setSocket2(string suffix, string data) |
| 210 | { |
| 211 | _LOG_DEBUG ("a2 set"); |
| 212 | ndn::Name name = m_p2; |
| 213 | name.append(suffix); |
| 214 | m_a2.set (name, data.c_str(), data.size()); |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | setSocket3(string suffix, string data) |
| 219 | { |
| 220 | _LOG_DEBUG ("a3 set"); |
| 221 | ndn::Name name = m_p3; |
| 222 | name.append(suffix); |
| 223 | m_a3.set (name, data.c_str(), data.size()); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | check(int round) |
| 228 | { |
| 229 | BOOST_CHECK_EQUAL(m_a1.toString(), m_a2.toString()); |
| 230 | BOOST_CHECK_EQUAL(m_a2.toString(), m_a3.toString()); |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | done() |
| 235 | { |
| 236 | m_s1.reset(); |
| 237 | m_s2.reset(); |
| 238 | m_s3.reset(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | TestSocketApp m_a1, m_a2, m_a3; |
| 243 | ndn::shared_ptr<ndn::ValidatorNull> m_validator; |
| 244 | ndn::shared_ptr<ndn::Face> m_face1, m_face2, m_face3; |
| 245 | ndn::Name m_p1, m_p2, m_p3; |
| 246 | ndn::shared_ptr<SyncSocket> m_s1, m_s2, m_s3; |
| 247 | ndn::Name m_syncPrefix; |
| 248 | }; |
| 249 | |
| 250 | class TestSet2{ |
| 251 | public: |
| 252 | TestSet2(ndn::shared_ptr<boost::asio::io_service> ioService) |
| 253 | : m_validator(new ndn::ValidatorNull()) |
| 254 | , m_face1(new ndn::Face(ioService)) |
| 255 | , m_face2(new ndn::Face(ioService)) |
| 256 | , m_p1("/xiaonei.com") |
| 257 | , m_p2("/mitbbs.com") |
| 258 | , m_syncPrefix("/this/is/the/prefix") |
| 259 | {} |
| 260 | |
| 261 | void |
| 262 | createSyncSocket1() |
| 263 | { |
| 264 | _LOG_DEBUG ("s1"); |
| 265 | m_s1 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face1, |
| 266 | bind(&TestSocketApp::fetchNumbers, &m_a1, _1, _2), |
| 267 | bind(&TestSocketApp::pass, &m_a1, _1)); |
| 268 | } |
| 269 | |
| 270 | void |
| 271 | createSyncSocket2() |
| 272 | { |
| 273 | _LOG_DEBUG ("s2"); |
| 274 | m_s2 = make_shared<SyncSocket>(m_syncPrefix, m_validator, m_face2, |
| 275 | bind(&TestSocketApp::fetchNumbers, &m_a2, _1, _2), |
| 276 | bind(&TestSocketApp::pass, &m_a2, _1)); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | publishSocket1(uint32_t session, string data) |
| 281 | { |
| 282 | _LOG_DEBUG ("s1 publish"); |
| 283 | m_s1->publishData (m_p1, session, data.c_str(), data.size(), 1000); |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | publishSocket2(uint32_t session, string data) |
| 288 | { |
| 289 | _LOG_DEBUG ("s2 publish"); |
| 290 | m_s2->publishData (m_p2, session, data.c_str(), data.size(), 1000); |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | setSocket1(const char* ptr, size_t size) |
| 295 | { |
| 296 | _LOG_DEBUG ("a1 setNum"); |
| 297 | m_a1.setNum (m_p1, ptr, size); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | setSocket2(const char* ptr, size_t size) |
| 302 | { |
| 303 | _LOG_DEBUG ("a2 setNum"); |
| 304 | m_a2.setNum (m_p2, ptr, size); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | check(int num) |
| 309 | { |
| 310 | _LOG_DEBUG ("codnum " << num); |
| 311 | _LOG_DEBUG ("a1 sum " << m_a1.sum); |
| 312 | _LOG_DEBUG ("a2 sum " << m_a2.sum); |
| 313 | |
| 314 | BOOST_CHECK(m_a1.sum == m_a2.sum && m_a1.sum == num); |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | done() |
| 319 | { |
| 320 | m_s1.reset(); |
| 321 | m_s2.reset(); |
| 322 | } |
| 323 | |
| 324 | TestSocketApp m_a1, m_a2; |
| 325 | ndn::shared_ptr<ndn::ValidatorNull> m_validator; |
| 326 | ndn::shared_ptr<ndn::Face> m_face1, m_face2; |
| 327 | ndn::Name m_p1, m_p2; |
| 328 | ndn::shared_ptr<SyncSocket> m_s1, m_s2; |
| 329 | ndn::Name m_syncPrefix; |
| 330 | }; |
| 331 | |
| 332 | BOOST_AUTO_TEST_CASE (AppSocketTest1) |
| 333 | { |
| 334 | INIT_LOGGERS (); |
| 335 | |
| 336 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 337 | ndn::Scheduler scheduler(*ioService); |
| 338 | TestSet1 testSet1(ioService); |
| 339 | |
| 340 | scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet1::createSyncSocket1, &testSet1)); |
| 341 | scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet1::createSyncSocket2, &testSet1)); |
| 342 | scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet1::createSyncSocket3, &testSet1)); |
| 343 | string data0 = "Very funny Scotty, now beam down my clothes"; |
| 344 | scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data0)); |
| 345 | scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/0", data0)); |
| 346 | scheduler.scheduleEvent(ndn::time::seconds(1.16), ndn::bind(&TestSet1::check, &testSet1, 1)); |
| 347 | string data1 = "Yes, give me that ketchup"; |
| 348 | string data2 = "Don't look conspicuous, it draws fire"; |
| 349 | scheduler.scheduleEvent(ndn::time::seconds(1.17), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data1)); |
| 350 | scheduler.scheduleEvent(ndn::time::seconds(1.18), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data2)); |
| 351 | scheduler.scheduleEvent(ndn::time::seconds(2.15), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/1", data1)); |
| 352 | scheduler.scheduleEvent(ndn::time::seconds(2.16), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/2", data2)); |
| 353 | scheduler.scheduleEvent(ndn::time::seconds(2.17), ndn::bind(&TestSet1::check, &testSet1, 2)); |
| 354 | string data3 = "You surf the Internet, I surf the real world"; |
| 355 | string data4 = "I got a fortune cookie once that said 'You like Chinese food'"; |
| 356 | string data5 = "Real men wear pink. Why? Because their wives make them"; |
| 357 | scheduler.scheduleEvent(ndn::time::seconds(3.18), ndn::bind(&TestSet1::publishSocket3, &testSet1, 0, data3)); |
| 358 | scheduler.scheduleEvent(ndn::time::seconds(3.20), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data4)); |
| 359 | scheduler.scheduleEvent(ndn::time::seconds(3.21), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data5)); |
| 360 | scheduler.scheduleEvent(ndn::time::seconds(4.21), ndn::bind(&TestSet1::setSocket3, &testSet1, "/0/0", data3)); |
| 361 | scheduler.scheduleEvent(ndn::time::seconds(4.22), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/0", data4)); |
| 362 | scheduler.scheduleEvent(ndn::time::seconds(4.23), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/1", data5)); |
| 363 | scheduler.scheduleEvent(ndn::time::seconds(4.30), ndn::bind(&TestSet1::check, &testSet1, 3)); |
| 364 | // not sure weither this is simultanous data generation from multiple sources |
| 365 | _LOG_DEBUG ("Simultaneous publishing"); |
| 366 | string data6 = "Shakespeare says: 'Prose before hos.'"; |
| 367 | string data7 = "Pick good people, talent never wears out"; |
| 368 | scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket1, &testSet1, 0, data6)); |
| 369 | scheduler.scheduleEvent(ndn::time::seconds(5.50), ndn::bind(&TestSet1::publishSocket2, &testSet1, 0, data7)); |
| 370 | scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket1, &testSet1, "/0/3", data6)); |
| 371 | scheduler.scheduleEvent(ndn::time::seconds(6.80), ndn::bind(&TestSet1::setSocket2, &testSet1, "/0/2", data7)); |
| 372 | scheduler.scheduleEvent(ndn::time::seconds(6.90), ndn::bind(&TestSet1::check, &testSet1, 4)); |
| 373 | scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet1::done, &testSet1)); |
| 374 | |
| 375 | ioService->run(); |
| 376 | } |
| 377 | |
| 378 | BOOST_AUTO_TEST_CASE (AppSocketTest2) |
| 379 | { |
| 380 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 381 | ndn::Scheduler scheduler(*ioService); |
| 382 | TestSet2 testSet2(ioService); |
| 383 | |
| 384 | scheduler.scheduleEvent(ndn::time::seconds(0.00), ndn::bind(&TestSet2::createSyncSocket1, &testSet2)); |
| 385 | scheduler.scheduleEvent(ndn::time::seconds(0.05), ndn::bind(&TestSet2::createSyncSocket2, &testSet2)); |
| 386 | uint32_t num[5] = {0, 1, 2, 3, 4}; |
| 387 | string data0((const char *) num, sizeof(num)); |
| 388 | scheduler.scheduleEvent(ndn::time::seconds(0.10), ndn::bind(&TestSet2::publishSocket1, &testSet2, 0, data0)); |
| 389 | scheduler.scheduleEvent(ndn::time::seconds(0.15), ndn::bind(&TestSet2::setSocket1, &testSet2, (const char *) num, sizeof (num))); |
| 390 | scheduler.scheduleEvent(ndn::time::seconds(1.00), ndn::bind(&TestSet2::check, &testSet2, 10)); |
| 391 | uint32_t newNum[5] = {9, 7, 2, 1, 1}; |
| 392 | string data1((const char *) newNum, sizeof(newNum)); |
| 393 | scheduler.scheduleEvent(ndn::time::seconds(1.10), ndn::bind(&TestSet2::publishSocket2, &testSet2, 0, data1)); |
| 394 | scheduler.scheduleEvent(ndn::time::seconds(1.15), ndn::bind(&TestSet2::setSocket2, &testSet2, (const char *) newNum, sizeof (newNum))); |
| 395 | scheduler.scheduleEvent(ndn::time::seconds(2.00), ndn::bind(&TestSet2::check, &testSet2, 30)); |
| 396 | scheduler.scheduleEvent(ndn::time::seconds(7.00), ndn::bind(&TestSet2::done, &testSet2)); |
| 397 | |
| 398 | ioService->run(); |
| 399 | } |