Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <sstream> |
| 8 | #include <iostream> |
| 9 | #include <time.h> |
| 10 | #include "../ndn-cpp/face.hpp" |
Jeff Thompson | f2d2186 | 2013-08-26 11:47:51 -0700 | [diff] [blame] | 11 | #include "../ndn-cpp/security/key-chain.hpp" |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace std; |
| 14 | using namespace ndn; |
| 15 | using namespace ptr_lib; |
| 16 | using namespace func_lib; |
| 17 | |
| 18 | class Echo { |
| 19 | public: |
| 20 | Echo() |
| 21 | { |
| 22 | interestCount_ = 0; |
| 23 | } |
| 24 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 25 | void operator() |
| 26 | (const ptr_lib::shared_ptr<const Name> &prefix, const ptr_lib::shared_ptr<const Interest> &interest, Transport &transport) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 27 | ++interestCount_; |
| 28 | |
| 29 | // Make and sign a Data packet. |
| 30 | Data data(interest->getName()); |
| 31 | string content(string("Echo ") + interest->getName().toUri()); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 32 | data.setContent((const unsigned char *)&content[0], content.size()); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 33 | data.getSignedInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
| 34 | KeyChain::defaultSign(data); |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 35 | shared_ptr<vector<unsigned char> > encodedData = data.wireEncode(); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 37 | cout << "Sent content " << content << endl; |
| 38 | transport.send(*encodedData); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int interestCount_; |
| 42 | }; |
| 43 | |
| 44 | int main(int argc, char** argv) |
| 45 | { |
| 46 | try { |
| 47 | Face face("localhost"); |
| 48 | |
| 49 | Echo echo; |
| 50 | Name prefix("/testecho"); |
| 51 | cout << "Register prefix " << prefix.toUri() << endl; |
| 52 | face.registerPrefix(prefix, ref(echo)); |
| 53 | |
Jeff Thompson | 9cc4be4 | 2013-08-27 18:12:41 -0700 | [diff] [blame] | 54 | // The main event loop. |
| 55 | // Wait forever to receive one interest for the prefix. |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 56 | while (echo.interestCount_ < 1) { |
| 57 | face.processEvents(); |
| 58 | // We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 59 | usleep(10000); |
| 60 | } |
| 61 | } catch (std::exception &e) { |
| 62 | cout << "exception: " << e.what() << endl; |
| 63 | } |
| 64 | return 0; |
| 65 | } |