blob: a4343430911c7b7a301aecf93afcb747d1b7e97f [file] [log] [blame]
Zhenkai Zhu64db03a2012-03-12 19:25:56 -07001/* -*- 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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25using boost::test_tools::output_test_stream;
26
27#include <boost/make_shared.hpp>
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070028#include <boost/date_time/posix_time/posix_time.hpp>
29
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070030#include "sync-logging.h"
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080031#include "sync-socket.h"
Alexander Afanasyevf46eac52013-07-26 11:27:39 -070032
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070033extern "C" {
34#include <unistd.h>
35}
36
37using namespace Sync;
38using namespace std;
39using namespace boost;
40
Alexander Afanasyev7df73332012-03-15 12:31:49 -070041INIT_LOGGER ("Test.AppSocket");
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070042
Zhenkai Zhua30e1782012-06-01 11:52:57 -070043#define PRINT
44//std::cout << "Line: " << __LINE__ << std::endl;
45
Zhenkai Zhu00304002012-03-12 21:32:30 -070046class TestSocketApp {
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070047public:
48 map<string, string> data;
Yingdi Yu43e71612013-10-30 22:19:31 -070049 void set(ndn::Ptr<ndn::Data> dataPacket) {
Alexander Afanasyev235c6d72012-03-15 22:28:43 -070050 // _LOG_FUNCTION (this << ", " << str1);
Yingdi Yu43e71612013-10-30 22:19:31 -070051 string str1(dataPacket->getName().toUri());
52 string str2(dataPacket->content().buf(), dataPacket->content().size());
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070053 data.insert(make_pair(str1, str2));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070054 // cout << str1 << ", " << str2 << endl;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070055 }
Yingdi Yu43e71612013-10-30 22:19:31 -070056
57 void set(string str1, const char * buf, int len) {
58 string str2(buf, len);
59 data.insert(make_pair(str1, str2));
60 }
Zhenkai Zhudc70a292012-06-01 14:00:59 -070061
Yingdi Yu43e71612013-10-30 22:19:31 -070062 void setNum(ndn::Ptr<ndn::Data> dataPacket) {
63 int n = dataPacket->content().size() / 4;
Zhenkai Zhudc70a292012-06-01 14:00:59 -070064 int *numbers = new int [n];
Yingdi Yu43e71612013-10-30 22:19:31 -070065 memcpy(numbers, dataPacket->content().buf(), dataPacket->content().size());
Zhenkai Zhudc70a292012-06-01 14:00:59 -070066 for (int i = 0; i < n; i++) {
67 sum += numbers[i];
68 }
69 delete numbers;
70
71 }
72
Yingdi Yu43e71612013-10-30 22:19:31 -070073 void setNum(string str1, const char * buf, int len) {
74 int n = len / 4;
75 int *numbers = new int [n];
76 memcpy(numbers, buf, len);
77 for (int i = 0; i < n; i++) {
78 sum += numbers[i];
79 }
80 delete numbers;
81 }
82
Zhenkai Zhudc70a292012-06-01 14:00:59 -070083 int sum;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -070084
Yingdi Yu43e71612013-10-30 22:19:31 -070085 void fetchAll(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070086 int n = v.size();
Zhenkai Zhua30e1782012-06-01 11:52:57 -070087
88 PRINT
89
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070090 for (int i = 0; i < n; i++) {
Zhenkai Zhua30e1782012-06-01 11:52:57 -070091 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070092 //PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -070093 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::set, this, _1));
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070094 }
95 }
96 }
97
Yingdi Yu43e71612013-10-30 22:19:31 -070098 void fetchNumbers(const vector<MissingDataInfo> &v, SyncSocket *socket) {
Zhenkai Zhudc70a292012-06-01 14:00:59 -070099 int n = v.size();
100
101 PRINT
102
103 std::cout << "In fetchNumbers. size of v is: " << n << std::endl;
104 for (int i = 0; i < n; i++) {
Zhenkai Zhu68f04d52012-06-05 14:07:41 -0700105 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;
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700106 for(SeqNo s = v[i].low; s <= v[i].high; ++s) {
107 PRINT
Yingdi Yu43e71612013-10-30 22:19:31 -0700108 socket->fetchData(v[i].prefix, s, bind(&TestSocketApp::setNum, this, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700109 }
110 }
111 }
112
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700113 void pass(const string &prefix) {
114 }
115
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700116 string toString(){
117 map<string, string>::iterator it = data.begin();
Zhenkai Zhu1a0fd5d2012-03-12 21:34:42 -0700118 string str = "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700119 for (; it != data.end(); ++it){
120 str += "<";
121 str += it->first;
122 str += "|";
123 str += it->second;
124 str += ">";
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700125 str += "\n";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700126 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700127
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700128 return str;
129 }
130
131};
132
133BOOST_AUTO_TEST_CASE (AppSocketTest)
134{
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700135 INIT_LOGGERS ();
136
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700137 TestSocketApp a1, a2, a3;
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700138
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700139 string syncPrefix("/let/us/sync");
140 string p1("/irl.cs.ucla.edu"), p2("/yakshi.org"), p3("/google.com");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700141
Yingdi Yu43e71612013-10-30 22:19:31 -0700142 ndn::Ptr<SyncPolicyManager> policyManager1 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/alice"), ndn::Name("/ndn/ucla.edu/alice/KEY/dsk-1382934202/ID-CERT/%FD%FF%FF%FF%FF%DEk%C0%0B"), ndn::Name(syncPrefix)));
143
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700144 _LOG_DEBUG ("s1");
Yingdi Yu43e71612013-10-30 22:19:31 -0700145 SyncSocket s1 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700146 this_thread::sleep (posix_time::milliseconds (50));
147 _LOG_DEBUG ("s2");
Yingdi Yu43e71612013-10-30 22:19:31 -0700148 SyncSocket s2 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700149 this_thread::sleep (posix_time::milliseconds (50));
Yingdi Yu43e71612013-10-30 22:19:31 -0700150 SyncSocket s3 (syncPrefix, policyManager1, bind(&TestSocketApp::fetchAll, &a3, _1, _2), bind(&TestSocketApp::pass, &a3, _1));
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700151 this_thread::sleep (posix_time::milliseconds (50));
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700152
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700153 // single source
154 string data0 = "Very funny Scotty, now beam down my clothes";
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -0700155 _LOG_DEBUG ("s1 publish");
Yingdi Yu43e71612013-10-30 22:19:31 -0700156 s1.publishData (p1, 0, data0.c_str(), data0.size(), 10);
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700157 this_thread::sleep (posix_time::milliseconds (1000));
Zhenkai Zhu00304002012-03-12 21:32:30 -0700158
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800159 // // from code logic, we won't be fetching our own data
160 // a1.set(p1 + "/0/0", data0.c_str(), data0.size());
161 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
162 // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700163
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800164 // // single source, multiple data at once
165 // string data1 = "Yes, give me that ketchup";
166 // string data2 = "Don't look conspicuous, it draws fire";
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700167
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800168 // _LOG_DEBUG ("s1 publish");
169 // s1.publishData (p1, 0, data1.c_str(), data1.size(), 10);
170 // _LOG_DEBUG ("s1 publish");
171 // s1.publishData (p1, 0, data2.c_str(), data2.size(), 10);
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700172 // this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800173
174 // // from code logic, we won't be fetching our own data
175 // a1.set(p1 + "/0/1", data1.c_str(), data1.size());
176 // a1.set(p1 + "/0/2", data2.c_str(), data2.size());
177 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
178 // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhu00304002012-03-12 21:32:30 -0700179
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800180 // // another single source
181 // string data3 = "You surf the Internet, I surf the real world";
182 // string data4 = "I got a fortune cookie once that said 'You like Chinese food'";
183 // string data5 = "Real men wear pink. Why? Because their wives make them";
184 // _LOG_DEBUG ("s3 publish");
185 // s3.publishData(p3, 0, data3.c_str(), data3.size(), 10);
186 // this_thread::sleep (posix_time::milliseconds (200));
187
188 // // another single source, multiple data at once
189 // s2.publishData(p2, 0, data4.c_str(), data4.size(), 10);
190 // s2.publishData(p2, 0, data5.c_str(), data5.size(), 10);
191 // this_thread::sleep (posix_time::milliseconds (1000));
Alexander Afanasyev235c6d72012-03-15 22:28:43 -0700192
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800193 // // from code logic, we won't be fetching our own data
194 // a3.set(p3 + "/0/0", data3.c_str(), data3.size());
195 // a2.set(p2 + "/0/0", data4.c_str(), data4.size());
196 // a2.set(p2 + "/0/1", data5.c_str(), data5.size());
197 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
198 // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Yingdi Yu43e71612013-10-30 22:19:31 -0700199
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800200 // // not sure weither this is simultanous data generation from multiple sources
201 // _LOG_DEBUG ("Simultaneous publishing");
202 // string data6 = "Shakespeare says: 'Prose before hos.'";
203 // string data7 = "Pick good people, talent never wears out";
204 // s1.publishData(p1, 0, data6.c_str(), data6.size(), 10);
205 // // this_thread::sleep (posix_time::milliseconds (1000));
206 // s2.publishData(p2, 0, data7.c_str(), data7.size(), 10);
207 // this_thread::sleep (posix_time::milliseconds (1500));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700208
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800209 // // from code logic, we won't be fetching our own data
210 // a1.set(p1 + "/0/3", data6.c_str(), data6.size());
211 // a2.set(p2 + "/0/2", data7.c_str(), data7.size());
212 // // a1.set(p1 + "/0/1", data6);
213 // // a2.set(p2 + "/0/0", data7);
214 // BOOST_CHECK_EQUAL(a1.toString(), a2.toString());
215 // BOOST_CHECK_EQUAL(a2.toString(), a3.toString());
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700216
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800217 // _LOG_DEBUG("Begin new test");
218 // std::cout << "Begin new Test " << std::endl;
219 // string syncRawPrefix = "/this/is/the/prefix";
220 // ndn::Ptr<SyncPolicyManager> policyManager2 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/alice"), ndn::Name("/ndn/ucla.edu/alice/KEY/dsk-1382934202/ID-CERT/%FD%FF%FF%FF%FF%DEk%C0%0B"), ndn::Name(syncRawPrefix)));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700221
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800222 // a1.sum = 0;
223 // a2.sum = 0;
224 // SyncSocket s4 (syncRawPrefix, policyManager2, bind(&TestSocketApp::fetchNumbers, &a1, _1, _2), bind(&TestSocketApp::pass, &a1, _1));
225 // SyncSocket s5 (syncRawPrefix, policyManager2, bind(&TestSocketApp::fetchNumbers, &a2, _1, _2), bind(&TestSocketApp::pass, &a2, _1));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700226
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800227 // int num[5] = {0, 1, 2, 3, 4};
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700228
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800229 // string p4 = "/xiaonei.com";
230 // string p5 = "/mitbbs.com";
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700231
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800232 // s4.publishData(p4, 0,(const char *) num, sizeof(num), 10);
233 // a1.setNum(p4, (const char *) num, sizeof (num));
Zhenkai Zhudc70a292012-06-01 14:00:59 -0700234
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800235 // this_thread::sleep (posix_time::milliseconds (1000));
236 // BOOST_CHECK(a1.sum == a2.sum && a1.sum == 10);
237
238 // int newNum[5] = {9, 7, 2, 1, 1};
239
240 // s5.publishData(p5, 0,(const char *) newNum, sizeof(newNum), 10);
241 // a2.setNum(p5, (const char *)newNum, sizeof (newNum));
242 // this_thread::sleep (posix_time::milliseconds (1000));
243 // BOOST_CHECK_EQUAL(a1.sum, a2.sum);
244 // BOOST_CHECK_EQUAL(a1.sum, 30);
245
246 // _LOG_DEBUG ("Finish");
Zhenkai Zhu64db03a2012-03-12 19:25:56 -0700247}