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() |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 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 | 1ec4d0e | 2013-09-11 14:06:41 -0700 | [diff] [blame] | 33 | data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0); |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 34 | KeyChain::defaultSign(data); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 35 | Blob 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 | } |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 61 | } catch (std::exception& e) { |
Jeff Thompson | cf624b9 | 2013-08-23 20:51:06 -0700 | [diff] [blame] | 62 | cout << "exception: " << e.what() << endl; |
| 63 | } |
| 64 | return 0; |
| 65 | } |