Last active
March 15, 2016 21:19
-
-
Save zhehaowang/2d0a4bc6201f4496bf36 to your computer and use it in GitHub Desktop.
ndn-cpp link test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Link test in NDN-CPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
linktest: | |
g++ -std=c++11 test-get-async.cpp -o testlinkconsumer -lndn-cpp | |
producer: | |
g++ -std=c++11 test-publish-async-nfd.cpp -o testlinkproducer -lndn-cpp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2013-2016 Regents of the University of California. | |
* @author: Jeff Thompson <[email protected]> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version, with the additional exemption that | |
* compiling, linking, and/or using OpenSSL is allowed. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* A copy of the GNU Lesser General Public License is in the file COPYING. | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include <unistd.h> | |
#include <ndn-cpp/face.hpp> | |
using namespace std; | |
using namespace ndn; | |
using namespace ndn::func_lib; | |
class Counter | |
{ | |
public: | |
Counter() { | |
callbackCount_ = 0; | |
} | |
void onData(const ptr_lib::shared_ptr<const Interest>& interest, const ptr_lib::shared_ptr<Data>& data) | |
{ | |
++callbackCount_; | |
cout << "Got data packet with name " << data->getName().toUri() << endl; | |
for (size_t i = 0; i < data->getContent().size(); ++i) | |
cout << (*data->getContent())[i]; | |
cout << endl; | |
} | |
void onTimeout(const ptr_lib::shared_ptr<const Interest>& interest) | |
{ | |
++callbackCount_; | |
cout << "Time out for interest " << interest->getName().toUri() << endl; | |
} | |
int callbackCount_; | |
}; | |
int main(int argc, char** argv) | |
{ | |
try { | |
Face face("131.179.210.47"); | |
// Counter holds data used by the callbacks. | |
Counter counter; | |
Name name("/org/openmhealth/zhehao/data"); | |
Interest interest(name); | |
Link link; | |
link.setName(Name("/org/openmhealth/zhehao/data/LINK")); | |
link.addDelegation(10, Name("/ndn/edu/ucla/remap/")); | |
link.addDelegation(20, Name("/ndn/edu/csu/")); | |
Blob linkEncoding = link.wireEncode(); | |
interest.setLinkWireEncoding(linkEncoding); | |
interest.setInterestLifetimeMilliseconds(4000); | |
cout << "Express name " << interest.getName().toUri() << endl; | |
face.expressInterest(interest, bind(&Counter::onData, &counter, _1, _2), bind(&Counter::onTimeout, &counter, _1)); | |
// The main event loop. | |
while (true) { | |
face.processEvents(); | |
// We need to sleep for a few milliseconds so we don't use 100% of the CPU. | |
usleep(10000); | |
} | |
} catch (std::exception& e) { | |
cout << "exception: " << e.what() << endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2013-2016 Regents of the University of California. | |
* @author: Jeff Thompson <[email protected]> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version, with the additional exemption that | |
* compiling, linking, and/or using OpenSSL is allowed. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
* A copy of the GNU Lesser General Public License is in the file COPYING. | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
#include <time.h> | |
#include <unistd.h> | |
#include <ndn-cpp/face.hpp> | |
#include <ndn-cpp/security/key-chain.hpp> | |
using namespace std; | |
using namespace ndn; | |
class Echo { | |
public: | |
Echo(KeyChain &keyChain, const Name& certificateName) | |
: keyChain_(keyChain), certificateName_(certificateName), responseCount_(0) | |
{ | |
} | |
// onInterest. | |
void operator() | |
(const ptr_lib::shared_ptr<const Name>& prefix, | |
const ptr_lib::shared_ptr<const Interest>& interest, Face& face, | |
uint64_t interestFilterId, | |
const ptr_lib::shared_ptr<const InterestFilter>& filter) | |
{ | |
cout << "Got interest " << interest->getName().toUri() << endl; | |
++responseCount_; | |
// Make and sign a Data packet. | |
Data data(interest->getName()); | |
string content(string("Echo ") + interest->getName().toUri()); | |
data.setContent((const uint8_t *)&content[0], content.size()); | |
keyChain_.sign(data, certificateName_); | |
cout << "Sent content " << content << endl; | |
face.putData(data); | |
} | |
// onRegisterFailed. | |
void operator()(const ptr_lib::shared_ptr<const Name>& prefix) | |
{ | |
++responseCount_; | |
cout << "Register failed for prefix " << prefix->toUri() << endl; | |
} | |
KeyChain keyChain_; | |
Name certificateName_; | |
int responseCount_; | |
}; | |
int main(int argc, char** argv) | |
{ | |
try { | |
// The default Face will connect using a Unix socket, or to "localhost". | |
Face face; | |
// Use the system default key chain and certificate name to sign commands. | |
KeyChain keyChain; | |
face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); | |
// Also use the default certificate name to sign data packets. | |
Echo echo(keyChain, keyChain.getDefaultCertificateName()); | |
// Whereas before, the registered prefix is: | |
//Name prefix("/org/openmhealth"); | |
Name prefix("/ndn/edu/ucla/remap/"); | |
Name interestNamePrefix("/org/openmhealth"); | |
InterestFilter interestFilter(interestNamePrefix); | |
cout << "Register prefix " << prefix.toUri() << endl; | |
// TODO: After we remove the registerPrefix with the deprecated OnInterest, | |
// we can remove the explicit cast to OnInterestCallback (needed for boost). | |
face.registerPrefix(prefix, (const OnInterestCallback&)func_lib::ref(echo), func_lib::ref(echo)); | |
face.setInterestFilter(interestFilter, (const OnInterestCallback&)func_lib::ref(echo)); | |
// The main event loop. | |
// Wait forever to receive one interest for the prefix. | |
while (true) { | |
face.processEvents(); | |
// We need to sleep for a few milliseconds so we don't use 100% of the CPU. | |
usleep(10000); | |
} | |
} catch (std::exception& e) { | |
cout << "exception: " << e.what() << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment